From 2639e2c46f44cfe99922ea8076cd8fb746c0dd12 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 7 Nov 2018 12:05:11 +0000 Subject: [PATCH] Sort API keys alphabetically MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently the order of API keys seems to be non-deterministic: https://github.com/alphagov/notifications-api/blob/d46caa184e3e91903829336788c96ffeb2baa57d/app/dao/api_key_dao.py#L32-L39 Generally we sort things alphabetically, unless there’s a good reason to do otherwise. This commit sorts the API keys alphabetically. --- app/models/service.py | 5 ++++- tests/app/main/views/test_api_integration.py | 21 ++++++++------------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/app/models/service.py b/app/models/service.py index 4b101d706..731b5fda6 100644 --- a/app/models/service.py +++ b/app/models/service.py @@ -340,7 +340,10 @@ class Service(): @cached_property def api_keys(self): - return api_key_api_client.get_api_keys(self.id)['apiKeys'] + return sorted( + api_key_api_client.get_api_keys(self.id)['apiKeys'], + key=lambda key: key['name'].lower(), + ) @property def api_key_names(self): diff --git a/tests/app/main/views/test_api_integration.py b/tests/app/main/views/test_api_integration.py index 665a44016..a0e62694a 100644 --- a/tests/app/main/views/test_api_integration.py +++ b/tests/app/main/views/test_api_integration.py @@ -190,22 +190,17 @@ def test_should_show_empty_api_keys_page( def test_should_show_api_keys_page( - logged_in_client, - api_user_active, - mock_login, + client_request, mock_get_api_keys, - mock_get_service, - mock_has_permissions, - fake_uuid, ): - response = logged_in_client.get(url_for('main.api_keys', service_id=fake_uuid)) + page = client_request.get('main.api_keys', service_id=SERVICE_ONE_ID) + rows = [normalize_spaces(row.text) for row in page.select('main tr')] - assert response.status_code == 200 - resp_data = response.get_data(as_text=True) - 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(fake_uuid) + assert rows[0] == 'API keys Action' + assert rows[1] == 'another key name Revoked 1 January at 1:00am' + assert rows[2] == 'some key name Revoke' + + mock_get_api_keys.assert_called_once_with(SERVICE_ONE_ID) @pytest.mark.parametrize('service_mock, expected_options', [