Skip to content

Commit

Permalink
Fix fake callback (#2178)
Browse files Browse the repository at this point in the history
* add fake pinpoint callback

* use correct callback generator

* refactor fake pinpoint callback code

---------

Co-authored-by: Jimmy Royer <[email protected]>
  • Loading branch information
sastels and jimleroyer authored Jun 4, 2024
1 parent af825ef commit 72eb7f7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
33 changes: 31 additions & 2 deletions app/celery/research_mode_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from app import create_uuid, notify_celery
from app.aws.mocks import (
pinpoint_delivered_callback,
pinpoint_failed_callback,
ses_hard_bounce_callback,
ses_notification_callback,
ses_soft_bounce_callback,
Expand All @@ -14,9 +16,11 @@
sns_success_callback,
)
from app.aws.s3 import file_exists
from app.celery.process_pinpoint_receipts_tasks import process_pinpoint_results
from app.celery.process_ses_receipts_tasks import process_ses_results
from app.celery.process_sns_receipts_tasks import process_sns_results
from app.config import QueueNames
from app.models import PINPOINT_PROVIDER, SNS_PROVIDER

temp_fail = "+15149301633"
perm_fail = "+15149301632"
Expand All @@ -29,8 +33,14 @@

def send_sms_response(provider, to, reference=None):
reference = reference or str(create_uuid())
body = aws_sns_callback(reference, to)
process_sns_results.apply_async([body], queue=QueueNames.RESEARCH_MODE)
if provider == SNS_PROVIDER:
body = aws_sns_callback(reference, to)
process_sns_results.apply_async([body], queue=QueueNames.RESEARCH_MODE)
elif provider == PINPOINT_PROVIDER:
body = aws_pinpoint_callback(reference, to)
process_pinpoint_results.apply_async([body], queue=QueueNames.RESEARCH_MODE)
else:
raise ValueError("Provider {} not supported".format(provider))
return reference


Expand Down Expand Up @@ -64,6 +74,25 @@ def aws_sns_callback(notification_id, to):
return sns_success_callback(notification_id, destination=to, timestamp=timestamp)


def aws_pinpoint_callback(notification_id, to):
now = datetime.now()
timestamp = now.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
using_test_perm_fail_number = to.strip().endswith(perm_fail)
using_test_temp_fail_number = to.strip().endswith(temp_fail)

if using_test_perm_fail_number or using_test_temp_fail_number:
return pinpoint_failed_callback(
"Phone is currently unreachable/unavailable"
if using_test_perm_fail_number
else "Phone carrier is currently unreachable/unavailable",
notification_id,
destination=to,
timestamp=timestamp,
)
else:
return pinpoint_delivered_callback(notification_id, destination=to, timestamp=timestamp)


@notify_celery.task(
bind=True,
name="create-fake-letter-response-file",
Expand Down
26 changes: 26 additions & 0 deletions tests/app/celery/test_research_mode_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from freezegun import freeze_time

from app.aws.mocks import (
pinpoint_delivered_callback,
pinpoint_failed_callback,
ses_notification_callback,
sns_failed_callback,
sns_success_callback,
Expand Down Expand Up @@ -50,6 +52,30 @@ def test_make_sns_success_callback(notify_api, mocker, phone_number, sns_callbac
assert message_celery == sns_callback(**sns_callback_args)


@pytest.mark.parametrize(
"phone_number, pinpoint_callback, pinpoint_callback_args",
[
("+15149301630", pinpoint_delivered_callback, {}),
("+15149301631", pinpoint_delivered_callback, {}),
("+15149301632", pinpoint_failed_callback, {"provider_response": "Phone is currently unreachable/unavailable"}),
("+15149301633", pinpoint_failed_callback, {"provider_response": "Phone carrier is currently unreachable/unavailable"}),
],
)
@freeze_time("2018-01-25 14:00:30")
def test_make_pinpoint_success_callback(notify_api, mocker, phone_number, pinpoint_callback, pinpoint_callback_args):
mock_task = mocker.patch("app.celery.research_mode_tasks.process_pinpoint_results")
some_ref = str(uuid.uuid4())
now = datetime.now()
timestamp = now.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]

send_sms_response("pinpoint", phone_number, some_ref)

mock_task.apply_async.assert_called_once_with(ANY, queue=QueueNames.RESEARCH_MODE)
message_celery = mock_task.apply_async.call_args[0][0][0]
pinpoint_callback_args.update({"reference": some_ref, "destination": phone_number, "timestamp": timestamp})
assert message_celery == pinpoint_callback(**pinpoint_callback_args)


def test_make_ses_callback(notify_api, mocker):
mock_task = mocker.patch("app.celery.research_mode_tasks.process_ses_results")
some_ref = str(uuid.uuid4())
Expand Down

0 comments on commit 72eb7f7

Please sign in to comment.