From 7769923ddadbe4fa3a59f03cbed737365d6f9f83 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 30 Aug 2016 11:00:22 +0100 Subject: [PATCH] Allow test key to send irrespective of daily limit 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 when you’ve sent up to your daily limit. --- app/notifications/rest.py | 5 ++++- tests/app/notifications/rest/test_send_notification.py | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/notifications/rest.py b/app/notifications/rest.py index 354cdbecb..5352f1063 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -215,7 +215,10 @@ def send_notification(notification_type): service_stats = sum(row.count for row in dao_fetch_todays_stats_for_service(service.id)) - if service_stats >= service.message_limit: + if all(( + api_user.key_type != KEY_TYPE_TEST, + service_stats >= service.message_limit + )): error = 'Exceeded send limits ({}) for today'.format(service.message_limit) raise InvalidRequest(error, status_code=429) diff --git a/tests/app/notifications/rest/test_send_notification.py b/tests/app/notifications/rest/test_send_notification.py index 96f1980c9..0a1951352 100644 --- a/tests/app/notifications/rest/test_send_notification.py +++ b/tests/app/notifications/rest/test_send_notification.py @@ -764,7 +764,11 @@ 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): +@pytest.mark.parametrize('restricted', [True, False]) +@pytest.mark.parametrize('limit', [0, 1]) +def test_should_send_email_to_anyone_with_test_key( + notify_api, sample_email_template, mocker, restricted, limit +): with notify_api.test_request_context(), notify_api.test_client() as client: mocker.patch('app.celery.tasks.send_email.apply_async') @@ -772,7 +776,8 @@ def test_should_send_email_to_anyone_with_test_key(notify_api, sample_email_temp 'to': 'anyone123@example.com', 'template': sample_email_template.id } - sample_email_template.service.restricted = True + sample_email_template.service.restricted = restricted + sample_email_template.service.message_limit = limit api_key = ApiKey( service=sample_email_template.service, name='test_key',