mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-18 05:51:44 -05:00
don't hit the query to get daily msg stats if redis is disabled
This commit is contained in:
@@ -23,7 +23,7 @@ from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
|
||||
|
||||
|
||||
def check_service_over_api_rate_limit(service, api_key):
|
||||
if current_app.config['API_RATE_LIMIT_ENABLED']:
|
||||
if current_app.config['API_RATE_LIMIT_ENABLED'] and current_app.config['REDIS_ENABLED']:
|
||||
cache_key = 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']
|
||||
@@ -33,7 +33,7 @@ def check_service_over_api_rate_limit(service, api_key):
|
||||
|
||||
|
||||
def check_service_over_daily_message_limit(key_type, service):
|
||||
if key_type != KEY_TYPE_TEST:
|
||||
if key_type != KEY_TYPE_TEST and current_app.config['REDIS_ENABLED']:
|
||||
cache_key = daily_limit_cache_key(service.id)
|
||||
service_stats = redis_store.get(cache_key)
|
||||
if not service_stats:
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import pytest
|
||||
from freezegun import freeze_time
|
||||
from flask import current_app
|
||||
|
||||
import app
|
||||
from app.models import INTERNATIONAL_SMS_TYPE, SMS_TYPE, EMAIL_TYPE
|
||||
from app.notifications.validators import (
|
||||
@@ -18,6 +19,8 @@ from app.v2.errors import (
|
||||
BadRequestError,
|
||||
TooManyRequestsError,
|
||||
RateLimitError)
|
||||
|
||||
from tests.conftest import set_config
|
||||
from tests.app.conftest import (
|
||||
sample_notification as create_notification,
|
||||
sample_service as create_service,
|
||||
@@ -26,6 +29,13 @@ from tests.app.conftest import (
|
||||
from tests.app.db import create_reply_to_email, create_service_sms_sender
|
||||
|
||||
|
||||
# all of these tests should have redis enabled (except where we specifically disable it)
|
||||
@pytest.fixture(scope='module', autouse=True)
|
||||
def enable_redis(notify_api):
|
||||
with set_config(notify_api, 'REDIS_ENABLED', True):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type', ['test', 'team', 'normal'])
|
||||
def test_check_service_message_limit_in_cache_with_unrestricted_service_is_allowed(
|
||||
key_type,
|
||||
@@ -78,6 +88,15 @@ def test_should_set_cache_value_as_value_from_database_if_cache_not_set(
|
||||
)
|
||||
|
||||
|
||||
def test_should_not_access_database_if_redis_disabled(notify_api, sample_service, mocker):
|
||||
with set_config(notify_api, 'REDIS_ENABLED', False):
|
||||
db_mock = mocker.patch('app.notifications.validators.services_dao')
|
||||
|
||||
check_service_over_daily_message_limit('normal', sample_service)
|
||||
|
||||
assert db_mock.method_calls == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type', ['team', 'normal'])
|
||||
def test_check_service_message_limit_over_message_limit_fails(key_type, notify_db, notify_db_session, mocker):
|
||||
with freeze_time("2016-01-01 12:00:00.000000"):
|
||||
|
||||
Reference in New Issue
Block a user