Merge branch 'main' into stvnrlly-paperless-api

This commit is contained in:
stvnrlly
2023-02-06 12:28:10 -05:00
87 changed files with 2484 additions and 2087 deletions

View File

@@ -1,5 +1,6 @@
import json
from datetime import datetime
from unittest.mock import ANY
from freezegun import freeze_time
@@ -14,11 +15,8 @@ from app.celery.research_mode_tasks import (
ses_notification_callback,
ses_soft_bounce_callback,
)
from app.celery.service_callback_tasks import (
create_delivery_status_callback_data,
)
from app.dao.notifications_dao import get_notification_by_id
from app.models import Complaint, Notification
from app.models import Complaint
from tests.app.conftest import create_sample_notification
from tests.app.db import (
create_notification,
@@ -156,7 +154,7 @@ def test_ses_callback_should_update_notification_status(
status='sending',
sent_at=datetime.utcnow()
)
callback_api = create_service_callback_api(
create_service_callback_api(
service=sample_email_template.service,
url="https://original_url.com"
)
@@ -167,9 +165,9 @@ def test_ses_callback_should_update_notification_status(
"callback.ses.elapsed-time", datetime.utcnow(), notification.sent_at
)
statsd_client.incr.assert_any_call("callback.ses.delivered")
updated_notification = Notification.query.get(notification.id)
encrypted_data = create_delivery_status_callback_data(updated_notification, callback_api)
send_mock.assert_called_once_with([str(notification.id), encrypted_data], queue="service-callbacks")
send_mock.assert_called_once_with([str(notification.id), ANY], queue="service-callbacks")
# assert second arg is an encrypted string
assert isinstance(send_mock.call_args.args[0][1], str)
def test_ses_callback_should_not_update_notification_status_if_already_delivered(sample_email_template, mocker):

View File

@@ -1,179 +0,0 @@
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.models import NOTIFICATION_TECHNICAL_FAILURE
@pytest.mark.skip(reason="Needs updating for TTS: Update with new providers")
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.skip(reason="Needs updating for TTS: Update with new providers")
@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
@pytest.mark.skip(reason="Needs updating for TTS: Update with new providers")
@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,
mocker,
status,
detailed_status_code,
sms_provider,
expected_notification_status,
reason
):
mock_logger = mocker.patch('app.celery.tasks.current_app.logger.info')
sample_notification.status = 'sending'
process_sms_client_response(status, str(sample_notification.id), sms_provider, detailed_status_code)
message = f"{sms_provider} callback returned status of {expected_notification_status}({status}): {reason}({detailed_status_code}) for reference: {sample_notification.id}" # noqa
mock_logger.assert_any_call(message)
assert sample_notification.status == expected_notification_status
@pytest.mark.skip(reason="Needs updating for TTS: Update with new providers")
@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,
mocker,
detailed_status_code,
expected_notification_status,
reason
):
mock_logger = mocker.patch('app.celery.tasks.current_app.logger.info')
sample_notification.status = 'sending'
process_sms_client_response('2', str(sample_notification.id), 'Firetext')
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
mock_logger.assert_called_with(message)
assert sample_notification.status == expected_notification_status
@pytest.mark.skip(reason="Needs updating for TTS: Update with new providers")
@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'
@pytest.mark.skip(reason="Needs updating for TTS: Update with new providers")
def test_process_sms_client_response_updates_notification_status_when_detailed_status_code_not_recognised(
sample_notification,
mocker,
):
mock_logger = mocker.patch('app.celery.tasks.current_app.logger.warning')
sample_notification.status = 'sending'
process_sms_client_response('2', str(sample_notification.id), 'Firetext')
process_sms_client_response('1', str(sample_notification.id), 'Firetext', '789')
mock_logger.assert_called_once_with('Failure code 789 from Firetext not recognised')
assert sample_notification.status == 'temporary-failure'
@pytest.mark.skip(reason="Needs updating for TTS: Update with new providers")
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()
@pytest.mark.skip(reason="Needs updating for TTS: Update with new providers")
@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
)
@pytest.mark.skip(reason="Needs updating for TTS: Update with new providers")
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
@pytest.mark.skip(reason="Needs updating for TTS: Update with new providers")
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()
@pytest.mark.skip(reason="Needs updating for TTS: Update with new providers")
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)
@pytest.mark.skip(reason="Needs updating for TTS: Update with new providers")
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'

View File

@@ -168,7 +168,7 @@ def test_create_nightly_billing_for_day_sms_rate_multiplier(
created_at=yesterday,
template=sample_template,
status='delivered',
sent_by='mmg',
sent_by='sns',
international=False,
rate_multiplier=1.0,
billable_units=1,
@@ -177,7 +177,7 @@ def test_create_nightly_billing_for_day_sms_rate_multiplier(
created_at=yesterday,
template=sample_template,
status='delivered',
sent_by='mmg',
sent_by='sns',
international=False,
rate_multiplier=second_rate,
billable_units=1,
@@ -212,7 +212,7 @@ def test_create_nightly_billing_for_day_different_templates(
created_at=yesterday,
template=sample_template,
status='delivered',
sent_by='mmg',
sent_by='sns',
international=False,
rate_multiplier=1.0,
billable_units=1,
@@ -221,7 +221,7 @@ def test_create_nightly_billing_for_day_different_templates(
created_at=yesterday,
template=sample_email_template,
status='delivered',
sent_by='ses',
sent_by='sns',
international=False,
rate_multiplier=0,
billable_units=0,
@@ -260,7 +260,7 @@ def test_create_nightly_billing_for_day_different_sent_by(
created_at=yesterday,
template=sample_template,
status='delivered',
sent_by='mmg',
sent_by='sns',
international=False,
rate_multiplier=1.0,
billable_units=1,
@@ -269,7 +269,7 @@ def test_create_nightly_billing_for_day_different_sent_by(
created_at=yesterday,
template=sample_template,
status='delivered',
sent_by='firetext',
sent_by='sns',
international=False,
rate_multiplier=1.0,
billable_units=1,

View File

@@ -6,11 +6,11 @@ from flask import json
from app.celery.research_mode_tasks import (
HTTPError,
firetext_callback,
mmg_callback,
create_fake_letter_response_file,
send_email_response,
send_sms_response,
ses_notification_callback,
sns_callback,
)
from app.config import QueueNames
from tests.conftest import Matcher
@@ -21,22 +21,24 @@ dvla_response_file_matcher = Matcher(
)
def test_make_mmg_callback(notify_api, rmock):
endpoint = "http://localhost:6011/notifications/sms/mmg"
@pytest.mark.skip(reason="Re-enable when SMS receipts exist")
def test_make_sns_callback(notify_api, rmock):
endpoint = "http://localhost:6011/notifications/sms/sns"
rmock.request(
"POST",
endpoint,
json={"status": "success"},
status_code=200)
send_sms_response("mmg", "1234", "07700900001")
send_sms_response("sns", "1234", "2028675309")
assert rmock.called
assert rmock.request_history[0].url == endpoint
assert json.loads(rmock.request_history[0].text)['MSISDN'] == '07700900001'
assert json.loads(rmock.request_history[0].text)['MSISDN'] == '2028675309'
@pytest.mark.skip(reason="Re-enable when SMS receipts exist")
def test_callback_logs_on_api_call_failure(notify_api, rmock, mocker):
endpoint = "http://localhost:6011/notifications/sms/mmg"
endpoint = "http://localhost:6011/notifications/sms/sns"
rmock.request(
"POST",
endpoint,
@@ -54,23 +56,6 @@ def test_callback_logs_on_api_call_failure(notify_api, rmock, mocker):
)
@pytest.mark.parametrize("phone_number",
["07700900001", "07700900002", "07700900003",
"07700900236"])
def test_make_firetext_callback(notify_api, rmock, phone_number):
endpoint = "http://localhost:6011/notifications/sms/firetext"
rmock.request(
"POST",
endpoint,
json="some data",
status_code=200)
send_sms_response("firetext", "1234", phone_number)
assert rmock.called
assert rmock.request_history[0].url == endpoint
assert 'mobile={}'.format(phone_number) in rmock.request_history[0].text
def test_make_ses_callback(notify_api, mocker):
mock_task = mocker.patch('app.celery.research_mode_tasks.process_ses_results')
some_ref = str(uuid.uuid4())
@@ -81,50 +66,31 @@ def test_make_ses_callback(notify_api, mocker):
assert mock_task.apply_async.call_args[0][0][0] == ses_notification_callback(some_ref)
@pytest.mark.parametrize("phone_number", ["07700900001", "+447700900001", "7700900001", "+44 7700900001",
"+447700900236"])
def test_delivered_mmg_callback(phone_number):
data = json.loads(mmg_callback("1234", phone_number))
@pytest.mark.skip(reason="Re-enable when SNS delivery receipts exist")
def test_delievered_sns_callback():
phone_number = "2028675309"
data = json.loads(sns_callback("1234", phone_number))
assert data['MSISDN'] == phone_number
assert data['status'] == "3"
assert data['reference'] == "mmg_reference"
assert data['reference'] == "sns_reference"
assert data['CID'] == "1234"
@pytest.mark.parametrize("phone_number", ["07700900002", "+447700900002", "7700900002", "+44 7700900002"])
def test_perm_failure_mmg_callback(phone_number):
data = json.loads(mmg_callback("1234", phone_number))
@pytest.mark.skip(reason="Re-enable when SNS delivery receipts exist")
def test_perm_failure_sns_callback():
phone_number = "2028675302"
data = json.loads(sns_callback("1234", phone_number))
assert data['MSISDN'] == phone_number
assert data['status'] == "5"
assert data['reference'] == "mmg_reference"
assert data['reference'] == "sns_reference"
assert data['CID'] == "1234"
@pytest.mark.parametrize("phone_number", ["07700900003", "+447700900003", "7700900003", "+44 7700900003"])
def test_temp_failure_mmg_callback(phone_number):
data = json.loads(mmg_callback("1234", phone_number))
@pytest.mark.skip(reason="Re-enable when SNS delivery receipts exist")
def test_temp_failure_sns_callback():
phone_number = "2028675303"
data = json.loads(sns_callback("1234", phone_number))
assert data['MSISDN'] == phone_number
assert data['status'] == "4"
assert data['reference'] == "mmg_reference"
assert data['reference'] == "sns_reference"
assert data['CID'] == "1234"
@pytest.mark.parametrize("phone_number", ["07700900001", "+447700900001", "7700900001", "+44 7700900001",
"+447700900256"])
def test_delivered_firetext_callback(phone_number):
assert firetext_callback('1234', phone_number) == {
'mobile': phone_number,
'status': '0',
'time': '2016-03-10 14:17:00',
'reference': '1234'
}
@pytest.mark.parametrize("phone_number", ["07700900002", "+447700900002", "7700900002", "+44 7700900002"])
def test_failure_firetext_callback(phone_number):
assert firetext_callback('1234', phone_number) == {
'mobile': phone_number,
'status': '1',
'time': '2016-03-10 14:17:00',
'reference': '1234'
}

View File

@@ -72,9 +72,6 @@ class AnyStringWith(str):
return self in other
mmg_error = {'Error': '40', 'Description': 'error'}
def _notification_json(template, to, personalisation=None, job_id=None, row_number=0):
return {
"template": str(template.id),
@@ -467,7 +464,6 @@ def test_should_send_template_to_correct_sms_task_and_persist(sample_template_wi
assert not persisted_notification.sent_by
assert not persisted_notification.job_id
assert persisted_notification.personalisation == {'name': 'Jo'}
assert persisted_notification._personalisation == encryption.encrypt({"name": "Jo"})
assert persisted_notification.notification_type == 'sms'
mocked_deliver_sms.assert_called_once_with(
[str(persisted_notification.id)],
@@ -500,10 +496,10 @@ def test_should_put_save_sms_task_in_research_mode_queue_if_research_mode_servic
def test_should_save_sms_if_restricted_service_and_valid_number(notify_db_session, mocker):
user = create_user(mobile_number="07700 900890")
user = create_user(mobile_number="202-867-5309")
service = create_service(user=user, restricted=True)
template = create_template(service=service)
notification = _notification_json(template, "+447700900890") # The users own number, but in a different format
notification = _notification_json(template, "+12028675309") # The users own number, but in a different format
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
@@ -516,7 +512,7 @@ def test_should_save_sms_if_restricted_service_and_valid_number(notify_db_sessio
)
persisted_notification = Notification.query.one()
assert persisted_notification.to == '+447700900890'
assert persisted_notification.to == '+12028675309'
assert persisted_notification.template_id == template.id
assert persisted_notification.template_version == template.version
assert persisted_notification.status == 'created'
@@ -551,11 +547,11 @@ def test_save_email_should_save_default_email_reply_to_text_on_notification(noti
assert persisted_notification.reply_to_text == 'reply_to@digital.gov.uk'
def test_save_sms_should_save_default_smm_sender_notification_reply_to_text_on(notify_db_session, mocker):
def test_save_sms_should_save_default_sms_sender_notification_reply_to_text_on(notify_db_session, mocker):
service = create_service_with_defined_sms_sender(sms_sender_value='12345')
template = create_template(service=service)
notification = _notification_json(template, to="07700 900205")
notification = _notification_json(template, to="2028675309")
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
notification_id = uuid.uuid4()
@@ -570,11 +566,11 @@ def test_save_sms_should_save_default_smm_sender_notification_reply_to_text_on(n
def test_should_not_save_sms_if_restricted_service_and_invalid_number(notify_db_session, mocker):
user = create_user(mobile_number="07700 900205")
user = create_user(mobile_number="2028675309")
service = create_service(user=user, restricted=True)
template = create_template(service=service)
notification = _notification_json(template, "07700 900849")
notification = _notification_json(template, "2028675400")
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
notification_id = uuid.uuid4()
@@ -665,14 +661,14 @@ def test_should_save_sms_template_to_and_persist_with_job_id(sample_job, mocker)
def test_should_not_save_sms_if_team_key_and_recipient_not_in_team(notify_db_session, mocker):
assert Notification.query.count() == 0
user = create_user(mobile_number="07700 900205")
user = create_user(mobile_number="2028675309")
service = create_service(user=user, restricted=True)
template = create_template(service=service)
team_members = [user.mobile_number for user in service.users]
assert "07890 300000" not in team_members
notification = _notification_json(template, "07700 900849")
notification = _notification_json(template, "2028675400")
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
notification_id = uuid.uuid4()
@@ -715,7 +711,6 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
assert not persisted_notification.sent_by
assert persisted_notification.job_row_number == 1
assert persisted_notification.personalisation == {'name': 'Jo'}
assert persisted_notification._personalisation == encryption.encrypt({"name": "Jo"})
assert persisted_notification.api_key_id is None
assert persisted_notification.key_type == KEY_TYPE_NORMAL
assert persisted_notification.notification_type == 'email'
@@ -930,10 +925,10 @@ def test_save_sms_does_not_send_duplicate_and_does_not_put_in_retry_queue(sample
def test_save_sms_uses_sms_sender_reply_to_text(mocker, notify_db_session):
service = create_service_with_defined_sms_sender(sms_sender_value='07123123123')
service = create_service_with_defined_sms_sender(sms_sender_value='2028675309')
template = create_template(service=service)
notification = _notification_json(template, to="07700 900205")
notification = _notification_json(template, to="2028675301")
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
notification_id = uuid.uuid4()
@@ -944,15 +939,15 @@ def test_save_sms_uses_sms_sender_reply_to_text(mocker, notify_db_session):
)
persisted_notification = Notification.query.one()
assert persisted_notification.reply_to_text == '447123123123'
assert persisted_notification.reply_to_text == '+12028675309'
def test_save_sms_uses_non_default_sms_sender_reply_to_text_if_provided(mocker, notify_db_session):
service = create_service_with_defined_sms_sender(sms_sender_value='07123123123')
service = create_service_with_defined_sms_sender(sms_sender_value='2028675309')
template = create_template(service=service)
new_sender = service_sms_sender_dao.dao_add_sms_sender_for_service(service.id, 'new-sender', False)
notification = _notification_json(template, to="07700 900205")
notification = _notification_json(template, to="202-867-5301")
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
notification_id = uuid.uuid4()
@@ -1472,7 +1467,7 @@ def test_save_api_email_dont_retry_if_notification_already_exists(sample_service
), (
save_sms,
'app.celery.provider_tasks.deliver_sms.apply_async',
'07700 900890',
'202-867-5309',
{'template_type': 'sms'}
),
))