Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve test_nvd_api #4934

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

22f1001635
Copy link
Contributor

Overview

This PR introduces comprehensive unit tests for critical functions in nvd_api.py, ensuring better test coverage without adding stress to the NVD API. All tests are mocked and avoid real API calls, adhering to the project's guidelines on minimizing external resource usage.


Changes

  1. New Unit Tests:

    • Added tests for convert_date_to_nvd_date_api2, get_reject_count_api2, nvd_count_metadata, validate_nvd_api, and load_nvd_request.
    • Mocked API responses using AsyncMock and FakeResponse to simulate various scenarios (e.g., invalid API keys, rejected CVEs).
  2. Preserved Existing Tests:

    • All original integration tests (e.g., test_nvd_incremental_update) remain unchanged and are conditionally skipped unless EXTERNAL_SYSTEM=1 is set.
  3. No Breaking Changes:

    • The new tests are additive and do not modify or override existing functionality.

Why This Matters

  • Improved Coverage: Core functions like date formatting, API key validation, and CVE rejection handling are now thoroughly tested.
  • Zero Real API Calls: All tests use mocked data, avoiding unnecessary load on the NVD API.
  • Safer Refactoring: Future changes to these functions can be validated without relying on external services.

Testing

  • New Tests:

    • test_convert_date_to_nvd_date_api2: Validates date formatting for NVD API compliance.
    • test_get_reject_count_api2: Ensures rejected CVEs are correctly counted.
    • test_nvd_count_metadata: Verifies metadata parsing from mocked API responses.
    • test_validate_nvd_api: Tests API key removal on invalid responses.
    • test_load_nvd_request: Checks pagination logic and total result calculation.
  • Existing Tests:

    • All original integration tests pass as expected.

Steps to test

EXTERNAL_SYSTEM=1 pytest -v test/test_nvd_api.py

Related Issues
Fixes #4877

@22f1001635 22f1001635 force-pushed the improve-test-nvd-api branch from 14275b0 to 3b9ed7e Compare March 18, 2025 19:32
@22f1001635
Copy link
Contributor Author

hi @terriko i have made ammends to the commit as i was seeing assertion errors for test_get_with_mock_load_nvd_request can you please take a look at the changes i have made and whether this aligns with what we had in mind towards coverage of nvd API

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

Successfully merging this pull request may close these issues.

test: Improve Tests for nvd_api.py file
2 participants