Files
notifications-admin/tests/app/main/views/test_notifications.py

363 lines
9.7 KiB
Python
Raw Permalink Normal View History

2018-03-01 15:38:42 +00:00
from functools import partial
import pytest
from flask import url_for
from freezegun import freeze_time
2025-07-21 16:37:55 -07:00
from app.enums import ServicePermission
2018-03-01 15:38:42 +00:00
from tests.conftest import (
SERVICE_ONE_ID,
create_active_caseworking_user,
create_active_user_with_permissions,
create_notification,
2018-03-01 15:38:42 +00:00
normalize_spaces,
)
2017-06-16 14:57:48 +01:00
@pytest.mark.parametrize(
("key_type", "notification_status", "expected_status"),
[
(None, "created", "Sending"),
(
None,
"sending",
"Pending. Messages will remain in pending state until carrier status is received, typically 5 minutes.",
),
(None, "delivered", "Delivered"),
(None, "failed", "Failed"),
(None, "temporary-failure", "Phone not accepting messages right now"),
(None, "permanent-failure", "Not delivered"),
(None, "technical-failure", "Technical failure"),
("team", "delivered", "Delivered"),
("live", "delivered", "Delivered"),
(
"test",
"sending",
"Pending. Messages will remain in pending state until carrier status "
"is received, typically 5 minutes. (test)",
),
("test", "delivered", "Delivered (test)"),
("test", "permanent-failure", "Not delivered (test)"),
],
)
@pytest.mark.parametrize(
"user",
[
create_active_user_with_permissions(),
create_active_caseworking_user(),
],
)
2017-06-16 14:57:48 +01:00
@freeze_time("2016-01-01 11:09:00.061258")
def test_notification_status_page_shows_details(
client_request,
mocker,
mock_has_no_jobs,
2017-06-16 14:57:48 +01:00
service_one,
fake_uuid,
user,
key_type,
notification_status,
expected_status,
2017-06-16 14:57:48 +01:00
):
mocker.patch("app.user_api_client.get_user", return_value=user)
notification = create_notification(
notification_status=notification_status, key_type=key_type
)
_mock_get_notification = mocker.patch(
"app.notification_api_client.get_notification", return_value=notification
)
2017-06-16 14:57:48 +01:00
page = client_request.get(
"main.view_notification",
service_id=service_one["id"],
notification_id=fake_uuid,
2017-06-16 14:57:48 +01:00
)
assert normalize_spaces(page.select(".sms-message-recipient")[0].text) == (
"To: 2021234567"
)
assert normalize_spaces(page.select(".sms-message-wrapper")[0].text) == (
"service one: hello Jo"
)
assert normalize_spaces(page.select(".ajax-block-container p")[0].text) == (
expected_status
)
2017-06-16 14:57:48 +01:00
_mock_get_notification.assert_called_with(service_one["id"], fake_uuid)
@pytest.mark.parametrize(
("notification_type", "notification_status", "expected_class"),
[
("sms", "failed", "error"),
("email", "failed", "error"),
("sms", "sent", "sent-international"),
("email", "sent", None),
("sms", "created", "default"),
("email", "created", "default"),
],
)
2020-09-18 16:26:08 +01:00
@freeze_time("2016-01-01 11:09:00.061258")
def test_notification_status_page_formats_email_and_sms_status_correctly(
client_request,
mocker,
mock_has_no_jobs,
service_one,
fake_uuid,
active_user_with_permissions,
notification_type,
notification_status,
expected_class,
):
mocker.patch(
"app.user_api_client.get_user", return_value=active_user_with_permissions
)
notification = create_notification(
notification_status=notification_status, template_type=notification_type
)
mocker.patch(
"app.notification_api_client.get_notification", return_value=notification
)
2020-09-18 16:26:08 +01:00
page = client_request.get(
"main.view_notification",
service_id=service_one["id"],
notification_id=fake_uuid,
)
assert page.select_one(
f".ajax-block-container p.notification-status.{expected_class}"
2020-09-18 16:26:08 +01:00
)
@pytest.mark.parametrize(
("template_redaction_setting", "expected_content"),
[
(False, "service one: hello Jo"),
(True, "service one: hello hidden"),
],
)
@freeze_time("2016-01-01 11:09:00.061258")
def test_notification_status_page_respects_redaction(
client_request,
mocker,
service_one,
fake_uuid,
template_redaction_setting,
expected_content,
):
_mock_get_notification = mocker.patch(
"app.notification_api_client.get_notification",
return_value=create_notification(
redact_personalisation=template_redaction_setting
),
)
page = client_request.get(
"main.view_notification",
service_id=service_one["id"],
notification_id=fake_uuid,
)
assert (
normalize_spaces(page.select(".sms-message-wrapper")[0].text)
== expected_content
)
_mock_get_notification.assert_called_with(
service_one["id"],
fake_uuid,
)
@pytest.mark.parametrize(
("extra_args", "expected_back_link"),
[
(
{},
partial(
url_for,
2025-10-10 15:40:40 -07:00
"main.all_jobs_activity",
),
),
(
{"from_job": "job_id"},
partial(url_for, "main.view_job", job_id="job_id"),
),
(
{"help": "0"},
None,
),
(
{"help": "1"},
None,
),
(
{"help": "2"},
None,
),
],
)
def test_notification_status_shows_expected_back_link(
client_request,
mocker,
mock_get_notification,
fake_uuid,
extra_args,
expected_back_link,
):
page = client_request.get(
"main.view_notification",
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
**extra_args,
)
Removed all govuk css (#2814) * Removed all govuk css * Updated reference files * Removing govuk js * Fixed casing for modules, removed unused page * Got more reference images * Updated template page * Removed govuk padding util * Updated hint to uswds hint * More govuk cleanup * Commiting backstopjs ref files * Fixed all unit tests that broke due to brittleness around govuk styling * Added new ref images * Final removal of govuk * Officially removed all govuk references * Updated reference file * Updated link to button * UI modernization * Cleanup * removed govuk escaping tests since they are no longer needed * Fix CodeQL security issue in escapeElementName function - Escape backslashes first before other special characters - Prevents potential double-escaping vulnerability - Addresses CodeQL alert about improper string escaping * Found more govuk removal. Fixed unit tests * Add missing pipeline check to pre-commit * updated test * Updated e2e test * More update to e2e test * Fixed another e2e test * Simple PR comments addressed * More updates * Updated backstop ref files * Refactored folder selection for non-admins * Updated redundant line * Updated tests to include correct mocks * Added more ref files * Addressing carlos comments * Addressing Carlo comments, cleanup of window initing * More cleanup and addressing carlo comments * Fixing a11 scan * Fixed a few issues with javascript * Fixed for pr * Fixing e2e tests * Tweaking e2e test * Added more ref files and cleaned up urls.js * Fixed bug with creating new template * Removed brittle test - addressed code ql comment * e2e race condition fix * More e2e test fixes * Updated e2e tests to not wait for text sent * Updated test to not wait for button click response * Made tear down more resilent if staging is down * reverted e2e test to what was working before main merge * Updated backstopRef images * Updated gulp to include job-polling differently
2025-10-06 09:38:54 -04:00
back_link = page.select_one("nav.usa-breadcrumb a")
if expected_back_link:
assert back_link["href"] == expected_back_link(service_id=SERVICE_ONE_ID)
else:
assert back_link is None
@pytest.mark.parametrize(
("time_of_viewing_page", "expected_message"),
[
(
"2012-01-01 06:01",
2024-02-23 14:59:25 -08:00
("sample template was sent by Test User today at 01:01 US/Eastern"),
),
(
"2012-01-02 06:01",
2024-02-23 14:59:25 -08:00
("sample template was sent by Test User yesterday at 01:01 US/Eastern"),
),
(
"2012-01-03 06:01",
2023-11-16 12:24:27 -08:00
(
2024-02-23 14:59:25 -08:00
"sample template was sent by Test User on 1 January at 01:01 US/Eastern"
2023-11-16 12:24:27 -08:00
),
),
(
"2013-01-03 06:01",
2023-11-16 12:24:27 -08:00
(
2024-02-23 14:59:25 -08:00
"sample template was sent by Test User on 1 January 2012 at 01:01 US/Eastern"
2023-11-16 12:24:27 -08:00
),
),
],
)
def test_notification_page_doesnt_link_to_template_in_tour(
mocker,
client_request,
fake_uuid,
mock_get_notification,
time_of_viewing_page,
expected_message,
):
with freeze_time("2012-01-01 06:01"):
notification = create_notification()
mocker.patch(
"app.notification_api_client.get_notification", return_value=notification
)
with freeze_time(time_of_viewing_page):
page = client_request.get(
"main.view_notification",
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
help=3,
)
assert len(page.select("main p:nth-of-type(1) a")) == 0
@pytest.mark.parametrize("notification_type", ["email", "sms"])
@freeze_time("2016-01-01 15:00")
def test_notification_page_does_not_show_cancel_link_for_sms_or_email_notifications(
client_request,
mocker,
fake_uuid,
notification_type,
):
notification = create_notification(
template_type=notification_type, notification_status="created"
)
mocker.patch(
"app.notification_api_client.get_notification", return_value=notification
)
page = client_request.get(
"main.view_notification",
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
)
assert "Cancel sending this letter" not in normalize_spaces(page.text)
@pytest.mark.parametrize(
("service_permissions", "template_type", "link_expected"),
[
([], "", False),
2025-07-21 16:37:55 -07:00
([ServicePermission.INBOUND_SMS], "email", False),
([ServicePermission.INBOUND_SMS], "sms", True),
],
)
def test_notification_page_has_link_to_send_another_for_sms(
client_request,
mocker,
fake_uuid,
service_one,
service_permissions,
template_type,
link_expected,
):
service_one["permissions"] = service_permissions
notification = create_notification(template_type=template_type)
mocker.patch(
"app.notification_api_client.get_notification", return_value=notification
)
page = client_request.get(
"main.view_notification",
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
)
last_paragraph = page.select("main p")[-1]
conversation_link = url_for(
".conversation",
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
_anchor="n{}".format(fake_uuid),
)
if link_expected:
assert normalize_spaces(last_paragraph.text) == (
"See all text messages sent to this phone number"
)
assert last_paragraph.select_one("a")["href"] == conversation_link
else:
assert conversation_link not in str(page.select_one("main"))
@pytest.mark.parametrize("notification_type", ["sms", "email"])
Show 'From' / 'Reply To' if set for notification Previously when a service had multiple "reply to" entries setup for email or SMS, we would show the one that was selected on all screens [1][2] except the final one, where the notification is actually sent. This fixes that, with the caveat that it will also show for services with only one "reply to" entry (see notes below) - we will look at making this consistent on the previous screens in the next commit. Here's a bit more detail on how this works: - If a service has multiple "reply to" entries, the journey to send a one-off message starts with a screen to select the "sender_id", which is otherwise "None" [3]. - The "sender_id" is subsequently resolved to an actual email / phone number by calling an API [4] and plucking it out of the response JSON. - The email / phone number then get rendered as part of the preview template [5][6]. - Unfortunately the "sender_id" is removed from the session by the time we get to the "view_notification" view [7]. - However, we can get back the equivalent text from the notification JSON, which is set by the API when the notification is created [8], give or take a bit of validation code [9][10]. - But the "reply_to_text" field is also set by the API when the service only has one "reply to" entry, so it will show then as well. We could add look at the number of "reply to" entries for the service, in order to consistently only show it when there is more the one. But it seems more useful to show it on previous screens, since it provides more information than is currently show (esp. for emails). [1]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L441-L442 [2]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L966-L967 [3]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L247 [4]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L1071-L1082 [5]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/templates/views/notifications/notification.html#L80 [6]: https://github.com/alphagov/notifications-utils/blob/master/notifications_utils/jinja_templates/sms_preview_template.jinja2 [7]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L1059 [8]: https://github.com/alphagov/notifications-api/blob/f8b4c9151cd135195c2fe416d4f54516d8d3833f/app/service/send_notification.py#L87-L93 [9]: https://github.com/alphagov/notifications-api/blob/f8b4c9151cd135195c2fe416d4f54516d8d3833f/app/models.py#L653 [10]: https://github.com/alphagov/notifications-utils/blob/master/notifications_utils/recipients.py#L482
2021-02-08 16:38:50 +00:00
def test_should_show_reply_to_from_notification(
mocker,
fake_uuid,
notification_type,
client_request,
):
notification = create_notification(
reply_to_text="reply to info", template_type=notification_type
)
mocker.patch(
"app.notification_api_client.get_notification", return_value=notification
)
Show 'From' / 'Reply To' if set for notification Previously when a service had multiple "reply to" entries setup for email or SMS, we would show the one that was selected on all screens [1][2] except the final one, where the notification is actually sent. This fixes that, with the caveat that it will also show for services with only one "reply to" entry (see notes below) - we will look at making this consistent on the previous screens in the next commit. Here's a bit more detail on how this works: - If a service has multiple "reply to" entries, the journey to send a one-off message starts with a screen to select the "sender_id", which is otherwise "None" [3]. - The "sender_id" is subsequently resolved to an actual email / phone number by calling an API [4] and plucking it out of the response JSON. - The email / phone number then get rendered as part of the preview template [5][6]. - Unfortunately the "sender_id" is removed from the session by the time we get to the "view_notification" view [7]. - However, we can get back the equivalent text from the notification JSON, which is set by the API when the notification is created [8], give or take a bit of validation code [9][10]. - But the "reply_to_text" field is also set by the API when the service only has one "reply to" entry, so it will show then as well. We could add look at the number of "reply to" entries for the service, in order to consistently only show it when there is more the one. But it seems more useful to show it on previous screens, since it provides more information than is currently show (esp. for emails). [1]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L441-L442 [2]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L966-L967 [3]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L247 [4]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L1071-L1082 [5]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/templates/views/notifications/notification.html#L80 [6]: https://github.com/alphagov/notifications-utils/blob/master/notifications_utils/jinja_templates/sms_preview_template.jinja2 [7]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L1059 [8]: https://github.com/alphagov/notifications-api/blob/f8b4c9151cd135195c2fe416d4f54516d8d3833f/app/service/send_notification.py#L87-L93 [9]: https://github.com/alphagov/notifications-api/blob/f8b4c9151cd135195c2fe416d4f54516d8d3833f/app/models.py#L653 [10]: https://github.com/alphagov/notifications-utils/blob/master/notifications_utils/recipients.py#L482
2021-02-08 16:38:50 +00:00
page = client_request.get(
"main.view_notification",
Show 'From' / 'Reply To' if set for notification Previously when a service had multiple "reply to" entries setup for email or SMS, we would show the one that was selected on all screens [1][2] except the final one, where the notification is actually sent. This fixes that, with the caveat that it will also show for services with only one "reply to" entry (see notes below) - we will look at making this consistent on the previous screens in the next commit. Here's a bit more detail on how this works: - If a service has multiple "reply to" entries, the journey to send a one-off message starts with a screen to select the "sender_id", which is otherwise "None" [3]. - The "sender_id" is subsequently resolved to an actual email / phone number by calling an API [4] and plucking it out of the response JSON. - The email / phone number then get rendered as part of the preview template [5][6]. - Unfortunately the "sender_id" is removed from the session by the time we get to the "view_notification" view [7]. - However, we can get back the equivalent text from the notification JSON, which is set by the API when the notification is created [8], give or take a bit of validation code [9][10]. - But the "reply_to_text" field is also set by the API when the service only has one "reply to" entry, so it will show then as well. We could add look at the number of "reply to" entries for the service, in order to consistently only show it when there is more the one. But it seems more useful to show it on previous screens, since it provides more information than is currently show (esp. for emails). [1]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L441-L442 [2]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L966-L967 [3]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L247 [4]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L1071-L1082 [5]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/templates/views/notifications/notification.html#L80 [6]: https://github.com/alphagov/notifications-utils/blob/master/notifications_utils/jinja_templates/sms_preview_template.jinja2 [7]: https://github.com/alphagov/notifications-admin/blob/93226ec5d6dde489fae6e2e605aa2cfc04a8eff0/app/main/views/send.py#L1059 [8]: https://github.com/alphagov/notifications-api/blob/f8b4c9151cd135195c2fe416d4f54516d8d3833f/app/service/send_notification.py#L87-L93 [9]: https://github.com/alphagov/notifications-api/blob/f8b4c9151cd135195c2fe416d4f54516d8d3833f/app/models.py#L653 [10]: https://github.com/alphagov/notifications-utils/blob/master/notifications_utils/recipients.py#L482
2021-02-08 16:38:50 +00:00
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
)
assert "reply to info" in page.text