Files
notifications-admin/tests/app/main/test_create_api_key_form.py
Chris Hill-Scott 65bb72ef2f Bump WTForms to 2.3.1
This involves three changes which broke our code.

To validate email addresses, the optional dependency `email-validator`
must be installed<sup>1</sup>. But since we don’t use WTForms’ email
validation, we shouldn’t need to subclass it – it can just be its own
self contained thing. Then we don’t need to add the extra dependency.

When rendering textareas, and extra `\r\n` is inserted at the beginning
<sup>2</sup>. Browsers will strip this when displaying the textbox and
submitting the form, but some of our tests need updating to account for
this.

The error message for when you don’t choose an option from some radio
buttons has now changed. Rather than just accepting WTForms’ new
message, this commit makes the error messages like the examples from
the Design System<sup>3</sup>. By default it will say ‘Select an
option’, but by passing in an extra parameter (`thing`) it can be
customised to be more specific, for example ‘Select a type of
organisation’.

***

1. https://github.com/wtforms/wtforms/pull/429
2. https://github.com/wtforms/wtforms/issues/238
3. https://design-system.service.gov.uk/components/radios/#error-messages
2020-04-23 12:57:10 +01:00

51 lines
1.3 KiB
Python

import pytest
from werkzeug.datastructures import MultiDict
from app.main.forms import CreateKeyForm
@pytest.mark.parametrize('expiry_date, expected_errors', (
(None, ['A key with this name already exists']),
('2001-01-01 01:01:01', None),
))
def test_return_validation_error_when_key_name_exists(
client,
expiry_date,
expected_errors,
):
_existing_keys = [
{
'name': 'some key',
'expiry_date': expiry_date,
},
{
'name': 'another key',
'expiry_date': None,
},
]
form = CreateKeyForm(
_existing_keys,
formdata=MultiDict([('key_name', 'Some key')])
)
form.key_type.choices = [('a', 'a'), ('b', 'b')]
form.validate()
assert form.errors.get('key_name') == expected_errors
@pytest.mark.parametrize(
'key_type, expected_error', [
('', 'Select the type of key'),
('invalid', 'Select the type of key')
]
)
def test_return_validation_error_when_key_type_not_chosen(client, key_type, expected_error):
form = CreateKeyForm(
[],
formdata=MultiDict([('key_name', 'Some key'), ('key_type', key_type)]))
form.key_type.choices = [('a', 'a'), ('b', 'b')]
form.validate()
assert form.errors['key_type'] == [expected_error]