DRY-up conditionally creating callback tasks

This removes 3 duplicate instances of the same code, which is still
tested implicitly via test_process_ses_receipt_tasks [1]. In the
next commit we'll make this test more explicit, to reflect that it's
now being reused elsewhere and shouldn't change arbitrarily.

We do lose the "print" statement from the command instance of the
code, but I think that's a very tolerable loss.

[1]: 16ec8ccb8a/tests/app/celery/test_process_ses_receipts_tasks.py (L94)
This commit is contained in:
Ben Thorner
2021-11-26 15:18:53 +00:00
parent aea555fce2
commit 04da017558
8 changed files with 25 additions and 117 deletions

View File

@@ -24,16 +24,11 @@ from app.celery.nightly_tasks import (
save_daily_notification_processing_time,
timeout_notifications,
)
from app.celery.service_callback_tasks import (
create_delivery_status_callback_data,
)
from app.config import QueueNames
from app.models import EMAIL_TYPE, LETTER_TYPE, SMS_TYPE, FactProcessingTime
from tests.app.db import (
create_job,
create_notification,
create_service,
create_service_callback_api,
create_service_data_retention,
create_template,
)
@@ -166,8 +161,8 @@ def test_delete_letter_notifications_older_than_retention_calls_child_task(notif
mocked.assert_called_once_with('letter')
def test_timeout_notifications_no_callbacks(mocker, sample_notification):
mock_update = mocker.patch('app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async')
def test_timeout_notifications(mocker, sample_notification):
mock_update = mocker.patch('app.celery.nightly_tasks.check_and_queue_callback_task')
mock_dao = mocker.patch('app.celery.nightly_tasks.dao_timeout_notifications')
mock_dao.return_value = [sample_notification]
@@ -177,19 +172,7 @@ def test_timeout_notifications_no_callbacks(mocker, sample_notification):
current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD')
)
mock_update.assert_not_called()
def test_timeout_notifications_with_callbacks(mocker, sample_notification):
mock_update = mocker.patch('app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async')
mock_dao = mocker.patch('app.celery.nightly_tasks.dao_timeout_notifications')
mock_dao.return_value = [sample_notification]
callback_api = create_service_callback_api(service=sample_notification.service)
timeout_notifications()
encrypted_data = create_delivery_status_callback_data(sample_notification, callback_api)
mock_update.assert_called_once_with([str(sample_notification.id), encrypted_data], queue=QueueNames.CALLBACKS)
mock_update.assert_called_once_with(sample_notification)
def test_delete_inbound_sms_calls_child_task(notify_api, mocker):

View File

@@ -8,12 +8,8 @@ from app import statsd_client
from app.celery.process_sms_client_response_tasks import (
process_sms_client_response,
)
from app.celery.service_callback_tasks import (
create_delivery_status_callback_data,
)
from app.clients import ClientException
from app.models import NOTIFICATION_TECHNICAL_FAILURE
from tests.app.db import create_service_callback_api
def test_process_sms_client_response_raises_error_if_reference_is_not_a_valid_uuid(client):
@@ -121,12 +117,7 @@ def test_process_sms_client_response_updates_notification_status_when_detailed_s
def test_sms_response_does_not_send_callback_if_notification_is_not_in_the_db(sample_service, mocker):
mocker.patch(
'app.celery.process_sms_client_response_tasks.get_service_delivery_status_callback_api_for_service',
return_value='mock-delivery-callback-for-service')
send_mock = mocker.patch(
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
)
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()
@@ -156,26 +147,17 @@ def test_process_sms_updates_billable_units_if_zero(sample_notification):
def test_process_sms_response_does_not_send_service_callback_for_pending_notifications(sample_notification, mocker):
mocker.patch(
'app.celery.process_sms_client_response_tasks.get_service_delivery_status_callback_api_for_service',
return_value='fake-callback')
send_mock = mocker.patch('app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async')
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.service_callback_tasks.send_delivery_status_to_service.apply_async'
)
callback_api = create_service_callback_api(service=sample_notification.service, url="https://original_url.com")
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')
encrypted_data = create_delivery_status_callback_data(sample_notification, callback_api)
send_mock.assert_called_once_with([reference, encrypted_data],
queue="service-callbacks")
send_mock.assert_called_once_with(sample_notification)
def test_process_sms_updates_sent_by_with_client_name_if_not_in_noti(sample_notification):

View File

@@ -1,9 +1,8 @@
import uuid
from app.commands import local_dev_broadcast_permissions, replay_callbacks
from app.config import QueueNames
from app.dao.services_dao import dao_add_user_to_service
from tests.app.db import create_service_callback_api, create_user
from tests.app.db import create_user
def test_local_dev_broadcast_permissions(
@@ -33,10 +32,7 @@ def test_replay_callbacks(
tmpdir,
notify_api,
):
mock_apply = mocker.patch('app.commands.send_delivery_status_to_service.apply_async')
mock_update = mocker.patch('app.commands.create_delivery_status_callback_data')
mock_update.return_value = 'encrypted_status_update'
mock_task = mocker.patch('app.commands.check_and_queue_callback_task')
file_path = tmpdir + 'callback_ids.txt'
missing_notification_id = uuid.uuid4()
@@ -48,20 +44,6 @@ def test_replay_callbacks(
replay_callbacks, ['-f', file_path]
)
mock_apply.assert_not_called()
assert f'{missing_notification_id} was not found' in result.output
assert "Callback api was not found" in result.output
# Now re-run with the callback API in place
create_service_callback_api(service=sample_service, bearer_token='foo')
result = notify_api.test_cli_runner().invoke(
replay_callbacks, ['-f', file_path]
)
mock_apply.assert_called_once_with(
[str(sample_notification.id), 'encrypted_status_update'],
queue=QueueNames.CALLBACKS
)
mock_task.assert_called_once_with(sample_notification)
assert result.exit_code == 0