mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-15 09:42:38 -05:00
Couple of bugs squashed.
1) It's incr not inc on the redis client, so renamed the calls everywhere 2) Redis returns bytes/string rather than an int if the value stored is an int. Cast the result to an int before use. Not you can set up the GET to do this transparently but I've not done this as we *may * use GETS for non-int and the callback sets up the cast for the connection not the call.
This commit is contained in:
@@ -151,7 +151,7 @@ def send_sms(self,
|
||||
e
|
||||
)
|
||||
else:
|
||||
redis_store.inc(cache_key(service_id))
|
||||
redis_store.incr(cache_key(service_id))
|
||||
|
||||
|
||||
@notify_celery.task(bind=True, name="send-email", max_retries=5, default_retry_delay=300)
|
||||
@@ -192,4 +192,4 @@ def send_email(self, service_id,
|
||||
e
|
||||
)
|
||||
else:
|
||||
redis_store.inc(cache_key(service_id))
|
||||
redis_store.incr(cache_key(service_id))
|
||||
|
||||
@@ -21,10 +21,10 @@ class RedisClient:
|
||||
if raise_exception:
|
||||
raise e
|
||||
|
||||
def inc(self, key, raise_exception=False):
|
||||
def incr(self, key, raise_exception=False):
|
||||
if self.active:
|
||||
try:
|
||||
return self.redis_store.inc(key)
|
||||
return self.redis_store.incr(key)
|
||||
except Exception as e:
|
||||
current_app.logger.exception(e)
|
||||
if raise_exception:
|
||||
|
||||
@@ -246,7 +246,7 @@ def send_notification(notification_type):
|
||||
|
||||
notification_id = create_uuid() if saved_notification is None else saved_notification.id
|
||||
notification.update({"template_version": template.version})
|
||||
redis_store.inc(redis.cache_key(service.id))
|
||||
redis_store.incr(redis.cache_key(service.id))
|
||||
return jsonify(
|
||||
data=get_notification_return_data(
|
||||
notification_id,
|
||||
|
||||
@@ -15,7 +15,7 @@ def check_service_message_limit(key_type, service):
|
||||
if not service_stats:
|
||||
service_stats = services_dao.fetch_todays_total_message_count(service.id)
|
||||
redis_store.set(cache_key, service_stats, ex=3600)
|
||||
if service_stats >= service.message_limit:
|
||||
if int(service_stats) >= service.message_limit:
|
||||
raise TooManyRequestsError(service.message_limit)
|
||||
|
||||
|
||||
|
||||
@@ -339,7 +339,7 @@ def test_should_send_template_to_correct_sms_task_and_persist(sample_template_wi
|
||||
to="+447234123123", personalisation={"name": "Jo"})
|
||||
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
redis_mock = mocker.patch('app.celery.tasks.redis_store.inc')
|
||||
redis_mock = mocker.patch('app.celery.tasks.redis_store.incr')
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
@@ -532,7 +532,7 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address(n
|
||||
|
||||
|
||||
def test_should_not_not_increment_counter_if_not_sending_sms(notify_db, notify_db_session, mocker):
|
||||
redis_mock = mocker.patch('app.celery.tasks.redis_store.inc')
|
||||
redis_mock = mocker.patch('app.celery.tasks.redis_store.incr')
|
||||
|
||||
user = sample_user(notify_db, notify_db_session)
|
||||
service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
|
||||
@@ -553,7 +553,7 @@ def test_should_not_not_increment_counter_if_not_sending_sms(notify_db, notify_d
|
||||
|
||||
|
||||
def test_should_not_not_increment_counter_if_not_sending_email(notify_db, notify_db_session, mocker):
|
||||
redis_mock = mocker.patch('app.celery.tasks.redis_store.inc')
|
||||
redis_mock = mocker.patch('app.celery.tasks.redis_store.incr')
|
||||
|
||||
user = sample_user(notify_db, notify_db_session)
|
||||
service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
|
||||
@@ -701,7 +701,7 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
|
||||
{"name": "Jo"},
|
||||
row_number=1)
|
||||
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
redis_mock = mocker.patch('app.celery.tasks.redis_store.inc')
|
||||
redis_mock = mocker.patch('app.celery.tasks.redis_store.incr')
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
|
||||
@@ -32,10 +32,10 @@ def test_should_not_raise_exception_if_raise_set_to_false(notify_api):
|
||||
redis_client.init_app(notify_api)
|
||||
redis_client.redis_store.get = Mock(side_effect=Exception())
|
||||
redis_client.redis_store.set = Mock(side_effect=Exception())
|
||||
redis_client.redis_store.inc = Mock(side_effect=Exception())
|
||||
redis_client.redis_store.incr = Mock(side_effect=Exception())
|
||||
assert redis_client.get('test') is None
|
||||
assert redis_client.set('test', 'test') is None
|
||||
assert redis_client.inc('test')is None
|
||||
assert redis_client.incr('test') is None
|
||||
|
||||
|
||||
def test_should_raise_exception_if_raise_set_to_true(notify_api):
|
||||
@@ -44,7 +44,7 @@ def test_should_raise_exception_if_raise_set_to_true(notify_api):
|
||||
redis_client.init_app(notify_api)
|
||||
redis_client.redis_store.get = Mock(side_effect=Exception('get failed'))
|
||||
redis_client.redis_store.set = Mock(side_effect=Exception('set failed'))
|
||||
redis_client.redis_store.inc = Mock(side_effect=Exception('inc failed'))
|
||||
redis_client.redis_store.incr = Mock(side_effect=Exception('inc failed'))
|
||||
with pytest.raises(Exception) as e:
|
||||
redis_client.get('test', raise_exception=True)
|
||||
assert str(e.value) == 'get failed'
|
||||
@@ -52,7 +52,7 @@ def test_should_raise_exception_if_raise_set_to_true(notify_api):
|
||||
redis_client.set('test', 'test', raise_exception=True)
|
||||
assert str(e.value) == 'set failed'
|
||||
with pytest.raises(Exception) as e:
|
||||
redis_client.inc('test', raise_exception=True)
|
||||
redis_client.incr('test', raise_exception=True)
|
||||
assert str(e.value) == 'inc failed'
|
||||
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ def test_should_increment_redis_cache_on_successful_email(
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocked_email = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
mocked_redis = mocker.patch('app.notifications.rest.redis_store.inc')
|
||||
mocked_redis = mocker.patch('app.notifications.rest.redis_store.incr')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
@@ -69,7 +69,7 @@ def test_should_increment_redis_cache_on_successful_sms(
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocked_sms = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
mocked_redis = mocker.patch('app.notifications.rest.redis_store.inc')
|
||||
mocked_redis = mocker.patch('app.notifications.rest.redis_store.incr')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
@@ -108,7 +108,7 @@ def test_should_not_increment_cache_on_failure(
|
||||
'app.celery.provider_tasks.deliver_{}.apply_async'.format(template_type),
|
||||
side_effect=Exception("failed to talk to SQS")
|
||||
)
|
||||
mocked_redis = mocker.patch('app.notifications.rest.redis_store.inc')
|
||||
mocked_redis = mocker.patch('app.notifications.rest.redis_store.incr')
|
||||
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
||||
template = sample_template if template_type == 'sms' else sample_email_template
|
||||
to = sample_template.service.created_by.mobile_number if template_type == 'sms' \
|
||||
|
||||
Reference in New Issue
Block a user