From 7fcd56dc022d76c8b55888850823532f87ee99a5 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 29 Jun 2016 17:10:49 +0100 Subject: [PATCH] Add radio buttons for choosing the API key type Best-guess wording for what the labels and question should be. Adds a macro for rendering radio buttons from a WTForms field. --- app/main/forms.py | 15 +++++++++++++- app/main/views/api_keys.py | 2 +- app/templates/components/radios.html | 23 ++++++++++++++++++++++ app/templates/views/api-keys/create.html | 4 +++- tests/app/main/test_create_api_key_form.py | 20 ++++++++++++++++++- tests/app/main/views/test_api_keys.py | 9 +++++++-- 6 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 app/templates/components/radios.html diff --git a/app/main/forms.py b/app/main/forms.py index 31ff09737..ec78e6cae 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -12,7 +12,8 @@ from wtforms import ( FileField, BooleanField, HiddenField, - IntegerField + IntegerField, + RadioField ) from wtforms.fields.html5 import EmailField, TelField from wtforms.validators import (DataRequired, Email, Length, Regexp) @@ -291,6 +292,18 @@ class CreateKeyForm(Form): self.existing_key_names = [x.lower() for x in existing_key_names] super(CreateKeyForm, self).__init__(*args, **kwargs) + key_type = RadioField( + 'What should Notify do when you use this key?', + choices=[ + ('normal', 'Send messages to anyone'), + ('test', 'Simulate sending messages to anyone'), + ('team', 'Only send messages to members of your team') + ], + validators=[ + DataRequired() + ] + ) + key_name = StringField(u'Description of key', validators=[ DataRequired(message='You need to give the key a name') ]) diff --git a/app/main/views/api_keys.py b/app/main/views/api_keys.py index aad602179..cd35e8964 100644 --- a/app/main/views/api_keys.py +++ b/app/main/views/api_keys.py @@ -30,7 +30,7 @@ def create_api_key(service_id): key_name=form.key_name.data) return render_template( 'views/api-keys/create.html', - key_name=form.key_name + form=form ) diff --git a/app/templates/components/radios.html b/app/templates/components/radios.html new file mode 100644 index 000000000..88beb1220 --- /dev/null +++ b/app/templates/components/radios.html @@ -0,0 +1,23 @@ +{% macro radios( + field, + hint=None +) %} +
+
+ + {{ field.label }} + {% if field.errors %} + + {{ field.errors[0] }} + + {% endif %} + + {% for option in field %} + + {% endfor %} +
+
+{% endmacro %} diff --git a/app/templates/views/api-keys/create.html b/app/templates/views/api-keys/create.html index 6b4a0d97d..474475618 100644 --- a/app/templates/views/api-keys/create.html +++ b/app/templates/views/api-keys/create.html @@ -1,6 +1,7 @@ {% extends "withnav_template.html" %} {% from "components/page-footer.html" import page_footer %} {% from "components/textbox.html" import textbox %} +{% from "components/radios.html" import radios %} {% block page_title %} Add a new API key – GOV.UK Notify @@ -13,7 +14,8 @@
- {{ textbox(key_name, hint='eg CRM application') }} + {{ radios(form.key_type) }} + {{ textbox(form.key_name, label='Name for this key') }} {{ page_footer('Continue') }}
diff --git a/tests/app/main/test_create_api_key_form.py b/tests/app/main/test_create_api_key_form.py index c13fe9fe3..498edc504 100644 --- a/tests/app/main/test_create_api_key_form.py +++ b/tests/app/main/test_create_api_key_form.py @@ -1,3 +1,5 @@ +import pytest + from werkzeug.datastructures import MultiDict from app.main.forms import CreateKeyForm @@ -11,4 +13,20 @@ def test_return_validation_error_when_key_name_exists(app_): form = CreateKeyForm(_get_names(), formdata=MultiDict([('key_name', 'Some key')])) form.validate() - assert {'key_name': ['A key with this name already exists']} == form.errors + assert form.errors['key_name'] == ['A key with this name already exists'] + + +@pytest.mark.parametrize( + 'key_type, expected_error', [ + ('', 'This field is required.'), + ('invalid', 'Not a valid choice') + ] +) +def test_return_validation_error_when_key_type_not_chosen(app_, key_type, expected_error): + + with app_.test_request_context(): + form = CreateKeyForm( + [], + formdata=MultiDict([('key_name', 'Some key'), ('key_type', key_type)])) + form.validate() + assert form.errors['key_type'] == [expected_error] diff --git a/tests/app/main/views/test_api_keys.py b/tests/app/main/views/test_api_keys.py index a1807a380..d7c51fcfc 100644 --- a/tests/app/main/views/test_api_keys.py +++ b/tests/app/main/views/test_api_keys.py @@ -72,8 +72,13 @@ def test_should_create_api_key_with_type_normal(app_, with app_.test_request_context(), app_.test_client() as client: client.login(api_user_active) - response = client.post(url_for('main.create_api_key', service_id=service_id), - data={'key_name': 'some default key name'}) + response = client.post( + url_for('main.create_api_key', service_id=service_id), + data={ + 'key_name': 'some default key name', + 'key_type': 'normal' + } + ) assert response.status_code == 200 assert 'some default key name' in response.get_data(as_text=True)