2016-06-01 15:59:44 +01:00
import pytest
import requests_mock
2016-09-22 17:18:05 +01:00
from requests import HTTPError
2017-04-05 15:31:37 +01:00
from requests.exceptions import ConnectTimeout , ReadTimeout
2016-06-01 15:59:44 +01:00
2016-09-22 17:18:05 +01:00
from app import mmg_client
from app.clients.sms import SmsClientResponseException
2017-04-05 15:31:37 +01:00
from app.clients.sms.mmg import get_mmg_responses , MMGClientResponseException
2016-04-06 14:31:33 +01:00
def test_should_return_correct_details_for_delivery ():
2018-02-07 17:49:15 +00:00
get_mmg_responses ( '3' ) == 'delivered'
2016-04-06 14:31:33 +01:00
2018-02-07 17:49:15 +00:00
def test_should_return_correct_details_for_temporary_failure ():
get_mmg_responses ( '4' ) == 'temporary-failure'
2016-04-06 14:31:33 +01:00
2018-02-07 17:49:15 +00:00
@pytest.mark.parametrize ( 'status' , [ '5' , '2' ])
def test_should_return_correct_details_for_bounced ( status ):
get_mmg_responses ( status ) == 'permanent-failure'
def test_should_be_raise_if_unrecognised_status_code ():
with pytest . raises ( KeyError ) as e :
get_mmg_responses ( '99' )
assert '99' in str ( e . value )
2016-06-01 15:59:44 +01:00
2016-07-20 10:40:50 +01:00
def test_send_sms_successful_returns_mmg_response ( notify_api , mocker ):
2016-06-01 15:59:44 +01:00
to = content = reference = 'foo'
response_dict = { 'Reference' : 12345678 }
with requests_mock . Mocker () as request_mock :
2019-04-12 12:03:58 +01:00
request_mock . post ( 'https://example.com/mmg' , json = response_dict , status_code = 200 )
2016-07-20 10:40:50 +01:00
response = mmg_client . send_sms ( to , content , reference )
2016-06-01 15:59:44 +01:00
response_json = response . json ()
assert response . status_code == 200
assert response_json [ 'Reference' ] == 12345678
2016-07-20 10:40:50 +01:00
def test_send_sms_calls_mmg_correctly ( notify_api , mocker ):
2016-06-01 15:59:44 +01:00
to = '+447234567890'
content = 'my message'
reference = 'my reference'
response_dict = { 'Reference' : 12345678 }
with requests_mock . Mocker () as request_mock :
2019-04-12 12:03:58 +01:00
request_mock . post ( 'https://example.com/mmg' , json = response_dict , status_code = 200 )
2016-07-20 10:40:50 +01:00
mmg_client . send_sms ( to , content , reference )
2016-06-01 15:59:44 +01:00
assert request_mock . call_count == 1
2019-04-12 12:03:58 +01:00
assert request_mock . request_history [ 0 ] . url == 'https://example.com/mmg'
2016-06-01 15:59:44 +01:00
assert request_mock . request_history [ 0 ] . method == 'POST'
request_args = request_mock . request_history [ 0 ] . json ()
assert request_args [ 'reqType' ] == 'BULK'
assert request_args [ 'MSISDN' ] == to
assert request_args [ 'msg' ] == content
2016-09-07 09:35:31 +01:00
assert request_args [ 'sender' ] == 'testing'
2016-06-01 15:59:44 +01:00
assert request_args [ 'cid' ] == reference
assert request_args [ 'multi' ] is True
2016-07-20 10:40:50 +01:00
def test_send_sms_raises_if_mmg_rejects ( notify_api , mocker ):
2016-06-01 15:59:44 +01:00
to = content = reference = 'foo'
response_dict = {
'Error' : 206 ,
'Description' : 'Some kind of error'
}
2016-09-22 17:18:05 +01:00
with pytest . raises ( SmsClientResponseException ) as exc , requests_mock . Mocker () as request_mock :
2019-04-12 12:03:58 +01:00
request_mock . post ( 'https://example.com/mmg' , json = response_dict , status_code = 400 )
2016-07-20 10:40:50 +01:00
mmg_client . send_sms ( to , content , reference )
2016-06-01 15:59:44 +01:00
2016-09-22 17:18:05 +01:00
assert exc . value . status_code == 400
assert '"Error": 206' in exc . value . text
assert '"Description": "Some kind of error"' in exc . value . text
assert type ( exc . value . exception ) == HTTPError
2016-07-01 14:06:32 +01:00
2016-07-20 10:40:50 +01:00
def test_send_sms_override_configured_shortcode_with_sender ( notify_api , mocker ):
2016-07-01 14:06:32 +01:00
to = '+447234567890'
content = 'my message'
reference = 'my reference'
response_dict = { 'Reference' : 12345678 }
sender = 'fromservice'
with requests_mock . Mocker () as request_mock :
2019-04-12 12:03:58 +01:00
request_mock . post ( 'https://example.com/mmg' , json = response_dict , status_code = 200 )
2016-07-20 10:40:50 +01:00
mmg_client . send_sms ( to , content , reference , sender = sender )
2016-07-01 14:06:32 +01:00
request_args = request_mock . request_history [ 0 ] . json ()
assert request_args [ 'sender' ] == 'fromservice'
2016-09-22 17:18:05 +01:00
def test_send_sms_raises_if_mmg_fails_to_return_json ( notify_api , mocker ):
to = content = reference = 'foo'
response_dict = 'NOT AT ALL VALID JSON {"key" : "value"}}'
with pytest . raises ( SmsClientResponseException ) as exc , requests_mock . Mocker () as request_mock :
2019-04-12 12:03:58 +01:00
request_mock . post ( 'https://example.com/mmg' , text = response_dict , status_code = 200 )
2016-09-22 17:18:05 +01:00
mmg_client . send_sms ( to , content , reference )
assert 'app.clients.sms.mmg.MMGClientResponseException: Code 200 text NOT AT ALL VALID JSON {"key" : "value"}} exception Expecting value: line 1 column 1 (char 0)' in str ( exc ) # noqa
assert exc . value . status_code == 200
assert exc . value . text == 'NOT AT ALL VALID JSON {"key" : "value"}}'
2017-04-05 15:31:37 +01:00
2017-04-05 16:30:53 +01:00
def test_send_sms_raises_if_mmg_rejects_with_connect_timeout ( rmock ):
2017-04-05 15:31:37 +01:00
to = content = reference = 'foo'
with pytest . raises ( MMGClientResponseException ) as exc :
2019-04-12 12:03:58 +01:00
rmock . register_uri ( 'POST' , 'https://example.com/mmg' , exc = ConnectTimeout )
2017-04-05 15:31:37 +01:00
mmg_client . send_sms ( to , content , reference )
assert exc . value . status_code == 504
assert exc . value . text == 'Gateway Time-out'
2017-04-05 16:30:53 +01:00
def test_send_sms_raises_if_mmg_rejects_with_read_timeout ( rmock ):
2017-04-05 15:31:37 +01:00
to = content = reference = 'foo'
with pytest . raises ( MMGClientResponseException ) as exc :
2019-04-12 12:03:58 +01:00
rmock . register_uri ( 'POST' , 'https://example.com/mmg' , exc = ReadTimeout )
2017-04-05 15:31:37 +01:00
mmg_client . send_sms ( to , content , reference )
assert exc . value . status_code == 504
assert exc . value . text == 'Gateway Time-out'