-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtest_process_sms_client_response_tasks.py
More file actions
176 lines (133 loc) · 7.04 KB
/
test_process_sms_client_response_tasks.py
File metadata and controls
176 lines (133 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import uuid
from datetime import datetime
import pytest
from freezegun import freeze_time
from app import statsd_client
from app.celery.process_sms_client_response_tasks import (
process_sms_client_response,
)
from app.clients import ClientException
from app.constants import NOTIFICATION_TECHNICAL_FAILURE
def test_process_sms_client_response_raises_error_if_reference_is_not_a_valid_uuid(client):
with pytest.raises(ValueError):
process_sms_client_response(status="000", provider_reference="something-bad", client_name="sms-client")
@pytest.mark.parametrize("client_name", ("Firetext", "MMG"))
def test_process_sms_response_raises_client_exception_for_unknown_status(
sample_notification,
mocker,
client_name,
):
with pytest.raises(ClientException) as e:
process_sms_client_response(
status="000",
provider_reference=str(sample_notification.id),
client_name=client_name,
)
assert f"{client_name} callback failed: status {'000'} not found." in str(e.value)
assert sample_notification.status == NOTIFICATION_TECHNICAL_FAILURE
@freeze_time("2020-10-20T03:06:07.3")
@pytest.mark.parametrize(
"status, detailed_status_code, sms_provider, expected_notification_status, reason",
[
("0", None, "Firetext", "delivered", None),
("1", "101", "Firetext", "permanent-failure", "Unknown Subscriber"),
("2", "102", "Firetext", "pending", "Absent Subscriber"),
("2", "1", "MMG", "permanent-failure", "Number does not exist"),
("3", "2", "MMG", "delivered", "Delivered to operator"),
("4", "27", "MMG", "temporary-failure", "Absent Subscriber"),
("5", "13", "MMG", "permanent-failure", "Sender id blacklisted"),
],
)
def test_process_sms_client_response_updates_notification_status(
sample_notification, caplog, status, detailed_status_code, sms_provider, expected_notification_status, reason
):
sample_notification.status = "sending"
with caplog.at_level("INFO"):
process_sms_client_response(
status,
str(sample_notification.id),
sms_provider,
detailed_status_code,
"2020-10-20T03:04:05.1",
"2020-10-20T03:05:06.2",
)
message = f"{sms_provider} callback returned status of {expected_notification_status}({status}): {reason}({detailed_status_code}) for reference: {sample_notification.id}" # noqa
record = next(r for r in caplog.records if "callback returned status of" in r.msg)
assert record.message == message
assert record.receipt_received_at == datetime(2020, 10, 20, 3, 5, 6, 200000)
assert record.receipt_received_ago == 61.1
assert record.delivered_at == datetime(2020, 10, 20, 3, 4, 5, 100000)
assert record.delivered_ago == 122.2
assert sample_notification.status == expected_notification_status
@pytest.mark.parametrize(
"detailed_status_code, expected_notification_status, reason",
[
("101", "permanent-failure", "Unknown Subscriber"),
("102", "temporary-failure", "Absent Subscriber"),
(None, "temporary-failure", None),
("000", "temporary-failure", "No error reported"),
],
)
def test_process_sms_client_response_updates_notification_status_when_called_second_time(
sample_notification, caplog, detailed_status_code, expected_notification_status, reason
):
sample_notification.status = "sending"
process_sms_client_response("2", str(sample_notification.id), "Firetext")
with caplog.at_level("INFO"):
process_sms_client_response("1", str(sample_notification.id), "Firetext", detailed_status_code)
if detailed_status_code:
message = f"Updating notification id {sample_notification.id} to status {expected_notification_status}, reason: {reason}" # noqa
assert message in caplog.messages
assert sample_notification.status == expected_notification_status
@pytest.mark.parametrize("detailed_status_code", ["102", None, "000"])
def test_process_sms_client_response_updates_notification_status_to_pending_with_and_without_failure_code_present(
sample_notification, mocker, detailed_status_code
):
sample_notification.status = "sending"
process_sms_client_response("2", str(sample_notification.id), "Firetext", detailed_status_code)
assert sample_notification.status == "pending"
def test_process_sms_client_response_updates_notification_status_when_detailed_status_code_not_recognised(
sample_notification, caplog
):
sample_notification.status = "sending"
process_sms_client_response("2", str(sample_notification.id), "Firetext")
with caplog.at_level("WARNING"):
process_sms_client_response("1", str(sample_notification.id), "Firetext", "789")
assert (
f"Failure code 789 from Firetext not recognised when processing notification {sample_notification.id}"
in caplog.messages
)
assert sample_notification.status == "temporary-failure"
def test_sms_response_does_not_send_callback_if_notification_is_not_in_the_db(sample_service, mocker):
send_mock = mocker.patch("app.celery.process_sms_client_response_tasks.check_and_queue_callback_task")
reference = str(uuid.uuid4())
process_sms_client_response(status="3", provider_reference=reference, client_name="MMG")
send_mock.assert_not_called()
@freeze_time("2001-01-01T12:00:00")
def test_process_sms_client_response_records_statsd_metrics(sample_notification, client, mocker):
mocker.patch("app.statsd_client.incr")
mocker.patch("app.statsd_client.timing_with_dates")
sample_notification.status = "sending"
sample_notification.sent_at = datetime.utcnow()
process_sms_client_response("0", str(sample_notification.id), "Firetext")
statsd_client.incr.assert_any_call("callback.firetext.delivered")
statsd_client.timing_with_dates.assert_any_call(
"callback.firetext.delivered.elapsed-time", datetime.utcnow(), sample_notification.sent_at
)
def test_process_sms_updates_billable_units_if_zero(sample_notification):
sample_notification.billable_units = 0
process_sms_client_response("3", str(sample_notification.id), "MMG")
assert sample_notification.billable_units == 1
def test_process_sms_response_does_not_send_service_callback_for_pending_notifications(sample_notification, mocker):
send_mock = mocker.patch("app.celery.process_sms_client_response_tasks.check_and_queue_callback_task")
process_sms_client_response("2", str(sample_notification.id), "Firetext")
send_mock.assert_not_called()
def test_outcome_statistics_called_for_successful_callback(sample_notification, mocker):
send_mock = mocker.patch("app.celery.process_sms_client_response_tasks.check_and_queue_callback_task")
reference = str(sample_notification.id)
process_sms_client_response("3", reference, "MMG")
send_mock.assert_called_once_with(sample_notification)
def test_process_sms_updates_sent_by_with_client_name_if_not_in_noti(sample_notification):
sample_notification.sent_by = None
process_sms_client_response("3", str(sample_notification.id), "MMG")
assert sample_notification.sent_by == "mmg"