diff --git a/app/main/forms.py b/app/main/forms.py index 726195f49..53ee5bdfe 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -1106,7 +1106,8 @@ class RenameServiceForm(StripWhitespaceForm): u'Service name', validators=[ DataRequired(message='Cannot be empty'), - MustContainAlphanumericCharacters() + MustContainAlphanumericCharacters(), + Length(max=255, message='Service name must be 255 characters or fewer') ]) @@ -1115,7 +1116,8 @@ class RenameOrganisationForm(StripWhitespaceForm): u'Organisation name', validators=[ DataRequired(message='Cannot be empty'), - MustContainAlphanumericCharacters() + MustContainAlphanumericCharacters(), + Length(max=255, message='Organisation name must be 255 characters or fewer') ]) @@ -1223,7 +1225,8 @@ class CreateServiceForm(StripWhitespaceForm): "What’s your service called?", validators=[ DataRequired(message='Cannot be empty'), - MustContainAlphanumericCharacters() + MustContainAlphanumericCharacters(), + Length(max=255, message='Service name must be 255 characters or fewer') ]) organisation_type = OrganisationTypeField('Who runs this service?') diff --git a/tests/app/main/views/organisations/test_organisation.py b/tests/app/main/views/organisations/test_organisation.py index 7d3e12de6..670dee96d 100644 --- a/tests/app/main/views/organisations/test_organisation.py +++ b/tests/app/main/views/organisations/test_organisation.py @@ -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( diff --git a/tests/app/main/views/test_add_service.py b/tests/app/main/views/test_add_service.py index 36c696a16..5c4e24e25 100644 --- a/tests/app/main/views/test_add_service.py +++ b/tests/app/main/views/test_add_service.py @@ -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( diff --git a/tests/app/main/views/test_service_settings.py b/tests/app/main/views/test_service_settings.py index 775c4b37c..a9e5e66d1 100644 --- a/tests/app/main/views/test_service_settings.py +++ b/tests/app/main/views/test_service_settings.py @@ -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', [