@@ -305,3 +305,182 @@ def test_get_fixing_vulnerabilities(self):
305305 serializer = PackageV2Serializer ()
306306 vulnerabilities = serializer .get_fixing_vulnerabilities (package )
307307 self .assertEqual (vulnerabilities , ["VCID-5678" ])
308+
309+ def test_bulk_lookup_with_valid_purls (self ):
310+ """
311+ Test bulk lookup with valid PURLs.
312+ """
313+ url = reverse ("package-v2-bulk-lookup" )
314+ data = {
"purls" : [
"pkg:pypi/[email protected] " ,
"pkg:npm/[email protected] " ]}
315+ response = self .client .post (url , data , format = "json" )
316+ self .assertEqual (response .status_code , status .HTTP_200_OK )
317+ self .assertEqual (len (response .data ), 2 )
318+ # Verify that the returned data matches the packages
319+ purls = [package ["purl" ] for package in response .data ]
320+ self .
assertIn (
"pkg:pypi/[email protected] " ,
purls )
321+ self .
assertIn (
"pkg:npm/[email protected] " ,
purls )
322+
323+ def test_bulk_lookup_with_invalid_purls (self ):
324+ """
325+ Test bulk lookup with invalid PURLs.
326+ """
327+ url = reverse ("package-v2-bulk-lookup" )
328+ data = {
"purls" : [
"pkg:pypi/[email protected] " ,
"pkg:npm/[email protected] " ]}
329+ response = self .client .post (url , data , format = "json" )
330+ self .assertEqual (response .status_code , status .HTTP_200_OK )
331+ # Since the packages don't exist, the response should be empty
332+ self .assertEqual (len (response .data ), 0 )
333+
334+ def test_bulk_lookup_with_empty_purls (self ):
335+ """
336+ Test bulk lookup with empty purls list.
337+ Should return 400 Bad Request.
338+ """
339+ url = reverse ("package-v2-bulk-lookup" )
340+ data = {"purls" : []}
341+ response = self .client .post (url , data , format = "json" )
342+ self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
343+ self .assertIn ("error" , response .data )
344+ self .assertIn ("message" , response .data )
345+ self .assertEqual (response .data ["message" ], "A non-empty 'purls' list of PURLs is required." )
346+
347+ def test_bulk_search_with_valid_purls (self ):
348+ """
349+ Test bulk search with valid PURLs.
350+ """
351+ url = reverse ("package-v2-bulk-search" )
352+ data = {
"purls" : [
"pkg:pypi/[email protected] " ,
"pkg:npm/[email protected] " ]}
353+ response = self .client .post (url , data , format = "json" )
354+ self .assertEqual (response .status_code , status .HTTP_200_OK )
355+ self .assertEqual (len (response .data ), 2 )
356+ purls = [package ["purl" ] for package in response .data ]
357+ self .
assertIn (
"pkg:pypi/[email protected] " ,
purls )
358+ self .
assertIn (
"pkg:npm/[email protected] " ,
purls )
359+
360+ def test_bulk_search_with_purl_only_true (self ):
361+ """
362+ Test bulk search with purl_only set to True.
363+ Should return only the PURLs of vulnerable packages.
364+ """
365+ url = reverse ("package-v2-bulk-search" )
366+ data = {
"purls" : [
"pkg:pypi/[email protected] " ,
"pkg:npm/[email protected] " ],
"purl_only" :
True }
367+ response = self .client .post (url , data , format = "json" )
368+ self .assertEqual (response .status_code , status .HTTP_200_OK )
369+ # Since purl_only=True, response should be a list of PURLs
370+ self .assertIsInstance (response .data , list )
371+ # Only vulnerable packages should be included
372+ self .assertEqual (len (response .data ), 1 )
373+ self .
assertEqual (
response .
data , [
"pkg:pypi/[email protected] " ])
374+
375+ def test_bulk_search_with_plain_purl_true (self ):
376+ """
377+ Test bulk search with plain_purl set to True.
378+ """
379+ url = reverse ("package-v2-bulk-search" )
380+ data = {
"purls" : [
"pkg:pypi/[email protected] " ,
"pkg:pypi/[email protected] " ],
"plain_purl" :
True }
381+ response = self .client .post (url , data , format = "json" )
382+ self .assertEqual (response .status_code , status .HTTP_200_OK )
383+ # Since plain_purl=True, packages with the same name and version are grouped
384+ self .assertEqual (len (response .data ), 1 )
385+ purls = [package ["purl" ] for package in response .data ]
386+ self .
assertIn (
"pkg:pypi/[email protected] " ,
purls [
0 ]
or "pkg:pypi/[email protected] " in purls [
0 ])
387+
388+ def test_bulk_search_with_purl_only_and_plain_purl_true (self ):
389+ """
390+ Test bulk search with purl_only and plain_purl both set to True.
391+ Should return only the plain PURLs of vulnerable packages.
392+ """
393+ url = reverse ("package-v2-bulk-search" )
394+ data = {
395+ "purls" : [
"pkg:pypi/[email protected] " ,
"pkg:pypi/[email protected] " ],
396+ "purl_only" : True ,
397+ "plain_purl" : True ,
398+ }
399+ response = self .client .post (url , data , format = "json" )
400+ self .assertEqual (response .status_code , status .HTTP_200_OK )
401+ # Response should be a list of plain PURLs
402+ self .assertIsInstance (response .data , list )
403+ # Only one plain PURL should be returned for vulnerable packages
404+ self .assertEqual (len (response .data ), 1 )
405+ self .
assertEqual (
response .
data , [
"pkg:pypi/[email protected] " ])
406+
407+ def test_bulk_search_with_invalid_purls (self ):
408+ """
409+ Test bulk search with invalid PURLs.
410+ """
411+ url = reverse ("package-v2-bulk-search" )
412+ data = {
"purls" : [
"pkg:pypi/[email protected] " ,
"pkg:npm/[email protected] " ]}
413+ response = self .client .post (url , data , format = "json" )
414+ self .assertEqual (response .status_code , status .HTTP_200_OK )
415+ self .assertEqual (len (response .data ), 0 )
416+
417+ def test_bulk_search_with_empty_purls (self ):
418+ """
419+ Test bulk search with empty purls list.
420+ Should return 400 Bad Request.
421+ """
422+ url = reverse ("package-v2-bulk-search" )
423+ data = {"purls" : []}
424+ response = self .client .post (url , data , format = "json" )
425+ self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
426+ self .assertIn ("error" , response .data )
427+ self .assertIn ("message" , response .data )
428+ self .assertEqual (response .data ["message" ], "A non-empty 'purls' list of PURLs is required." )
429+
430+ def test_all_vulnerable_packages (self ):
431+ """
432+ Test the 'all' endpoint that returns all vulnerable package URLs.
433+ """
434+ url = reverse ("package-v2-all" )
435+ response = self .client .get (url , format = "json" )
436+ self .assertEqual (response .status_code , status .HTTP_200_OK )
437+ # Since package1 and package3 are vulnerable, they should be returned
438+ expected_purls = [
"pkg:pypi/[email protected] " ]
439+ self .assertEqual (sorted (response .data ), sorted (expected_purls ))
440+
441+ def test_lookup_with_valid_purl (self ):
442+ """
443+ Test the 'lookup' endpoint with a valid PURL.
444+ """
445+ url = reverse ("package-v2-lookup" )
446+ data = {
"purl" :
"pkg:pypi/[email protected] " }
447+ response = self .client .post (url , data , format = "json" )
448+ self .assertEqual (response .status_code , status .HTTP_200_OK )
449+ self .assertEqual (len (response .data ), 1 )
450+ self .
assertEqual (
response .
data [
0 ][
"purl" ],
"pkg:pypi/[email protected] " )
451+ self .assertEqual (response .data [0 ]["affected_by_vulnerabilities" ], ["VCID-1234" ])
452+
453+ def test_lookup_with_invalid_purl (self ):
454+ """
455+ Test the 'lookup' endpoint with a PURL that does not exist.
456+ Should return an empty list.
457+ """
458+ url = reverse ("package-v2-lookup" )
459+ data = {
"purl" :
"pkg:pypi/[email protected] " }
460+ response = self .client .post (url , data , format = "json" )
461+ self .assertEqual (response .status_code , status .HTTP_200_OK )
462+ # No packages should be returned
463+ self .assertEqual (len (response .data ), 0 )
464+
465+ def test_lookup_with_missing_purl (self ):
466+ """
467+ Test the 'lookup' endpoint without providing a 'purl'.
468+ Should return 400 Bad Request.
469+ """
470+ url = reverse ("package-v2-lookup" )
471+ data = {}
472+ response = self .client .post (url , data , format = "json" )
473+ self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
474+ self .assertIn ("error" , response .data )
475+ self .assertIn ("message" , response .data )
476+ self .assertEqual (response .data ["message" ], "A 'purl' is required." )
477+
478+ def test_lookup_with_invalid_purl_format (self ):
479+ """
480+ Test the 'lookup' endpoint with an invalid PURL format.
481+ Should return 400 Bad Request.
482+ """
483+ url = reverse ("package-v2-lookup" )
484+ data = {"purl" : "invalid_purl_format" }
485+ response = self .client .post (url , data , format = "json" )
486+ self .assertEqual (response .status_code , status .HTTP_200_OK )
0 commit comments