Skip to content

Commit 9eaea76

Browse files
committed
Update unittest with changes of #315
1 parent 49827b0 commit 9eaea76

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

tests/test_gateways.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# -*- coding: utf-8 -*-
2-
2+
from django.contrib.messages import get_messages
33
from django.test import TestCase
44
from django.test.utils import override_settings
55
from django.urls import reverse
66
from django.utils import translation
77
from django.utils.six.moves.urllib.parse import urlencode
88
from phonenumber_field.phonenumber import PhoneNumber
9-
109
from two_factor.gateways.fake import Fake
1110
from two_factor.gateways.twilio.gateway import Twilio
11+
from two_factor.middleware.threadlocals import get_current_request
1212

1313
try:
1414
from unittest.mock import patch, Mock
@@ -81,6 +81,39 @@ def test_gateway(self, client):
8181
from_='+456', to='+123', method='GET', timeout=15,
8282
url='http://testserver/twilio/inbound/two_factor/%s/?locale=en-gb' % code)
8383

84+
@override_settings(
85+
TWILIO_ACCOUNT_SID='SID',
86+
TWILIO_AUTH_TOKEN='TOKEN',
87+
TWILIO_CALLER_ID='+456',
88+
TWILIO_ERROR_MESSAGE='Error sending SMS'
89+
)
90+
@patch('two_factor.gateways.twilio.gateway.Client')
91+
def test_gateway_error_handled(self, client):
92+
twilio = Twilio()
93+
client.assert_called_with('SID', 'TOKEN')
94+
95+
client.return_value.messages.create.side_effect = Mock(side_effect=Exception('Test'))
96+
code = '123456'
97+
twilio.send_sms(device=Mock(number=PhoneNumber.from_string('+123')), token=code)
98+
request = get_current_request()
99+
storage = get_messages(request)
100+
assert 'Error sending SMS' in [str(message) for message in storage]
101+
102+
@override_settings(
103+
TWILIO_ACCOUNT_SID='SID',
104+
TWILIO_AUTH_TOKEN='TOKEN',
105+
TWILIO_CALLER_ID='+456',
106+
)
107+
@patch('two_factor.gateways.twilio.gateway.Client')
108+
def test_gateway_error_not_handled(self, client):
109+
twilio = Twilio()
110+
client.assert_called_with('SID', 'TOKEN')
111+
112+
client.return_value.messages.create.side_effect = Mock(side_effect=Exception('Test'))
113+
with self.assertRaises(Exception):
114+
code = '123456'
115+
twilio.send_sms(device=Mock(number=PhoneNumber.from_string('+123')), token=code)
116+
84117
@override_settings(
85118
TWILIO_ACCOUNT_SID='SID',
86119
TWILIO_AUTH_TOKEN='TOKEN',

0 commit comments

Comments
 (0)