cve-bin-tool
cve-bin-tool copied to clipboard
fix: improve test_nvd_api
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
-
New Unit Tests:
- Added tests for
convert_date_to_nvd_date_api2,get_reject_count_api2,nvd_count_metadata,validate_nvd_api, andload_nvd_request. - Mocked API responses using
AsyncMockandFakeResponseto simulate various scenarios (e.g., invalid API keys, rejected CVEs).
- Added tests for
-
Preserved Existing Tests:
- All original integration tests (e.g.,
test_nvd_incremental_update) remain unchanged and are conditionally skipped unlessEXTERNAL_SYSTEM=1is set.
- All original integration tests (e.g.,
-
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
hi @terriko 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