1111import os
1212from urllib .parse import quote
1313
14+ from django .core .cache import cache
1415from django .test import TestCase
1516from django .test import TransactionTestCase
1617from django .test .client import RequestFactory
@@ -452,10 +453,8 @@ def add_aliases(vuln, aliases):
452453
453454class APIPerformanceTest (TestCase ):
454455 def setUp (self ):
455- self .user = ApiUser .objects .create_api_user (username = "e@mail.com" )
456- self .auth = f"Token { self .user .auth_token .key } "
456+ cache .clear ()
457457 self .csrf_client = APIClient (enforce_csrf_checks = True )
458- self .csrf_client .credentials (HTTP_AUTHORIZATION = self .auth )
459458
460459 # This setup creates the following data:
461460 # vulnerabilities: vul1, vul2, vul3
@@ -503,7 +502,7 @@ def setUp(self):
503502 set_as_fixing (package = self .pkg_2_13_2 , vulnerability = self .vul1 )
504503
505504 def test_api_packages_all_num_queries (self ):
506- with self .assertNumQueries (4 ):
505+ with self .assertNumQueries (3 ):
507506 # There are 4 queries:
508507 # 1. SAVEPOINT
509508 # 2. Authenticating user
@@ -519,22 +518,22 @@ def test_api_packages_all_num_queries(self):
519518 ]
520519
521520 def test_api_packages_single_num_queries (self ):
522- with self .assertNumQueries (8 ):
521+ with self .assertNumQueries (7 ):
523522 self .csrf_client .get (f"/api/packages/{ self .pkg_2_14_0_rc1 .id } " , format = "json" )
524523
525524 def test_api_packages_single_with_purl_in_query_num_queries (self ):
526- with self .assertNumQueries (9 ):
525+ with self .assertNumQueries (8 ):
527526 self .csrf_client .get (f"/api/packages/?purl={ self .pkg_2_14_0_rc1 .purl } " , format = "json" )
528527
529528 def test_api_packages_single_with_purl_no_version_in_query_num_queries (self ):
530- with self .assertNumQueries (64 ):
529+ with self .assertNumQueries (63 ):
531530 self .csrf_client .get (
532531 f"/api/packages/?purl=pkg:maven/com.fasterxml.jackson.core/jackson-databind" ,
533532 format = "json" ,
534533 )
535534
536535 def test_api_packages_bulk_search (self ):
537- with self .assertNumQueries (45 ):
536+ with self .assertNumQueries (44 ):
538537 packages = [self .pkg_2_12_6 , self .pkg_2_12_6_1 , self .pkg_2_13_1 ]
539538 purls = [p .purl for p in packages ]
540539
@@ -547,7 +546,7 @@ def test_api_packages_bulk_search(self):
547546 ).json ()
548547
549548 def test_api_packages_with_lookup (self ):
550- with self .assertNumQueries (14 ):
549+ with self .assertNumQueries (13 ):
551550 data = {"purl" : self .pkg_2_12_6 .purl }
552551
553552 resp = self .csrf_client .post (
@@ -557,7 +556,7 @@ def test_api_packages_with_lookup(self):
557556 ).json ()
558557
559558 def test_api_packages_bulk_lookup (self ):
560- with self .assertNumQueries (45 ):
559+ with self .assertNumQueries (44 ):
561560 packages = [self .pkg_2_12_6 , self .pkg_2_12_6_1 , self .pkg_2_13_1 ]
562561 purls = [p .purl for p in packages ]
563562
@@ -572,10 +571,8 @@ def test_api_packages_bulk_lookup(self):
572571
573572class APITestCasePackage (TestCase ):
574573 def setUp (self ):
575- self .user = ApiUser .objects .create_api_user (username = "e@mail.com" )
576- self .auth = f"Token { self .user .auth_token .key } "
574+ cache .clear ()
577575 self .csrf_client = APIClient (enforce_csrf_checks = True )
578- self .csrf_client .credentials (HTTP_AUTHORIZATION = self .auth )
579576
580577 # This setup creates the following data:
581578 # vulnerabilities: vul1, vul2, vul3
@@ -766,7 +763,7 @@ def test_api_with_wrong_namespace_filter(self):
766763 self .assertEqual (response ["count" ], 0 )
767764
768765 def test_api_with_all_vulnerable_packages (self ):
769- with self .assertNumQueries (4 ):
766+ with self .assertNumQueries (3 ):
770767 # There are 4 queries:
771768 # 1. SAVEPOINT
772769 # 2. Authenticating user
0 commit comments