Skip to content

Commit 0843e68

Browse files
committed
Validate that file doesn’t exist in subject
1 parent 3fdd5e3 commit 0843e68

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

app/main/forms.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3216,7 +3216,8 @@ def validate_report_has_been_processed(self, field):
32163216

32173217
class TemplateEmailFilesUploadForm(StripWhitespaceForm):
32183218
def __init__(self, *args, template, **kwargs):
3219-
self.existing_file_names = InsensitiveSet(file.filename for file in template.existing_files)
3219+
self.existing_file_names = InsensitiveSet(file.filename for file in template.email_files)
3220+
self.placeholders_in_subject = InsensitiveSet(UtilsField(template._subject).placeholders)
32203221
super().__init__(*args, **kwargs)
32213222

32223223
allowed_file_formats = {
@@ -3256,6 +3257,12 @@ def validate_file(self, field):
32563257
if field.data.filename in self.existing_file_names:
32573258
raise ValidationError(f"Your template already has a file called ‘{field.data.filename}’")
32583259

3260+
if field.data.filename in self.placeholders_in_subject:
3261+
raise ValidationError(
3262+
f"You cannot put a file in the subject of a template "
3263+
f"– remove (({field.data.filename})) or rename your file"
3264+
)
3265+
32593266

32603267
class TemplateEmailFileLinkTextForm(StripWhitespaceForm):
32613268
link_text = GovukTextInputField("Link text (optional)")

tests/app/main/views/test_template_email_files.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,3 +1073,51 @@ def test_upload_file_returns_error_if_file_with_same_name_exists(
10731073
assert mock_template_update.call_args_list == []
10741074
assert mock_s3.call_args_list == []
10751075
assert mock_post.call_args_list == []
1076+
1077+
1078+
@pytest.mark.parametrize(
1079+
"subject",
1080+
(
1081+
("Please download ((tests/test_pdf_files/one_page_pdf.pdf))"),
1082+
("Please download ((tests/test_pdf_files/ONE-PAGE PDF.PDF))"),
1083+
),
1084+
)
1085+
def test_upload_file_returns_error_if_placeholder_exists_in_subject(
1086+
client_request,
1087+
fake_uuid,
1088+
service_one,
1089+
mocker,
1090+
subject,
1091+
):
1092+
service_one["permissions"] += ["send_files_via_ui"]
1093+
service_one["contact_link"] = "https://example.com"
1094+
mocker.patch(
1095+
"app.service_api_client.get_service_template",
1096+
return_value={
1097+
"data": create_template(
1098+
template_id=fake_uuid,
1099+
template_type="email",
1100+
subject=subject,
1101+
)
1102+
},
1103+
)
1104+
mock_antivirus = mocker.patch("app.extensions.antivirus_client.scan", return_value=True)
1105+
mock_s3 = mocker.patch("app.s3_client.s3_template_email_file_upload_client.utils_s3upload")
1106+
mock_post = mocker.patch("app.template_email_file_client.post")
1107+
mock_template_update = mocker.patch("app.service_api_client.update_service_template")
1108+
with open("tests/test_pdf_files/one_page_pdf.pdf", "rb") as file:
1109+
page = client_request.post(
1110+
"main.upload_template_email_files",
1111+
service_id=SERVICE_ONE_ID,
1112+
template_id=fake_uuid,
1113+
_data={"file": file},
1114+
_expected_status=200,
1115+
)
1116+
assert normalize_spaces(page.select_one(".govuk-error-message").text) == (
1117+
"You cannot put a file in the subject of a template – "
1118+
"remove ((tests/test_pdf_files/one_page_pdf.pdf)) or rename your file"
1119+
)
1120+
assert mock_antivirus.called is True
1121+
assert mock_template_update.call_args_list == []
1122+
assert mock_s3.call_args_list == []
1123+
assert mock_post.call_args_list == []

0 commit comments

Comments
 (0)