mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-02 12:48:32 -04:00
Add sms sender to service to be used in sms templates
in place of default numeric short code. If not present default short code is used.
This commit is contained in:
@@ -14,9 +14,7 @@ from app.clients.sms import SmsClientException
|
||||
from app.dao import notifications_dao, provider_details_dao
|
||||
from app.dao import provider_statistics_dao
|
||||
from app.models import Notification, NotificationStatistics, Job
|
||||
from tests.app.conftest import (
|
||||
sample_notification
|
||||
)
|
||||
from tests.app.conftest import sample_notification
|
||||
|
||||
|
||||
def test_should_by_10_second_delay_as_default():
|
||||
@@ -49,6 +47,7 @@ def test_should_by_240_minute_delay_on_retry_two():
|
||||
|
||||
def test_should_return_highest_priority_active_provider(notify_db, notify_db_session):
|
||||
providers = provider_details_dao.get_provider_details_by_notification_type('sms')
|
||||
|
||||
first = providers[0]
|
||||
second = providers[1]
|
||||
|
||||
@@ -100,7 +99,8 @@ def test_should_send_personalised_template_to_correct_sms_provider_and_persist(
|
||||
mmg_client.send_sms.assert_called_once_with(
|
||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
||||
content="Sample service: Hello Jo",
|
||||
reference=str(db_notification.id)
|
||||
reference=str(db_notification.id),
|
||||
sender=None
|
||||
)
|
||||
notification = Notification.query.filter_by(id=db_notification.id).one()
|
||||
|
||||
@@ -139,7 +139,8 @@ def test_send_sms_should_use_template_version_from_notification_not_latest(
|
||||
mmg_client.send_sms.assert_called_once_with(
|
||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
||||
content="Sample service: This is a template",
|
||||
reference=str(db_notification.id)
|
||||
reference=str(db_notification.id),
|
||||
sender=None
|
||||
)
|
||||
|
||||
persisted_notification = notifications_dao.get_notification(sample_template.service_id, db_notification.id)
|
||||
@@ -289,3 +290,36 @@ def test_should_go_into_technical_error_if_exceeds_retries(
|
||||
job = Job.query.get(notification.job.id)
|
||||
assert job.notification_count == 1
|
||||
assert job.notifications_failed == 1
|
||||
|
||||
|
||||
def test_should_send_sms_sender_from_service_if_present(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
sample_template,
|
||||
mocker):
|
||||
db_notification = sample_notification(notify_db, notify_db_session, template=sample_template,
|
||||
to_field="+447234123123",
|
||||
status='created')
|
||||
|
||||
sample_service.sms_sender = 'elevenchars'
|
||||
notify_db.session.add(sample_service)
|
||||
notify_db.session.commit()
|
||||
|
||||
mocker.patch('app.mmg_client.send_sms')
|
||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
||||
mocker.patch('app.statsd_client.incr')
|
||||
mocker.patch('app.statsd_client.timing_with_dates')
|
||||
mocker.patch('app.statsd_client.timing')
|
||||
|
||||
send_sms_to_provider(
|
||||
db_notification.service_id,
|
||||
db_notification.id
|
||||
)
|
||||
|
||||
mmg_client.send_sms.assert_called_once_with(
|
||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
||||
content="Sample service: This is a template",
|
||||
reference=str(db_notification.id),
|
||||
sender=sample_service.sms_sender
|
||||
)
|
||||
|
||||
@@ -94,3 +94,20 @@ def test_send_sms_raises_if_firetext_rejects(mocker, mock_firetext_client):
|
||||
|
||||
assert exc.value.code == 1
|
||||
assert exc.value.description == 'Some kind of error'
|
||||
|
||||
|
||||
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'
|
||||
|
||||
@@ -77,3 +77,18 @@ def test_send_sms_raises_if_mmg_rejects(mocker, mock_mmg_client):
|
||||
|
||||
assert exc.value.code == 206
|
||||
assert exc.value.description == 'Some kind of error'
|
||||
|
||||
|
||||
def test_send_sms_override_configured_shortcode_with_sender(mocker, mock_mmg_client):
|
||||
to = '+447234567890'
|
||||
content = 'my message'
|
||||
reference = 'my reference'
|
||||
response_dict = {'Reference': 12345678}
|
||||
sender = 'fromservice'
|
||||
|
||||
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, sender=sender)
|
||||
|
||||
request_args = request_mock.request_history[0].json()
|
||||
assert request_args['sender'] == 'fromservice'
|
||||
|
||||
@@ -1029,3 +1029,60 @@ def test_get_all_notifications_for_service_in_order(notify_api, notify_db, notif
|
||||
assert resp['notifications'][1]['to'] == notification_2.to
|
||||
assert resp['notifications'][2]['to'] == notification_1.to
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_set_sms_sender_for_service(notify_api, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
resp = client.get(
|
||||
'/service/{}'.format(sample_service.id),
|
||||
headers=[auth_header]
|
||||
)
|
||||
json_resp = json.loads(resp.get_data(as_text=True))
|
||||
assert resp.status_code == 200
|
||||
assert json_resp['data']['name'] == sample_service.name
|
||||
|
||||
data = {
|
||||
'sms_sender': 'elevenchars',
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
resp = client.post(
|
||||
'/service/{}'.format(sample_service.id),
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
)
|
||||
result = json.loads(resp.get_data(as_text=True))
|
||||
assert resp.status_code == 200
|
||||
assert result['data']['sms_sender'] == 'elevenchars'
|
||||
|
||||
|
||||
def test_set_sms_sender_for_service_rejects_invalid_characters(notify_api, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
resp = client.get(
|
||||
'/service/{}'.format(sample_service.id),
|
||||
headers=[auth_header]
|
||||
)
|
||||
json_resp = json.loads(resp.get_data(as_text=True))
|
||||
assert resp.status_code == 200
|
||||
assert json_resp['data']['name'] == sample_service.name
|
||||
|
||||
data = {
|
||||
'sms_sender': 'invalid####',
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
resp = client.post(
|
||||
'/service/{}'.format(sample_service.id),
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
)
|
||||
result = json.loads(resp.get_data(as_text=True))
|
||||
assert resp.status_code == 400
|
||||
assert result['result'] == 'error'
|
||||
assert result['message'] == {'sms_sender': ['Only alphanumeric characters allowed']}
|
||||
|
||||
@@ -7,7 +7,6 @@ from alembic.command import upgrade
|
||||
from alembic.config import Config
|
||||
from flask.ext.migrate import Migrate, MigrateCommand
|
||||
from flask.ext.script import Manager
|
||||
from sqlalchemy.schema import MetaData
|
||||
|
||||
from app import create_app, db
|
||||
|
||||
|
||||
Reference in New Issue
Block a user