Skip to content

Commit

Permalink
make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
sastels committed Apr 12, 2024
1 parent 1a6ba9e commit 6e3d339
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
14 changes: 7 additions & 7 deletions app/aws/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,39 +206,38 @@ 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)


# 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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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": {},
}
}
14 changes: 9 additions & 5 deletions app/celery/process_pinpoint_receipts_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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):

Check notice

Code scanning / CodeQL

Explicit returns mixed with implicit (fall through) returns Note

Mixing implicit and explicit returns may indicate an error as implicit returns always return None.
try:
receipt = json.loads(response)
receipt = json.loads(response["Message"])
reference = receipt["messageId"]
status = receipt["messageStatus"]
provider_response = receipt["messageStatusDescription"]
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions tests/app/celery/test_process_pinpoint_receipts_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down Expand Up @@ -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",
)
)

Expand Down Expand Up @@ -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")

0 comments on commit 6e3d339

Please sign in to comment.