Add form validation for max service and org name

There was a recent error in the logs because a service tried to change
its name to one exceeding 255 characters (which is a limit on the
database field). We can easily catch these errors on the form, so that
the user doesn't see an error page.
This commit is contained in:
Katie Smith
2020-11-30 17:08:40 +00:00
parent 4e96f82c67
commit 57189f57e4
4 changed files with 45 additions and 28 deletions

View File

@@ -288,30 +288,23 @@ def test_should_add_service_and_redirect_to_dashboard_when_existing_service(
assert session['service_id'] == 101
def test_should_return_form_errors_when_service_name_is_empty(
@pytest.mark.parametrize('name, error_message', [
('', 'Cannot be empty'),
('.', 'Must include at least two alphanumeric characters'),
('a' * 256, 'Service name must be 255 characters or fewer'),
])
def test_add_service_fails_if_service_name_fails_validation(
client_request,
mock_get_organisation_by_domain,
name,
error_message,
):
page = client_request.post(
'main.add_service',
_data={},
_data={"name": name},
_expected_status=200,
)
assert 'Cannot be empty' in page.text
def test_add_service_fails_if_service_name_has_less_than_2_alphanumeric_characters(
client_request,
mock_get_organisation_by_domain,
):
page = client_request.post(
'main.add_service',
_data={"name": "."},
_expected_status=200,
)
error_message = page.find("span", {"class": "govuk-error-message"}).text
assert 'Must include at least two alphanumeric characters' in error_message
assert error_message in page.find("span", {"class": "govuk-error-message"}).text
def test_should_return_form_errors_with_duplicate_service_name_regardless_of_case(