Skip to content

Commit

Permalink
Test /api/cpe with package related vulnerabilities
Browse files Browse the repository at this point in the history
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
  • Loading branch information
keshav-space committed Oct 25, 2024
1 parent ac3a1c9 commit 68d6f0c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions vulnerabilities/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,46 @@ def test_api_response(self):
self.assertEqual(response["count"], 1)


class TestCPEApiWithPackageVulnerabilityRelation(TestCase):
def setUp(self):
self.user = ApiUser.objects.create_api_user(username="e@mail.com")
self.auth = f"Token {self.user.auth_token.key}"
self.csrf_client = APIClient(enforce_csrf_checks=True)
self.csrf_client.credentials(HTTP_AUTHORIZATION=self.auth)
self.vulnerability = Vulnerability.objects.create(summary="test")
self.affected_package, _ = Package.objects.get_or_create_from_purl(
purl="pkg:nginx/nginx@v3.4"
)
self.fixed_package, _ = Package.objects.get_or_create_from_purl(purl="pkg:nginx/nginx@v4.0")
AffectedByPackageRelatedVulnerability.objects.create(
vulnerability=self.vulnerability,
created_by="test",
package=self.affected_package,
confidence=100,
)
FixingPackageRelatedVulnerability.objects.create(
vulnerability=self.vulnerability,
created_by="test",
package=self.fixed_package,
confidence=100,
)
for i in range(0, 10):
ref, _ = VulnerabilityReference.objects.get_or_create(
reference_id=f"cpe:/a:nginx:{i}",
url=f"https://nvd.nist.gov/vuln/search/results?adv_search=true&isCpeNameSearch=true&query=cpe:/a:nginx:{i}",
)
VulnerabilityRelatedReference.objects.create(
reference=ref, vulnerability=self.vulnerability
)

def test_cpe_api(self):
response = self.csrf_client.get("/api/cpes/", format="json")
self.assertEqual(status.HTTP_200_OK, response.status_code)

response_data = response.json()
self.assertEqual(1, response_data["count"])


class AliasApi(TestCase):
def setUp(self):
self.user = ApiUser.objects.create_api_user(username="e@mail.com")
Expand Down

0 comments on commit 68d6f0c

Please sign in to comment.