|
16 | 16 | from dojo.finding import views |
17 | 17 | from dojo.finding.helper import save_endpoints_template, save_vulnerability_ids_template |
18 | 18 | from dojo.models import ( |
| 19 | + Dojo_User, |
19 | 20 | Engagement, |
20 | 21 | Finding, |
21 | 22 | Finding_Template, |
@@ -107,9 +108,14 @@ def create_user(is_staff): |
107 | 108 |
|
108 | 109 | @staticmethod |
109 | 110 | def create_user_with_role(product, role_name, *, is_staff=False): |
110 | | - """Create a user with a specific role on a product""" |
| 111 | + """ |
| 112 | + Create a user with a specific role on a product. Under the |
| 113 | + legacy authorization model the role is informational only — |
| 114 | + callers needing actual access must add the user to |
| 115 | + product.authorized_users. |
| 116 | + """ |
111 | 117 | user_count = User.objects.count() |
112 | | - user = User() |
| 118 | + user = Dojo_User() |
113 | 119 | user.is_staff = is_staff |
114 | 120 | user.is_superuser = False |
115 | 121 | user.username = f"TestUser{role_name}{user_count}" |
@@ -219,41 +225,48 @@ def test_unauthorized_apply_template_to_finding_fails(self): |
219 | 225 | "impact": "template impact"}, |
220 | 226 | ) |
221 | 227 |
|
222 | | - def test_reader_role_cannot_apply_template(self): |
223 | | - """Test that a Reader role user (read-only) cannot apply template""" |
224 | | - reader_user = FindingTemplateTestUtil.create_user_with_role( |
225 | | - self.finding.test.engagement.product, "Reader", is_staff=False, |
226 | | - ) |
| 228 | + def test_authorized_user_can_apply_template(self): |
| 229 | + """ |
| 230 | + Legacy: any user in product.authorized_users can apply a template |
| 231 | + (Reader/Writer/Maintainer/Owner all collapse to one bit of access). |
| 232 | + """ |
| 233 | + product = self.finding.test.engagement.product |
| 234 | + member = FindingTemplateTestUtil.create_user_with_role(product, "Writer", is_staff=False) |
| 235 | + product.authorized_users.add(member) |
227 | 236 | request = FindingTemplateTestUtil.create_post_request( |
228 | | - reader_user, self.apply_template_url, |
| 237 | + member, self.apply_template_url, |
229 | 238 | data={"title": "Finding for Testing Apply Template functionality", |
230 | 239 | "cwe": "89", |
231 | 240 | "severity": "High", |
232 | 241 | "description": "Finding for Testing Apply Template Functionality", |
233 | 242 | "mitigation": "template mitigation", |
234 | 243 | "impact": "template impact"}, |
235 | 244 | ) |
236 | | - with impersonate(reader_user), self.assertRaises(PermissionDenied): |
237 | | - views.apply_template_to_finding(request, fid=self.finding.id, tid=self.template.id) |
| 245 | + with impersonate(member): |
| 246 | + result = views.apply_template_to_finding(request, fid=self.finding.id, tid=self.template.id) |
| 247 | + self.assertEqual(302, result.status_code) |
| 248 | + self.assertEqual(f"/finding/{self.finding.id}", result.url) |
238 | 249 |
|
239 | | - def test_writer_role_can_apply_template(self): |
240 | | - """Test that a Writer role user (non-staff) can apply template""" |
241 | | - writer_user = FindingTemplateTestUtil.create_user_with_role( |
242 | | - self.finding.test.engagement.product, "Writer", is_staff=False, |
243 | | - ) |
| 250 | + def test_non_member_cannot_apply_template(self): |
| 251 | + """ |
| 252 | + Legacy: a user with no authorized_users membership and no staff |
| 253 | + flag is denied — covers the case test_reader_role_cannot_apply_template |
| 254 | + used to assert under RBAC. |
| 255 | + """ |
| 256 | + product = self.finding.test.engagement.product |
| 257 | + outsider = FindingTemplateTestUtil.create_user_with_role(product, "Reader", is_staff=False) |
| 258 | + # Deliberately NOT added to product.authorized_users. |
244 | 259 | request = FindingTemplateTestUtil.create_post_request( |
245 | | - writer_user, self.apply_template_url, |
| 260 | + outsider, self.apply_template_url, |
246 | 261 | data={"title": "Finding for Testing Apply Template functionality", |
247 | 262 | "cwe": "89", |
248 | 263 | "severity": "High", |
249 | 264 | "description": "Finding for Testing Apply Template Functionality", |
250 | 265 | "mitigation": "template mitigation", |
251 | 266 | "impact": "template impact"}, |
252 | 267 | ) |
253 | | - with impersonate(writer_user): |
254 | | - result = views.apply_template_to_finding(request, fid=self.finding.id, tid=self.template.id) |
255 | | - self.assertEqual(302, result.status_code) |
256 | | - self.assertEqual(f"/finding/{self.finding.id}", result.url) |
| 268 | + with impersonate(outsider), self.assertRaises(PermissionDenied): |
| 269 | + views.apply_template_to_finding(request, fid=self.finding.id, tid=self.template.id) |
257 | 270 |
|
258 | 271 | def test_apply_template_to_finding_with_illegal_finding_fails(self): |
259 | 272 | with self.assertRaises(Http404): |
|
0 commit comments