diff --git a/app/main/views/templates.py b/app/main/views/templates.py index b5b261a97..1e81ad8f8 100644 --- a/app/main/views/templates.py +++ b/app/main/views/templates.py @@ -218,6 +218,7 @@ def add_template_by_type(service_id, template_folder_id=None): service_id, 'Main heading', 'normal', + template_folder_id ) return redirect(url_for( '.view_template', @@ -351,7 +352,8 @@ def add_service_template(service_id, template_type, template_folder_id=None): form.template_content.data, service_id, form.subject.data if hasattr(form, 'subject') else None, - form.process_type.data + form.process_type.data, + template_folder_id ) except HTTPError as e: if ( diff --git a/app/models/service.py b/app/models/service.py index 097e25579..d4aa8270b 100644 --- a/app/models/service.py +++ b/app/models/service.py @@ -103,11 +103,10 @@ class Service(): def get_templates(self, template_type='all', template_folder_id=None): if isinstance(template_type, str): template_type = [template_type] - return [ template for template in self.all_templates if (set(template_type) & {'all', template['template_type']}) - and template.get('folder_id') == template_folder_id + and template.get('folder') == template_folder_id ] @property diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py index 78d03a563..63a5e1e9f 100644 --- a/app/notify_client/service_api_client.py +++ b/app/notify_client/service_api_client.py @@ -126,7 +126,8 @@ class ServiceAPIClient(NotifyAdminAPIClient): return self.delete(endpoint, data) @cache.delete('service-{service_id}-templates') - def create_service_template(self, name, type_, content, service_id, subject=None, process_type='normal'): + def create_service_template(self, name, type_, content, service_id, subject=None, process_type='normal', + parent_folder_id=None): """ Create a service template. """ @@ -135,12 +136,16 @@ class ServiceAPIClient(NotifyAdminAPIClient): "template_type": type_, "content": content, "service": service_id, - "process_type": process_type + "process_type": process_type, } if subject: data.update({ 'subject': subject }) + if parent_folder_id: + data.update({ + 'parent_folder_id': parent_folder_id + }) data = _attach_current_user(data) endpoint = "/service/{0}/template".format(service_id) return self.post(endpoint, data) diff --git a/tests/app/main/views/test_template_folders.py b/tests/app/main/views/test_template_folders.py index 2e6479d0d..2b58fa9a8 100644 --- a/tests/app/main/views/test_template_folders.py +++ b/tests/app/main/views/test_template_folders.py @@ -164,3 +164,47 @@ def test_should_show_templates_folder_page( assert page_links[index].text.strip() == expected_link mock_get_service_templates.assert_called_once_with(SERVICE_ONE_ID) + + +@pytest.mark.parametrize("template_type", ["email", "sms"]) +def test_add_template_by_type_should_redirect_to_add_service_template( + client_request, + service_one, + template_type, + mock_get_service_templates, + mock_get_organisations_and_services_for_user, +): + service_one['permissions'] += ['edit_folders'] + client_request.post( + 'main.add_template_by_type', + service_id=SERVICE_ONE_ID, + template_folder_id=PARENT_FOLDER_ID, + _data={'template_type': template_type}, + _expected_redirect=url_for('main.add_service_template', + service_id=SERVICE_ONE_ID, + template_type=template_type, + template_folder_id=PARENT_FOLDER_ID, + _external=True), + ) + + +def test_add_template_by_type_should_redirect_to_view_template_for_letter( + client_request, + service_one, + mock_get_service_templates, + mock_get_organisations_and_services_for_user, + fake_uuid, + mock_create_service_template +): + service_one['permissions'] += ['edit_folders'] + service_one['permissions'] += ['letter'] + client_request.post( + 'main.add_template_by_type', + service_id=SERVICE_ONE_ID, + template_folder_id=PARENT_FOLDER_ID, + _data={'template_type': 'letter'}, + _expected_redirect=url_for('main.view_template', + service_id=SERVICE_ONE_ID, + template_id='Untitled', + _external=True), + ) diff --git a/tests/app/main/views/test_templates.py b/tests/app/main/views/test_templates.py index 33bcb3cac..f2757366a 100644 --- a/tests/app/main/views/test_templates.py +++ b/tests/app/main/views/test_templates.py @@ -1393,7 +1393,8 @@ def test_should_create_sms_template_without_downgrading_unicode_characters( msg, # content ANY, # service_id ANY, # subject - ANY # process_type + ANY, # process_type + ANY, # parent_folder_id ) assert resp.status_code == 302 diff --git a/tests/conftest.py b/tests/conftest.py index 3574c50f1..8747c7822 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -903,8 +903,8 @@ def mock_get_service_letter_template(mocker, content=None, subject=None): @pytest.fixture(scope='function') def mock_create_service_template(mocker, fake_uuid): - def _create(name, type_, content, service, subject=None, process_type=None): - template = template_json(fake_uuid, name, type_, content, service, process_type) + def _create(name, type_, content, service, subject=None, process_type=None, parent_folder_id=None): + template = template_json(fake_uuid, name, type_, content, service, process_type, parent_folder_id) return {'data': template} return mocker.patch( @@ -925,7 +925,7 @@ def mock_update_service_template(mocker): @pytest.fixture(scope='function') def mock_create_service_template_content_too_big(mocker): - def _create(name, type_, content, service, subject=None, process_type=None): + def _create(name, type_, content, service, subject=None, process_type=None, parent_folder_id=None): json_mock = Mock(return_value={ 'message': {'content': ["Content has a character count greater than the limit of 459"]}, 'result': 'error'