From 5eaec8c2351084dd5b54b37049aab257d3744a28 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 30 Aug 2016 10:47:27 +0100 Subject: [PATCH] Allow test key to send irrespective of trial mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When you use a simulate API key it should behave like a live service, except for actually sending the messages. There should be no limits even if the service is in trial mode. This commit removes the restriction on sending messages to peole outside your team when you’re in trial mode. --- app/notifications/rest.py | 16 +++++--- .../rest/test_send_notification.py | 37 ++++++++++++++++++- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/app/notifications/rest.py b/app/notifications/rest.py index f9fa85e2f..354cdbecb 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -15,7 +15,7 @@ from notifications_utils.renderers import PassThrough from app.clients.email.aws_ses import get_aws_responses from app import api_user, encryption, create_uuid, DATETIME_FORMAT, DATE_FORMAT, statsd_client from app.dao.services_dao import dao_fetch_todays_stats_for_service -from app.models import KEY_TYPE_TEAM +from app.models import KEY_TYPE_TEAM, KEY_TYPE_TEST from app.dao import ( templates_dao, services_dao, @@ -259,12 +259,16 @@ def send_notification(notification_type): errors = {'content': [message]} raise InvalidRequest(errors, status_code=400) - if (service.restricted or api_user.key_type == KEY_TYPE_TEAM) and not allowed_to_send_to( - notification['to'], - itertools.chain.from_iterable( - [user.mobile_number, user.email_address] for user in service.users + if all(( + api_user.key_type != KEY_TYPE_TEST, + service.restricted or api_user.key_type == KEY_TYPE_TEAM, + not allowed_to_send_to( + notification['to'], + itertools.chain.from_iterable( + [user.mobile_number, user.email_address] for user in service.users + ) ) - ): + )): if (api_user.key_type == KEY_TYPE_TEAM): message = 'Can’t send to this recipient using a team-only API key' else: diff --git a/tests/app/notifications/rest/test_send_notification.py b/tests/app/notifications/rest/test_send_notification.py index 47626ee85..96f1980c9 100644 --- a/tests/app/notifications/rest/test_send_notification.py +++ b/tests/app/notifications/rest/test_send_notification.py @@ -10,7 +10,7 @@ from notifications_python_client.authentication import create_jwt_token import app from app import encryption -from app.models import ApiKey, KEY_TYPE_TEAM +from app.models import ApiKey, KEY_TYPE_TEAM, KEY_TYPE_TEST from app.dao.templates_dao import dao_get_all_templates_for_service, dao_update_template from app.dao.services_dao import dao_update_service from app.dao.api_key_dao import save_model_api_key @@ -764,6 +764,41 @@ def test_should_send_email_if_team_api_key_and_a_service_user(notify_api, sample assert response.status_code == 201 +def test_should_send_email_to_anyone_with_test_key(notify_api, sample_email_template, mocker): + with notify_api.test_request_context(), notify_api.test_client() as client: + mocker.patch('app.celery.tasks.send_email.apply_async') + + data = { + 'to': 'anyone123@example.com', + 'template': sample_email_template.id + } + sample_email_template.service.restricted = True + api_key = ApiKey( + service=sample_email_template.service, + name='test_key', + created_by=sample_email_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/email', + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))] + ) + + app.celery.tasks.send_email.apply_async.assert_called_once_with( + ANY, + kwargs={ + 'api_key_id': str(api_key.id), + 'key_type': api_key.key_type + }, + queue='email' + ) + assert response.status_code == 201 + + def test_should_send_sms_if_team_api_key_and_a_service_user(notify_api, sample_template, mocker): with notify_api.test_request_context(), notify_api.test_client() as client: mocker.patch('app.celery.tasks.send_sms.apply_async')