Skip to content

Commit

Permalink
Merge branch 'main' into fix-fake-callback
Browse files Browse the repository at this point in the history
  • Loading branch information
sastels authored Jun 4, 2024
2 parents c76661a + af825ef commit 16f70b6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
25 changes: 24 additions & 1 deletion app/aws/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def pinpoint_successful_callback(reference=None, timestamp=1467074434, destinati
"eventVersion": "1.0",
"eventTimestamp": timestamp,
"isFinal": False,
"originationPhoneNumber": "+18078061258",
"originationPhoneNumber": "+13655550100",
"destinationPhoneNumber": destination,
"isoCountryCode": "CA",
"mcc": "302",
Expand Down Expand Up @@ -245,6 +245,29 @@ def pinpoint_delivered_callback(reference=None, timestamp=1467074434, destinatio
return _pinpoint_callback(body)


def pinpoint_shortcode_delivered_callback(reference=None, timestamp=1467074434, destination="+1XXX5550100"):
body = {
"eventType": "TEXT_SUCCESSFUL",
"eventVersion": "1.0",
"eventTimestamp": timestamp,
"isFinal": True,
"originationPhoneNumber": "555555",
"destinationPhoneNumber": destination,
"isoCountryCode": "CA",
"messageId": reference,
"messageRequestTimestamp": timestamp,
"messageEncoding": "GSM",
"messageType": "TRANSACTIONAL",
"messageStatus": "SUCCESSFUL",
"messageStatusDescription": "Message has been accepted by phone carrier",
"totalMessageParts": 1,
"totalMessagePrice": 0.02183,
"totalCarrierFee": 0.005,
}

return _pinpoint_callback(body)


# Note that 1467074434 = 2016-06-28 00:40:34.558 UTC
def pinpoint_failed_callback(provider_response, reference=None, timestamp=1467074434, destination="+1XXX5550100"):
body = {
Expand Down
8 changes: 5 additions & 3 deletions app/celery/process_pinpoint_receipts_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ def process_pinpoint_results(self, response):
reference = receipt["messageId"]
status = receipt["messageStatus"]
provider_response = receipt["messageStatusDescription"]
isFinal = receipt["isFinal"]

notification_status = determine_pinpoint_status(status, provider_response)
notification_status = determine_pinpoint_status(status, provider_response, isFinal)

if notification_status == NOTIFICATION_SENT:
return # we don't want to update the status to sent if it's already sent
Expand Down Expand Up @@ -116,18 +117,19 @@ def process_pinpoint_results(self, response):
self.retry(queue=QueueNames.RETRY)


def determine_pinpoint_status(status: str, provider_response: str) -> Union[str, None]:
def determine_pinpoint_status(status: str, provider_response: str, isFinal: bool) -> Union[str, None]:
"""Determine the notification status based on the SMS status and provider response.
Args:
status (str): message status from AWS
provider_response (str): detailed status from the SMS provider
isFinal (bool): whether this is the last update for this send
Returns:
Union[str, None]: the notification status or None if the status is not handled
"""

if status == "DELIVERED":
if status == "DELIVERED" or status == "SUCCESSFUL" and isFinal:
return NOTIFICATION_DELIVERED
elif status == "SUCCESSFUL": # carrier has accepted the message but it hasn't gone to the phone yet
return NOTIFICATION_SENT
Expand Down
14 changes: 11 additions & 3 deletions tests/app/celery/test_process_pinpoint_receipts_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from app.aws.mocks import (
pinpoint_delivered_callback,
pinpoint_failed_callback,
pinpoint_shortcode_delivered_callback,
pinpoint_successful_callback,
)
from app.celery.process_pinpoint_receipts_tasks import process_pinpoint_results
Expand All @@ -28,7 +29,14 @@
)


def test_process_pinpoint_results_delivered(sample_template, notify_db, notify_db_session, mocker):
@pytest.mark.parametrize(
"callback, expected_response",
[
(pinpoint_delivered_callback, "Message has been accepted by phone"),
(pinpoint_shortcode_delivered_callback, "Message has been accepted by phone carrier"),
],
)
def test_process_pinpoint_results_delivered(sample_template, notify_db, notify_db_session, callback, expected_response, mocker):
mock_logger = mocker.patch("app.celery.process_pinpoint_receipts_tasks.current_app.logger.info")
mock_callback_task = mocker.patch("app.notifications.callbacks._check_and_queue_callback_task")

Expand All @@ -43,11 +51,11 @@ def test_process_pinpoint_results_delivered(sample_template, notify_db, notify_d
)
assert get_notification_by_id(notification.id).status == NOTIFICATION_SENT

process_pinpoint_results(pinpoint_delivered_callback(reference="ref"))
process_pinpoint_results(callback(reference="ref"))

assert mock_callback_task.called_once_with(get_notification_by_id(notification.id))
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"
assert get_notification_by_id(notification.id).provider_response == expected_response

mock_logger.assert_called_once_with(f"Pinpoint callback return status of delivered for notification: {notification.id}")

Expand Down

0 comments on commit 16f70b6

Please sign in to comment.