@@ -388,7 +388,7 @@ def add_aliases(vuln, aliases):
388388        Alias .objects .create (alias = alias , vulnerability = vuln )
389389
390390
391- class  APITestCasePackage (TestCase ):
391+ class  APIPerformanceTest (TestCase ):
392392    def  setUp (self ):
393393        self .
user  =  ApiUser .
objects .
create_api_user (
username = "[email protected] " )
 394394        self .auth  =  f"Token { self .user .auth_token .key }  " 
@@ -439,18 +439,124 @@ def setUp(self):
439439        set_as_affected_by (package = self .pkg_2_13_2 , vulnerability = self .vul2 )
440440        set_as_fixing (package = self .pkg_2_13_2 , vulnerability = self .vul1 )
441441
442-     def  test_api_with_package_with_no_vulnerabilities (self ):
443-         affected_vulnerabilities  =  []
444-         vuln  =  {
445-             "foo" : "bar" ,
446-         }
442+     def  test_api_packages_all_num_queries (self ):
443+         with  self .assertNumQueries (4 ):
444+             # There are 4 queries: 
445+             # 1. SAVEPOINT 
446+             # 2. Authenticating user 
447+             # 3. Get all vulnerable packages 
448+             # 4. RELEASE SAVEPOINT 
449+             response  =  self .csrf_client .get (f"/api/packages/all" , format = "json" ).data 
450+ 
451+             assert  len (response ) ==  3 
452+             assert  response  ==  [
453+                 "pkg:maven/com.fasterxml.jackson.core/[email protected] " ,
 454+                 "pkg:maven/com.fasterxml.jackson.core/[email protected] " ,
 455+                 "pkg:maven/com.fasterxml.jackson.core/[email protected] " ,
 456+             ]
457+ 
458+     def  test_api_packages_single_num_queries (self ):
459+         with  self .assertNumQueries (8 ):
460+             self .csrf_client .get (f"/api/packages/{ self .pkg_2_14_0_rc1 .id }  " , format = "json" )
461+ 
462+     def  test_api_packages_single_with_purl_in_query_num_queries (self ):
463+         with  self .assertNumQueries (9 ):
464+             self .csrf_client .get (f"/api/packages/?purl={ self .pkg_2_14_0_rc1 .purl }  " , format = "json" )
465+ 
466+     def  test_api_packages_single_with_purl_no_version_in_query_num_queries (self ):
467+         with  self .assertNumQueries (64 ):
468+             self .csrf_client .get (
469+                 f"/api/packages/?purl=pkg:maven/com.fasterxml.jackson.core/jackson-databind" ,
470+                 format = "json" ,
471+             )
447472
448-         package_with_no_vulnerabilities  =  MinimalPackageSerializer .get_vulnerability (
449-             self ,
450-             vuln ,
473+     def  test_api_packages_bulk_search (self ):
474+         with  self .assertNumQueries (45 ):
475+             packages  =  [self .pkg_2_12_6 , self .pkg_2_12_6_1 , self .pkg_2_13_1 ]
476+             purls  =  [p .purl  for  p  in  packages ]
477+ 
478+             data  =  {"purls" : purls , "purl_only" : False , "plain_purl" : True }
479+ 
480+             resp  =  self .csrf_client .post (
481+                 f"/api/packages/bulk_search" ,
482+                 data = json .dumps (data ),
483+                 content_type = "application/json" ,
484+             ).json ()
485+ 
486+     def  test_api_packages_with_lookup (self ):
487+         with  self .assertNumQueries (14 ):
488+             data  =  {"purl" : self .pkg_2_12_6 .purl }
489+ 
490+             resp  =  self .csrf_client .post (
491+                 f"/api/packages/lookup" ,
492+                 data = json .dumps (data ),
493+                 content_type = "application/json" ,
494+             ).json ()
495+ 
496+     def  test_api_packages_bulk_lookup (self ):
497+         with  self .assertNumQueries (45 ):
498+             packages  =  [self .pkg_2_12_6 , self .pkg_2_12_6_1 , self .pkg_2_13_1 ]
499+             purls  =  [p .purl  for  p  in  packages ]
500+ 
501+             data  =  {"purls" : purls }
502+ 
503+             resp  =  self .csrf_client .post (
504+                 f"/api/packages/bulk_lookup" ,
505+                 data = json .dumps (data ),
506+                 content_type = "application/json" ,
507+             ).json ()
508+ 
509+ 
510+ class  APITestCasePackage (TestCase ):
511+     def  setUp (self ):
512+         self .
user  =  ApiUser .
objects .
create_api_user (
username = "[email protected] " )
 513+         self .auth  =  f"Token { self .user .auth_token .key }  " 
514+         self .csrf_client  =  APIClient (enforce_csrf_checks = True )
515+         self .csrf_client .credentials (HTTP_AUTHORIZATION = self .auth )
516+ 
517+         # This setup creates the following data: 
518+         # vulnerabilities: vul1, vul2, vul3 
519+         # pkg:maven/com.fasterxml.jackson.core/jackson-databind 
520+         # with these versions: 
521+         # pkg_2_12_6:     @ 2.12.6       affected by        fixing vul3 
522+         # pkg_2_12_6_1:   @ 2.12.6.1     affected by vul2   fixing vul1 
523+         # pkg_2_13_1:     @ 2.13.1       affected by vul1   fixing vul3 
524+         # pkg_2_13_2:     @ 2.13.2       affected by vul2   fixing vul1 
525+         # pkg_2_14_0_rc1: @ 2.14.0-rc1   affected by        fixing 
526+ 
527+         # searched-for pkg's vuln 
528+         self .vul1  =  create_vuln ("VCID-vul1-vul1-vul1" , ["CVE-2020-36518" , "GHSA-57j2-w4cx-62h2" ])
529+         self .vul2  =  create_vuln ("VCID-vul2-vul2-vul2" )
530+         # This is the vuln fixed by the searched-for pkg -- and by a lesser version (created below), 
531+         # which WILL be included in the API 
532+         self .vul3  =  create_vuln ("VCID-vul3-vul3-vul3" , ["CVE-2021-46877" , "GHSA-3x8x-79m2-3w2w" ])
533+ 
534+         from_purl  =  Package .objects .from_purl 
535+         # lesser-version pkg that also fixes the vuln fixed by the searched-for pkg 
536+         self .
pkg_2_12_6  =  from_purl (
"pkg:maven/com.fasterxml.jackson.core/[email protected] " )
 537+         # this is a lesser version omitted from the API that fixes searched-for pkg's vuln 
538+         self .pkg_2_12_6_1  =  from_purl (
539+             "pkg:maven/com.fasterxml.jackson.core/[email protected] "  540+         )
541+         # searched-for pkg 
542+         self .
pkg_2_13_1  =  from_purl (
"pkg:maven/com.fasterxml.jackson.core/[email protected] " )
 543+         # this is a greater version that fixes searched-for pkg's vuln 
544+         self .
pkg_2_13_2  =  from_purl (
"pkg:maven/com.fasterxml.jackson.core/[email protected] " )
 545+         # This addresses both next and latest non-vulnerable pkg 
546+         self .pkg_2_14_0_rc1  =  from_purl (
547+             "pkg:maven/com.fasterxml.jackson.core/[email protected] "  451548        )
452549
453-         assert  package_with_no_vulnerabilities  is  None 
550+         set_as_fixing (package = self .pkg_2_12_6 , vulnerability = self .vul3 )
551+ 
552+         set_as_affected_by (package = self .pkg_2_12_6_1 , vulnerability = self .vul2 )
553+         set_as_fixing (package = self .pkg_2_12_6_1 , vulnerability = self .vul1 )
554+ 
555+         set_as_affected_by (package = self .pkg_2_13_1 , vulnerability = self .vul1 )
556+         set_as_fixing (package = self .pkg_2_13_1 , vulnerability = self .vul3 )
557+ 
558+         set_as_affected_by (package = self .pkg_2_13_2 , vulnerability = self .vul2 )
559+         set_as_fixing (package = self .pkg_2_13_2 , vulnerability = self .vul1 )
454560
455561    def  test_api_with_lesser_and_greater_fixed_by_packages (self ):
456562        response  =  self .csrf_client .get (f"/api/packages/{ self .pkg_2_13_1 .id }  " , format = "json" ).data 
0 commit comments