예제 #1
0
def handler(event, context):
    verify_deploy_stage()
    batches = [
        SMSBatchSchema().loads(record["body"]) for record in event["Records"]
    ]
    send_sms_batches(batches)
    return {"statusCode": 200}
예제 #2
0
def handler(event: dict, context: dict) -> dict:
    verify_deploy_stage()
    batches = [
        SMSBatch(**json.loads(record["body"])) for record in event["Records"]
    ]
    send_sms_batches(batches)
    return {"statusCode": 200}
예제 #3
0
    def test_calls_twilio_for_multiple_batches(self, sleep_mock, twilio_mock,
                                               *args):
        phone_1 = "+14801234321"
        phone_2 = "+14801234321"
        batches = [
            SMSBatch(
                phone_number=phone_1,
                messages=[
                    SMS(body="hello"),
                    SMS(body="how are you"),
                    SMS(body="goodbye"),
                ],
                idempotency_key="foo",
            ),
            SMSBatch(
                phone_number=phone_2,
                messages=[SMS(body="another"),
                          SMS(body="batch")],
                idempotency_key="foo",
            ),
        ]
        send_sms_batches(batches)
        self.assertEqual(twilio_mock.send_message.call_count, 5)

        call_args = self._get_twilio_call_args(twilio_mock)
        for i, args in enumerate(call_args[:3]):
            self.assertEqual(args[0], phone_1)
            self.assertEqual(args[1], batches[0].messages[i].body)

        for i, args in enumerate(call_args[3:]):
            self.assertEqual(args[0], phone_2)
            self.assertEqual(args[1], batches[1].messages[i].body)
예제 #4
0
 def test_do_not_sleep_on_single_message(self, sleep_mock, twilio_mock,
                                         *args):
     batches = [
         SMSBatch(phone_number="+15551234321",
                  messages=[SMS(body="hello")],
                  idempotency_key="foo")
     ]
     send_sms_batches(batches)
     self.assertEqual(sleep_mock.call_count, 0)
예제 #5
0
 def test_sleeps_longer_after_media_url(self, sleep_mock, twilio_mock,
                                        *args):
     batches = [
         SMSBatch(
             phone_number="+14801234321",
             messages=[
                 SMS(body="hello", media_url="www.cat.gif"),
                 SMS(body="how are you"),
             ],
             idempotency_key="foo",
         )
     ]
     send_sms_batches(batches)
     sleep_mock.assert_called_once_with(DELAY_SECONDS_AFTER_MEDIA)
예제 #6
0
 def test_sleep_between_set_messages(self, sleep_mock, twilio_mock, *args):
     batches = [
         SMSBatch(
             phone_number="+14801234321",
             messages=[
                 SMS(body="hello"),
                 SMS(body="how are you"),
                 SMS(body="goodbye"),
             ],
             idempotency_key="foo",
         )
     ]
     send_sms_batches(batches)
     self.assertEqual(sleep_mock.call_count, 2)
예제 #7
0
 def test_doesnt_send_to_fake_phones(self, sleep_mock, twilio_mock, *args):
     phone = "+15559990000"
     batches = [
         SMSBatch(
             phone_number=phone,
             messages=[
                 SMS(body="hello"),
                 SMS(body="how are you"),
                 SMS(body="goodbye"),
             ],
             idempotency_key="foo",
         )
     ]
     send_sms_batches(batches)
     self.assertEqual(twilio_mock.send_message.call_count, 0)
예제 #8
0
    def test_uses_messaging_service_sid(self, sleep_mock, twilio_mock, *args):
        phone = "+14801234321"
        batches = [
            SMSBatch(
                phone_number=phone,
                messages=[
                    SMS(body="hello"),
                    SMS(body="how are you"),
                    SMS(body="goodbye"),
                ],
                idempotency_key="foo",
                messaging_service_sid="foo-bar-baz",
            )
        ]
        send_sms_batches(batches)
        self.assertEqual(twilio_mock.send_message.call_count, 3)

        call_args = self._get_twilio_call_args(twilio_mock)
        for i, args in enumerate(call_args):
            self.assertEqual(args[3], "foo-bar-baz")
예제 #9
0
    def test_calls_twilio_for_one_batch(self, sleep_mock, twilio_mock, *args):
        phone = "+14801234321"
        batches = [
            SMSBatch(
                phone_number=phone,
                messages=[
                    SMS(body="hello"),
                    SMS(body="how are you"),
                    SMS(body="goodbye"),
                ],
                idempotency_key="foo",
            )
        ]
        send_sms_batches(batches)
        self.assertEqual(twilio_mock.send_message.call_count, 3)

        call_args = self._get_twilio_call_args(twilio_mock)
        for i, args in enumerate(call_args):
            self.assertEqual(args[0], phone)
            self.assertEqual(args[1], batches[0].messages[i].body)