mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-16 20:48:37 -04:00
Removed/replaced retention redis count with notification count from db call
This commit is contained in:
committed by
Carlo Costino
parent
246e23f193
commit
a346a734fc
@@ -1,7 +1,6 @@
|
||||
import datetime
|
||||
import uuid
|
||||
from collections import namedtuple
|
||||
from unittest.mock import call
|
||||
|
||||
import pytest
|
||||
from boto3.exceptions import Boto3Error
|
||||
@@ -21,8 +20,7 @@ from app.notifications.process_notifications import (
|
||||
)
|
||||
from app.serialised_models import SerialisedTemplate
|
||||
from app.v2.errors import BadRequestError
|
||||
from tests.app.db import create_api_key, create_service, create_template
|
||||
from tests.conftest import set_config
|
||||
from tests.app.db import create_service, create_template
|
||||
|
||||
|
||||
def test_create_content_for_notification_passes(sample_email_template):
|
||||
@@ -189,103 +187,102 @@ def test_persist_notification_cache_is_not_incremented_on_failure_to_create_noti
|
||||
mocked_redis.assert_not_called()
|
||||
|
||||
|
||||
def test_persist_notification_does_not_increment_cache_if_test_key(
|
||||
notify_api, sample_template, sample_job, mocker, sample_test_api_key
|
||||
):
|
||||
daily_limit_cache = mocker.patch(
|
||||
"app.notifications.process_notifications.redis_store.incr"
|
||||
)
|
||||
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 0
|
||||
with set_config(notify_api, "REDIS_ENABLED", True):
|
||||
persist_notification(
|
||||
template_id=sample_template.id,
|
||||
template_version=sample_template.version,
|
||||
recipient="+447111111111",
|
||||
service=sample_template.service,
|
||||
personalisation={},
|
||||
notification_type="sms",
|
||||
api_key_id=sample_test_api_key.id,
|
||||
key_type=sample_test_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
|
||||
# def test_persist_notification_does_not_increment_cache_if_test_key(
|
||||
# notify_api, sample_template, sample_job, mocker, sample_test_api_key
|
||||
# ):
|
||||
# mocker.patch(
|
||||
# "app.notifications.process_notifications.dao_get_notification_count_for_service",
|
||||
# return_value=1,
|
||||
# )
|
||||
#
|
||||
# assert Notification.query.count() == 0
|
||||
# assert NotificationHistory.query.count() == 0
|
||||
# with set_config(notify_api, "REDIS_ENABLED", True):
|
||||
# persist_notification(
|
||||
# template_id=sample_template.id,
|
||||
# template_version=sample_template.version,
|
||||
# recipient="+447111111111",
|
||||
# service=sample_template.service,
|
||||
# personalisation={},
|
||||
# notification_type="sms",
|
||||
# api_key_id=sample_test_api_key.id,
|
||||
# key_type=sample_test_api_key.key_type,
|
||||
# job_id=sample_job.id,
|
||||
# job_row_number=100,
|
||||
# reference="ref",
|
||||
# )
|
||||
#
|
||||
# assert Notification.query.count() == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("restricted_service", [True, False])
|
||||
@freeze_time("2016-01-01 11:09:00.061258")
|
||||
def test_persist_notification_increments_cache_for_trial_or_live_service(
|
||||
notify_api, notify_db_session, mocker, restricted_service
|
||||
):
|
||||
service = create_service(restricted=restricted_service)
|
||||
template = create_template(service=service)
|
||||
api_key = create_api_key(service=service)
|
||||
mocker.patch(
|
||||
"app.notifications.process_notifications.redis_store.get", return_value=1
|
||||
)
|
||||
mock_incr = mocker.patch("app.notifications.process_notifications.redis_store.incr")
|
||||
with set_config(notify_api, "REDIS_ENABLED", True):
|
||||
persist_notification(
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient="+447111111122",
|
||||
service=template.service,
|
||||
personalisation={},
|
||||
notification_type="sms",
|
||||
api_key_id=api_key.id,
|
||||
key_type=api_key.key_type,
|
||||
reference="ref2",
|
||||
)
|
||||
|
||||
assert mock_incr.call_count == 1
|
||||
mock_incr.assert_has_calls(
|
||||
[
|
||||
# call(str(service.id) + "-2016-01-01-count", ),
|
||||
call(
|
||||
"2016-01-01-total",
|
||||
)
|
||||
]
|
||||
)
|
||||
# @pytest.mark.parametrize("restricted_service", [True, False])
|
||||
# @freeze_time("2016-01-01 11:09:00.061258")
|
||||
# def test_persist_notification_increments_cache_for_trial_or_live_service(
|
||||
# notify_api, notify_db_session, mocker, restricted_service
|
||||
# ):
|
||||
# service = create_service(restricted=restricted_service)
|
||||
# template = create_template(service=service)
|
||||
# api_key = create_api_key(service=service)
|
||||
# mocker.patch(
|
||||
# "app.notifications.process_notifications.redis_store.get", return_value=1
|
||||
# )
|
||||
# mock_incr = mocker.patch("app.notifications.process_notifications.redis_store.incr")
|
||||
# with set_config(notify_api, "REDIS_ENABLED", True):
|
||||
# persist_notification(
|
||||
# template_id=template.id,
|
||||
# template_version=template.version,
|
||||
# recipient="+447111111122",
|
||||
# service=template.service,
|
||||
# personalisation={},
|
||||
# notification_type="sms",
|
||||
# api_key_id=api_key.id,
|
||||
# key_type=api_key.key_type,
|
||||
# reference="ref2",
|
||||
# )
|
||||
#
|
||||
# assert mock_incr.call_count == 1
|
||||
# mock_incr.assert_has_calls(
|
||||
# [
|
||||
# # call(str(service.id) + "-2016-01-01-count", ),
|
||||
# call(
|
||||
# "2016-01-01-total",
|
||||
# )
|
||||
# ]
|
||||
# )
|
||||
|
||||
|
||||
@pytest.mark.parametrize("restricted_service", [True, False])
|
||||
@freeze_time("2016-01-01 11:09:00.061258")
|
||||
def test_persist_notification_sets_daily_limit_cache_if_one_does_not_exists(
|
||||
notify_api, notify_db_session, mocker, restricted_service
|
||||
):
|
||||
service = create_service(restricted=restricted_service)
|
||||
template = create_template(service=service)
|
||||
api_key = create_api_key(service=service)
|
||||
mocker.patch(
|
||||
"app.notifications.process_notifications.redis_store.get", return_value=None
|
||||
)
|
||||
mock_set = mocker.patch("app.notifications.process_notifications.redis_store.set")
|
||||
with set_config(notify_api, "REDIS_ENABLED", True):
|
||||
persist_notification(
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient="+447111111122",
|
||||
service=template.service,
|
||||
personalisation={},
|
||||
notification_type="sms",
|
||||
api_key_id=api_key.id,
|
||||
key_type=api_key.key_type,
|
||||
reference="ref2",
|
||||
)
|
||||
|
||||
assert mock_set.call_count == 1
|
||||
mock_set.assert_has_calls(
|
||||
[
|
||||
# call(str(service.id) + "-2016-01-01-count", 1, ex=86400),
|
||||
call("2016-01-01-total", 1, ex=86400)
|
||||
]
|
||||
)
|
||||
# @pytest.mark.parametrize("restricted_service", [True, False])
|
||||
# @freeze_time("2016-01-01 11:09:00.061258")
|
||||
# def test_persist_notification_sets_daily_limit_cache_if_one_does_not_exists(
|
||||
# notify_api, notify_db_session, mocker, restricted_service
|
||||
# ):
|
||||
# service = create_service(restricted=restricted_service)
|
||||
# template = create_template(service=service)
|
||||
# api_key = create_api_key(service=service)
|
||||
# mocker.patch(
|
||||
# "app.notifications.process_notifications.redis_store.get", return_value=None
|
||||
# )
|
||||
# mock_set = mocker.patch("app.notifications.process_notifications.redis_store.set")
|
||||
# with set_config(notify_api, "REDIS_ENABLED", True):
|
||||
# persist_notification(
|
||||
# template_id=template.id,
|
||||
# template_version=template.version,
|
||||
# recipient="+447111111122",
|
||||
# service=template.service,
|
||||
# personalisation={},
|
||||
# notification_type="sms",
|
||||
# api_key_id=api_key.id,
|
||||
# key_type=api_key.key_type,
|
||||
# reference="ref2",
|
||||
# )
|
||||
#
|
||||
# assert mock_set.call_count == 1
|
||||
# mock_set.assert_has_calls(
|
||||
# [
|
||||
# # call(str(service.id) + "-2016-01-01-count", 1, ex=86400),
|
||||
# call("2016-01-01-total", 1, ex=86400)
|
||||
# ]
|
||||
# )
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
@@ -55,11 +55,31 @@ def enable_redis(notify_api):
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key_type", ["team", "normal"])
|
||||
def test_check_service_message_limit_over_total_limit_fails(
|
||||
def test_check_service_over_total_message_limit_fails(
|
||||
key_type, mocker, notify_db_session
|
||||
):
|
||||
service = create_service()
|
||||
mocker.patch("app.redis_store.get", return_value="5001")
|
||||
mocker.patch(
|
||||
"app.redis_store.get",
|
||||
return_value="250001",
|
||||
)
|
||||
|
||||
with pytest.raises(TotalRequestsError) as e:
|
||||
check_service_over_total_message_limit(key_type, service)
|
||||
assert e.value.status_code == 429
|
||||
assert e.value.message == "Exceeded total application limits (250000) for today"
|
||||
assert e.value.fields == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key_type", ["team", "normal"])
|
||||
def test_check_application_over_retention_limit_fails(
|
||||
key_type, mocker, notify_db_session
|
||||
):
|
||||
service = create_service()
|
||||
mocker.patch(
|
||||
"app.notifications.validators.dao_get_notification_count_for_service",
|
||||
return_value="5001",
|
||||
)
|
||||
|
||||
with pytest.raises(TotalRequestsError) as e:
|
||||
check_application_over_retention_limit(key_type, service)
|
||||
|
||||
Reference in New Issue
Block a user