Skip to content

Commit

Permalink
add pinpoint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sastels committed May 13, 2024
1 parent 5717361 commit 727070f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
7 changes: 3 additions & 4 deletions app/clients/sms/aws_pinpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import phonenumbers

from app.clients.sms import SmsClient
from app.config import Config


class AwsPinpointClient(SmsClient):
Expand All @@ -26,10 +25,10 @@ def send_sms(self, to, content, reference, multi=True, sender=None, template_id=
messageType = "TRANSACTIONAL"
matched = False

if template_id is not None and str(template_id) in Config.AWS_PINPOINT_SC_TEMPLATE_IDS:
pool_id = Config.AWS_PINPOINT_SC_POOL_ID
if template_id is not None and str(template_id) in self.current_app.config["AWS_PINPOINT_SC_TEMPLATE_IDS"]:
pool_id = self.current_app.config["AWS_PINPOINT_SC_POOL_ID"]
else:
pool_id = Config.AWS_PINPOINT_DEFAULT_POOL_ID
pool_id = self.current_app.config["AWS_PINPOINT_DEFAULT_POOL_ID"]

for match in phonenumbers.PhoneNumberMatcher(to, "US"):
matched = True
Expand Down
76 changes: 76 additions & 0 deletions tests/app/clients/test_aws_pinpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import pytest

from app import aws_pinpoint_client
from tests.conftest import set_config_values


def test_send_sms_sends_to_default_pool(notify_api, mocker, sample_template):
boto_mock = mocker.patch.object(aws_pinpoint_client, "_client", create=True)
mocker.patch.object(aws_pinpoint_client, "statsd_client", create=True)
to = "6135555555"
content = "foo"
reference = "ref"

with set_config_values(
notify_api,
{
"AWS_PINPOINT_SC_POOL_ID": "sc_pool_id",
"AWS_PINPOINT_DEFAULT_POOL_ID": "default_pool_id",
"AWS_PINPOINT_CONFIGURATION_SET_NAME": "config_set_name",
"AWS_PINPOINT_SC_TEMPLATE_IDS": [],
},
):
aws_pinpoint_client.send_sms(to, content, reference=reference, template_id=sample_template.id)

boto_mock.send_text_message.assert_called_once_with(
DestinationPhoneNumber="+16135555555",
OriginationIdentity="default_pool_id",
MessageBody=content,
MessageType="TRANSACTIONAL",
ConfigurationSetName="config_set_name",
)


def test_send_sms_sends_to_shortcode_pool(notify_api, mocker, sample_template):
boto_mock = mocker.patch.object(aws_pinpoint_client, "_client", create=True)
mocker.patch.object(aws_pinpoint_client, "statsd_client", create=True)
to = "6135555555"
content = "foo"
reference = "ref"

with set_config_values(
notify_api,
{
"AWS_PINPOINT_SC_POOL_ID": "sc_pool_id",
"AWS_PINPOINT_DEFAULT_POOL_ID": "default_pool_id",
"AWS_PINPOINT_CONFIGURATION_SET_NAME": "config_set_name",
"AWS_PINPOINT_SC_TEMPLATE_IDS": [str(sample_template.id)],
},
):
with notify_api.app_context():
aws_pinpoint_client.send_sms(to, content, reference=reference, template_id=sample_template.id)

boto_mock.send_text_message.assert_called_once_with(
DestinationPhoneNumber="+16135555555",
OriginationIdentity="sc_pool_id",
MessageBody=content,
MessageType="TRANSACTIONAL",
ConfigurationSetName="config_set_name",
)


def test_send_sms_returns_raises_error_if_there_is_no_valid_number_is_found(notify_api, mocker):
mocker.patch.object(aws_pinpoint_client, "_client", create=True)
mocker.patch.object(aws_pinpoint_client, "statsd_client", create=True)

to = ""
content = reference = "foo"

with pytest.raises(ValueError) as excinfo:
aws_pinpoint_client.send_sms(to, content, reference)

assert "No valid numbers found for SMS delivery" in str(excinfo.value)


# TODO: make sure fixed long code sends and sends to us numbers go through old SNS flow.
# That's not tested here.

0 comments on commit 727070f

Please sign in to comment.