From 5b52b6f9bf33d09b63b618304222724c3e3d8f46 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Tue, 13 Jul 2021 17:06:35 +0100 Subject: [PATCH] Clear user cache when broadcast service settings form is submitted When the broadcast service settings form is submitted it now removes all permissions for users in notifications-api. This means it should be clearing the user cache. --- app/main/views/service_settings.py | 5 ++++- app/notify_client/service_api_client.py | 7 +++++-- tests/app/main/views/test_service_settings.py | 2 ++ .../notify_client/test_service_api_client.py | 19 ++++++++++++++++++- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/main/views/service_settings.py b/app/main/views/service_settings.py index 95a2be0aa..911992d26 100644 --- a/app/main/views/service_settings.py +++ b/app/main/views/service_settings.py @@ -396,11 +396,14 @@ def service_confirm_broadcast_account_type(service_id, account_type): abort(404) if form.validate_on_submit(): + cached_service_user_ids = [user.id for user in current_service.active_users] + service_api_client.set_service_broadcast_settings( current_service.id, service_mode=form.account_type.service_mode, broadcast_channel=form.account_type.broadcast_channel, - provider_restriction=form.account_type.provider_restriction + provider_restriction=form.account_type.provider_restriction, + cached_service_user_ids=cached_service_user_ids ) create_broadcast_account_type_change_event( service_id=current_service.id, diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py index 3be73ad38..f610700a9 100644 --- a/app/notify_client/service_api_client.py +++ b/app/notify_client/service_api_client.py @@ -618,13 +618,16 @@ class ServiceAPIClient(NotifyAdminAPIClient): @cache.delete('service-{service_id}') def set_service_broadcast_settings( - self, service_id, service_mode, broadcast_channel, provider_restriction + self, service_id, service_mode, broadcast_channel, provider_restriction, cached_service_user_ids ): """ service_mode is one of "training" or "live" - broadcast channel is one of "test" or "severe" + broadcast channel is one of "operator", "test", "severe", "government" provider_restriction is one of "all", "three", "o2", "vodafone", "ee" """ + if cached_service_user_ids: + redis_client.delete(*map('user-{}'.format, cached_service_user_ids)) + data = { "service_mode": service_mode, "broadcast_channel": broadcast_channel, diff --git a/tests/app/main/views/test_service_settings.py b/tests/app/main/views/test_service_settings.py index 48e092be6..9ed5729e6 100644 --- a/tests/app/main/views/test_service_settings.py +++ b/tests/app/main/views/test_service_settings.py @@ -5883,6 +5883,7 @@ def test_service_confirm_broadcast_account_type_posts_data_to_api_and_redirects( broadcast_channel, allowed_broadcast_provider, fake_uuid, + mock_get_users_by_service, ): set_service_broadcast_settings_mock = mocker.patch('app.service_api_client.set_service_broadcast_settings') mock_event_handler = mocker.patch('app.main.views.service_settings.create_broadcast_account_type_change_event') @@ -5901,6 +5902,7 @@ def test_service_confirm_broadcast_account_type_posts_data_to_api_and_redirects( service_mode=service_mode, broadcast_channel=broadcast_channel, provider_restriction=allowed_broadcast_provider, + cached_service_user_ids=[fake_uuid] ) mock_event_handler.assert_called_once_with( service_id=SERVICE_ONE_ID, diff --git a/tests/app/notify_client/test_service_api_client.py b/tests/app/notify_client/test_service_api_client.py index eae5a3ee5..6a058c1eb 100644 --- a/tests/app/notify_client/test_service_api_client.py +++ b/tests/app/notify_client/test_service_api_client.py @@ -404,7 +404,7 @@ def test_returns_value_from_cache( (service_api_client, 'delete_sms_sender', [SERVICE_ONE_ID, ''], {}), (service_api_client, 'update_service_callback_api', [SERVICE_ONE_ID] + [''] * 4, {}), (service_api_client, 'create_service_callback_api', [SERVICE_ONE_ID] + [''] * 3, {}), - (service_api_client, 'set_service_broadcast_settings', [SERVICE_ONE_ID, 'training', 'severe', None], {}), + (service_api_client, 'set_service_broadcast_settings', [SERVICE_ONE_ID, 'training', 'severe', 'all', []], {}), (user_api_client, 'add_user_to_service', [SERVICE_ONE_ID, uuid4(), [], []], {}), (invite_api_client, 'accept_invite', [SERVICE_ONE_ID, uuid4()], {}), ]) @@ -492,6 +492,23 @@ def test_deletes_cached_users_when_archiving_service(mocker, mock_get_service_te assert call(f'service-{SERVICE_ONE_ID}-template-*') in mock_redis_delete_by_pattern.call_args_list +def test_deletes_cached_users_when_changing_broadcast_service_settings(mocker): + mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete') + + mocker.patch('notifications_python_client.base.BaseAPIClient.request', return_value={'data': ""}) + + service_api_client.set_service_broadcast_settings(SERVICE_ONE_ID, + 'live', + 'severe', + 'all', + ["my-user-id1", "my-user-id2"]) + + assert mock_redis_delete.call_args_list == [ + call('user-my-user-id1', 'user-my-user-id2'), + call(f'service-{SERVICE_ONE_ID}'), + ] + + def test_client_gets_guest_list(mocker): client = ServiceAPIClient() mock_get = mocker.patch.object(client, 'get', return_value=['a', 'b', 'c'])