Skip to content

test: add validation and reachability tests for /scan-url#19

Merged
ionfwsrijan merged 4 commits into
ionfwsrijan:mainfrom
lakshay122007:test/12-scan-url-validation
Jun 2, 2026
Merged

test: add validation and reachability tests for /scan-url#19
ionfwsrijan merged 4 commits into
ionfwsrijan:mainfrom
lakshay122007:test/12-scan-url-validation

Conversation

@lakshay122007
Copy link
Copy Markdown
Contributor

Before opening: make sure there is an issue tracking this work, and link it below. PRs without a linked issue may be closed without review.

Linked issue

Closes #12

What this PR does

This PR adds comprehensive test coverage for the /scan-url endpoint to prevent regressions in URL validation and error handling. It introduces isolated tests using mocked httpx and file system operations to simulate various network states and user inputs without executing real network calls.

Type of change

  • Bug fix
  • New feature
  • ML model / training pipeline
  • Refactor (no behaviour change)
  • Documentation
  • Tests only

ML tier (if applicable)

  • Tier 1 — Triage
  • Tier 2 — Predictive
  • Tier 3 — Autonomous
  • Not ML-related

Changes

Backend

  • Added backend/tests/test_scan_url.py with tests covering invalid URL formats, 404 Not Found responses, and httpx.TimeoutException.
  • Added a success test case (200 OK) that fully mocks download_to_path, unzip_to_dir, and _scan_repo_dir to test the route logic cleanly without triggering actual file downloads or background CLI tools.

Testing

How did you test this?

I ran the new test file locally using pytest backend/tests/test_scan_url.py. All four test cases passed successfully. I verified that no actual network requests or file system modifications occurred during the test execution due to the mocks.
Screenshot 2026-06-02 at 2 42 25 PM

Checklist

  • Tested locally end-to-end (upload ZIP or GitHub URL → scan → findings returned correctly)
  • New ML model falls back gracefully when model file is absent
  • No new console.error or unhandled Python exceptions introduced
  • Added or updated tests where applicable
  • requirements.txt / package.json updated if new dependencies added
  • New model files (.pkl, .pt, etc.) are gitignored, not committed

Anything reviewers should focus on

Reviewers can check the mocking strategy in test_scan_url_success. download_to_path (as an AsyncMock), unzip_to_dir, and _scan_repo_dir are all fully patched so that the CI pipeline can run this test rapidly without requiring the Semgrep/OSV/Gitleaks binaries to be installed in the [environment.](url)

@lakshay122007
Copy link
Copy Markdown
Contributor Author

hi @ionfwsrijan Please verify the tests - i have checked them locally also(all 4 passing) . Let me know if any changes required. Thanks!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new backend test module to cover the /scan-url FastAPI endpoint’s URL validation and GitHub reachability handling, aiming to prevent regressions in error handling without performing real network calls.

Changes:

  • Added tests for invalid GitHub URL format (422) and repository reachability failures (404 → 422, timeout → 422).
  • Added a “success” test that mocks download/unzip/scan steps to exercise the happy-path route logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/tests/test_scan_url.py Outdated
Comment thread backend/tests/test_scan_url.py Outdated
Comment on lines +38 to +39
mock_client = mock_async_client.return_value.__aenter__.return_value
mock_client.head.side_effect = httpx.TimeoutException("timeout")
Comment thread backend/tests/test_scan_url.py Outdated
Comment on lines +57 to +60
mock_client = mock_async_client.return_value.__aenter__.return_value
mock_response = AsyncMock()
mock_response.status_code = 200
mock_client.head.return_value = mock_response
@ionfwsrijan
Copy link
Copy Markdown
Owner

@lakshay122007 Great work! The code looks clean to me.

@ionfwsrijan
Copy link
Copy Markdown
Owner

@lakshay122007 Can you address copilot suggestions? They look valid to me

@lakshay122007
Copy link
Copy Markdown
Contributor Author

yes - i am just verifying them just a moment

lakshay122007 and others added 3 commits June 2, 2026 15:45
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@lakshay122007
Copy link
Copy Markdown
Contributor Author

Done @ionfwsrijan

@ionfwsrijan ionfwsrijan merged commit 297fad9 into ionfwsrijan:main Jun 2, 2026
6 checks passed
@ionfwsrijan
Copy link
Copy Markdown
Owner

@lakshay122007 Thanks for you valuable contribution

@ionfwsrijan
Copy link
Copy Markdown
Owner

@lakshay122007 You may raise new issues if you find some or just hit me up on dc ill give you a couple of feature suggestions

@ionfwsrijan ionfwsrijan added enhancement New feature or request backend Backend issues easy Easy difficulty SSoC26 labels Jun 2, 2026
@ionfwsrijan
Copy link
Copy Markdown
Owner

@lakshay122007 Join our dc server! https://discord.gg/fRwjghKPJ

@ionfwsrijan
Copy link
Copy Markdown
Owner

@lakshay122007 I've posted a couple of new feature ideas and also mentors are there to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend issues easy Easy difficulty enhancement New feature or request SSoC26

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] /scan-url: add tests for URL validation and reachability checks

3 participants