Add capability for simulated numbers and email addresses

This commit is contained in:
Imdad Ahad
2016-09-13 17:00:28 +01:00
parent d2b936e631
commit 9df24b39ce
3 changed files with 94 additions and 20 deletions

View File

@@ -210,7 +210,6 @@ def send_notification(notification_type):
template_id=notification['template'], template_id=notification['template'],
service_id=service_id service_id=service_id
) )
errors = unarchived_template_schema.validate({'archived': template.archived}) errors = unarchived_template_schema.validate({'archived': template.archived})
if errors: if errors:
raise InvalidRequest(errors, status_code=400) raise InvalidRequest(errors, status_code=400)
@@ -261,24 +260,29 @@ def send_notification(notification_type):
status_code=400 status_code=400
) )
notification_id = create_uuid() if not _simulated_recipient(notification['to'], notification_type):
notification.update({"template_version": template.version}) notification_id = create_uuid()
persist_notification( notification.update({"template_version": template.version})
service, persist_notification(
notification_id, service,
notification,
datetime.utcnow().strftime(DATETIME_FORMAT),
notification_type,
str(api_user.id),
api_user.key_type
)
return jsonify(
data=get_notification_return_data(
notification_id, notification_id,
notification, notification,
template_object) datetime.utcnow().strftime(DATETIME_FORMAT),
), 201 notification_type,
str(api_user.id),
api_user.key_type
)
return jsonify(
data=get_notification_return_data(
notification_id,
notification,
template_object)
), 201
else:
return jsonify(
data=_get_simulated_recipient_response_data(template_object)
), 200
def get_notification_return_data(notification_id, notification, template): def get_notification_return_data(notification_id, notification, template):
@@ -304,6 +308,24 @@ def get_notification_statistics_for_day():
return jsonify(data=data), 200 return jsonify(data=data), 200
def _simulated_recipient(to_address, notification_type):
return (to_address in current_app.config['SIMULATED_SMS_NUMBERS']
if notification_type == SMS_TYPE
else to_address in current_app.config['SIMULATED_EMAIL_ADDRESSES'])
def _get_simulated_recipient_response_data(template_object):
response_data = {
'body': template_object.replaced,
'template_version': template_object._template['version']
}
if template_object._template['template_type'] == 'email':
response_data.update({'subject': template_object.replaced_subject})
return response_data
def persist_notification( def persist_notification(
service, service,
notification_id, notification_id,

View File

@@ -142,6 +142,13 @@ class Config(object):
SENDING_NOTIFICATIONS_TIMEOUT_PERIOD = 259200 SENDING_NOTIFICATIONS_TIMEOUT_PERIOD = 259200
SIMULATED_EMAIL_ADDRESSES = ('simulate-delivered@notifications.service.gov.uk',
'simulate-permanent-failure@notifications.service.gov.uk',
'simulate-temporary-failure@notifications.service.gov.uk',
)
SIMULATED_SMS_NUMBERS = ('+447700900000', '+447700900111', '+447700900222')
###################### ######################
# Config overrides ### # Config overrides ###

View File

@@ -674,14 +674,11 @@ def test_should_not_return_html_in_body(notify_api, notify_db, notify_db_session
def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api, sample_email_template, mocker): def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api, sample_email_template, mocker):
with notify_api.test_request_context(), notify_api.test_client() as client: with notify_api.test_request_context(), notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async') mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
data = { data = {
'to': "not-someone-we-trust@email-address.com", 'to': "not-someone-we-trust@email-address.com",
'template': str(sample_email_template.id), 'template': str(sample_email_template.id),
} }
# import pdb
# pdb.set_trace()
auth_header = create_authorization_header(service_id=sample_email_template.service_id, key_type=KEY_TYPE_TEAM) auth_header = create_authorization_header(service_id=sample_email_template.service_id, key_type=KEY_TYPE_TEAM)
response = client.post( response = client.post(
@@ -944,3 +941,51 @@ def test_should_delete_sms_notification_and_return_error_if_sqs_fails(notify_api
assert response.status_code == 500 assert response.status_code == 500
assert not notifications_dao.get_notification_by_id(fake_uuid) assert not notifications_dao.get_notification_by_id(fake_uuid)
assert not NotificationHistory.query.get(fake_uuid) assert not NotificationHistory.query.get(fake_uuid)
@pytest.mark.parametrize('to_email', [
'simulate-delivered@notifications.service.gov.uk',
'simulate-permanent-failure@notifications.service.gov.uk',
'simulate-temporary-failure@notifications.service.gov.uk'
])
def test_should_not_send_email_if_simulated_email_address(client, to_email, sample_email_template, mocker):
apply_async = mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
data = {
'to': to_email,
'template': sample_email_template.id
}
auth_header = create_authorization_header(service_id=sample_email_template.service_id)
response = client.post(
path='/notifications/email',
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 200
apply_async.assert_not_called()
@pytest.mark.parametrize('to_sms', [
'07700 900000',
'07700 900111',
'07700 900222'
])
def test_should_not_send_sms_if_simulated_sms_number(client, to_sms, sample_template, mocker):
apply_async = mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
data = {
'to': to_sms,
'template': sample_template.id
}
auth_header = create_authorization_header(service_id=sample_template.service_id)
response = client.post(
path='/notifications/sms',
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 200
apply_async.assert_not_called()