diff --git a/app/main/views/api_keys.py b/app/main/views/api_keys.py index 95048eabf..8dbe00334 100644 --- a/app/main/views/api_keys.py +++ b/app/main/views/api_keys.py @@ -80,7 +80,6 @@ def whitelist(service_id): def api_keys(service_id): return render_template( 'views/api/keys.html', - keys=api_key_api_client.get_api_keys(service_id=service_id)['apiKeys'] ) @@ -88,10 +87,7 @@ def api_keys(service_id): @login_required @user_has_permissions('manage_api_keys', restrict_admin_usage=True) def create_api_key(service_id): - key_names = [ - key['name'] for key in api_key_api_client.get_api_keys(service_id=service_id)['apiKeys'] - ] - form = CreateKeyForm(key_names) + form = CreateKeyForm(current_service.api_key_names) form.key_type.choices = [ (KEY_TYPE_NORMAL, 'Live – sends to anyone'), (KEY_TYPE_TEAM, 'Team and whitelist – limits who you can send to'), @@ -137,7 +133,6 @@ def revoke_api_key(service_id, key_id): return render_template( 'views/api/keys.html', revoke_key=key_name, - keys=api_key_api_client.get_api_keys(service_id=service_id)['apiKeys'], ) elif request.method == 'POST': api_key_api_client.revoke_api_key(service_id=service_id, key_id=key_id) diff --git a/app/models/service.py b/app/models/service.py index 7780347f4..6cf3a28f2 100644 --- a/app/models/service.py +++ b/app/models/service.py @@ -1,6 +1,7 @@ from notifications_utils.field import Field from werkzeug.utils import cached_property +from app.notify_client.api_key_api_client import api_key_api_client from app.notify_client.billing_api_client import billing_api_client from app.notify_client.email_branding_client import email_branding_client from app.notify_client.inbound_number_client import inbound_number_client @@ -335,3 +336,11 @@ class Service(): template_ids=ids_to_move & self.all_template_ids, folder_ids=ids_to_move & self.all_template_folder_ids, ) + + @cached_property + def api_keys(self): + return api_key_api_client.get_api_keys(self.id)['apiKeys'] + + @property + def api_key_names(self): + return [key['name'] for key in self.api_keys] diff --git a/app/templates/views/api/keys.html b/app/templates/views/api/keys.html index 7e53cd4ab..5bbfc0e28 100644 --- a/app/templates/views/api/keys.html +++ b/app/templates/views/api/keys.html @@ -37,7 +37,7 @@
{% call(item, row_number) list_table( - keys, + current_service.api_keys, empty_message="You haven’t created any API keys yet", caption="API keys", caption_visible=false, diff --git a/tests/app/main/views/test_api_integration.py b/tests/app/main/views/test_api_integration.py index e393e3c44..94223c825 100644 --- a/tests/app/main/views/test_api_integration.py +++ b/tests/app/main/views/test_api_integration.py @@ -186,7 +186,7 @@ def test_should_show_empty_api_keys_page( assert response.status_code == 200 assert 'You haven’t created any API keys yet' in response.get_data(as_text=True) assert 'Create an API key' in response.get_data(as_text=True) - mock_get_no_api_keys.assert_called_once_with(service_id=service_id) + mock_get_no_api_keys.assert_called_once_with(service_id) def test_should_show_api_keys_page( @@ -205,7 +205,7 @@ def test_should_show_api_keys_page( assert 'some key name' in resp_data assert 'another key name' in resp_data assert 'Revoked 1 January at 1:00am' in resp_data - mock_get_api_keys.assert_called_once_with(service_id=fake_uuid) + mock_get_api_keys.assert_called_once_with(fake_uuid) @pytest.mark.parametrize('service_mock, expected_options', [ @@ -323,7 +323,7 @@ def test_should_show_confirm_revoke_api_key( service_id='596364a0-858e-42c8-9062-a8fe822260eb', ), call( - service_id='596364a0-858e-42c8-9062-a8fe822260eb' + '596364a0-858e-42c8-9062-a8fe822260eb' ), ]