Skip to content

Commit

Permalink
add fake pinpoint callback
Browse files Browse the repository at this point in the history
  • Loading branch information
sastels committed May 23, 2024
1 parent 6919e3c commit c5dae4d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
30 changes: 28 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_failed_callback,
pinpoint_successful_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,22 @@ 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]

if to.strip().endswith(perm_fail):
return pinpoint_failed_callback(
"Phone is currently unreachable/unavailable", notification_id, destination=to, timestamp=timestamp
)
elif to.strip().endswith(temp_fail):
return pinpoint_failed_callback(
"Phone carrier is currently unreachable/unavailable", notification_id, destination=to, timestamp=timestamp
)
else:
return pinpoint_successful_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_failed_callback,
pinpoint_successful_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_successful_callback, {}),
("+15149301631", pinpoint_successful_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 c5dae4d

Please sign in to comment.