2016-09-22 17:18:05 +01:00
|
|
|
from requests import HTTPError
|
2016-06-01 15:59:44 +01:00
|
|
|
from urllib.parse import parse_qs
|
|
|
|
|
|
2016-03-21 09:20:38 +00:00
|
|
|
import pytest
|
2016-06-01 15:59:44 +01:00
|
|
|
import requests_mock
|
2016-03-21 09:20:38 +00:00
|
|
|
|
2016-09-22 17:18:05 +01:00
|
|
|
from app.clients.sms.firetext import get_firetext_responses, SmsClientResponseException
|
2016-03-21 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_return_correct_details_for_delivery():
|
2016-04-06 14:31:33 +01:00
|
|
|
response_dict = get_firetext_responses('0')
|
|
|
|
|
assert response_dict['message'] == 'Delivered'
|
|
|
|
|
assert response_dict['notification_status'] == 'delivered'
|
|
|
|
|
assert response_dict['notification_statistics_status'] == 'delivered'
|
|
|
|
|
assert response_dict['success']
|
2016-03-21 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_return_correct_details_for_bounced():
|
2016-04-06 14:31:33 +01:00
|
|
|
response_dict = get_firetext_responses('1')
|
|
|
|
|
assert response_dict['message'] == 'Declined'
|
2016-05-24 15:32:48 +01:00
|
|
|
assert response_dict['notification_status'] == 'permanent-failure'
|
2016-04-06 14:31:33 +01:00
|
|
|
assert response_dict['notification_statistics_status'] == 'failure'
|
|
|
|
|
assert not response_dict['success']
|
2016-03-21 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_return_correct_details_for_complaint():
|
2016-04-06 14:31:33 +01:00
|
|
|
response_dict = get_firetext_responses('2')
|
|
|
|
|
assert response_dict['message'] == 'Undelivered (Pending with Network)'
|
2016-05-26 16:46:00 +01:00
|
|
|
assert response_dict['notification_status'] == 'pending'
|
|
|
|
|
assert response_dict['notification_statistics_status'] is None
|
2016-04-08 16:13:10 +01:00
|
|
|
assert response_dict['success']
|
2016-03-21 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_none_if_unrecognised_status_code():
|
|
|
|
|
with pytest.raises(KeyError) as e:
|
2016-04-06 14:31:33 +01:00
|
|
|
get_firetext_responses('99')
|
2016-03-21 09:20:38 +00:00
|
|
|
assert '99' in str(e.value)
|
2016-06-01 15:59:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_send_sms_successful_returns_firetext_response(mocker, mock_firetext_client):
|
|
|
|
|
to = content = reference = 'foo'
|
|
|
|
|
response_dict = {
|
|
|
|
|
'data': [],
|
|
|
|
|
'description': 'SMS successfully queued',
|
|
|
|
|
'code': 0,
|
|
|
|
|
'responseData': 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
with requests_mock.Mocker() as request_mock:
|
|
|
|
|
request_mock.post('https://www.firetext.co.uk/api/sendsms/json', json=response_dict, status_code=200)
|
|
|
|
|
response = mock_firetext_client.send_sms(to, content, reference)
|
|
|
|
|
|
|
|
|
|
response_json = response.json()
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response_json['code'] == 0
|
|
|
|
|
assert response_json['description'] == 'SMS successfully queued'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_send_sms_calls_firetext_correctly(mocker, mock_firetext_client):
|
|
|
|
|
to = '+447234567890'
|
|
|
|
|
content = 'my message'
|
|
|
|
|
reference = 'my reference'
|
|
|
|
|
response_dict = {
|
|
|
|
|
'code': 0,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
with requests_mock.Mocker() as request_mock:
|
|
|
|
|
request_mock.post('https://www.firetext.co.uk/api/sendsms/json', json=response_dict, status_code=200)
|
|
|
|
|
mock_firetext_client.send_sms(to, content, reference)
|
|
|
|
|
|
|
|
|
|
assert request_mock.call_count == 1
|
|
|
|
|
assert request_mock.request_history[0].url == 'https://www.firetext.co.uk/api/sendsms/json'
|
|
|
|
|
assert request_mock.request_history[0].method == 'POST'
|
|
|
|
|
|
|
|
|
|
request_args = parse_qs(request_mock.request_history[0].text)
|
|
|
|
|
assert request_args['apiKey'][0] == 'foo'
|
|
|
|
|
assert request_args['from'][0] == 'bar'
|
|
|
|
|
assert request_args['to'][0] == '447234567890'
|
|
|
|
|
assert request_args['message'][0] == content
|
|
|
|
|
assert request_args['reference'][0] == reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_send_sms_raises_if_firetext_rejects(mocker, mock_firetext_client):
|
|
|
|
|
to = content = reference = 'foo'
|
|
|
|
|
response_dict = {
|
|
|
|
|
'data': [],
|
|
|
|
|
'description': 'Some kind of error',
|
|
|
|
|
'code': 1,
|
|
|
|
|
'responseData': ''
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-22 17:18:05 +01:00
|
|
|
with pytest.raises(SmsClientResponseException) as exc, requests_mock.Mocker() as request_mock:
|
2016-06-01 15:59:44 +01:00
|
|
|
request_mock.post('https://www.firetext.co.uk/api/sendsms/json', json=response_dict, status_code=200)
|
|
|
|
|
mock_firetext_client.send_sms(to, content, reference)
|
|
|
|
|
|
2016-09-22 17:18:05 +01:00
|
|
|
assert exc.value.status_code == 200
|
|
|
|
|
assert '"description": "Some kind of error"' in exc.value.text
|
|
|
|
|
assert '"code": 1' in exc.value.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_send_sms_raises_if_firetext_rejects(mocker, mock_firetext_client):
|
|
|
|
|
to = content = reference = 'foo'
|
|
|
|
|
response_dict = {"something": "gone bad"}
|
|
|
|
|
|
|
|
|
|
with pytest.raises(SmsClientResponseException) as exc, requests_mock.Mocker() as request_mock:
|
|
|
|
|
request_mock.post('https://www.firetext.co.uk/api/sendsms/json', json=response_dict, status_code=400)
|
|
|
|
|
mock_firetext_client.send_sms(to, content, reference)
|
|
|
|
|
|
|
|
|
|
assert exc.value.status_code == 400
|
|
|
|
|
assert exc.value.text == '{"something": "gone bad"}'
|
|
|
|
|
assert type(exc.value.exception) == HTTPError
|
2016-07-01 14:06:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_send_sms_override_configured_shortcode_with_sender(mocker, mock_firetext_client):
|
|
|
|
|
to = '+447234567890'
|
|
|
|
|
content = 'my message'
|
|
|
|
|
reference = 'my reference'
|
|
|
|
|
response_dict = {
|
|
|
|
|
'code': 0,
|
|
|
|
|
}
|
|
|
|
|
sender = 'fromservice'
|
|
|
|
|
|
|
|
|
|
with requests_mock.Mocker() as request_mock:
|
|
|
|
|
request_mock.post('https://www.firetext.co.uk/api/sendsms/json', json=response_dict, status_code=200)
|
|
|
|
|
mock_firetext_client.send_sms(to, content, reference, sender=sender)
|
|
|
|
|
|
|
|
|
|
request_args = parse_qs(request_mock.request_history[0].text)
|
|
|
|
|
assert request_args['from'][0] == 'fromservice'
|