mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 08:51:30 -05:00
tests for send_sms through mmg/firetext
This commit is contained in:
@@ -6,3 +6,4 @@ pytest-cov==2.2.0
|
|||||||
mock==1.0.1
|
mock==1.0.1
|
||||||
moto==0.4.19
|
moto==0.4.19
|
||||||
freezegun==0.3.6
|
freezegun==0.3.6
|
||||||
|
requests-mock==0.7.0
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import pytest
|
from urllib.parse import parse_qs
|
||||||
|
|
||||||
from app.clients.sms.firetext import get_firetext_responses
|
import pytest
|
||||||
|
import requests_mock
|
||||||
|
|
||||||
|
from app.clients.sms.firetext import (get_firetext_responses, FiretextClientException)
|
||||||
|
|
||||||
|
|
||||||
def test_should_return_correct_details_for_delivery():
|
def test_should_return_correct_details_for_delivery():
|
||||||
@@ -31,3 +34,63 @@ def test_should_be_none_if_unrecognised_status_code():
|
|||||||
with pytest.raises(KeyError) as e:
|
with pytest.raises(KeyError) as e:
|
||||||
get_firetext_responses('99')
|
get_firetext_responses('99')
|
||||||
assert '99' in str(e.value)
|
assert '99' in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
|
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': ''
|
||||||
|
}
|
||||||
|
|
||||||
|
with pytest.raises(FiretextClientException) as exc, 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 exc.value.code == 1
|
||||||
|
assert exc.value.description == 'Some kind of error'
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
from app.clients.sms.mmg import get_mmg_responses
|
import pytest
|
||||||
|
import requests_mock
|
||||||
|
|
||||||
|
from app.clients.sms.mmg import (get_mmg_responses, MMGClientException)
|
||||||
|
|
||||||
|
|
||||||
def test_should_return_correct_details_for_delivery():
|
def test_should_return_correct_details_for_delivery():
|
||||||
@@ -23,3 +26,54 @@ def test_should_be_none_if_unrecognised_status_code():
|
|||||||
assert response_dict['notification_status'] == 'failed'
|
assert response_dict['notification_status'] == 'failed'
|
||||||
assert response_dict['notification_statistics_status'] == 'failure'
|
assert response_dict['notification_statistics_status'] == 'failure'
|
||||||
assert not response_dict['success']
|
assert not response_dict['success']
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_sms_successful_returns_mmg_response(mocker, mock_mmg_client):
|
||||||
|
to = content = reference = 'foo'
|
||||||
|
response_dict = {'Reference': 12345678}
|
||||||
|
|
||||||
|
with requests_mock.Mocker() as request_mock:
|
||||||
|
request_mock.post('https://www.mmgrp.co.uk/API/json/api.php', json=response_dict, status_code=200)
|
||||||
|
response = mock_mmg_client.send_sms(to, content, reference)
|
||||||
|
|
||||||
|
response_json = response.json()
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response_json['Reference'] == 12345678
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_sms_calls_mmg_correctly(mocker, mock_mmg_client):
|
||||||
|
to = '+447234567890'
|
||||||
|
content = 'my message'
|
||||||
|
reference = 'my reference'
|
||||||
|
response_dict = {'Reference': 12345678}
|
||||||
|
|
||||||
|
with requests_mock.Mocker() as request_mock:
|
||||||
|
request_mock.post('https://www.mmgrp.co.uk/API/json/api.php', json=response_dict, status_code=200)
|
||||||
|
mock_mmg_client.send_sms(to, content, reference)
|
||||||
|
|
||||||
|
assert request_mock.call_count == 1
|
||||||
|
assert request_mock.request_history[0].url == 'https://www.mmgrp.co.uk/API/json/api.php'
|
||||||
|
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
|
||||||
|
assert request_args['sender'] == 'bar'
|
||||||
|
assert request_args['cid'] == reference
|
||||||
|
assert request_args['multi'] is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_sms_raises_if_mmg_rejects(mocker, mock_mmg_client):
|
||||||
|
to = content = reference = 'foo'
|
||||||
|
response_dict = {
|
||||||
|
'Error': 206,
|
||||||
|
'Description': 'Some kind of error'
|
||||||
|
}
|
||||||
|
|
||||||
|
with pytest.raises(MMGClientException) as exc, requests_mock.Mocker() as request_mock:
|
||||||
|
request_mock.post('https://www.mmgrp.co.uk/API/json/api.php', json=response_dict, status_code=400)
|
||||||
|
mock_mmg_client.send_sms(to, content, reference)
|
||||||
|
|
||||||
|
assert exc.value.code == 206
|
||||||
|
assert exc.value.description == 'Some kind of error'
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import pytest
|
import uuid
|
||||||
from datetime import (datetime, date)
|
from datetime import (datetime, date)
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from app import db
|
from app import db
|
||||||
from app.models import (
|
from app.models import (
|
||||||
User,
|
User,
|
||||||
@@ -20,7 +23,8 @@ from app.dao.api_key_dao import save_model_api_key
|
|||||||
from app.dao.jobs_dao import dao_create_job
|
from app.dao.jobs_dao import dao_create_job
|
||||||
from app.dao.notifications_dao import dao_create_notification
|
from app.dao.notifications_dao import dao_create_notification
|
||||||
from app.dao.invited_user_dao import save_invited_user
|
from app.dao.invited_user_dao import save_invited_user
|
||||||
import uuid
|
from app.clients.sms.firetext import FiretextClient
|
||||||
|
from app.clients.sms.mmg import MMGClient
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='function')
|
@pytest.fixture(scope='function')
|
||||||
@@ -527,3 +531,27 @@ def sample_notification_statistics(notify_db,
|
|||||||
notify_db.session.add(stats)
|
notify_db.session.add(stats)
|
||||||
notify_db.session.commit()
|
notify_db.session.commit()
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='function')
|
||||||
|
def mock_firetext_client(mocker, statsd_client=None):
|
||||||
|
client = FiretextClient()
|
||||||
|
statsd_client = statsd_client or mocker.Mock()
|
||||||
|
current_app = mocker.Mock(config={
|
||||||
|
'FIRETEXT_API_KEY': 'foo',
|
||||||
|
'FIRETEXT_NUMBER': 'bar'
|
||||||
|
})
|
||||||
|
client.init_app(current_app, statsd_client)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='function')
|
||||||
|
def mock_mmg_client(mocker, statsd_client=None):
|
||||||
|
client = MMGClient()
|
||||||
|
statsd_client = statsd_client or mocker.Mock()()
|
||||||
|
current_app = mocker.Mock(config={
|
||||||
|
'MMG_API_KEY': 'foo',
|
||||||
|
'MMG_FROM_NUMBER': 'bar'
|
||||||
|
})
|
||||||
|
client.init_app(current_app, statsd_client)
|
||||||
|
return client
|
||||||
|
|||||||
Reference in New Issue
Block a user