@@ -209,35 +209,32 @@ def test_apply_template_to_finding_with_data_saves_success(self):
209209
210210 def test_unauthorized_apply_template_to_finding_fails (self ):
211211 """Test that a non-superuser without permissions cannot apply template"""
212- user = FindingTemplateTestUtil .create_user (is_staff = False )
213- self .client .force_login (user )
214- url = f"/finding/{ self .finding .id } /{ self .template .id } /apply_template_to_finding"
215- response = self .client .post (url , data = {
216- "title" : "Finding for Testing Apply Template functionality" ,
217- "cwe" : "89" ,
218- "severity" : "High" ,
219- "description" : "Finding for Testing Apply Template Functionality" ,
220- "mitigation" : "template mitigation" ,
221- "impact" : "template impact" ,
222- })
223- self .assertEqual (response .status_code , 403 )
212+ with self .assertRaises (PermissionDenied ):
213+ self .make_request (user_is_staff = False , finding_id = self .finding .id , template_id = self .template .id ,
214+ data = {"title" : "Finding for Testing Apply Template functionality" ,
215+ "cwe" : "89" ,
216+ "severity" : "High" ,
217+ "description" : "Finding for Testing Apply Template Functionality" ,
218+ "mitigation" : "template mitigation" ,
219+ "impact" : "template impact" },
220+ )
224221
225222 def test_reader_role_cannot_apply_template (self ):
226223 """Test that a Reader role user (read-only) cannot apply template"""
227224 reader_user = FindingTemplateTestUtil .create_user_with_role (
228225 self .finding .test .engagement .product , "Reader" , is_staff = False ,
229226 )
230- self . client . force_login ( reader_user )
231- url = f"/finding/ { self .finding . id } / { self . template . id } /apply_template_to_finding"
232- response = self . client . post ( url , data = {
233- "title" : "Finding for Testing Apply Template functionality " ,
234- "cwe " : "89 " ,
235- "severity " : "High " ,
236- "description" : "Finding for Testing Apply Template Functionality " ,
237- "mitigation " : "template mitigation" ,
238- "impact" : "template impact" ,
239- })
240- self . assertEqual ( response . status_code , 403 )
227+ request = FindingTemplateTestUtil . create_post_request (
228+ reader_user , self .apply_template_url ,
229+ data = {"title" : "Finding for Testing Apply Template functionality" ,
230+ "cwe" : "89 " ,
231+ "severity " : "High " ,
232+ "description " : "Finding for Testing Apply Template Functionality " ,
233+ "mitigation" : "template mitigation " ,
234+ "impact " : "template impact" } ,
235+ )
236+ with impersonate ( reader_user ), self . assertRaises ( PermissionDenied ):
237+ views . apply_template_to_finding ( request , fid = self . finding . id , tid = self . template . id )
241238
242239 def test_writer_role_can_apply_template (self ):
243240 """Test that a Writer role user (non-staff) can apply template"""
@@ -351,11 +348,8 @@ def make_request(self, user_is_staff, finding_id, template_id, data=None):
351348 return views .choose_finding_template_options (request , tid = template_id , fid = finding_id )
352349
353350 def test_unauthorized_choose_finding_template_options_fails (self ):
354- user = FindingTemplateTestUtil .create_user (is_staff = False )
355- self .client .force_login (user )
356- url = f"/finding/{ self .template .id } /{ self .finding .id } /choose_finding_template_options"
357- response = self .client .get (url )
358- self .assertEqual (response .status_code , 403 )
351+ with self .assertRaises (PermissionDenied ):
352+ self .make_request (user_is_staff = False , finding_id = self .finding .id , template_id = self .template .id )
359353
360354 def test_authorized_choose_finding_template_options_success (self ):
361355 result = self .make_request (user_is_staff = True , finding_id = self .finding .id , template_id = self .template .id )
@@ -446,10 +440,9 @@ def test_mktemplate_requires_permission(self):
446440 user .is_superuser = False
447441 user .save ()
448442
449- self .client .force_login (user )
450- url = f"/finding/{ self .finding .id } /mktemplate"
451- response = self .client .get (url )
452- self .assertEqual (response .status_code , 403 )
443+ # Should raise PermissionDenied
444+ with self .assertRaises (PermissionDenied ):
445+ self .make_request (user , self .finding .id )
453446
454447
455448@versioned_fixtures
@@ -596,10 +589,9 @@ def test_add_finding_from_template_requires_permission(self):
596589 unauthorized_user .is_superuser = False
597590 unauthorized_user .save ()
598591
599- self .client .force_login (unauthorized_user )
600- url = f"/test/{ self .test .id } /add_findings/{ self .template .id } "
601- response = self .client .get (url )
602- self .assertEqual (response .status_code , 403 )
592+ # Should raise PermissionDenied
593+ with self .assertRaises (PermissionDenied ):
594+ self .make_get_request (unauthorized_user , self .test .id , self .template .id )
603595
604596 def test_add_finding_from_template_updates_template_last_used (self ):
605597 """Test that template.last_used is updated when creating finding"""
0 commit comments