Files
notifications-api/tests/app/celery/test_test_key_tasks.py

91 lines
2.8 KiB
Python
Raw Normal View History

import uuid
from unittest.mock import ANY
import pytest
from flask import json
2023-08-25 12:09:00 -07:00
from app.celery.test_key_tasks import (
2021-03-10 13:55:06 +00:00
HTTPError,
send_email_response,
send_sms_response,
ses_notification_callback,
sns_callback,
)
2021-03-10 13:55:06 +00:00
from app.config import QueueNames
from app.enums import NotificationStatus
from app.models import Notification
from tests.conftest import Matcher
dvla_response_file_matcher = Matcher(
2023-08-29 14:54:30 -07:00
"dvla_response_file",
lambda x: "NOTIFY-20180125140000-RSP.TXT" < x <= "NOTIFY-20180125140030-RSP.TXT",
)
2023-05-22 09:12:12 -07:00
def test_make_sns_callback(notify_api, rmock, mocker):
endpoint = "http://localhost:6011/notifications/sms/sns"
2023-08-29 14:54:30 -07:00
get_notification_by_id = mocker.patch(
"app.celery.test_key_tasks.get_notification_by_id"
)
2023-05-22 09:12:12 -07:00
n = Notification()
n.id = 1234
n.status = NotificationStatus.DELIVERED
2023-05-22 09:12:12 -07:00
get_notification_by_id.return_value = n
2023-08-29 14:54:30 -07:00
rmock.request("POST", endpoint, json={"status": "success"}, status_code=200)
2023-05-22 09:12:12 -07:00
send_sms_response("sns", "1234")
assert rmock.called
assert rmock.request_history[0].url == endpoint
2023-08-29 14:54:30 -07:00
assert json.loads(rmock.request_history[0].text)["status"] == "delivered"
def test_callback_logs_on_api_call_failure(notify_api, rmock, mocker):
endpoint = "http://localhost:6011/notifications/sms/sns"
2023-08-29 14:54:30 -07:00
get_notification_by_id = mocker.patch(
"app.celery.test_key_tasks.get_notification_by_id"
)
2023-05-22 09:12:12 -07:00
n = Notification()
n.id = 1234
n.status = NotificationStatus.FAILED
2023-05-22 09:12:12 -07:00
get_notification_by_id.return_value = n
rmock.request(
2023-08-29 14:54:30 -07:00
"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):
2023-05-22 09:12:12 -07:00
send_sms_response("sns", "1234")
assert rmock.called
assert rmock.request_history[0].url == endpoint
mock_logger.assert_called_once_with(
2023-08-29 14:54:30 -07:00
"API POST request on http://localhost:6011/notifications/sms/sns failed with status 500"
)
def test_make_ses_callback(notify_api, mocker):
2023-08-29 14:54:30 -07:00
mock_task = mocker.patch("app.celery.test_key_tasks.process_ses_results")
some_ref = str(uuid.uuid4())
send_email_response(reference=some_ref, to="test@test.com")
2023-08-25 12:09:00 -07:00
mock_task.apply_async.assert_called_once_with(ANY, queue=QueueNames.SEND_EMAIL)
2023-08-29 14:54:30 -07:00
assert mock_task.apply_async.call_args[0][0][0] == ses_notification_callback(
some_ref
)
2023-05-22 09:12:12 -07:00
def test_delivered_sns_callback(mocker):
2023-08-29 14:54:30 -07:00
get_notification_by_id = mocker.patch(
"app.celery.test_key_tasks.get_notification_by_id"
)
2023-05-22 09:12:12 -07:00
n = Notification()
n.id = 1234
n.status = NotificationStatus.DELIVERED
2023-05-22 09:12:12 -07:00
get_notification_by_id.return_value = n
2023-05-22 09:12:12 -07:00
data = json.loads(sns_callback("1234"))
2023-08-29 14:54:30 -07:00
assert data["status"] == "delivered"
assert data["CID"] == "1234"