From 6e3d339564772d423c7264b244fdffc29fbd6da9 Mon Sep 17 00:00:00 2001 From: Stephen Astels Date: Fri, 12 Apr 2024 15:12:52 -0400 Subject: [PATCH] make tests pass --- app/aws/mocks.py | 14 +++++++------- app/celery/process_pinpoint_receipts_tasks.py | 14 +++++++++----- .../celery/test_process_pinpoint_receipts_tasks.py | 10 +++++----- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/app/aws/mocks.py b/app/aws/mocks.py index 11d619d823..62d6c21c0c 100644 --- a/app/aws/mocks.py +++ b/app/aws/mocks.py @@ -206,14 +206,14 @@ def pinpoint_success_callback(reference=None, timestamp=1467074434, destination= "mnc": "610", "carrierName": "Bell Cellular Inc. / Aliant Telecom", "messageId": reference, - "messageRequestTimestamp": 1712944267685, + "messageRequestTimestamp": timestamp, "messageEncoding": "GSM", "messageType": "TRANSACTIONAL", "messageStatus": "DELIVERED", "messageStatusDescription": "Message has been accepted by phone", "totalMessageParts": 1, "totalMessagePrice": 0.00581, - "totalCarrierFee": 0.006 + "totalCarrierFee": 0.006, } return _pinpoint_callback(body) @@ -221,24 +221,23 @@ def pinpoint_success_callback(reference=None, timestamp=1467074434, destination= # Note that 1467074434 = 2016-06-28 00:40:34.558 UTC def pinpoint_failed_callback(provider_response, reference=None, timestamp=1467074434, destination="+1XXX5550100"): - body = { "eventType": "TEXT_CARRIER_UNREACHABLE", "eventVersion": "1.0", "eventTimestamp": timestamp, "isFinal": True, - "originationPhoneNumber": "+13655362471", + "originationPhoneNumber": "+13655550100", "destinationPhoneNumber": destination, "isoCountryCode": "CA", "messageId": reference, - "messageRequestTimestamp": 1712944592827, + "messageRequestTimestamp": timestamp, "messageEncoding": "GSM", "messageType": "TRANSACTIONAL", "messageStatus": "CARRIER_UNREACHABLE", "messageStatusDescription": provider_response, "totalMessageParts": 1, "totalMessagePrice": 0.00581, - "totalCarrierFee": 0.006 + "totalCarrierFee": 0.006, } return _pinpoint_callback(body) @@ -320,6 +319,7 @@ def _sns_callback(body): "MessageAttributes": {}, } + # TODO: can we just use the _sns_callback() function instead of this one? def _pinpoint_callback(body): return { @@ -334,4 +334,4 @@ def _pinpoint_callback(body): "SigningCertUrl": "https://sns.ca-central-1.amazonaws.com/SimpleNotificationService-[REDACTED].pem", "UnsubscribeUrl": "https://sns.ca-central-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=[REACTED]", "MessageAttributes": {}, - } \ No newline at end of file + } diff --git a/app/celery/process_pinpoint_receipts_tasks.py b/app/celery/process_pinpoint_receipts_tasks.py index 3d533dc442..21410bdcc8 100644 --- a/app/celery/process_pinpoint_receipts_tasks.py +++ b/app/celery/process_pinpoint_receipts_tasks.py @@ -18,8 +18,7 @@ from app.notifications.callbacks import _check_and_queue_callback_task from celery.exceptions import Retry - -# Pinpoint receipts are of the form: +# Pinpoint receipts are of the form: # { # "eventType": "TEXT_DELIVERED", # "eventVersion": "1.0", @@ -42,11 +41,12 @@ # "totalCarrierFee": 0.006 # } + @notify_celery.task(bind=True, name="process-pinpoint-result", max_retries=5, default_retry_delay=300) @statsd(namespace="tasks") def process_pinpoint_results(self, response): try: - receipt = json.loads(response) + receipt = json.loads(response["Message"]) reference = receipt["messageId"] status = receipt["messageStatus"] provider_response = receipt["messageStatusDescription"] @@ -92,12 +92,16 @@ def process_pinpoint_results(self, response): ) ) else: - current_app.logger.info(f"Pinpoint callback return status of {notification_status} for notification: {notification.id}") + current_app.logger.info( + f"Pinpoint callback return status of {notification_status} for notification: {notification.id}" + ) statsd_client.incr(f"callback.sns.{notification_status}") # TODO: do we want a Pinpoint metric here? if notification.sent_at: - statsd_client.timing_with_dates("callback.sns.elapsed-time", datetime.utcnow(), notification.sent_at) # TODO: do we want a Pinpoint metric here? + statsd_client.timing_with_dates( + "callback.sns.elapsed-time", datetime.utcnow(), notification.sent_at + ) # TODO: do we want a Pinpoint metric here? _check_and_queue_callback_task(notification) diff --git a/tests/app/celery/test_process_pinpoint_receipts_tasks.py b/tests/app/celery/test_process_pinpoint_receipts_tasks.py index 8e19e69300..e60180a0c9 100644 --- a/tests/app/celery/test_process_pinpoint_receipts_tasks.py +++ b/tests/app/celery/test_process_pinpoint_receipts_tasks.py @@ -39,7 +39,7 @@ def test_process_pinpoint_results_delivered(sample_template, notify_db, notify_d assert get_notification_by_id(notification.id).status == NOTIFICATION_SENT assert process_pinpoint_results(pinpoint_success_callback(reference="ref")) assert get_notification_by_id(notification.id).status == NOTIFICATION_DELIVERED - assert get_notification_by_id(notification.id).provider_response == "Message has been accepted by phone carrier" + assert get_notification_by_id(notification.id).provider_response == "Message has been accepted by phone" mock_logger.assert_called_once_with(f"Pinpoint callback return status of delivered for notification: {notification.id}") @@ -155,7 +155,7 @@ def test_process_pinpoint_results_does_not_process_other_providers(sample_templa reference="ref1", sent_at=datetime.utcnow(), status=NOTIFICATION_SENT, - sent_by="pinpoint", + sent_by="sns", ) ) @@ -183,9 +183,9 @@ def test_process_pinpoint_results_calls_service_callback(sample_template, notify assert process_pinpoint_results(pinpoint_success_callback(reference="ref")) assert get_notification_by_id(notification.id).status == NOTIFICATION_DELIVERED - assert get_notification_by_id(notification.id).provider_response == "Message has been accepted by phone carrier" - statsd_client.timing_with_dates.assert_any_call("callback.pinpoint.elapsed-time", datetime.utcnow(), notification.sent_at) - statsd_client.incr.assert_any_call("callback.pinpoint.delivered") + assert get_notification_by_id(notification.id).provider_response == "Message has been accepted by phone" + statsd_client.timing_with_dates.assert_any_call("callback.sns.elapsed-time", datetime.utcnow(), notification.sent_at) + statsd_client.incr.assert_any_call("callback.sns.delivered") updated_notification = get_notification_by_id(notification.id) signed_data = create_delivery_status_callback_data(updated_notification, callback_api) send_mock.assert_called_once_with([str(notification.id), signed_data], queue="service-callbacks")