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

@@ -164,10 +164,17 @@ def test_create_new_organisation_validates(
assert mock_create_organisation.called is False
def test_create_new_organisation_fails_if_new_name_has_less_than_2_alphanumeric_characters(
@pytest.mark.parametrize('name, error_message', [
('', 'Cannot be empty'),
('a', 'at least two alphanumeric characters'),
('a' * 256, 'Organisation name must be 255 characters or fewer'),
])
def test_create_new_organisation_fails_with_incorrect_input(
client_request,
platform_admin_user,
mocker,
name,
error_message,
):
mock_create_organisation = mocker.patch(
'app.organisations_client.create_organisation'
@@ -177,14 +184,14 @@ def test_create_new_organisation_fails_if_new_name_has_less_than_2_alphanumeric_
page = client_request.post(
'.add_organisation',
_data={
'name': ".",
'name': name,
'organisation_type': 'local',
'crown_status': 'non-crown',
},
_expected_status=200,
)
assert mock_create_organisation.called is False
assert page.find("span", {"class": "govuk-error-message"})
assert error_message in page.select_one('.govuk-error-message').text
@pytest.mark.parametrize('organisation_type, organisation, expected_status', (
@@ -1077,20 +1084,27 @@ def test_update_organisation_name(
assert mock_organisation_name_is_unique.called
@pytest.mark.parametrize('name, error_message', [
('', 'Cannot be empty'),
('a', 'at least two alphanumeric characters'),
('a' * 256, 'Organisation name must be 255 characters or fewer'),
])
def test_update_organisation_with_incorrect_input(
platform_admin_client,
organisation_one,
mock_get_organisation,
name,
error_message
):
response = platform_admin_client.post(
url_for('.edit_organisation_name', org_id=organisation_one['id']),
data={'name': ''}
data={'name': name}
)
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert "Cannot be empty" in page.select_one('.govuk-error-message').text
assert error_message in page.select_one('.govuk-error-message').text
def test_update_organisation_with_non_unique_name(

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(

View File

@@ -483,20 +483,27 @@ def test_should_not_hit_api_if_service_name_hasnt_changed(
assert not mock_update_service.called
def test_service_name_change_fails_if_new_name_has_less_than_2_alphanumeric_characters(
@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_service_name_change_fails_if_new_name_fails_validation(
client_request,
mock_update_service,
mock_service_name_is_unique,
name,
error_message,
):
page = client_request.post(
'main.service_name_change',
service_id=SERVICE_ONE_ID,
_data={'name': "."},
_data={'name': name},
_expected_status=200,
)
assert not mock_service_name_is_unique.called
assert not mock_update_service.called
assert page.find("span", {"class": "govuk-error-message"})
assert error_message in page.find("span", {"class": "govuk-error-message"}).text
@pytest.mark.parametrize('user, expected_text, expected_link', [