cve-bin-tool icon indicating copy to clipboard operation
cve-bin-tool copied to clipboard

fix: improve test_nvd_api

Open Saksham-Sirohi opened this issue 10 months ago • 1 comments

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

Saksham-Sirohi avatar Mar 10 '25 20:03 Saksham-Sirohi

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

Saksham-Sirohi avatar Jun 23 '25 13:06 Saksham-Sirohi