2017-07-07 15:34:06 +01:00
|
|
|
|
import json
|
2018-02-20 11:22:17 +00:00
|
|
|
|
from datetime import datetime
|
2018-05-03 15:40:24 +01:00
|
|
|
|
from unittest.mock import Mock
|
2017-05-24 13:19:31 +01:00
|
|
|
|
|
2018-02-20 11:22:17 +00:00
|
|
|
|
import pytest
|
|
|
|
|
|
from flask import url_for
|
2017-09-27 17:38:42 +01:00
|
|
|
|
from freezegun import freeze_time
|
2018-02-20 11:22:17 +00:00
|
|
|
|
from notifications_python_client.errors import HTTPError
|
2018-04-25 14:12:58 +01:00
|
|
|
|
|
|
|
|
|
|
from app.main.views.conversation import get_user_number
|
2017-05-24 13:19:31 +01:00
|
|
|
|
from tests.conftest import (
|
|
|
|
|
|
SERVICE_ONE_ID,
|
2017-07-07 13:32:07 +01:00
|
|
|
|
mock_get_notifications,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
normalize_spaces,
|
2017-05-24 13:19:31 +01:00
|
|
|
|
)
|
2018-02-20 11:22:17 +00:00
|
|
|
|
|
2017-06-14 15:37:57 +01:00
|
|
|
|
|
2017-07-07 13:32:07 +01:00
|
|
|
|
def test_get_user_phone_number_when_only_inbound_exists(mocker):
|
|
|
|
|
|
|
|
|
|
|
|
mock_get_inbound_sms = mocker.patch(
|
|
|
|
|
|
'app.main.views.conversation.service_api_client.get_inbound_sms_by_id',
|
|
|
|
|
|
return_value={
|
2017-09-29 10:15:17 +01:00
|
|
|
|
'user_number': '4407900900123',
|
|
|
|
|
|
'notify_number': '07900000002'
|
2017-07-07 13:32:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
mock_get_notification = mocker.patch(
|
|
|
|
|
|
'app.main.views.conversation.notification_api_client.get_notification',
|
2018-05-03 15:40:24 +01:00
|
|
|
|
side_effect=HTTPError(response=Mock(status_code=404)),
|
2017-07-07 13:32:07 +01:00
|
|
|
|
)
|
2017-06-14 15:37:57 +01:00
|
|
|
|
assert get_user_number('service', 'notification') == '07900 900123'
|
|
|
|
|
|
mock_get_inbound_sms.assert_called_once_with('service', 'notification')
|
|
|
|
|
|
assert mock_get_notification.called is False
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-07 13:32:07 +01:00
|
|
|
|
def test_get_user_phone_number_when_only_outbound_exists(mocker):
|
|
|
|
|
|
mock_get_inbound_sms = mocker.patch(
|
|
|
|
|
|
'app.main.views.conversation.service_api_client.get_inbound_sms_by_id',
|
2018-05-03 15:40:24 +01:00
|
|
|
|
side_effect=HTTPError(response=Mock(status_code=404)),
|
2017-07-07 13:32:07 +01:00
|
|
|
|
)
|
|
|
|
|
|
mock_get_notification = mocker.patch(
|
|
|
|
|
|
'app.main.views.conversation.notification_api_client.get_notification',
|
|
|
|
|
|
return_value={
|
|
|
|
|
|
'to': '15550000000'
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
2017-06-14 15:37:57 +01:00
|
|
|
|
assert get_user_number('service', 'notification') == '+1 555-000-0000'
|
|
|
|
|
|
mock_get_inbound_sms.assert_called_once_with('service', 'notification')
|
|
|
|
|
|
mock_get_notification.assert_called_once_with('service', 'notification')
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-27 10:56:03 +01:00
|
|
|
|
def test_get_user_phone_number_raises_if_both_api_requests_fail(mocker):
|
2017-07-07 13:32:07 +01:00
|
|
|
|
mock_get_inbound_sms = mocker.patch(
|
|
|
|
|
|
'app.main.views.conversation.service_api_client.get_inbound_sms_by_id',
|
2018-05-03 15:40:24 +01:00
|
|
|
|
side_effect=HTTPError(response=Mock(status_code=404)),
|
2017-07-07 13:32:07 +01:00
|
|
|
|
)
|
|
|
|
|
|
mock_get_notification = mocker.patch(
|
|
|
|
|
|
'app.main.views.conversation.notification_api_client.get_notification',
|
2018-05-03 15:40:24 +01:00
|
|
|
|
side_effect=HTTPError(response=Mock(status_code=404)),
|
2017-07-07 13:32:07 +01:00
|
|
|
|
)
|
2017-06-14 15:37:57 +01:00
|
|
|
|
with pytest.raises(HTTPError):
|
|
|
|
|
|
get_user_number('service', 'notification')
|
|
|
|
|
|
mock_get_inbound_sms.assert_called_once_with('service', 'notification')
|
|
|
|
|
|
mock_get_notification.assert_called_once_with('service', 'notification')
|
2017-05-24 13:19:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-07-07 10:04:49 +01:00
|
|
|
|
@pytest.mark.parametrize('outbound_redacted, expected_outbound_content', [
|
|
|
|
|
|
(True, 'Hello hidden'),
|
|
|
|
|
|
(False, 'Hello Jo'),
|
|
|
|
|
|
])
|
2017-05-24 13:19:31 +01:00
|
|
|
|
@freeze_time("2012-01-01 00:00:00")
|
|
|
|
|
|
def test_view_conversation(
|
2017-07-07 13:32:07 +01:00
|
|
|
|
client_request,
|
2017-07-07 10:04:49 +01:00
|
|
|
|
mocker,
|
|
|
|
|
|
api_user_active,
|
2018-05-03 15:40:24 +01:00
|
|
|
|
mock_get_inbound_sms_by_id_with_no_messages,
|
2017-05-24 13:19:31 +01:00
|
|
|
|
mock_get_notification,
|
2017-07-07 13:32:07 +01:00
|
|
|
|
fake_uuid,
|
2017-07-07 10:04:49 +01:00
|
|
|
|
outbound_redacted,
|
|
|
|
|
|
expected_outbound_content,
|
2017-09-27 17:38:42 +01:00
|
|
|
|
mock_get_inbound_sms
|
2017-05-24 13:19:31 +01:00
|
|
|
|
):
|
|
|
|
|
|
|
2018-03-12 13:28:20 +00:00
|
|
|
|
mock = mock_get_notifications(
|
2017-07-07 10:04:49 +01:00
|
|
|
|
mocker,
|
|
|
|
|
|
api_user_active,
|
|
|
|
|
|
template_content='Hello ((name))',
|
|
|
|
|
|
personalisation={'name': 'Jo'},
|
|
|
|
|
|
redact_personalisation=outbound_redacted,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2017-07-07 13:32:07 +01:00
|
|
|
|
page = client_request.get(
|
2017-05-24 13:19:31 +01:00
|
|
|
|
'main.conversation',
|
|
|
|
|
|
service_id=SERVICE_ONE_ID,
|
|
|
|
|
|
notification_id=fake_uuid,
|
2017-07-07 13:32:07 +01:00
|
|
|
|
)
|
2017-05-24 13:19:31 +01:00
|
|
|
|
|
|
|
|
|
|
messages = page.select('.sms-message-wrapper')
|
|
|
|
|
|
statuses = page.select('.sms-message-status')
|
|
|
|
|
|
|
2017-06-07 15:46:34 +01:00
|
|
|
|
assert len(messages) == 13
|
|
|
|
|
|
assert len(statuses) == 13
|
2017-06-14 15:43:42 +01:00
|
|
|
|
|
|
|
|
|
|
for index, expected in enumerate([
|
|
|
|
|
|
(
|
|
|
|
|
|
'message-8',
|
2017-09-29 10:15:17 +01:00
|
|
|
|
'yesterday at 2:59pm',
|
2017-06-14 15:43:42 +01:00
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
'message-7',
|
2017-09-29 10:15:17 +01:00
|
|
|
|
'yesterday at 2:59pm',
|
2017-06-14 15:43:42 +01:00
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
'message-6',
|
2017-09-29 10:15:17 +01:00
|
|
|
|
'yesterday at 4:59pm',
|
2017-06-14 15:43:42 +01:00
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
'message-5',
|
2017-09-29 10:15:17 +01:00
|
|
|
|
'yesterday at 6:59pm',
|
2017-06-14 15:43:42 +01:00
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
'message-4',
|
2017-09-29 10:15:17 +01:00
|
|
|
|
'yesterday at 8:59pm',
|
2017-06-14 15:43:42 +01:00
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
'message-3',
|
2017-09-29 10:15:17 +01:00
|
|
|
|
'yesterday at 10:59pm',
|
2017-06-14 15:43:42 +01:00
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
'message-2',
|
2017-09-29 10:15:17 +01:00
|
|
|
|
'yesterday at 10:59pm',
|
2017-06-14 15:43:42 +01:00
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
'message-1',
|
2017-09-29 10:15:17 +01:00
|
|
|
|
'yesterday at 11:00pm',
|
2017-06-14 15:43:42 +01:00
|
|
|
|
),
|
|
|
|
|
|
(
|
2017-07-07 10:04:49 +01:00
|
|
|
|
expected_outbound_content,
|
2017-06-14 15:43:42 +01:00
|
|
|
|
'yesterday at midnight',
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
2017-07-07 10:04:49 +01:00
|
|
|
|
expected_outbound_content,
|
2017-06-14 15:43:42 +01:00
|
|
|
|
'yesterday at midnight',
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
2017-07-07 10:04:49 +01:00
|
|
|
|
expected_outbound_content,
|
2017-06-14 15:43:42 +01:00
|
|
|
|
'yesterday at midnight',
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
2017-07-07 10:04:49 +01:00
|
|
|
|
expected_outbound_content,
|
2017-06-14 15:43:42 +01:00
|
|
|
|
'yesterday at midnight',
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
2017-07-07 10:04:49 +01:00
|
|
|
|
expected_outbound_content,
|
2017-06-14 15:43:42 +01:00
|
|
|
|
'yesterday at midnight',
|
|
|
|
|
|
),
|
|
|
|
|
|
]):
|
|
|
|
|
|
assert (
|
|
|
|
|
|
normalize_spaces(messages[index].text),
|
|
|
|
|
|
normalize_spaces(statuses[index].text),
|
|
|
|
|
|
) == expected
|
2017-07-07 15:34:06 +01:00
|
|
|
|
|
2018-05-03 15:40:24 +01:00
|
|
|
|
mock_get_inbound_sms.assert_called_once_with(SERVICE_ONE_ID, user_number='07123 456789')
|
2018-03-12 13:28:20 +00:00
|
|
|
|
mock.assert_called_once_with(SERVICE_ONE_ID, to='07123 456789', template_type='sms')
|
|
|
|
|
|
|
2017-07-07 15:34:06 +01:00
|
|
|
|
|
|
|
|
|
|
def test_view_conversation_updates(
|
|
|
|
|
|
logged_in_client,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
fake_uuid,
|
2018-05-03 15:40:24 +01:00
|
|
|
|
mock_get_inbound_sms_by_id_with_no_messages,
|
2017-07-07 15:34:06 +01:00
|
|
|
|
mock_get_notification,
|
|
|
|
|
|
):
|
|
|
|
|
|
|
2018-05-03 15:40:24 +01:00
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.main.views.conversation.service_api_client.get_inbound_sms_by_id',
|
|
|
|
|
|
side_effect=HTTPError(response=Mock(status_code=404)),
|
|
|
|
|
|
)
|
2017-07-07 15:34:06 +01:00
|
|
|
|
mock_get_partials = mocker.patch(
|
|
|
|
|
|
'app.main.views.conversation.get_conversation_partials',
|
|
|
|
|
|
return_value={'messages': 'foo'}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
response = logged_in_client.get(url_for(
|
|
|
|
|
|
'main.conversation_updates',
|
|
|
|
|
|
service_id=SERVICE_ONE_ID,
|
|
|
|
|
|
notification_id=fake_uuid,
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
assert json.loads(response.get_data(as_text=True)) == {'messages': 'foo'}
|
|
|
|
|
|
|
|
|
|
|
|
mock_get_partials.assert_called_once_with(SERVICE_ONE_ID, '07123 456789')
|
2017-09-27 17:38:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2012-01-01 00:00:00")
|
|
|
|
|
|
def test_view_conversation_with_empty_inbound(
|
|
|
|
|
|
client_request,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
api_user_active,
|
2018-05-03 15:40:24 +01:00
|
|
|
|
mock_get_inbound_sms_by_id_with_no_messages,
|
2017-09-27 17:38:42 +01:00
|
|
|
|
mock_get_notification,
|
|
|
|
|
|
mock_get_notifications_with_no_notifications,
|
|
|
|
|
|
fake_uuid
|
|
|
|
|
|
):
|
|
|
|
|
|
mock_get_inbound_sms = mocker.patch(
|
|
|
|
|
|
'app.main.views.conversation.service_api_client.get_inbound_sms',
|
2018-03-21 15:08:03 +00:00
|
|
|
|
return_value={
|
|
|
|
|
|
'has_next': False,
|
|
|
|
|
|
'data': [{
|
|
|
|
|
|
'user_number': '07900000001',
|
|
|
|
|
|
'notify_number': '07900000002',
|
|
|
|
|
|
'content': '',
|
|
|
|
|
|
'created_at': datetime.utcnow().isoformat(),
|
|
|
|
|
|
'id': fake_uuid
|
|
|
|
|
|
}]
|
|
|
|
|
|
}
|
2017-09-27 17:38:42 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
page = client_request.get(
|
|
|
|
|
|
'main.conversation',
|
|
|
|
|
|
service_id=SERVICE_ONE_ID,
|
|
|
|
|
|
notification_id=fake_uuid,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
messages = page.select('.sms-message-wrapper')
|
|
|
|
|
|
assert len(messages) == 1
|
2017-10-18 14:51:26 +01:00
|
|
|
|
assert mock_get_inbound_sms.called is True
|
2017-10-15 12:59:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_conversation_links_to_reply(
|
|
|
|
|
|
client_request,
|
|
|
|
|
|
fake_uuid,
|
2018-05-03 15:40:24 +01:00
|
|
|
|
mock_get_inbound_sms_by_id_with_no_messages,
|
2017-10-15 12:59:36 +01:00
|
|
|
|
mock_get_notification,
|
|
|
|
|
|
mock_get_notifications,
|
|
|
|
|
|
mock_get_inbound_sms,
|
|
|
|
|
|
):
|
|
|
|
|
|
page = client_request.get(
|
|
|
|
|
|
'main.conversation',
|
|
|
|
|
|
service_id=SERVICE_ONE_ID,
|
|
|
|
|
|
notification_id=fake_uuid,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert page.select('main p')[-1].select_one('a')['href'] == (
|
|
|
|
|
|
url_for(
|
|
|
|
|
|
'.conversation_reply',
|
|
|
|
|
|
service_id=SERVICE_ONE_ID,
|
|
|
|
|
|
notification_id=fake_uuid,
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_conversation_reply_shows_templates(
|
|
|
|
|
|
client_request,
|
|
|
|
|
|
fake_uuid,
|
|
|
|
|
|
mock_get_service_templates,
|
|
|
|
|
|
):
|
|
|
|
|
|
page = client_request.get(
|
|
|
|
|
|
'main.conversation_reply',
|
|
|
|
|
|
service_id=SERVICE_ONE_ID,
|
|
|
|
|
|
notification_id=fake_uuid,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
for index, expected in enumerate([
|
|
|
|
|
|
'sms_template_one',
|
|
|
|
|
|
'sms_template_two',
|
|
|
|
|
|
]):
|
|
|
|
|
|
link = page.select('.message-name')[index]
|
|
|
|
|
|
assert normalize_spaces(link.text) == expected
|
|
|
|
|
|
assert link.select_one('a')['href'].startswith(
|
|
|
|
|
|
url_for(
|
2017-10-15 13:40:49 +01:00
|
|
|
|
'main.conversation_reply_with_template',
|
2017-10-15 12:59:36 +01:00
|
|
|
|
service_id=SERVICE_ONE_ID,
|
2017-10-15 13:40:49 +01:00
|
|
|
|
notification_id=fake_uuid,
|
2017-10-15 12:59:36 +01:00
|
|
|
|
template_id='',
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2017-10-15 13:40:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_conversation_reply_redirects_with_phone_number_from_notification(
|
|
|
|
|
|
client_request,
|
|
|
|
|
|
fake_uuid,
|
2018-05-03 15:40:24 +01:00
|
|
|
|
mock_get_inbound_sms_by_id_with_no_messages,
|
2017-10-15 13:40:49 +01:00
|
|
|
|
mock_get_notification,
|
|
|
|
|
|
mock_get_service_template,
|
|
|
|
|
|
):
|
|
|
|
|
|
|
|
|
|
|
|
page = client_request.get(
|
|
|
|
|
|
'main.conversation_reply_with_template',
|
|
|
|
|
|
service_id=SERVICE_ONE_ID,
|
|
|
|
|
|
notification_id=fake_uuid,
|
|
|
|
|
|
template_id=fake_uuid,
|
|
|
|
|
|
_follow_redirects=True,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
for element, expected_text in [
|
2018-08-06 11:20:50 +01:00
|
|
|
|
('h1', 'Preview of ‘Two week reminder’'),
|
2017-10-15 13:40:49 +01:00
|
|
|
|
('.sms-message-recipient', 'To: 07123 456789'),
|
|
|
|
|
|
('.sms-message-wrapper', 'service one: Template <em>content</em> with & entity'),
|
|
|
|
|
|
]:
|
|
|
|
|
|
assert normalize_spaces(page.select_one(element).text) == expected_text
|
2017-11-23 15:08:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_user_phone_number_when_not_a_standard_phone_number(mocker):
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.main.views.conversation.service_api_client.get_inbound_sms_by_id',
|
|
|
|
|
|
return_value={
|
|
|
|
|
|
'user_number': 'ALPHANUM3R1C',
|
|
|
|
|
|
'notify_number': '07900000002'
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.main.views.conversation.notification_api_client.get_notification',
|
|
|
|
|
|
side_effect=HTTPError,
|
|
|
|
|
|
)
|
|
|
|
|
|
assert get_user_number('service', 'notification') == 'ALPHANUM3R1C'
|