2017-11-17 13:41:45 +00:00
|
|
|
import uuid
|
2023-03-02 20:20:31 -05:00
|
|
|
from unittest.mock import ANY
|
2017-11-17 13:41:45 +00:00
|
|
|
|
2017-01-20 13:17:53 +00:00
|
|
|
import pytest
|
2023-03-02 20:20:31 -05:00
|
|
|
from flask import json
|
2017-11-17 13:41:45 +00:00
|
|
|
|
2016-05-31 16:55:26 +01:00
|
|
|
from app.celery.research_mode_tasks import (
|
2021-03-10 13:55:06 +00:00
|
|
|
HTTPError,
|
|
|
|
|
send_email_response,
|
|
|
|
|
send_sms_response,
|
2016-05-31 16:55:26 +01:00
|
|
|
ses_notification_callback,
|
2022-12-21 15:10:44 -05:00
|
|
|
sns_callback,
|
2016-05-31 16:55:26 +01:00
|
|
|
)
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.config import QueueNames
|
2023-03-02 20:20:31 -05:00
|
|
|
from tests.conftest import Matcher
|
2018-07-17 15:27:43 +01:00
|
|
|
|
|
|
|
|
dvla_response_file_matcher = Matcher(
|
|
|
|
|
'dvla_response_file',
|
|
|
|
|
lambda x: 'NOTIFY-20180125140000-RSP.TXT' < x <= 'NOTIFY-20180125140030-RSP.TXT'
|
|
|
|
|
)
|
2016-05-31 16:55:26 +01:00
|
|
|
|
|
|
|
|
|
2022-12-21 15:10:44 -05:00
|
|
|
@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"
|
2016-05-31 16:55:26 +01:00
|
|
|
rmock.request(
|
|
|
|
|
"POST",
|
|
|
|
|
endpoint,
|
|
|
|
|
json={"status": "success"},
|
|
|
|
|
status_code=200)
|
2023-01-04 16:35:25 -05:00
|
|
|
send_sms_response("sns", "1234", "2028675309")
|
2016-05-31 16:55:26 +01:00
|
|
|
|
|
|
|
|
assert rmock.called
|
2016-06-09 14:14:27 +01:00
|
|
|
assert rmock.request_history[0].url == endpoint
|
2023-01-04 16:35:25 -05:00
|
|
|
assert json.loads(rmock.request_history[0].text)['MSISDN'] == '2028675309'
|
2016-05-31 16:55:26 +01:00
|
|
|
|
|
|
|
|
|
2022-12-21 15:10:44 -05:00
|
|
|
@pytest.mark.skip(reason="Re-enable when SMS receipts exist")
|
2020-12-21 14:30:39 +00:00
|
|
|
def test_callback_logs_on_api_call_failure(notify_api, rmock, mocker):
|
2022-12-21 15:10:44 -05:00
|
|
|
endpoint = "http://localhost:6011/notifications/sms/sns"
|
2020-12-21 14:30:39 +00:00
|
|
|
rmock.request(
|
|
|
|
|
"POST",
|
|
|
|
|
endpoint,
|
|
|
|
|
json={"error": "something went wrong"},
|
|
|
|
|
status_code=500)
|
|
|
|
|
mock_logger = mocker.patch('app.celery.tasks.current_app.logger.error')
|
|
|
|
|
|
|
|
|
|
with pytest.raises(HTTPError):
|
|
|
|
|
send_sms_response("mmg", "1234", "07700900001")
|
|
|
|
|
|
|
|
|
|
assert rmock.called
|
|
|
|
|
assert rmock.request_history[0].url == endpoint
|
|
|
|
|
mock_logger.assert_called_once_with(
|
|
|
|
|
'API POST request on http://localhost:6011/notifications/sms/mmg failed with status 500'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2017-11-17 13:41:45 +00:00
|
|
|
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())
|
2016-05-31 16:55:26 +01:00
|
|
|
|
2017-11-17 13:41:45 +00:00
|
|
|
send_email_response(reference=some_ref, to="test@test.com")
|
|
|
|
|
|
|
|
|
|
mock_task.apply_async.assert_called_once_with(ANY, queue=QueueNames.RESEARCH_MODE)
|
|
|
|
|
assert mock_task.apply_async.call_args[0][0][0] == ses_notification_callback(some_ref)
|
2016-05-31 16:55:26 +01:00
|
|
|
|
|
|
|
|
|
2022-12-21 15:10:44 -05:00
|
|
|
@pytest.mark.skip(reason="Re-enable when SNS delivery receipts exist")
|
|
|
|
|
def test_delievered_sns_callback():
|
2023-01-04 16:35:25 -05:00
|
|
|
phone_number = "2028675309"
|
2022-12-21 15:10:44 -05:00
|
|
|
data = json.loads(sns_callback("1234", phone_number))
|
2017-01-20 13:17:53 +00:00
|
|
|
assert data['MSISDN'] == phone_number
|
2016-06-01 16:57:57 +01:00
|
|
|
assert data['status'] == "3"
|
2022-12-21 15:10:44 -05:00
|
|
|
assert data['reference'] == "sns_reference"
|
2016-05-31 16:55:26 +01:00
|
|
|
assert data['CID'] == "1234"
|
|
|
|
|
|
|
|
|
|
|
2022-12-21 15:10:44 -05:00
|
|
|
@pytest.mark.skip(reason="Re-enable when SNS delivery receipts exist")
|
|
|
|
|
def test_perm_failure_sns_callback():
|
2023-01-04 16:35:25 -05:00
|
|
|
phone_number = "2028675302"
|
2022-12-21 15:10:44 -05:00
|
|
|
data = json.loads(sns_callback("1234", phone_number))
|
2017-01-20 13:17:53 +00:00
|
|
|
assert data['MSISDN'] == phone_number
|
2016-05-31 16:55:26 +01:00
|
|
|
assert data['status'] == "5"
|
2022-12-21 15:10:44 -05:00
|
|
|
assert data['reference'] == "sns_reference"
|
2016-05-31 16:55:26 +01:00
|
|
|
assert data['CID'] == "1234"
|
|
|
|
|
|
|
|
|
|
|
2022-12-21 15:10:44 -05:00
|
|
|
@pytest.mark.skip(reason="Re-enable when SNS delivery receipts exist")
|
|
|
|
|
def test_temp_failure_sns_callback():
|
2023-01-04 16:35:25 -05:00
|
|
|
phone_number = "2028675303"
|
2022-12-21 15:10:44 -05:00
|
|
|
data = json.loads(sns_callback("1234", phone_number))
|
2017-01-20 13:17:53 +00:00
|
|
|
assert data['MSISDN'] == phone_number
|
2016-05-31 16:55:26 +01:00
|
|
|
assert data['status'] == "4"
|
2022-12-21 15:10:44 -05:00
|
|
|
assert data['reference'] == "sns_reference"
|
2016-05-31 16:55:26 +01:00
|
|
|
assert data['CID'] == "1234"
|