|
6 | 6 |
|
7 | 7 | # Ghostwriter Libraries |
8 | 8 | from ghostwriter.factories import ( |
| 9 | + ClientFactory, |
| 10 | + DocTypeFactory, |
9 | 11 | EvidenceOnFindingFactory, |
10 | 12 | EvidenceOnReportFactory, |
11 | 13 | FindingNoteFactory, |
|
16 | 18 | ReportFindingLinkFactory, |
17 | 19 | ReportObservationLinkFactory, |
18 | 20 | ReportDocxTemplateFactory, |
| 21 | + ReportPptxTemplateFactory, |
19 | 22 | SeverityFactory, |
20 | 23 | UserFactory, |
21 | 24 | ) |
@@ -104,6 +107,111 @@ def test_invalid_pptx_template(self): |
104 | 107 | self.assertEqual(len(errors), 1) |
105 | 108 | self.assertEqual(errors[0].code, "invalid_choice") |
106 | 109 |
|
| 110 | + def test_client_scoped_templates_are_limited_to_selected_project_client(self): |
| 111 | + ProjectAssignmentFactory(operator=self.user, project=self.project) |
| 112 | + same_client_docx = ReportDocxTemplateFactory(client=self.project.client) |
| 113 | + same_client_pptx = ReportPptxTemplateFactory(client=self.project.client) |
| 114 | + foreign_docx = ReportDocxTemplateFactory(client=ClientFactory()) |
| 115 | + foreign_pptx = ReportPptxTemplateFactory(client=ClientFactory()) |
| 116 | + |
| 117 | + form = self.form_data( |
| 118 | + user=self.user, |
| 119 | + title="Scoped Template Report", |
| 120 | + archived=False, |
| 121 | + project_id=self.project.pk, |
| 122 | + docx_template_id=same_client_docx.pk, |
| 123 | + pptx_template_id=same_client_pptx.pk, |
| 124 | + delivered=False, |
| 125 | + ) |
| 126 | + |
| 127 | + self.assertTrue(form.is_valid(), form.errors) |
| 128 | + self.assertIn(same_client_docx, form.fields["docx_template"].queryset) |
| 129 | + self.assertIn(same_client_pptx, form.fields["pptx_template"].queryset) |
| 130 | + self.assertNotIn(foreign_docx, form.fields["docx_template"].queryset) |
| 131 | + self.assertNotIn(foreign_pptx, form.fields["pptx_template"].queryset) |
| 132 | + |
| 133 | + def test_client_scoped_template_for_other_client_is_invalid(self): |
| 134 | + ProjectAssignmentFactory(operator=self.user, project=self.project) |
| 135 | + foreign_docx = ReportDocxTemplateFactory(client=ClientFactory()) |
| 136 | + foreign_pptx = ReportPptxTemplateFactory(client=ClientFactory()) |
| 137 | + |
| 138 | + form = self.form_data( |
| 139 | + user=self.user, |
| 140 | + title="Foreign Template Report", |
| 141 | + archived=False, |
| 142 | + project_id=self.project.pk, |
| 143 | + docx_template_id=foreign_docx.pk, |
| 144 | + pptx_template_id=foreign_pptx.pk, |
| 145 | + delivered=False, |
| 146 | + ) |
| 147 | + |
| 148 | + self.assertFalse(form.is_valid()) |
| 149 | + self.assertEqual(form["docx_template"].errors.as_data()[0].code, "invalid_choice") |
| 150 | + self.assertEqual(form["pptx_template"].errors.as_data()[0].code, "invalid_choice") |
| 151 | + |
| 152 | + def test_disabled_project_field_ignores_submitted_project_for_template_choices(self): |
| 153 | + ProjectAssignmentFactory(operator=self.user, project=self.project) |
| 154 | + other_project = ProjectFactory() |
| 155 | + other_docx = ReportDocxTemplateFactory(client=other_project.client) |
| 156 | + other_pptx = ReportPptxTemplateFactory(client=other_project.client) |
| 157 | + |
| 158 | + form = ReportForm( |
| 159 | + user=self.user, |
| 160 | + project=self.project, |
| 161 | + instance=self.report, |
| 162 | + data={ |
| 163 | + "title": "Crafted Report Update", |
| 164 | + "archived": False, |
| 165 | + "project": other_project.pk, |
| 166 | + "docx_template": other_docx.pk, |
| 167 | + "pptx_template": other_pptx.pk, |
| 168 | + "delivered": False, |
| 169 | + }, |
| 170 | + ) |
| 171 | + |
| 172 | + self.assertTrue(form.fields["project"].disabled) |
| 173 | + self.assertFalse(form.is_valid()) |
| 174 | + self.assertEqual(form["docx_template"].errors.as_data()[0].code, "invalid_choice") |
| 175 | + self.assertEqual(form["pptx_template"].errors.as_data()[0].code, "invalid_choice") |
| 176 | + |
| 177 | + def test_template_choices_ignore_inaccessible_submitted_project(self): |
| 178 | + ProjectAssignmentFactory(operator=self.user, project=self.project) |
| 179 | + inaccessible_project = ProjectFactory() |
| 180 | + inaccessible_docx = ReportDocxTemplateFactory(client=inaccessible_project.client) |
| 181 | + inaccessible_pptx = ReportPptxTemplateFactory(client=inaccessible_project.client) |
| 182 | + |
| 183 | + form = self.form_data( |
| 184 | + user=self.user, |
| 185 | + title="Inaccessible Project Template Report", |
| 186 | + archived=False, |
| 187 | + project_id=inaccessible_project.pk, |
| 188 | + docx_template_id=inaccessible_docx.pk, |
| 189 | + pptx_template_id=inaccessible_pptx.pk, |
| 190 | + delivered=False, |
| 191 | + ) |
| 192 | + |
| 193 | + self.assertFalse(form.fields["project"].disabled) |
| 194 | + self.assertNotIn(inaccessible_docx, form.fields["docx_template"].queryset) |
| 195 | + self.assertNotIn(inaccessible_pptx, form.fields["pptx_template"].queryset) |
| 196 | + self.assertFalse(form.is_valid()) |
| 197 | + self.assertEqual(form["project"].errors.as_data()[0].code, "invalid_choice") |
| 198 | + self.assertEqual(form["docx_template"].errors.as_data()[0].code, "invalid_choice") |
| 199 | + self.assertEqual(form["pptx_template"].errors.as_data()[0].code, "invalid_choice") |
| 200 | + |
| 201 | + def test_template_choices_handle_non_integer_submitted_project(self): |
| 202 | + form = self.form_data( |
| 203 | + user=self.user, |
| 204 | + title="Invalid Project Template Report", |
| 205 | + archived=False, |
| 206 | + project_id="not-a-project-id", |
| 207 | + docx_template_id=self.report.docx_template.pk, |
| 208 | + pptx_template_id=self.report.pptx_template.pk, |
| 209 | + delivered=False, |
| 210 | + ) |
| 211 | + |
| 212 | + self.assertFalse(form.is_valid()) |
| 213 | + self.assertEqual(form["project"].errors.as_data()[0].code, "invalid_choice") |
| 214 | + |
107 | 215 |
|
108 | 216 | class ReportObservationLinkUpdateFormTests(TestCase): |
109 | 217 | """Collection of tests for :form:`reporting.ReportObservationLinkForm`.""" |
@@ -486,6 +594,48 @@ def test_mismatch_pptx_template(self): |
486 | 594 | self.assertEqual(len(errors), 1) |
487 | 595 | self.assertEqual(errors[0].code, "invalid_choice") |
488 | 596 |
|
| 597 | + def test_client_scoped_template_for_other_client_is_invalid(self): |
| 598 | + foreign_docx = ReportDocxTemplateFactory(client=ClientFactory()) |
| 599 | + foreign_pptx = ReportPptxTemplateFactory(client=ClientFactory()) |
| 600 | + |
| 601 | + form = self.form_data( |
| 602 | + instance=self.report, |
| 603 | + docx_template=foreign_docx.pk, |
| 604 | + pptx_template=foreign_pptx.pk, |
| 605 | + ) |
| 606 | + |
| 607 | + self.assertFalse(form.is_valid()) |
| 608 | + self.assertEqual(form["docx_template"].errors.as_data()[0].code, "invalid_choice") |
| 609 | + self.assertEqual(form["pptx_template"].errors.as_data()[0].code, "invalid_choice") |
| 610 | + |
| 611 | + def test_client_scoped_template_for_report_client_is_valid(self): |
| 612 | + docx_template = ReportDocxTemplateFactory(client=self.report.project.client) |
| 613 | + pptx_template = ReportPptxTemplateFactory(client=self.report.project.client) |
| 614 | + |
| 615 | + form = self.form_data( |
| 616 | + instance=self.report, |
| 617 | + docx_template=docx_template.pk, |
| 618 | + pptx_template=pptx_template.pk, |
| 619 | + ) |
| 620 | + |
| 621 | + self.assertTrue(form.is_valid(), form.errors) |
| 622 | + |
| 623 | + def test_mixed_case_document_type_templates_are_valid(self): |
| 624 | + docx_type = DocTypeFactory(doc_type="DoCx", extension="docx", name="DoCx") |
| 625 | + pptx_type = DocTypeFactory(doc_type="PpTx", extension="pptx", name="PpTx") |
| 626 | + docx_template = ReportDocxTemplateFactory(doc_type=docx_type) |
| 627 | + pptx_template = ReportPptxTemplateFactory(doc_type=pptx_type) |
| 628 | + |
| 629 | + form = self.form_data( |
| 630 | + instance=self.report, |
| 631 | + docx_template=docx_template.pk, |
| 632 | + pptx_template=pptx_template.pk, |
| 633 | + ) |
| 634 | + |
| 635 | + self.assertIn(docx_template, form.fields["docx_template"].queryset) |
| 636 | + self.assertIn(pptx_template, form.fields["pptx_template"].queryset) |
| 637 | + self.assertTrue(form.is_valid(), form.errors) |
| 638 | + |
489 | 639 |
|
490 | 640 | class SeverityFormTests(TestCase): |
491 | 641 | """Collection of tests for :form:`reporting.SeverityForm`.""" |
|
0 commit comments