11import logging
22from contextlib import contextmanager
3- from unittest .mock import patch
43
54from crum import impersonate
65from django .contrib .contenttypes .models import ContentType
2019 Product_Type ,
2120 Test ,
2221 User ,
22+ UserContactInfo ,
2323)
2424
2525from .dojo_test_case import DojoTestCase , get_unit_tests_scans_path
2626
27+ logging .basicConfig (level = logging .DEBUG )
2728logger = logging .getLogger (__name__ )
2829
30+
2931STACK_HAWK_FILENAME = get_unit_tests_scans_path ("stackhawk" ) / "stackhawk_many_vul_without_duplicated_findings.json"
3032STACK_HAWK_SUBSET_FILENAME = get_unit_tests_scans_path ("stackhawk" ) / "stackhawk_many_vul_without_duplicated_findings_subset.json"
3133STACK_HAWK_SCAN_TYPE = "StackHawk HawkScan"
@@ -39,6 +41,9 @@ class TestDojoImporterPerformance(DojoTestCase):
3941 def setUp (self ):
4042 super ().setUp ()
4143
44+ testuser = User .objects .create (username = "admin" )
45+ UserContactInfo .objects .create (user = testuser , block_execution = False )
46+
4247 self .system_settings (enable_webhooks_notifications = False )
4348 self .system_settings (enable_product_grade = False )
4449 self .system_settings (enable_github = False )
@@ -67,6 +72,14 @@ def assertNumAsyncTask(self, num):
6772 )
6873 raise self .failureException (msg )
6974
75+ tasks = dojo_async_task_counter .get_tasks ()
76+ tasks_str = "\n " .join (str (task ) for task in tasks )
77+ msg = (
78+ f"Correct number of { num } celery tasks were created.\n "
79+ f"Tasks created:\n { tasks_str } "
80+ )
81+ logger .debug (msg )
82+
7083 def import_reimport_performance (self , expected_num_queries1 , expected_num_async_tasks1 , expected_num_queries2 , expected_num_async_tasks2 , expected_num_queries3 , expected_num_async_tasks3 ):
7184 """
7285 Log output can be quite large as when the assertNumQueries fails, all queries are printed.
@@ -158,53 +171,61 @@ def import_reimport_performance(self, expected_num_queries1, expected_num_async_
158171 reimporter = DefaultReImporter (** reimport_options )
159172 test , _ , _len_new_findings , _len_closed_findings , _ , _ , _ = reimporter .process_scan (scan )
160173
161- def test_import_reimport_reimport_performance (self ):
174+ # patch the we_want_async decorator to always return True so we don't depend on block_execution flag shenanigans
175+ # @patch("dojo.decorators.we_want_async", return_value=True)
176+ # def test_import_reimport_reimport_performance_async(self, mock):
177+ def test_import_reimport_reimport_performance_async (self ):
162178 self .import_reimport_performance (
163- expected_num_queries1 = 712 ,
179+ expected_num_queries1 = 682 ,
164180 expected_num_async_tasks1 = 10 ,
165- expected_num_queries2 = 656 ,
181+ expected_num_queries2 = 610 ,
166182 expected_num_async_tasks2 = 22 ,
167- expected_num_queries3 = 332 ,
183+ expected_num_queries3 = 292 ,
168184 expected_num_async_tasks3 = 20 ,
169185 )
170186
171- @patch ("dojo.decorators.we_want_async" , return_value = False )
172- def test_import_reimport_reimport_performance_no_async (self , mock ):
187+ # @patch("dojo.decorators.we_want_async", return_value=False)
188+ # def test_import_reimport_reimport_performance_no_async(self, mock):
189+ def test_import_reimport_reimport_performance_no_async (self ):
173190 """
174191 This test checks the performance of the importers when they are run in sync mode.
175192 The reason for this is that we also want to be aware of when a PR affects the number of queries
176193 or async tasks created by a background task.
177194 The impersonate context manager above does not work as expected for disabling async,
178195 so we patch the we_want_async decorator to always return False.
179196 """
197+ testuser = User .objects .get (username = "admin" )
198+ testuser .usercontactinfo .block_execution = True
199+ testuser .usercontactinfo .save ()
180200 self .import_reimport_performance (
181- expected_num_queries1 = 712 ,
201+ expected_num_queries1 = 682 ,
182202 expected_num_async_tasks1 = 10 ,
183- expected_num_queries2 = 656 ,
203+ expected_num_queries2 = 615 ,
184204 expected_num_async_tasks2 = 22 ,
185- expected_num_queries3 = 332 ,
205+ expected_num_queries3 = 297 ,
186206 expected_num_async_tasks3 = 20 ,
187207 )
188208
189- @patch ("dojo.decorators.we_want_async" , return_value = False )
190- def test_import_reimport_reimport_performance_no_async_with_product_grading (self , mock ):
209+ # @patch("dojo.decorators.we_want_async", return_value=False)
210+ # def test_import_reimport_reimport_performance_no_async_with_product_grading(self, mock):
211+ def test_import_reimport_reimport_performance_no_async_with_product_grading (self ):
191212 """
192213 This test checks the performance of the importers when they are run in sync mode.
193214 The reason for this is that we also want to be aware of when a PR affects the number of queries
194215 or async tasks created by a background task.
195216 The impersonate context manager above does not work as expected for disabling async,
196217 so we patch the we_want_async decorator to always return False.
197218 """
219+ testuser = User .objects .get (username = "admin" )
220+ testuser .usercontactinfo .block_execution = True
221+ testuser .usercontactinfo .save ()
198222 self .system_settings (enable_product_grade = True )
199- # Refresh the cache with the new settings
200- from dojo .middleware import DojoSytemSettingsMiddleware
201- DojoSytemSettingsMiddleware .load ()
202223
203224 self .import_reimport_performance (
204- expected_num_queries1 = 732 ,
225+ expected_num_queries1 = 702 ,
205226 expected_num_async_tasks1 = 15 ,
206- expected_num_queries2 = 686 ,
227+ expected_num_queries2 = 645 ,
207228 expected_num_async_tasks2 = 28 ,
208- expected_num_queries3 = 357 ,
229+ expected_num_queries3 = 322 ,
209230 expected_num_async_tasks3 = 25 ,
210231 )
0 commit comments