Skip to content

Commit

Permalink
Update unittest with changes of jazzband#315
Browse files Browse the repository at this point in the history
  • Loading branch information
yoandyshyno committed Oct 1, 2019
1 parent 49827b0 commit 9eaea76
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions tests/test_gateways.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# -*- 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
from django.utils import translation
from django.utils.six.moves.urllib.parse import urlencode
from phonenumber_field.phonenumber import PhoneNumber

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
Expand Down Expand Up @@ -81,6 +81,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',
Expand Down

0 comments on commit 9eaea76

Please sign in to comment.