diff --git a/app/notifications/process_notifications.py b/app/notifications/process_notifications.py index d049871d0..3de694b0c 100644 --- a/app/notifications/process_notifications.py +++ b/app/notifications/process_notifications.py @@ -57,10 +57,11 @@ def persist_notification(template_id, ) if not simulated: dao_create_notification(notification) - if redis_store.get(redis.daily_limit_cache_key(service.id)): - redis_store.incr(redis.daily_limit_cache_key(service.id)) - if redis_store.get_all_from_hash(cache_key_for_service_template_counter(service.id)): - redis_store.increment_hash_value(cache_key_for_service_template_counter(service.id), template_id) + if key_type != KEY_TYPE_TEST: + if redis_store.get(redis.daily_limit_cache_key(service.id)): + redis_store.incr(redis.daily_limit_cache_key(service.id)) + if redis_store.get_all_from_hash(cache_key_for_service_template_counter(service.id)): + redis_store.increment_hash_value(cache_key_for_service_template_counter(service.id), template_id) current_app.logger.info( "{} {} created at {}".format(notification.notification_type, notification.id, notification.created_at) ) diff --git a/tests/app/notifications/test_process_notification.py b/tests/app/notifications/test_process_notification.py index 477c45306..caacde72b 100644 --- a/tests/app/notifications/test_process_notification.py +++ b/tests/app/notifications/test_process_notification.py @@ -15,6 +15,7 @@ from app.notifications.process_notifications import (create_content_for_notifica simulated_recipient) from app.utils import cache_key_for_service_template_counter from app.v2.errors import BadRequestError +from tests.app.conftest import sample_api_key as create_api_key def test_create_content_for_notification_passes(sample_email_template): @@ -128,6 +129,37 @@ def test_cache_is_not_incremented_on_failure_to_persist_notification(sample_api_ mock_service_template_cache.assert_not_called() +def test_persist_notification_does_not_increment_cache_if_test_key( + notify_db, notify_db_session, sample_template, sample_job, mocker +): + api_key = create_api_key(notify_db=notify_db, notify_db_session=notify_db_session, service=sample_template.service, + key_type='test') + mocker.patch('app.notifications.process_notifications.redis_store.get', return_value="cache") + mocker.patch('app.notifications.process_notifications.redis_store.get_all_from_hash', return_value="cache") + daily_limit_cache = mocker.patch('app.notifications.process_notifications.redis_store.incr') + template_usage_cache = mocker.patch('app.notifications.process_notifications.redis_store.increment_hash_value') + + assert Notification.query.count() == 0 + assert NotificationHistory.query.count() == 0 + persist_notification( + sample_template.id, + sample_template.version, + '+447111111111', + sample_template.service, + {}, + 'sms', + api_key.id, + api_key.key_type, + job_id=sample_job.id, + job_row_number=100, + reference="ref") + + assert Notification.query.count() == 1 + + assert not daily_limit_cache.called + assert not template_usage_cache.called + + @freeze_time("2016-01-01 11:09:00.061258") def test_persist_notification_with_optionals(sample_job, sample_api_key, mocker): assert Notification.query.count() == 0