11import datetime
2- from unittest import skip
32
43from django .contrib .auth .models import User
54from django .contrib .messages .storage .fallback import FallbackStorage
@@ -21,8 +20,8 @@ def create():
2120 settings .save ()
2221
2322 p = Product ()
24- p .Name = "Test Product"
25- p .Description = "Product for Testing Apply Template functionality"
23+ p .name = "Test Product"
24+ p .description = "Product for Testing Apply Template functionality"
2625 p .prod_type = Product_Type .objects .get (id = 1 )
2726 p .save ()
2827
@@ -53,7 +52,9 @@ def create():
5352 f .reporter = user
5453 f .last_reviewed = timezone .now ()
5554 f .last_reviewed_by = user
55+ f .cve = None # Set explicitly as it's required (blank=False)
5656 f .save ()
57+ return f
5758
5859
5960class FindingTemplateMother :
@@ -67,6 +68,7 @@ def create():
6768 tmp .mitigation = "Finding Template Mitigation"
6869 tmp .impact = "Finding Template Impact"
6970 tmp .save ()
71+ return tmp
7072
7173
7274class FindingTemplateTestUtil :
@@ -79,6 +81,7 @@ def create_user(is_staff):
7981 user_count = User .objects .count ()
8082 user = User ()
8183 user .is_staff = is_staff
84+ user .is_superuser = is_staff # Superuser has all permissions
8285 user .username = "TestUser" + str (user_count )
8386 user .save ()
8487 return user
@@ -104,15 +107,13 @@ def create_post_request(user, path, data):
104107 return post_request
105108
106109
107- @skip ("outdated so doesn't work with current fixture" )
108110class TestApplyFindingTemplate (DojoTestCase ):
109111 fixtures = ["dojo_testdata.json" ]
110112
111- apply_template_url = "finding/2/2/apply_template_to_finding"
112-
113113 def setUp (self ):
114- FindingMother .create ()
115- FindingTemplateMother .create ()
114+ self .finding = FindingMother .create ()
115+ self .template = FindingTemplateMother .create ()
116+ self .apply_template_url = f"finding/{ self .finding .id } /{ self .template .id } /apply_template_to_finding"
116117
117118 def make_request (self , user_is_staff , finding_id , template_id , data = None ):
118119 user = FindingTemplateTestUtil .create_user (user_is_staff )
@@ -122,20 +123,21 @@ def make_request(self, user_is_staff, finding_id, template_id, data=None):
122123 else :
123124 request = FindingTemplateTestUtil .create_get_request (user , self .apply_template_url )
124125
125- return views .apply_template_to_finding (request , finding_id , template_id )
126+ return views .apply_template_to_finding (request , fid = finding_id , tid = template_id )
126127
127128 def test_apply_template_to_finding_with_data_does_not_display_error_success (self ):
128- result = self .make_request (user_is_staff = True , finding_id = 1 , template_id = 1 ,
129+ result = self .make_request (user_is_staff = True , finding_id = self . finding . id , template_id = self . template . id ,
129130 data = {"title" : "Finding for Testing Apply Template functionality" ,
130131 "cwe" : "89" ,
131132 "severity" : "High" ,
132133 "description" : "Finding for Testing Apply Template Functionality" ,
133134 "mitigation" : "template mitigation" ,
134135 "impact" : "template impact" })
135- self .assertNotContains (result , "There appears to be errors on the form" , 302 )
136+ self .assertEqual (result .status_code , 302 )
137+ self .assertNotIn ("There appears to be errors on the form" , str (result ))
136138
137139 def test_apply_template_to_finding_with_data_returns_to_view_success (self ):
138- result = self .make_request (user_is_staff = True , finding_id = 1 , template_id = 1 ,
140+ result = self .make_request (user_is_staff = True , finding_id = self . finding . id , template_id = self . template . id ,
139141 data = {"title" : "Finding for Testing Apply Template functionality" ,
140142 "cwe" : "89" ,
141143 "severity" : "High" ,
@@ -144,7 +146,7 @@ def test_apply_template_to_finding_with_data_returns_to_view_success(self):
144146 "impact" : "template impact" })
145147 self .assertIsNotNone (result )
146148 self .assertEqual (302 , result .status_code )
147- self .assertEqual ("/finding/1 " , result .url )
149+ self .assertEqual (f "/finding/{ self . finding . id } " , result .url )
148150
149151 def test_apply_template_to_finding_with_data_saves_success (self ):
150152 test_title = "Finding for Testing Apply Template functionality"
@@ -154,24 +156,25 @@ def test_apply_template_to_finding_with_data_saves_success(self):
154156 test_mitigation = "template mitigation"
155157 test_impact = "template impact"
156158
157- self .make_request (user_is_staff = True , finding_id = 1 , template_id = 1 ,
159+ self .make_request (user_is_staff = True , finding_id = self . finding . id , template_id = self . template . id ,
158160 data = {"title" : test_title ,
159161 "cwe" : test_cwe ,
160162 "severity" : test_severity ,
161163 "description" : test_description ,
162164 "mitigation" : test_mitigation ,
163165 "impact" : test_impact })
164166
165- f = Finding .objects .get (id = 1 )
166- self .assertEqual (test_title , f .title )
167+ f = Finding .objects .get (id = self .finding .id )
168+ # Title is automatically title-cased by Finding.save()
169+ self .assertEqual ("Finding for Testing Apply Template Functionality" , f .title )
167170 self .assertEqual (test_cwe , f .cwe )
168171 self .assertEqual (test_severity , f .severity )
169172 self .assertEqual (test_description , f .description )
170173 self .assertEqual (test_mitigation , f .mitigation )
171174 self .assertEqual (test_impact , f .impact )
172175
173176 def test_unauthorized_apply_template_to_finding_fails (self ):
174- result = self .make_request (user_is_staff = False , finding_id = 1 , template_id = 1 ,
177+ result = self .make_request (user_is_staff = False , finding_id = self . finding . id , template_id = self . template . id ,
175178 data = {"title" : "Finding for Testing Apply Template functionality" ,
176179 "cwe" : "89" ,
177180 "severity" : "High" ,
@@ -183,19 +186,21 @@ def test_unauthorized_apply_template_to_finding_fails(self):
183186 self .assertIn ("login" , result .url )
184187
185188 def test_apply_template_to_finding_with_illegal_finding_fails (self ):
186- self .make_request (user_is_staff = True , finding_id = None , template_id = 1 )
189+ with self .assertRaises (Http404 ):
190+ self .make_request (user_is_staff = True , finding_id = 99999 , template_id = self .template .id )
187191
188192 def test_apply_template_to_finding_with_illegal_template_fails (self ):
189- self .make_request (user_is_staff = True , finding_id = 1 , template_id = None )
193+ with self .assertRaises (Http404 ):
194+ self .make_request (user_is_staff = True , finding_id = self .finding .id , template_id = 99999 )
190195
191196 def test_apply_template_to_finding_with_no_data_returns_view_success (self ):
192- result = self .make_request (user_is_staff = True , finding_id = 1 , template_id = 1 , data = None )
197+ result = self .make_request (user_is_staff = True , finding_id = self . finding . id , template_id = self . template . id , data = None )
193198 self .assertIsNotNone (result )
194199 self .assertEqual (302 , result .status_code )
195- self .assertEqual ("/finding/1 " , result .url )
200+ self .assertEqual (f "/finding/{ self . finding . id } " , result .url )
196201
197202 def test_apply_template_to_finding_without_required_field_displays_field_title_success (self ):
198- result = self .make_request (user_is_staff = True , finding_id = 1 , template_id = 1 ,
203+ result = self .make_request (user_is_staff = True , finding_id = self . finding . id , template_id = self . template . id ,
199204 data = {"title" : "" ,
200205 "cwe" : "89" ,
201206 "severity" : "High" ,
@@ -205,7 +210,7 @@ def test_apply_template_to_finding_without_required_field_displays_field_title_s
205210 self .assertContains (result , "The title is required." )
206211
207212 def test_apply_template_to_finding_without_required_field_displays_error_success (self ):
208- result = self .make_request (user_is_staff = True , finding_id = 1 , template_id = 1 ,
213+ result = self .make_request (user_is_staff = True , finding_id = self . finding . id , template_id = self . template . id ,
209214 data = {"title" : "" ,
210215 "cwe" : "89" ,
211216 "severity" : "High" ,
@@ -215,14 +220,13 @@ def test_apply_template_to_finding_without_required_field_displays_error_success
215220 self .assertContains (result , "There appears to be errors on the form" )
216221
217222
218- @skip ("outdated so doesn't work with current fixture" )
219223class TestFindTemplateToApply (DojoTestCase ):
220224 fixtures = ["dojo_testdata.json" ]
221- choose_template_url = "finding/2/find_template_to_apply"
222225
223226 def setUp (self ):
224- FindingMother .create ()
225- FindingTemplateMother .create ()
227+ self .finding = FindingMother .create ()
228+ self .template = FindingTemplateMother .create ()
229+ self .choose_template_url = f"finding/{ self .finding .id } /find_template_to_apply"
226230
227231 def make_request (self , user_is_staff , finding_id , data = None ):
228232 user = FindingTemplateTestUtil .create_user (user_is_staff )
@@ -232,34 +236,33 @@ def make_request(self, user_is_staff, finding_id, data=None):
232236 else :
233237 request = FindingTemplateTestUtil .create_get_request (user , self .choose_template_url )
234238
235- return views .find_template_to_apply (request , finding_id )
239+ return views .find_template_to_apply (request , fid = finding_id )
236240
237241 def test_unauthorized_find_template_to_apply_fails (self ):
238- result = self .make_request (user_is_staff = False , finding_id = 1 )
242+ result = self .make_request (user_is_staff = False , finding_id = self . finding . id )
239243 self .assertEqual (302 , result .status_code )
240244 self .assertIn ("login" , result .url )
241245
242246 def test_authorized_find_template_to_apply_success (self ):
243- result = self .make_request (user_is_staff = True , finding_id = 1 )
247+ result = self .make_request (user_is_staff = True , finding_id = self . finding . id )
244248 self .assertEqual (200 , result .status_code )
245249
246250 def test_find_template_to_apply_displays_templates_success (self ):
247- result = self .make_request (user_is_staff = True , finding_id = 1 )
251+ result = self .make_request (user_is_staff = True , finding_id = self . finding . id )
248252 self .assertContains (result , "Finding Template for Testing Apply Template functionality" )
249253
250254 def test_find_template_to_apply_displays_breadcrumb (self ):
251- result = self .make_request (user_is_staff = True , finding_id = 1 )
255+ result = self .make_request (user_is_staff = True , finding_id = self . finding . id )
252256 self .assertContains (result , "Apply Template to Finding" )
253257
254258
255- @skip ("outdated so doesn't work with current fixture" )
256259class TestChooseFindingTemplateOptions (DojoTestCase ):
257260 fixtures = ["dojo_testdata.json" ]
258- finding_template_options_url = "finding/2/2/choose_finding_template_options"
259261
260262 def setUp (self ):
261- FindingMother .create ()
262- FindingTemplateMother .create ()
263+ self .finding = FindingMother .create ()
264+ self .template = FindingTemplateMother .create ()
265+ self .finding_template_options_url = f"finding/{ self .template .id } /{ self .finding .id } /choose_finding_template_options"
263266
264267 def make_request (self , user_is_staff , finding_id , template_id , data = None ):
265268 user = FindingTemplateTestUtil .create_user (user_is_staff )
@@ -269,27 +272,25 @@ def make_request(self, user_is_staff, finding_id, template_id, data=None):
269272 else :
270273 request = FindingTemplateTestUtil .create_get_request (user , self .finding_template_options_url )
271274
272- return views .choose_finding_template_options (request , finding_id , template_id )
275+ return views .choose_finding_template_options (request , tid = template_id , fid = finding_id )
273276
274277 def test_unauthorized_choose_finding_template_options_fails (self ):
275- result = self .make_request (user_is_staff = False , finding_id = 1 , template_id = 1 )
278+ result = self .make_request (user_is_staff = False , finding_id = self . finding . id , template_id = self . template . id )
276279 self .assertEqual (302 , result .status_code )
277280 self .assertIn ("login" , result .url )
278281
279282 def test_authorized_choose_finding_template_options_success (self ):
280- result = self .make_request (user_is_staff = True , finding_id = 1 , template_id = 1 )
283+ result = self .make_request (user_is_staff = True , finding_id = self . finding . id , template_id = self . template . id )
281284 self .assertEqual (200 , result .status_code )
282285
283286 def test_choose_finding_template_options_with_invalid_finding_fails (self ):
284287 with self .assertRaises (Http404 ):
285- result = self .make_request (user_is_staff = True , finding_id = 0 , template_id = 1 )
286- self .assertEqual (404 , result .status_code )
288+ self .make_request (user_is_staff = True , finding_id = 0 , template_id = self .template .id )
287289
288290 def test_choose_finding_template_options_with_invalid_template_fails (self ):
289291 with self .assertRaises (Http404 ):
290- result = self .make_request (user_is_staff = True , finding_id = 1 , template_id = 0 )
291- self .assertEqual (404 , result .status_code )
292+ self .make_request (user_is_staff = True , finding_id = self .finding .id , template_id = 0 )
292293
293294 def test_choose_finding_template_options_with_valid_finding_and_template_renders_apply_finding_template_view (self ):
294- result = self .make_request (user_is_staff = True , finding_id = 1 , template_id = 1 )
295+ result = self .make_request (user_is_staff = True , finding_id = self . finding . id , template_id = self . template . id )
295296 self .assertContains (result , "<h3> Apply template to a Finding</h3>" )
0 commit comments