Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add message if send_sms fail with an Exception #316

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tests/test_gateways.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest.mock import Mock, patch
from urllib.parse import urlencode

from django.contrib.messages import get_messages
from django.test import TestCase
from django.test.utils import override_settings
from django.urls import reverse
Expand All @@ -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


class TwilioGatewayTest(TestCase):
Expand Down Expand Up @@ -76,6 +78,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',
Expand Down
25 changes: 20 additions & 5 deletions two_factor/gateways/twilio/gateway.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from urllib.parse import urlencode

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 gettext, pgettext
Expand Down Expand Up @@ -33,6 +34,12 @@ 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 display in case of error
sending a SMS.

``TWILIO_ERROR_MESSAGE``
Should be set to a string with a custom text.

.. _Twilio: http://www.twilio.com/
"""
def __init__(self):
Expand All @@ -52,11 +59,19 @@ def make_call(self, device, token):
url=uri, method='GET', timeout=15)

def send_sms(self, device, token):
body = gettext('Your authentication token is %s') % token
self.client.messages.create(
to=device.number.as_e164,
from_=getattr(settings, 'TWILIO_CALLER_ID'),
body=body)
body = ugettext('Your authentication token is %s') % token
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):
Expand Down