From d438109630c15da173d6bc525771ac5cc75773b0 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 5 Oct 2017 12:06:40 +0100 Subject: [PATCH] Add page to change organisation type If a user picks the wrong thing, we should be able to override it when they go live. --- app/main/forms.py | 26 ++++--- app/main/views/service_settings.py | 23 ++++++ app/templates/views/service-settings.html | 2 +- .../set-organisation-type.html | 22 ++++++ tests/app/main/views/test_service_settings.py | 72 ++++++++++++++++++- 5 files changed, 133 insertions(+), 12 deletions(-) create mode 100644 app/templates/views/service-settings/set-organisation-type.html diff --git a/app/main/forms.py b/app/main/forms.py index 803698290..5b5273b99 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -141,6 +141,18 @@ def sms_code(): message='Code not found')]) +def organisation_type(): + return RadioField( + 'Who runs this service?', + choices=[ + ('central', 'Central government'), + ('local', 'Local government'), + ('nhs', 'NHS'), + ], + validators=[DataRequired()], + ) + + class LoginForm(Form): email_address = StringField('Email address', validators=[ Length(min=5, max=255), @@ -227,15 +239,11 @@ class CreateServiceForm(Form): validators=[ DataRequired(message='Can’t be empty') ]) - organisation_type = RadioField( - 'Who runs this service?', - choices=[ - ('central', 'Central government'), - ('local', 'Local government'), - ('nhs', 'NHS'), - ], - validators=[DataRequired()], - ) + organisation_type = organisation_type() + + +class OrganisationTypeForm(Form): + organisation_type = organisation_type() class ConfirmPasswordForm(Form): diff --git a/app/main/views/service_settings.py b/app/main/views/service_settings.py index 171bc8cec..c4c788920 100644 --- a/app/main/views/service_settings.py +++ b/app/main/views/service_settings.py @@ -34,6 +34,7 @@ from app.main.forms import ( LetterBranding, ServiceInboundApiForm, InternationalSMSForm, + OrganisationTypeForm, ) from app import user_api_client, current_service, organisations_client, inbound_number_client from notifications_utils.formatters import formatted_list @@ -574,6 +575,28 @@ def service_set_letter_contact_block(service_id): ) +@main.route("/services//service-settings/set-organisation-type", methods=['GET', 'POST']) +@login_required +@user_has_permissions(admin_override=True) +def set_organisation_type(service_id): + + form = OrganisationTypeForm(organisation_type=current_service.get('organisation_type')) + + if form.validate_on_submit(): + service_api_client.update_service( + service_id, + organisation_type=form.organisation_type.data, + ) + return redirect(url_for('.service_settings', service_id=service_id)) + + form.organisation_type.data = current_service.get('organisation_type') + + return render_template( + 'views/service-settings/set-organisation-type.html', + form=form, + ) + + @main.route("/services//service-settings/set-branding-and-org", methods=['GET', 'POST']) @login_required @user_has_permissions(admin_override=True) diff --git a/app/templates/views/service-settings.html b/app/templates/views/service-settings.html index 6585bd31f..77033ba94 100644 --- a/app/templates/views/service-settings.html +++ b/app/templates/views/service-settings.html @@ -193,7 +193,7 @@ {{ optional_text_field( (current_service.organisation_type or '')|title ) }} - {{ text_field() }} + {{ edit_field('Change', url_for('.set_organisation_type', service_id=current_service.id)) }} {% endcall %} {% call row() %} {{ text_field('Free text message allowance')}} diff --git a/app/templates/views/service-settings/set-organisation-type.html b/app/templates/views/service-settings/set-organisation-type.html new file mode 100644 index 000000000..afa9474ed --- /dev/null +++ b/app/templates/views/service-settings/set-organisation-type.html @@ -0,0 +1,22 @@ +{% extends "withnav_template.html" %} +{% from "components/radios.html" import radios, branding_radios %} +{% from "components/page-footer.html" import page_footer %} + +{% block service_page_title %} + Set branding and organisation +{% endblock %} + +{% block maincolumn_content %} + +

Set organisation type

+
+ {{ radios(form.organisation_type) }} + {{ page_footer( + 'Save', + back_link=url_for('.service_settings', service_id=current_service.id), + back_link_text='Back to settings' + ) }} +
+ + +{% endblock %} diff --git a/tests/app/main/views/test_service_settings.py b/tests/app/main/views/test_service_settings.py index 1efc246cb..d0fa1d07d 100644 --- a/tests/app/main/views/test_service_settings.py +++ b/tests/app/main/views/test_service_settings.py @@ -22,7 +22,7 @@ from tests.conftest import ( get_non_default_reply_to_email_address, get_default_letter_contact_block, get_non_default_letter_contact_block, - SERVICE_ONE_ID + SERVICE_ONE_ID, ) @@ -65,7 +65,7 @@ from tests.conftest import ( 'Send letters Off Change', 'Label Value Action', - 'Organisation type Central', + 'Organisation type Central Change', 'Free text message allowance 250,000', 'Email branding GOV.UK Change', 'Letter branding HM Government Change', @@ -1362,6 +1362,74 @@ def test_should_set_branding_and_organisations( ) +@pytest.mark.parametrize('method', ['get', 'post']) +def test_organisation_type_pages_are_platform_admin_only( + client_request, + method, +): + getattr(client_request, method)( + 'main.set_organisation_type', + service_id=SERVICE_ONE_ID, + _expected_status=403, + _test_page_title=False, + ) + + +def test_should_show_page_to_set_organisation_type( + logged_in_platform_admin_client, +): + response = logged_in_platform_admin_client.get(url_for( + 'main.set_organisation_type', + service_id=SERVICE_ONE_ID + )) + assert response.status_code == 200 + page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') + + labels = page.select('label') + checked_radio_buttons = page.select('input[checked]') + + assert len(checked_radio_buttons) == 1 + assert checked_radio_buttons[0]['value'] == 'central' + + assert len(labels) == 3 + for index, expected in enumerate(( + 'Central government', + 'Local government', + 'NHS', + )): + assert normalize_spaces(labels[index].text) == expected + + +@pytest.mark.parametrize('organisation_type', [ + 'central', + 'local', + 'nhs', + pytest.mark.xfail('private sector'), +]) +def test_should_set_organisaton_type( + logged_in_platform_admin_client, + mock_update_service, + organisation_type, +): + response = logged_in_platform_admin_client.post( + url_for( + 'main.set_organisation_type', + service_id=SERVICE_ONE_ID, + ), + data={ + 'organisation_type': organisation_type, + 'organisation': 'organisation-id' + }, + ) + assert response.status_code == 302 + assert response.location == url_for('main.service_settings', service_id=SERVICE_ONE_ID, _external=True) + + mock_update_service.assert_called_once_with( + SERVICE_ONE_ID, + organisation_type=organisation_type, + ) + + def test_switch_service_enable_letters( logged_in_platform_admin_client, service_one,