From 8f82642422288612b4e7cc0793fe26ea7ade49c8 Mon Sep 17 00:00:00 2001 From: Ken Tsang Date: Fri, 25 Aug 2017 16:07:30 +0100 Subject: [PATCH] Prevent v2 notifications POST when in trial mode --- app/v2/notifications/post_notifications.py | 3 +++ .../test_post_letter_notifications.py | 23 ++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index c27ca8dc5..c1bcd168e 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -150,6 +150,9 @@ def process_letter_notification(*, letter_data, api_key, template): if api_key.key_type == KEY_TYPE_TEAM: raise BadRequestError(message='Cannot send letters with a team api key', status_code=403) + if api_key.service.restricted: + raise BadRequestError(message='Cannot send letters when service is in trial mode', status_code=403) + job = create_letter_api_job(template) notification = create_letter_notification(letter_data, job, api_key) diff --git a/tests/app/v2/notifications/test_post_letter_notifications.py b/tests/app/v2/notifications/test_post_letter_notifications.py index 429e53eb8..cb9d4bb29 100644 --- a/tests/app/v2/notifications/test_post_letter_notifications.py +++ b/tests/app/v2/notifications/test_post_letter_notifications.py @@ -17,13 +17,12 @@ from app.variables import LETTER_TEST_API_FILENAME from app.variables import LETTER_API_FILENAME from tests import create_authorization_header -from tests.app.db import create_service -from tests.app.db import create_template +from tests.app.db import create_service, create_template def letter_request(client, data, service_id, key_type=KEY_TYPE_NORMAL, _expected_status=201): resp = client.post( - url_for('v2_notifications.post_notification', notification_type='letter'), + url_for('v2_notifications.post_notification', notification_type=LETTER_TYPE), data=json.dumps(data), headers=[ ('Content-Type', 'application/json'), @@ -232,3 +231,21 @@ def test_post_letter_notification_doesnt_accept_team_key(client, sample_letter_t assert error_json['status_code'] == 403 assert error_json['errors'] == [{'error': 'BadRequestError', 'message': 'Cannot send letters with a team api key'}] + + +def test_post_letter_notification_doesnt_send_in_trial(client, sample_trial_letter_template): + data = { + 'template_id': str(sample_trial_letter_template.id), + 'personalisation': {'address_line_1': 'Foo', 'address_line_2': 'Bar', 'postcode': 'Baz'} + } + + error_json = letter_request( + client, + data, + sample_trial_letter_template.service_id, + _expected_status=403 + ) + + assert error_json['status_code'] == 403 + assert error_json['errors'] == [ + {'error': 'BadRequestError', 'message': 'Cannot send letters when service is in trial mode'}]