def test_gateway(self, client):
        twilio = Twilio()
        client.assert_called_with('SID', 'TOKEN')

        for code in ['654321', '054321', '87654321', '07654321']:
            twilio.make_call(device=Mock(number='+123'), token=code)
            client.return_value.calls.create.assert_called_with(
                from_='+456', to='+123', method='GET', if_machine='Hangup', timeout=15,
                url='http://testserver/twilio/inbound/two_factor/%s/?locale=en-us' % code)

            twilio.send_sms(device=Mock(number='+123'), token=code)
            client.return_value.sms.messages.create.assert_called_with(
                to='+123', body='Your authentication token is %s' % code, from_='+456')

            client.return_value.calls.create.reset_mock()
            with translation.override('en-gb'):
                twilio.make_call(device=Mock(number='+123'), token=code)
                client.return_value.calls.create.assert_called_with(
                    from_='+456', to='+123', method='GET', if_machine='Hangup', timeout=15,
                    url='http://testserver/twilio/inbound/two_factor/%s/?locale=en-gb' % code)

            client.return_value.calls.create.reset_mock()
            with translation.override('en-gb'):
                twilio.make_call(device=Mock(number='+123'), token=code)
                client.return_value.calls.create.assert_called_with(
                    from_='+456', to='+123', method='GET', if_machine='Hangup', timeout=15,
                    url='http://testserver/twilio/inbound/two_factor/%s/?locale=en-gb' % code)
    def test_gateway(self, client):
        twilio = Twilio()
        client.assert_called_with('SID', 'TOKEN')

        for code in ['654321', '054321', '87654321', '07654321']:
            twilio.make_call(device=Mock(number=PhoneNumber.from_string('+123')), token=code)
            client.return_value.calls.create.assert_called_with(
                from_='+456', to='+123', method='GET', timeout=15,
                url='http://testserver/twilio/inbound/two_factor/%s/?locale=en-us' % code)

            twilio.send_sms(device=Mock(number=PhoneNumber.from_string('+123')), token=code)
            client.return_value.messages.create.assert_called_with(
                to='+123', body='Your authentication token is %s' % code, from_='+456')

            client.return_value.calls.create.reset_mock()
            with translation.override('en-gb'):
                twilio.make_call(device=Mock(number=PhoneNumber.from_string('+123')), token=code)
                client.return_value.calls.create.assert_called_with(
                    from_='+456', to='+123', method='GET', timeout=15,
                    url='http://testserver/twilio/inbound/two_factor/%s/?locale=en-gb' % code)

            client.return_value.calls.create.reset_mock()
            with translation.override('en-gb'):
                twilio.make_call(device=Mock(number=PhoneNumber.from_string('+123')), token=code)
                client.return_value.calls.create.assert_called_with(
                    from_='+456', to='+123', method='GET', timeout=15,
                    url='http://testserver/twilio/inbound/two_factor/%s/?locale=en-gb' % code)
Exemple #3
0
    def test_gateway(self, client):
        twilio = Twilio()
        client.assert_called_with('SID', 'TOKEN')

        for code in ['654321', '054321', '87654321', '07654321']:
            twilio.make_call(
                device=Mock(number=PhoneNumber.from_string('+123')),
                token=code)
            client.return_value.calls.create.assert_called_with(
                from_='+456',
                to='+123',
                method='GET',
                timeout=15,
                url=
                'http://testserver/twilio/inbound/two_factor/%s/?locale=en-us'
                % code)

            twilio.send_sms(
                device=Mock(number=PhoneNumber.from_string('+123')),
                token=code)

            client.return_value.messages.create.assert_called_with(
                to='+123',
                body=render_to_string('two_factor/twilio/sms_message.html',
                                      {'token': code}),
                from_='+456')

            client.return_value.calls.create.reset_mock()
            with translation.override('en-gb'):
                twilio.make_call(
                    device=Mock(number=PhoneNumber.from_string('+123')),
                    token=code)
                client.return_value.calls.create.assert_called_with(
                    from_='+456',
                    to='+123',
                    method='GET',
                    timeout=15,
                    url=
                    'http://testserver/twilio/inbound/two_factor/%s/?locale=en-gb'
                    % code)

            client.return_value.calls.create.reset_mock()
            with translation.override('en-gb'):
                twilio.make_call(
                    device=Mock(number=PhoneNumber.from_string('+123')),
                    token=code)
                client.return_value.calls.create.assert_called_with(
                    from_='+456',
                    to='+123',
                    method='GET',
                    timeout=15,
                    url=
                    'http://testserver/twilio/inbound/two_factor/%s/?locale=en-gb'
                    % code)
    def test_gateway(self, client):
        twilio = Twilio()
        client.assert_called_with('SID', 'TOKEN')

        twilio.make_call(device=Mock(number='+123'), token='654321')
        client.return_value.calls.create.assert_called_with(
            from_='+456',
            to='+123',
            method='GET',
            url=
            'http://testserver/twilio/inbound/two_factor/654321/?locale=en-us')

        twilio.send_sms(device=Mock(number='+123'), token='654321')
        client.return_value.sms.messages.create.assert_called_with(
            to='+123',
            body='Your authentication token is 654321',
            from_='+456')

        client.return_value.calls.create.reset_mock()
        with translation.override('en-gb'):
            twilio.make_call(device=Mock(number='+123'), token='654321')
            client.return_value.calls.create.assert_called_with(
                from_='+456',
                to='+123',
                method='GET',
                url=
                'http://testserver/twilio/inbound/two_factor/654321/?locale=en-gb'
            )
    def test_invalid_twilio_language(self, client):
        # This test assumes an invalid twilio voice language being present in
        # the Arabic translation. Might need to create a faux translation when
        # the translation is fixed.

        url = reverse('two_factor:twilio_call_app', args=['123456'])
        with self.assertRaises(NotImplementedError):
            self.client.get('%s?%s' % (url, urlencode({'locale': 'ar'})))

        # make_call doesn't use the voice_language, but it should raise early
        # to ease debugging.
        with self.assertRaises(NotImplementedError):
            twilio = Twilio()
            with translation.override('ar'):
                twilio.make_call(device=Mock(number='+123'), token='654321')
    def test_invalid_twilio_language(self, client):
        # This test assumes an invalid twilio voice language being present in
        # the Arabic translation. Might need to create a faux translation when
        # the translation is fixed.

        url = reverse('two_factor:twilio_call_app', args=['123456'])
        with self.assertRaises(NotImplementedError):
            self.client.get('%s?%s' % (url, urlencode({'locale': 'ar'})))

        # make_call doesn't use the voice_language, but it should raise early
        # to ease debugging.
        with self.assertRaises(NotImplementedError):
            twilio = Twilio()
            with translation.override('ar'):
                twilio.make_call(device=Mock(number='+123'), token='654321')
    def test_gateway(self, client):
        twilio = Twilio()
        client.assert_called_with("SID", "TOKEN")

        twilio.make_call(device=Mock(number="+123"), token="654321")
        client.return_value.calls.create.assert_called_with(
            from_="+456",
            to="+123",
            method="GET",
            url="http://testserver/twilio/inbound/two_factor/654321/?locale=en-us",
        )

        twilio.send_sms(device=Mock(number="+123"), token="654321")
        client.return_value.sms.messages.create.assert_called_with(
            to="+123", body="Your authentication token is 654321", from_="+456"
        )

        client.return_value.calls.create.reset_mock()
        with translation.override("en-gb"):
            twilio.make_call(device=Mock(number="+123"), token="654321")
            client.return_value.calls.create.assert_called_with(
                from_="+456",
                to="+123",
                method="GET",
                url="http://testserver/twilio/inbound/two_factor/654321/?locale=en-gb",
            )
    def test_gateway(self, client):
        twilio = Twilio()
        client.assert_called_with("SID", "TOKEN")

        for code in ["654321", "054321", "87654321", "07654321"]:
            twilio.make_call(device=Mock(number="+123"), token=code)
            client.return_value.calls.create.assert_called_with(
                from_="+456",
                to="+123",
                method="GET",
                if_machine="Hangup",
                timeout=15,
                url="http://testserver/twilio/inbound/two_factor/%s/?locale=en-us" % code,
            )

            twilio.send_sms(device=Mock(number="+123"), token=code)
            client.return_value.sms.messages.create.assert_called_with(
                to="+123", body="Your authentication token is %s" % code, from_="+456"
            )

            client.return_value.calls.create.reset_mock()
            with translation.override("en-gb"):
                twilio.make_call(device=Mock(number="+123"), token=code)
                client.return_value.calls.create.assert_called_with(
                    from_="+456",
                    to="+123",
                    method="GET",
                    if_machine="Hangup",
                    timeout=15,
                    url="http://testserver/twilio/inbound/two_factor/%s/?locale=en-gb" % code,
                )

            client.return_value.calls.create.reset_mock()
            with translation.override("en-gb"):
                twilio.make_call(device=Mock(number="+123"), token=code)
                client.return_value.calls.create.assert_called_with(
                    from_="+456",
                    to="+123",
                    method="GET",
                    if_machine="Hangup",
                    timeout=15,
                    url="http://testserver/twilio/inbound/two_factor/%s/?locale=en-gb" % code,
                )
    def test_gateway(self, client):
        twilio = Twilio()
        client.assert_called_with('SID', 'TOKEN')

        twilio.make_call(device=Mock(number='+123'), token='654321')
        client.return_value.calls.create.assert_called_with(
            from_='+456', to='+123', method='GET', if_machine='Hangup', timeout=15,
            url='http://testserver/twilio/inbound/two_factor/654321/?locale=en-us')

        twilio.send_sms(device=Mock(number='+123'), token='654321')
        client.return_value.sms.messages.create.assert_called_with(
            to='+123', body='Your authentication token is 654321', from_='+456')

        client.return_value.calls.create.reset_mock()
        with translation.override('en-gb'):
            twilio.make_call(device=Mock(number='+123'), token='654321')
            client.return_value.calls.create.assert_called_with(
                from_='+456', to='+123', method='GET', if_machine='Hangup', timeout=15,
                url='http://testserver/twilio/inbound/two_factor/654321/?locale=en-gb')