From 02c09a15248125b330ffe9fe03b5f7982d78e32b Mon Sep 17 00:00:00 2001 From: Yoandy Sanchez Date: Tue, 1 Oct 2019 09:23:02 +0200 Subject: [PATCH] Update unittest with changes of #315 --- tests/test_gateways.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/test_gateways.py b/tests/test_gateways.py index 16bb3f195..fa9e7a539 100644 --- a/tests/test_gateways.py +++ b/tests/test_gateways.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +from django.contrib.messages import get_messages from django.test import TestCase from django.test.utils import override_settings from django.urls import reverse @@ -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 try: from unittest.mock import patch, Mock @@ -81,6 +83,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',