diff --git a/app/notify_client/cache.py b/app/notify_client/cache.py index 36d769df2..bc8d9d865 100644 --- a/app/notify_client/cache.py +++ b/app/notify_client/cache.py @@ -61,9 +61,12 @@ def delete(key_format): @wraps(client_method) def new_client_method(client_instance, *args, **kwargs): - redis_key = _make_key(key_format, client_method, args, kwargs) - redis_client.delete(redis_key) - return client_method(client_instance, *args, **kwargs) + try: + api_response = client_method(client_instance, *args, **kwargs) + finally: + redis_key = _make_key(key_format, client_method, args, kwargs) + redis_client.delete(redis_key) + return api_response return new_client_method diff --git a/tests/app/notify_client/test_email_branding_client.py b/tests/app/notify_client/test_email_branding_client.py index 3ea1aa293..86953e201 100644 --- a/tests/app/notify_client/test_email_branding_client.py +++ b/tests/app/notify_client/test_email_branding_client.py @@ -85,6 +85,6 @@ def test_update_email_branding(mocker, fake_uuid): data=org_data ) assert mock_redis_delete.call_args_list == [ - call('email_branding'), call('email_branding-{}'.format(fake_uuid)), + call('email_branding'), ] diff --git a/tests/app/notify_client/test_letter_branding_client.py b/tests/app/notify_client/test_letter_branding_client.py index 3b2d74a32..589403933 100644 --- a/tests/app/notify_client/test_letter_branding_client.py +++ b/tests/app/notify_client/test_letter_branding_client.py @@ -68,6 +68,6 @@ def test_update_letter_branding(mocker, fake_uuid): data=branding ) assert mock_redis_delete.call_args_list == [ - call('letter_branding'), call('letter_branding-{}'.format(fake_uuid)), + call('letter_branding'), ] diff --git a/tests/app/notify_client/test_organisation_client.py b/tests/app/notify_client/test_organisation_client.py index d171b0fb4..0ec857f50 100644 --- a/tests/app/notify_client/test_organisation_client.py +++ b/tests/app/notify_client/test_organisation_client.py @@ -150,7 +150,7 @@ def test_update_organisation_when_not_updating_org_type(mocker, fake_uuid): url='/organisations/{}'.format(fake_uuid), data={'foo': 'bar'} ) - assert mock_redis_delete.call_args_list == [call('domains'), call('organisations')] + assert mock_redis_delete.call_args_list == [call('organisations'), call('domains')] def test_update_organisation_when_updating_org_type_and_org_has_services(mocker, fake_uuid): @@ -168,9 +168,9 @@ def test_update_organisation_when_updating_org_type_and_org_has_services(mocker, data={'organisation_type': 'central'} ) assert mock_redis_delete.call_args_list == [ - call('domains'), - call('organisations'), call('service-a', 'service-b', 'service-c'), + call('organisations'), + call('domains'), ] @@ -189,6 +189,6 @@ def test_update_organisation_when_updating_org_type_but_org_has_no_services(mock data={'organisation_type': 'central'} ) assert mock_redis_delete.call_args_list == [ - call('domains'), call('organisations'), + call('domains'), ] diff --git a/tests/app/notify_client/test_service_api_client.py b/tests/app/notify_client/test_service_api_client.py index d12e16b01..801b7e742 100644 --- a/tests/app/notify_client/test_service_api_client.py +++ b/tests/app/notify_client/test_service_api_client.py @@ -388,29 +388,29 @@ def test_deletes_service_cache( 'service-{}-templates'.format(SERVICE_ONE_ID), ]), ('update_service_template', [FAKE_TEMPLATE_ID, 'foo', 'sms', 'bar', SERVICE_ONE_ID], [ - 'service-{}-templates'.format(SERVICE_ONE_ID), - 'template-{}-version-None'.format(FAKE_TEMPLATE_ID), 'template-{}-versions'.format(FAKE_TEMPLATE_ID), + 'template-{}-version-None'.format(FAKE_TEMPLATE_ID), + 'service-{}-templates'.format(SERVICE_ONE_ID), ]), ('redact_service_template', [SERVICE_ONE_ID, FAKE_TEMPLATE_ID], [ - 'service-{}-templates'.format(SERVICE_ONE_ID), - 'template-{}-version-None'.format(FAKE_TEMPLATE_ID), 'template-{}-versions'.format(FAKE_TEMPLATE_ID), + 'template-{}-version-None'.format(FAKE_TEMPLATE_ID), + 'service-{}-templates'.format(SERVICE_ONE_ID), ]), ('update_service_template_sender', [SERVICE_ONE_ID, FAKE_TEMPLATE_ID, 'foo'], [ - 'service-{}-templates'.format(SERVICE_ONE_ID), - 'template-{}-version-None'.format(FAKE_TEMPLATE_ID), 'template-{}-versions'.format(FAKE_TEMPLATE_ID), + 'template-{}-version-None'.format(FAKE_TEMPLATE_ID), + 'service-{}-templates'.format(SERVICE_ONE_ID), ]), ('update_service_template_postage', [SERVICE_ONE_ID, FAKE_TEMPLATE_ID, 'first'], [ - 'service-{}-templates'.format(SERVICE_ONE_ID), - 'template-{}-version-None'.format(FAKE_TEMPLATE_ID), 'template-{}-versions'.format(FAKE_TEMPLATE_ID), + 'template-{}-version-None'.format(FAKE_TEMPLATE_ID), + 'service-{}-templates'.format(SERVICE_ONE_ID), ]), ('delete_service_template', [SERVICE_ONE_ID, FAKE_TEMPLATE_ID], [ - 'service-{}-templates'.format(SERVICE_ONE_ID), - 'template-{}-version-None'.format(FAKE_TEMPLATE_ID), 'template-{}-versions'.format(FAKE_TEMPLATE_ID), + 'template-{}-version-None'.format(FAKE_TEMPLATE_ID), + 'service-{}-templates'.format(SERVICE_ONE_ID), ]), ]) def test_deletes_caches_when_modifying_templates( diff --git a/tests/app/notify_client/test_template_folder_client.py b/tests/app/notify_client/test_template_folder_client.py index a679d9e98..f1b8726e1 100644 --- a/tests/app/notify_client/test_template_folder_client.py +++ b/tests/app/notify_client/test_template_folder_client.py @@ -73,13 +73,13 @@ def test_move_templates_and_folders(mocker): }, ) assert mock_redis_delete.call_args_list == [ - call('service-{}-template-folders'.format(some_service_id)), - call('service-{}-templates'.format(some_service_id)), call( 'template-a-version-None', 'template-b-version-None', 'template-c-version-None', ), + call('service-{}-templates'.format(some_service_id)), + call('service-{}-template-folders'.format(some_service_id)), ] diff --git a/tests/app/notify_client/test_user_client.py b/tests/app/notify_client/test_user_client.py index 1ab9389b0..40e84344a 100644 --- a/tests/app/notify_client/test_user_client.py +++ b/tests/app/notify_client/test_user_client.py @@ -234,7 +234,7 @@ def test_add_user_to_service_calls_correct_endpoint_and_deletes_keys_from_cache( mock_post.assert_called_once_with(expected_url, data=data) assert mock_redis_delete.call_args_list == [ - call('service-{service_id}'.format(service_id=service_id)), - call('service-{service_id}-template-folders'.format(service_id=service_id)), call('user-{user_id}'.format(user_id=user_id)), + call('service-{service_id}-template-folders'.format(service_id=service_id)), + call('service-{service_id}'.format(service_id=service_id)), ]