Skip to content

Commit

Permalink
Ensure that when an API call is made on
Browse files Browse the repository at this point in the history
- a TEST key
- a research-mode service

We put the task into the research-mode queue NOT the production delivery queue.
  • Loading branch information
Martyn Inglis committed Sep 28, 2016
1 parent 3618a39 commit b9f0659
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 7 deletions.
11 changes: 9 additions & 2 deletions app/notifications/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,17 @@ def persist_notification(
)

try:
research_mode = service.research_mode or key_type == KEY_TYPE_TEST
if notification_type == SMS_TYPE:
provider_tasks.deliver_sms.apply_async((str(notification_id)), queue='send-sms')
provider_tasks.deliver_sms.apply_async(
(str(notification_id)),
queue='send-sms' if not research_mode else 'research-mode'
)
if notification_type == EMAIL_TYPE:
provider_tasks.deliver_email.apply_async((str(notification_id)), queue='send-email')
provider_tasks.deliver_email.apply_async(
(str(notification_id)),
queue='send-email' if not research_mode else 'research-mode'
)
except Exception as e:
current_app.logger.exception("Failed to send to SQS exception", e)
dao_delete_notifications_and_history_by_id(notification_id)
Expand Down
5 changes: 3 additions & 2 deletions tests/app/celery/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,9 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address(n
Notification.query.filter_by(id=notification_id).one()


def test_should_put_send_email_task_in_research_mode_queue_if_research_mode_service(notify_db, notify_db_session, mocker):
def test_should_put_send_email_task_in_research_mode_queue_if_research_mode_service(
notify_db, notify_db_session, mocker
):
service = sample_service(notify_db, notify_db_session)
service.research_mode = True
services_dao.dao_update_service(service)
Expand All @@ -543,7 +545,6 @@ def test_should_put_send_email_task_in_research_mode_queue_if_research_mode_serv
)



def test_should_send_sms_template_to_and_persist_with_job_id(sample_job, sample_api_key, mocker):
notification = _notification_json(
sample_job.template,
Expand Down
104 changes: 101 additions & 3 deletions tests/app/notifications/rest/test_send_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from flask import (json, current_app)
from freezegun import freeze_time
from mock import ANY
from notifications_python_client.authentication import create_jwt_token

import app
Expand All @@ -18,8 +19,8 @@
sample_notification as create_sample_notification,
sample_service as create_sample_service,
sample_email_template as create_sample_email_template,
sample_template as create_sample_template
)
sample_template as create_sample_template,
sample_service, sample_template, sample_email_template)


def test_create_sms_should_reject_if_missing_required_fields(notify_api, sample_api_key, mocker):
Expand Down Expand Up @@ -744,6 +745,39 @@ def test_should_send_email_if_team_api_key_and_a_service_user(notify_api, sample
assert response.status_code == 201


@pytest.mark.parametrize('restricted', [True, False])
@pytest.mark.parametrize('limit', [0, 1])
def test_should_send_sms_to_anyone_with_test_key(
notify_api, sample_template, mocker, restricted, limit, fake_uuid
):
with notify_api.test_request_context(), notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)

data = {
'to': '07811111111',
'template': sample_template.id
}
sample_template.service.restricted = restricted
sample_template.service.message_limit = limit
api_key = ApiKey(
service=sample_template.service,
name='test_key',
created_by=sample_template.created_by,
key_type=KEY_TYPE_TEST
)
save_model_api_key(api_key)
auth_header = create_jwt_token(secret=api_key.unsigned_secret, client_id=str(api_key.service_id))

response = client.post(
path='/notifications/sms',
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))]
)
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with((fake_uuid), queue='research-mode')
assert response.status_code == 201


@pytest.mark.parametrize('restricted', [True, False])
@pytest.mark.parametrize('limit', [0, 1])
def test_should_send_email_to_anyone_with_test_key(
Expand Down Expand Up @@ -775,7 +809,7 @@ def test_should_send_email_to_anyone_with_test_key(
)

app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
(fake_uuid), queue='send-email')
(fake_uuid), queue='research-mode')
assert response.status_code == 201


Expand Down Expand Up @@ -1024,3 +1058,67 @@ def test_should_error_if_notification_type_does_not_match_template_type(
assert json_resp['result'] == 'error'
assert '{0} template is not suitable for {1} notification'.format(template_type, notification_type) \
in json_resp['message']


def test_should_send_sms_in_research_mode_queue_if_research_mode_service(
notify_api, notify_db, notify_db_session, mocker
):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')

service = sample_service(notify_db, notify_db_session)
service.research_mode = True
dao_update_service(service)

template = sample_template(notify_db, notify_db_session, service=service)

data = {
'to': service.created_by.mobile_number,
'template': template.id
}

auth_header = create_authorization_header(service_id=service.id)

response = client.post(
path='/notifications/sms',
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])

app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with(
(ANY),
queue="research-mode"
)
assert response.status_code == 201


def test_should_send_email_in_research_mode_queue_if_research_mode_service(
notify_api, notify_db, notify_db_session, mocker
):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')

service = sample_service(notify_db, notify_db_session)
service.research_mode = True
dao_update_service(service)

template = sample_email_template(notify_db, notify_db_session, service=service)

data = {
'to': service.created_by.email_address,
'template': template.id
}

auth_header = create_authorization_header(service_id=service.id)

response = client.post(
path='/notifications/email',
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])

app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
(ANY),
queue="research-mode"
)
assert response.status_code == 201

0 comments on commit b9f0659

Please sign in to comment.