mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Cache services and API keys in memory
Same as we’ve done for templates. For high volume services this should mean avoiding calls to external services, either the database or Redis. TTL is set to 2 seconds, so that’s the maximum time it will take for revoking an API key or renaming a service to propagate. Some of the tests created services with the same service ID. This caused intermittent failures because the cache relies on unique service IDs (like we have in the real world) to key itself.
This commit is contained in:
@@ -8,9 +8,18 @@ import pytest
|
||||
from flask import json, current_app, request
|
||||
from freezegun import freeze_time
|
||||
from notifications_python_client.authentication import create_jwt_token
|
||||
from unittest.mock import call
|
||||
|
||||
from app import api_user
|
||||
from app.dao.api_key_dao import get_unsigned_secrets, save_model_api_key, get_unsigned_secret, expire_api_key
|
||||
from app.dao.api_key_dao import (
|
||||
get_unsigned_secrets,
|
||||
save_model_api_key,
|
||||
get_unsigned_secret,
|
||||
expire_api_key,
|
||||
get_model_api_keys,
|
||||
)
|
||||
from app.dao.services_dao import dao_fetch_service_by_id
|
||||
|
||||
from app.models import ApiKey, KEY_TYPE_NORMAL
|
||||
from app.authentication.auth import AuthError, requires_admin_auth, requires_auth, GENERAL_TOKEN_ERROR_MESSAGE
|
||||
|
||||
@@ -457,3 +466,28 @@ def test_proxy_key_on_admin_auth_endpoint(notify_api, check_proxy_header, header
|
||||
]
|
||||
)
|
||||
assert response.status_code == expected_status
|
||||
|
||||
|
||||
def test_should_cache_service_and_api_key_lookups(mocker, client, sample_api_key):
|
||||
|
||||
mock_get_api_keys = mocker.patch(
|
||||
'app.serialised_models.get_model_api_keys',
|
||||
wraps=get_model_api_keys,
|
||||
)
|
||||
mock_get_service = mocker.patch(
|
||||
'app.serialised_models.dao_fetch_service_by_id',
|
||||
wraps=dao_fetch_service_by_id,
|
||||
)
|
||||
|
||||
for i in range(5):
|
||||
token = __create_token(sample_api_key.service_id)
|
||||
client.get('/notifications', headers={
|
||||
'Authorization': f'Bearer {token}'
|
||||
})
|
||||
|
||||
assert mock_get_api_keys.call_args_list == [
|
||||
call(str(sample_api_key.service_id))
|
||||
]
|
||||
assert mock_get_service.call_args_list == [
|
||||
call(str(sample_api_key.service_id))
|
||||
]
|
||||
|
||||
@@ -1070,7 +1070,10 @@ def test_post_notifications_saves_email_to_queue(client, notify_db_session, mock
|
||||
save_email_task = mocker.patch("app.celery.tasks.save_api_email.apply_async")
|
||||
mock_send_task = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
|
||||
service = create_service(service_id='941b6f9a-50d7-4742-8d50-f365ca74bf27', service_name='high volume service')
|
||||
service = create_service(
|
||||
service_id=current_app.config['HIGH_VOLUME_SERVICE'][0],
|
||||
service_name='high volume service',
|
||||
)
|
||||
template = create_template(service=service, content='((message))', template_type=EMAIL_TYPE)
|
||||
data = {
|
||||
"email_address": "joe.citizen@example.com",
|
||||
@@ -1101,7 +1104,10 @@ def test_post_notifications_saves_email_normally_if_save_email_to_queue_fails(cl
|
||||
)
|
||||
mock_send_task = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
|
||||
service = create_service(service_id='941b6f9a-50d7-4742-8d50-f365ca74bf27', service_name='high volume service')
|
||||
service = create_service(
|
||||
service_id=current_app.config['HIGH_VOLUME_SERVICE'][1],
|
||||
service_name='high volume service',
|
||||
)
|
||||
template = create_template(service=service, content='((message))', template_type=EMAIL_TYPE)
|
||||
data = {
|
||||
"email_address": "joe.citizen@example.com",
|
||||
@@ -1130,8 +1136,10 @@ def test_post_notifications_doesnt_save_email_to_queue_for_test_emails(client, n
|
||||
save_email_task = mocker.patch("app.celery.tasks.save_api_email.apply_async")
|
||||
mock_send_task = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
|
||||
service = create_service(service_id='941b6f9a-50d7-4742-8d50-f365ca74bf27', service_name='high volume service')
|
||||
# create_api_key(service=service, key_type='test')
|
||||
service = create_service(
|
||||
service_id=current_app.config['HIGH_VOLUME_SERVICE'][2],
|
||||
service_name='high volume service',
|
||||
)
|
||||
template = create_template(service=service, content='((message))', template_type=EMAIL_TYPE)
|
||||
data = {
|
||||
"email_address": "joe.citizen@example.com",
|
||||
@@ -1160,7 +1168,10 @@ def test_post_notifications_doesnt_save_email_to_queue_for_sms(client, notify_db
|
||||
save_email_task = mocker.patch("app.celery.tasks.save_api_email.apply_async")
|
||||
mock_send_task = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
|
||||
service = create_service(service_id='941b6f9a-50d7-4742-8d50-f365ca74bf27', service_name='high volume service')
|
||||
service = create_service(
|
||||
service_id=current_app.config['HIGH_VOLUME_SERVICE'][3],
|
||||
service_name='high volume service',
|
||||
)
|
||||
template = create_template(service=service, content='((message))', template_type=SMS_TYPE)
|
||||
data = {
|
||||
"phone_number": '+447700900855',
|
||||
|
||||
Reference in New Issue
Block a user