|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | - |
| 2 | +from django.contrib.messages import get_messages |
3 | 3 | from django.test import TestCase |
4 | 4 | from django.test.utils import override_settings |
5 | 5 | from django.urls import reverse |
6 | 6 | from django.utils import translation |
7 | 7 | from django.utils.six.moves.urllib.parse import urlencode |
8 | 8 | from phonenumber_field.phonenumber import PhoneNumber |
9 | | - |
10 | 9 | from two_factor.gateways.fake import Fake |
11 | 10 | from two_factor.gateways.twilio.gateway import Twilio |
| 11 | +from two_factor.middleware.threadlocals import get_current_request |
12 | 12 |
|
13 | 13 | try: |
14 | 14 | from unittest.mock import patch, Mock |
@@ -81,6 +81,39 @@ def test_gateway(self, client): |
81 | 81 | from_='+456', to='+123', method='GET', timeout=15, |
82 | 82 | url='http://testserver/twilio/inbound/two_factor/%s/?locale=en-gb' % code) |
83 | 83 |
|
| 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 | + |
84 | 117 | @override_settings( |
85 | 118 | TWILIO_ACCOUNT_SID='SID', |
86 | 119 | TWILIO_AUTH_TOKEN='TOKEN', |
|
0 commit comments