From 90d4fcaa52f2dbc8d0f7dad3fc8e4e6d88ae008d Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 7 Oct 2016 14:52:19 +0100 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20let=20people=20create=20a=20nor?= =?UTF-8?q?mal=20key=20in=20trial=20mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit You can’t properly use a normal key when your service is in trial mode. It’s theoretically useful to create a live key in preparation for going live. This utitily outweighs the confusion it causes for people creating their first keys in trial mode. We should just remove the confusing option. --- app/main/views/api_keys.py | 9 ++++----- tests/app/main/views/test_api_keys.py | 29 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/main/views/api_keys.py b/app/main/views/api_keys.py index b8a8b813f..2a8856c79 100644 --- a/app/main/views/api_keys.py +++ b/app/main/views/api_keys.py @@ -68,13 +68,12 @@ def create_api_key(service_id): key['name'] for key in api_key_api_client.get_api_keys(service_id=service_id)['apiKeys'] ] form = CreateKeyForm(key_names) - form.key_type.choices = [ - (KEY_TYPE_NORMAL, 'Send messages to anyone{}'.format( - ', once this service is not in trial mode' if current_service['restricted'] else '' - )), + form.key_type.choices = filter(None, [ + (KEY_TYPE_NORMAL, 'Send messages to anyone{}') + if not current_service['restricted'] else None, (KEY_TYPE_TEST, 'Simulate sending messages to anyone'), (KEY_TYPE_TEAM, 'Only send messages to your team or whitelist') - ] + ]) if form.validate_on_submit(): secret = api_key_api_client.create_api_key( service_id=service_id, diff --git a/tests/app/main/views/test_api_keys.py b/tests/app/main/views/test_api_keys.py index 5cb3b8b3b..84789ac61 100644 --- a/tests/app/main/views/test_api_keys.py +++ b/tests/app/main/views/test_api_keys.py @@ -147,7 +147,7 @@ def test_should_create_api_key_with_type_normal(app_, api_user_active, mock_login, mock_get_api_keys, - mock_get_service, + mock_get_live_service, mock_has_permissions, fake_uuid, mocker): @@ -173,6 +173,33 @@ def test_should_create_api_key_with_type_normal(app_, }) +def test_cant_create_normal_api_key_in_trial_mode( + client, + api_user_active, + mock_login, + mock_get_api_keys, + mock_get_service, + mock_has_permissions, + fake_uuid, + mocker +): + mock_post = mocker.patch('app.notify_client.api_key_api_client.ApiKeyApiClient.post') + + client.login(api_user_active) + response = client.post( + url_for('main.create_api_key', service_id=uuid.uuid4()), + data={ + 'key_name': 'some default key name', + 'key_type': 'normal' + } + ) + assert response.status_code == 200 + page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') + assert page.find('span', {'class': 'error-message'}).text.strip() == 'Not a valid choice' + + mock_post.assert_not_called() + + def test_should_show_confirm_revoke_api_key(app_, api_user_active, mock_login,