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