diff --git a/app/main/forms.py b/app/main/forms.py index 8a4f09e2c..a013733a4 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -1997,12 +1997,8 @@ class TemplateAndFoldersSelectionForm(Form): ] self.add_template_by_template_type.choices = list(filter(None, [ - # We want to show email and text message to everyone, - # whether or not the service has them switched on. The - # option to add letter or broadcast templates should only - # be shown to services which have that permission - ('email', 'Email'), - ('sms', 'Text message'), + ('email', 'Email') if 'email' in available_template_types else None, + ('sms', 'Text message') if 'sms' in available_template_types else None, ('letter', 'Letter') if 'letter' in available_template_types else None, ('broadcast', 'Broadcast') if 'broadcast' in available_template_types else None, ('copy-existing', 'Copy an existing template') if allow_adding_copy_of_template else None, diff --git a/tests/app/main/views/test_templates.py b/tests/app/main/views/test_templates.py index e67ffa759..aa2b4ad62 100644 --- a/tests/app/main/views/test_templates.py +++ b/tests/app/main/views/test_templates.py @@ -287,8 +287,8 @@ def test_should_show_live_search_if_service_has_lots_of_folders( assert count_of_templates == 4 -@pytest.mark.parametrize('extra_permissions, expected_values, expected_labels', ( - pytest.param([], [ +@pytest.mark.parametrize('service_permissions, expected_values, expected_labels', ( + pytest.param(['email', 'sms'], [ 'email', 'sms', 'copy-existing', @@ -297,7 +297,12 @@ def test_should_show_live_search_if_service_has_lots_of_folders( 'Text message', 'Copy an existing template', ]), - pytest.param(['letter'], [ + pytest.param(['broadcast'], [ + 'broadcast', + ], [ + 'Broadcast', + ]), + pytest.param(['email', 'sms', 'letter'], [ 'email', 'sms', 'letter', @@ -314,11 +319,11 @@ def test_should_show_new_template_choices_if_service_has_folder_permission( service_one, mock_get_service_templates, mock_get_template_folders, - extra_permissions, + service_permissions, expected_values, expected_labels, ): - service_one['permissions'] += extra_permissions + service_one['permissions'] = service_permissions page = client_request.get( 'main.choose_template', @@ -339,6 +344,39 @@ def test_should_show_new_template_choices_if_service_has_folder_permission( ] == expected_labels +@pytest.mark.parametrize("permissions,are_data_attrs_added", [ + (['sms'], True), + (['email'], True), + (['letter'], True), + (['broadcast'], True), + (['sms', 'email'], False), +]) +def test_should_add_data_attributes_for_services_that_only_allow_one_type_of_notifications( + client_request, + service_one, + mock_get_service_templates, + mock_get_template_folders, + permissions, + are_data_attrs_added +): + service_one['permissions'] = permissions + + page = client_request.get( + 'main.choose_template', + service_id=SERVICE_ONE_ID, + ) + + if not page.select('#add_new_template_form'): + raise ElementNotFound() + + if are_data_attrs_added: + assert page.find(id='add_new_template_form').attrs['data-channel'] == permissions[0] + assert page.find(id='add_new_template_form').attrs['data-service'] == SERVICE_ONE_ID + else: + assert page.find(id='add_new_template_form').attrs.get('data-channel') is None + assert page.find(id='add_new_template_form').attrs.get('data-service') is None + + def test_should_show_page_for_one_template( client_request, mock_get_service_template,