Skip to content

Commit 2719095

Browse files
authored
Merge pull request #5830 from alphagov/add-banner-on-file-add
Display a banner when a file is added to a template
2 parents fc374e5 + bec9d5e commit 2719095

File tree

3 files changed

+50
-55
lines changed

3 files changed

+50
-55
lines changed

app/main/views/template_email_files.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def manage_a_template_email_file(service_id, template_id, template_email_file_id
110110

111111
@main.route(
112112
"/services/<uuid:service_id>/templates/<uuid:template_id>/files/<uuid:template_email_file_id>/make-live",
113-
methods=["GET", "POST"],
113+
methods=["POST"],
114114
)
115115
@service_has_permission("send_files_via_ui")
116116
@user_has_permissions("manage_templates")
@@ -131,6 +131,7 @@ def make_file_live(service_id, template_id, template_email_file_id):
131131
content=new_content,
132132
)
133133
template_email_file.update(pending=False)
134+
flash(f"‘{template_email_file.filename}’ added to template", "default_with_tick")
134135
return redirect(url_for("main.view_template", service_id=current_service.id, template_id=template.id))
135136

136137

app/templates/views/templates/email-template-files/email_file.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
{% from "components/page-header.html" import page_header %}
33
{% from "govuk_frontend_jinja/components/back-link/macro.html" import govukBackLink %}
44
{% from "govuk_frontend_jinja/components/summary-list/macro.html" import govukSummaryList %}
5+
{% from "components/form.html" import form_wrapper %}
56
{% from "components/page-footer.html" import page_footer %}
67
{% from "components/banner.html" import banner %}
7-
{% from "govuk_frontend_jinja/components/button/macro.html" import govukButton %}
88

99
{% set title = template_email_file.filename %}
1010
{% block service_page_title %}
@@ -99,10 +99,9 @@
9999
})
100100
}}
101101
{% if template_email_file.pending %}
102-
{{ govukButton({
103-
"text": "Add to template",
104-
"href": url_for('main.make_file_live',service_id=service_id,template_id=template_id,template_email_file_id=template_email_file.id,)
105-
}) }}
102+
{% call form_wrapper(action=url_for('main.make_file_live',service_id=service_id,template_id=template_id,template_email_file_id=template_email_file.id,)) %}
103+
{{ page_footer("Add to template") }}
104+
{% endcall %}
106105
{% else %}
107106
{{ page_footer(
108107
delete_link_text="Remove this file",

tests/app/main/views/test_template_email_files.py

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SERVICE_ONE_ID,
1212
create_template,
1313
normalize_spaces,
14+
sample_uuid,
1415
)
1516

1617

@@ -572,13 +573,43 @@ def test_create_file_redirects_to_manage_files_page(
572573
_follow_redirects=True,
573574
)
574575
assert mock_create_file.call_args[1]["pending"] # check the created file is set to pending
575-
assert normalize_spaces(page.select_one(".govuk-button")) == "Add to template"
576+
assert normalize_spaces(page.select_one("form .govuk-button")) == "Add to template"
577+
assert page.select_one("form").get("method") == "post"
576578
assert (
577-
page.select_one(".govuk-button").get("href")
579+
page.select_one("form").get("action")
578580
== f"/services/{SERVICE_ONE_ID}/templates/{fake_uuid}/files/{file_id}/make-live"
579581
)
580582

581583

584+
def test_make_live_is_post_only(client_request, service_one, fake_uuid):
585+
service_one["permissions"] += ["send_files_via_ui"]
586+
client_request.get(
587+
"main.make_file_live",
588+
service_id=SERVICE_ONE_ID,
589+
template_id=fake_uuid,
590+
template_email_file_id=fake_uuid,
591+
_expected_status=405,
592+
_test_page_title=False,
593+
)
594+
595+
596+
@pytest.mark.parametrize(
597+
"template_content, expected_calls",
598+
(
599+
(
600+
"Template content",
601+
[
602+
call(
603+
template_id=sample_uuid(),
604+
service_id=SERVICE_ONE_ID,
605+
content="Template content\n\n((test_file_1.csv))",
606+
)
607+
],
608+
),
609+
("Already has placeholder ((test_file_1.csv))", []),
610+
("Already has placeholder in different case/whitespace ((TEST FILE 1.csv))", []),
611+
),
612+
)
582613
def test_make_live_endpoint_calls_update_with_correct_args(
583614
client_request,
584615
service_one,
@@ -588,6 +619,8 @@ def test_make_live_endpoint_calls_update_with_correct_args(
588619
active_user_with_permissions,
589620
mock_update_service,
590621
mock_get_service_email_template,
622+
template_content,
623+
expected_calls,
591624
):
592625
service_one["permissions"] += ["send_files_via_ui"]
593626
mocker.patch(
@@ -596,6 +629,7 @@ def test_make_live_endpoint_calls_update_with_correct_args(
596629
"data": create_template(
597630
template_id=fake_uuid,
598631
template_type="email",
632+
content=template_content,
599633
)
600634
},
601635
)
@@ -614,22 +648,17 @@ def test_make_live_endpoint_calls_update_with_correct_args(
614648
)
615649
mock_template_update = mocker.patch("app.service_api_client.update_service_template")
616650
mock_template_email_file_update = mocker.patch("app.models.template_email_file.TemplateEmailFile.update")
617-
client_request.get(
651+
page = client_request.post(
618652
"main.make_file_live",
619653
service_id=SERVICE_ONE_ID,
620654
template_id=fake_uuid,
621655
template_email_file_id=file_data["id"],
622-
_expected_status=302,
623-
_expected_redirect=url_for(
624-
"main.view_template",
625-
service_id=SERVICE_ONE_ID,
626-
template_id=fake_uuid,
627-
),
628-
)
629-
mock_template_update.assert_called_once_with(
630-
template_id=fake_uuid, service_id=SERVICE_ONE_ID, content=f"Template content\n\n(({file_data['filename']}))"
656+
_follow_redirects=True,
631657
)
632-
mock_template_email_file_update.assert_called_once_with(pending=False)
658+
assert mock_template_update.call_args_list == expected_calls
659+
assert mock_template_email_file_update.call_args_list == [call(pending=False)]
660+
assert normalize_spaces(page.select_one("h1.folder-heading")) == "sample template"
661+
assert normalize_spaces(page.select_one(".banner-default-with-tick")) == "‘test_file_1.csv’ added to template"
633662

634663

635664
@pytest.mark.parametrize("pending", [True, False])
@@ -897,48 +926,14 @@ def test_upload_file_page_validates_extentions(
897926
assert not error_message
898927

899928

900-
def test_file_upload_calls_template_update(
901-
client_request,
902-
fake_uuid,
903-
service_one,
904-
mocker,
905-
):
906-
service_one["permissions"] += ["send_files_via_ui"]
907-
service_one["contact_link"] = "https://example.com"
908-
mocker.patch(
909-
"app.service_api_client.get_service_template",
910-
return_value={
911-
"data": create_template(
912-
template_id=fake_uuid,
913-
template_type="email",
914-
content="This is a file with a file placeholder",
915-
)
916-
},
917-
)
918-
mock_antivirus = mocker.patch("app.extensions.antivirus_client.scan", return_value=True)
919-
mock_s3 = mocker.patch("app.s3_client.s3_template_email_file_upload_client.utils_s3upload")
920-
mock_post = mocker.patch("app.template_email_file_client.post")
921-
with open("tests/test_pdf_files/one_page_pdf.pdf", "rb") as file:
922-
client_request.post(
923-
"main.upload_template_email_files",
924-
service_id=SERVICE_ONE_ID,
925-
template_id=fake_uuid,
926-
_data={"file": file},
927-
_expected_status=302,
928-
)
929-
assert mock_antivirus.called
930-
assert mock_s3.called
931-
assert mock_post.called
932-
933-
934929
@pytest.mark.parametrize(
935930
"template_content",
936931
(
932+
"This is a file with a file placeholder",
937933
"This is a file with a file placeholder ((tests/test_pdf_files/one_page_pdf.pdf))",
938-
"This is a file with a file placeholder ((tests/test_pdf_files/ONE-PAGE PDF.PDF))",
939934
),
940935
)
941-
def test_upload_file_does_not_update_template_when_placeholder_already_exists(
936+
def test_upload_file_does_not_update_template_content(
942937
client_request,
943938
fake_uuid,
944939
service_one,

0 commit comments

Comments
 (0)