@@ -274,12 +274,19 @@ def test_api_response(self):
274274
275275class BulkSearchAPI (TestCase ):
276276 def setUp (self ):
277- cpes = [
277+ self . exclusive_cpes = [
278278 "cpe:/a:nginx:1.0.7" ,
279279 "cpe:/a:nginx:1.0.15" ,
280280 "cpe:/a:nginx:1.14.1" ,
281281 "cpe:/a:nginx:1.15.5" ,
282282 "cpe:/a:nginx:1.15.6" ,
283+ ]
284+ vuln = Vulnerability .objects .create (summary = "test" )
285+ for cpe in self .exclusive_cpes :
286+ ref = VulnerabilityReference .objects .create (reference_id = cpe )
287+ VulnerabilityRelatedReference .objects .create (reference = ref , vulnerability = vuln )
288+ second_vuln = Vulnerability .objects .create (summary = "test-A" )
289+ self .non_exclusive_cpes = [
283290 "cpe:/a:nginx:1.16.1" ,
284291 "cpe:/a:nginx:1.17.2" ,
285292 "cpe:/a:nginx:1.17.3" ,
@@ -288,19 +295,67 @@ def setUp(self):
288295 "cpe:/a:nginx:1.20.0" ,
289296 "cpe:/a:nginx:1.21.0" ,
290297 ]
291- self .cpes = cpes
292- vuln = Vulnerability .objects .create (summary = "test" )
293- for cpe in cpes :
298+ third_vuln = Vulnerability .objects .create (summary = "test-B" )
299+ for cpe in self .non_exclusive_cpes :
294300 ref = VulnerabilityReference .objects .create (reference_id = cpe )
295- VulnerabilityRelatedReference .objects .create (reference = ref , vulnerability = vuln )
301+ VulnerabilityRelatedReference .objects .create (reference = ref , vulnerability = second_vuln )
302+ VulnerabilityRelatedReference .objects .create (reference = ref , vulnerability = third_vuln )
296303
297- def test_api_response_with_one_vulnerability (self ):
304+ def test_api_response_with_with_exclusive_cpes_associated_with_two_vulnerabilities (self ):
298305 request_body = {
299- "cpes" : self .cpes ,
306+ "cpes" : self .exclusive_cpes ,
300307 }
301308 response = self .client .post (
302309 "/api/cpes/bulk_search" ,
303310 data = request_body ,
304311 content_type = "application/json" ,
305312 ).json ()
306313 assert len (response ) == 1
314+ assert response [0 ]["summary" ] == "test"
315+ references_in_vuln = response [0 ]["references" ]
316+ cpes = [ref ["reference_id" ] for ref in references_in_vuln ]
317+ assert set (cpes ) == set (self .exclusive_cpes )
318+
319+ def test_api_response_with_no_cpe_associated (self ):
320+ request_body = {
321+ "cpes" : ["cpe:/a:nginx:1.10.7" ],
322+ }
323+ response = self .client .post (
324+ "/api/cpes/bulk_search" ,
325+ data = request_body ,
326+ content_type = "application/json" ,
327+ ).json ()
328+ assert len (response ) == 0
329+
330+ def test_api_response_with_with_non_exclusive_cpes_associated_with_two_vulnerabilities (self ):
331+ request_body = {
332+ "cpes" : self .non_exclusive_cpes ,
333+ }
334+ response = self .client .post (
335+ "/api/cpes/bulk_search" ,
336+ data = request_body ,
337+ content_type = "application/json" ,
338+ ).json ()
339+ assert len (response ) == 2
340+
341+ def test_with_empty_list (self ):
342+ request_body = {
343+ "cpes" : [],
344+ }
345+ response = self .client .post (
346+ "/api/cpes/bulk_search" ,
347+ data = request_body ,
348+ content_type = "application/json" ,
349+ ).json ()
350+ assert response == {"Error" : "A non-empty 'cpe' list of package URLs is required." }
351+
352+ def test_with_invalid_cpes (self ):
353+ request_body = {
354+ "cpes" : ["CVE-2022-2022" ],
355+ }
356+ response = self .client .post (
357+ "/api/cpes/bulk_search" ,
358+ data = request_body ,
359+ content_type = "application/json" ,
360+ ).json ()
361+ assert response == {"Error" : "Invalid CPE: CVE-2022-2022" }
0 commit comments