From 5a1018ee00f9a39b2cdef28a3e625f7e272e2fab Mon Sep 17 00:00:00 2001 From: Yoandy Date: Sun, 29 Sep 2019 23:23:24 +0200 Subject: [PATCH 1/4] #315 Update documentation in Twilio gateway class --- two_factor/gateways/twilio/gateway.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/two_factor/gateways/twilio/gateway.py b/two_factor/gateways/twilio/gateway.py index e3200f46b..41aa148f5 100644 --- a/two_factor/gateways/twilio/gateway.py +++ b/two_factor/gateways/twilio/gateway.py @@ -38,6 +38,11 @@ class Twilio(object): ``TWILIO_CALLER_ID`` Should be set to a verified phone number. Twilio_ differentiates between numbers verified for making phone calls and sending text messages. + + Optionally you may set an error message to be displayed incase of error. + + ``TWILIO_ERROR_MESSAGE`` + Should be set to a string with a custom text. .. _Twilio: http://www.twilio.com/ """ From 49827b0377d9f3b6ba16b6880977e1fa8e80012b Mon Sep 17 00:00:00 2001 From: Yoandy Date: Sun, 29 Sep 2019 23:01:36 +0200 Subject: [PATCH 2/4] if TWILIO_ERROR_MESSAGE is configured in settings, then an error message is added to the messages instance with this text. --- two_factor/gateways/twilio/gateway.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/two_factor/gateways/twilio/gateway.py b/two_factor/gateways/twilio/gateway.py index 41aa148f5..e8f60cfb5 100644 --- a/two_factor/gateways/twilio/gateway.py +++ b/two_factor/gateways/twilio/gateway.py @@ -1,6 +1,7 @@ from __future__ import absolute_import from django.conf import settings +from django.contrib import messages from django.urls import reverse from django.utils import translation from django.utils.translation import pgettext, ugettext @@ -38,9 +39,9 @@ class Twilio(object): ``TWILIO_CALLER_ID`` Should be set to a verified phone number. Twilio_ differentiates between numbers verified for making phone calls and sending text messages. - + Optionally you may set an error message to be displayed incase of error. - + ``TWILIO_ERROR_MESSAGE`` Should be set to a string with a custom text. @@ -64,10 +65,18 @@ def make_call(self, device, token): def send_sms(self, device, token): body = ugettext('Your authentication token is %s') % token - self.client.messages.create( - to=device.number.as_e164, - from_=getattr(settings, 'TWILIO_CALLER_ID'), - body=body) + try: + self.client.messages.create( + to=device.number.as_e164, + from_=getattr(settings, 'TWILIO_CALLER_ID'), + body=body) + except Exception: + twilio_error_message = getattr(settings, 'TWILIO_ERROR_MESSAGE', None) + if twilio_error_message: + request = get_current_request() + messages.add_message(request, messages.ERROR, twilio_error_message) + else: + raise def validate_voice_locale(locale): From 02c09a15248125b330ffe9fe03b5f7982d78e32b Mon Sep 17 00:00:00 2001 From: Yoandy Sanchez Date: Tue, 1 Oct 2019 09:23:02 +0200 Subject: [PATCH 3/4] Update unittest with changes of #315 --- tests/test_gateways.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/test_gateways.py b/tests/test_gateways.py index 16bb3f195..fa9e7a539 100644 --- a/tests/test_gateways.py +++ b/tests/test_gateways.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +from django.contrib.messages import get_messages from django.test import TestCase from django.test.utils import override_settings from django.urls import reverse @@ -9,6 +10,7 @@ from two_factor.gateways.fake import Fake from two_factor.gateways.twilio.gateway import Twilio +from two_factor.middleware.threadlocals import get_current_request try: from unittest.mock import patch, Mock @@ -81,6 +83,39 @@ def test_gateway(self, client): from_='+456', to='+123', method='GET', timeout=15, url='http://testserver/twilio/inbound/two_factor/%s/?locale=en-gb' % code) + @override_settings( + TWILIO_ACCOUNT_SID='SID', + TWILIO_AUTH_TOKEN='TOKEN', + TWILIO_CALLER_ID='+456', + TWILIO_ERROR_MESSAGE='Error sending SMS' + ) + @patch('two_factor.gateways.twilio.gateway.Client') + def test_gateway_error_handled(self, client): + twilio = Twilio() + client.assert_called_with('SID', 'TOKEN') + + client.return_value.messages.create.side_effect = Mock(side_effect=Exception('Test')) + code = '123456' + twilio.send_sms(device=Mock(number=PhoneNumber.from_string('+123')), token=code) + request = get_current_request() + storage = get_messages(request) + assert 'Error sending SMS' in [str(message) for message in storage] + + @override_settings( + TWILIO_ACCOUNT_SID='SID', + TWILIO_AUTH_TOKEN='TOKEN', + TWILIO_CALLER_ID='+456', + ) + @patch('two_factor.gateways.twilio.gateway.Client') + def test_gateway_error_not_handled(self, client): + twilio = Twilio() + client.assert_called_with('SID', 'TOKEN') + + client.return_value.messages.create.side_effect = Mock(side_effect=Exception('Test')) + with self.assertRaises(Exception): + code = '123456' + twilio.send_sms(device=Mock(number=PhoneNumber.from_string('+123')), token=code) + @override_settings( TWILIO_ACCOUNT_SID='SID', TWILIO_AUTH_TOKEN='TOKEN', From 9186a5206572b69625107cbcc967382447085fca Mon Sep 17 00:00:00 2001 From: Yoandy Sanchez Date: Thu, 3 Oct 2019 10:41:56 +0200 Subject: [PATCH 4/4] Update documentation. Add correction in text. --- two_factor/gateways/twilio/gateway.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/two_factor/gateways/twilio/gateway.py b/two_factor/gateways/twilio/gateway.py index e8f60cfb5..2b7584ac9 100644 --- a/two_factor/gateways/twilio/gateway.py +++ b/two_factor/gateways/twilio/gateway.py @@ -40,7 +40,8 @@ class Twilio(object): Should be set to a verified phone number. Twilio_ differentiates between numbers verified for making phone calls and sending text messages. - Optionally you may set an error message to be displayed incase of error. + Optionally you may set an error message to display in case of error + sending a SMS. ``TWILIO_ERROR_MESSAGE`` Should be set to a string with a custom text.