mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-24 01:11:38 -05:00
Merge pull request #875 from alphagov/fix-redis-test-bug
Stop notifications with test api key incrementing the cache
This commit is contained in:
@@ -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)
|
||||
)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user