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

364 lines
9.7 KiB
Python
Raw Normal View History

2018-03-01 15:38:42 +00:00
from functools import partial
2017-06-16 12:13:21 +01:00
2018-02-20 11:22:17 +00:00
import pytest
from flask import url_for
from freezegun import freeze_time
2018-04-25 14:12:58 +01:00
2018-03-01 15:38:42 +00:00
from tests.conftest import (
SERVICE_ONE_ID,
2019-12-19 16:59:07 +00:00
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
2017-06-16 12:13:21 +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,
2017-06-19 14:37:16 +01:00
mocker,
2018-07-30 17:40:32 +01:00
mock_has_no_jobs,
2017-06-16 14:57:48 +01:00
service_one,
fake_uuid,
2018-06-12 16:17:20 +01:00
user,
2018-09-06 10:41:59 +01:00
key_type,
2017-06-19 14:37:16 +01:00
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-19 14:37:16 +01:00
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
)
_mock_get_notification.assert_called_with(service_one["id"], fake_uuid)
2017-06-24 17:29:28 +01:00
@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"),
],
)
2017-06-24 17:29:28 +01:00
@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
),
2017-06-24 17:29:28 +01:00
)
page = client_request.get(
"main.view_notification",
service_id=service_one["id"],
notification_id=fake_uuid,
2017-06-24 17:29:28 +01:00
)
assert (
normalize_spaces(page.select(".sms-message-wrapper")[0].text)
== expected_content
)
2017-06-24 17:29:28 +01:00
_mock_get_notification.assert_called_with(
service_one["id"],
2017-06-24 17:29:28 +01:00
fake_uuid,
)
2017-07-04 09:32:09 +01:00
@pytest.mark.parametrize(
"extra_args, expected_back_link",
[
(
{},
partial(
url_for,
"main.view_notifications",
message_type="sms",
status="sending,delivered,failed",
),
),
(
{"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,
)
back_link = page.select_one(".usa-back-link")
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",
("sample template was sent by Test User today at 06:01 UTC"),
),
(
"2012-01-02 06:01",
("sample template was sent by Test User yesterday at 06:01 UTC"),
),
(
"2012-01-03 06:01",
("sample template was sent by Test User on 1 January at 06:01 UTC"),
),
(
"2013-01-03 06:01",
("sample template was sent by Test User on 1 January 2012 at 06:01 UTC"),
),
),
)
2017-07-04 09:32:09 +01:00
def test_notification_page_doesnt_link_to_template_in_tour(
2020-02-19 17:11:31 +00:00
mocker,
2017-07-04 09:32:09 +01:00
client_request,
fake_uuid,
mock_get_notification,
2020-02-19 17:11:31 +00:00
time_of_viewing_page,
expected_message,
2017-07-04 09:32:09 +01:00
):
with freeze_time("2012-01-01 06:01"):
2020-02-19 17:11:31 +00:00
notification = create_notification()
mocker.patch(
"app.notification_api_client.get_notification", return_value=notification
)
2020-02-19 17:11:31 +00:00
with freeze_time(time_of_viewing_page):
page = client_request.get(
"main.view_notification",
2020-02-19 17:11:31 +00:00
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
help=3,
)
2017-07-04 09:32:09 +01:00
assert normalize_spaces(page.select("main p:nth-of-type(1)")[0].text) == (
2020-02-19 17:11:31 +00:00
expected_message
2017-07-04 09:32:09 +01:00
)
assert len(page.select("main p:nth-of-type(1) a")) == 0
2017-07-13 13:05:41 +01:00
@pytest.mark.parametrize("notification_type", ["email", "sms"])
@freeze_time("2016-01-01 15:00")
2018-12-05 10:58:27 +00: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
)
2018-12-05 10:58:27 +00:00
page = client_request.get(
"main.view_notification",
2018-12-05 10:58:27 +00:00
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
)
assert "Cancel sending this letter" not in normalize_spaces(page.text)
2018-12-05 10:58:27 +00:00
@pytest.mark.parametrize(
"service_permissions, template_type, link_expected",
[
([], "", False),
(["inbound_sms"], "email", False),
(["inbound_sms"], "sms", True),
],
)
2017-10-16 10:30:59 +01:00
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
)
2017-10-16 10:30:59 +01:00
page = client_request.get(
"main.view_notification",
2017-10-16 10:30:59 +01:00
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),
)
2017-10-16 10:30:59 +01:00
if link_expected:
assert normalize_spaces(last_paragraph.text) == (
"See all text messages sent to this phone number"
2017-10-16 10:30:59 +01:00
)
assert last_paragraph.select_one("a")["href"] == conversation_link
2017-10-16 10:30:59 +01:00
else:
assert conversation_link not in str(page.select_one("main"))
@pytest.mark.parametrize("notification_type", ["sms", "email"])
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
)
page = client_request.get(
"main.view_notification",
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
)
assert "reply to info" in page.text