diff --git a/app/config.py b/app/config.py index 12a3f8d09..d2737cc9c 100644 --- a/app/config.py +++ b/app/config.py @@ -215,6 +215,7 @@ class Development(Config): Queue('research-mode', Exchange('default'), routing_key='research-mode') ] API_HOST_NAME = "http://localhost:6011" + API_RATE_LIMIT_ENABLED = True class Test(Config): @@ -237,6 +238,7 @@ class Test(Config): Queue('research-mode', Exchange('default'), routing_key='research-mode') ] REDIS_ENABLED = True + API_RATE_LIMIT_ENABLED = True API_HOST_NAME = "http://localhost:6011" API_KEY_LIMITS = { @@ -260,6 +262,7 @@ class Preview(Config): NOTIFY_ENVIRONMENT = 'preview' CSV_UPLOAD_BUCKET_NAME = 'preview-notifications-csv-upload' FROM_NUMBER = 'preview' + API_RATE_LIMIT_ENABLED = True class Staging(Config): @@ -268,6 +271,7 @@ class Staging(Config): CSV_UPLOAD_BUCKET_NAME = 'staging-notify-csv-upload' STATSD_ENABLED = True FROM_NUMBER = 'stage' + API_RATE_LIMIT_ENABLED = True class Live(Config): @@ -279,6 +283,7 @@ class Live(Config): FUNCTIONAL_TEST_PROVIDER_SERVICE_ID = '6c1d81bb-dae2-4ee9-80b0-89a4aae9f649' FUNCTIONAL_TEST_PROVIDER_SMS_TEMPLATE_ID = 'ba9e1789-a804-40b8-871f-cc60d4c1286f' PERFORMANCE_PLATFORM_ENABLED = True + API_RATE_LIMIT_ENABLED = False class CloudFoundryConfig(Config): diff --git a/app/notifications/validators.py b/app/notifications/validators.py index f826e1e0e..a0d7f2539 100644 --- a/app/notifications/validators.py +++ b/app/notifications/validators.py @@ -10,17 +10,18 @@ from notifications_utils.clients import redis def check_service_over_api_rate_limit(service, api_key): - cache_key = redis.rate_limit_cache_key(service.id, api_key.key_type) - rate_limit = current_app.config['API_KEY_LIMITS'][api_key.key_type]['limit'] - interval = current_app.config['API_KEY_LIMITS'][api_key.key_type]['interval'] - if redis_store.exceeded_rate_limit( - cache_key, - rate_limit, - interval): - raise RateLimitError( - rate_limit, - interval, - api_key.key_type) + if current_app.config['API_RATE_LIMIT_ENABLED']: + cache_key = redis.rate_limit_cache_key(service.id, api_key.key_type) + rate_limit = current_app.config['API_KEY_LIMITS'][api_key.key_type]['limit'] + interval = current_app.config['API_KEY_LIMITS'][api_key.key_type]['interval'] + if redis_store.exceeded_rate_limit( + cache_key, + rate_limit, + interval): + raise RateLimitError( + rate_limit, + interval, + api_key.key_type) def check_service_over_daily_message_limit(key_type, service): diff --git a/tests/app/notifications/test_validators.py b/tests/app/notifications/test_validators.py index 40ce22b0f..4ce51c24b 100644 --- a/tests/app/notifications/test_validators.py +++ b/tests/app/notifications/test_validators.py @@ -1,6 +1,6 @@ import pytest from freezegun import freeze_time - +from flask import current_app import app from app.notifications.validators import ( check_service_over_daily_message_limit, @@ -320,3 +320,20 @@ def test_that_when_not_exceeded_rate_limit_request_succeeds( limit, interval ) + + +def test_should_not_rate_limit_if_limiting_is_disabled( + notify_db, + notify_db_session, + mocker): + with freeze_time("2016-01-01 12:00:00.000000"): + current_app.config['API_RATE_LIMIT_ENABLED'] = False + + mocker.patch('app.redis_store.exceeded_rate_limit', return_value=False) + mocker.patch('app.notifications.validators.services_dao') + + service = create_service(notify_db, notify_db_session, restricted=True) + api_key = sample_api_key(notify_db, notify_db_session, service=service) + + check_service_over_api_rate_limit(service, api_key) + assert not app.redis_store.exceeded_rate_limit.called