Skip to content

Commit 02c09a1

Browse files
committed
Update unittest with changes of #315
1 parent 49827b0 commit 02c09a1

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tests/test_gateways.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
from django.contrib.messages import get_messages
34
from django.test import TestCase
45
from django.test.utils import override_settings
56
from django.urls import reverse
@@ -9,6 +10,7 @@
910

1011
from two_factor.gateways.fake import Fake
1112
from two_factor.gateways.twilio.gateway import Twilio
13+
from two_factor.middleware.threadlocals import get_current_request
1214

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

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

0 commit comments

Comments
 (0)