diff --git a/app/main/views/templates.py b/app/main/views/templates.py index 69e2cda335..415d974dc0 100644 --- a/app/main/views/templates.py +++ b/app/main/views/templates.py @@ -189,7 +189,8 @@ def preview_template(service_id, template_id=None): template["content"], service_id, template["subject"], - template["process_type"], + None if template["process_type"] == TC_PRIORITY_VALUE else template["process_type"], + template["template_category_id"] if current_app.config["FF_TEMPLATE_CATEGORY"] else None, ) else: new_template = service_api_client.create_service_template( @@ -198,8 +199,9 @@ def preview_template(service_id, template_id=None): template["content"], service_id, template["subject"], - template["process_type"], + None if template["process_type"] == TC_PRIORITY_VALUE else template["process_type"], template["folder"], + template["template_category_id"] if current_app.config["FF_TEMPLATE_CATEGORY"] else None, ) template_id = new_template["data"]["id"] @@ -791,6 +793,7 @@ def add_service_template(service_id, template_type, template_folder_id=None): # "id": None, "process_type": form.process_type.data, "folder": template_folder_id, + "template_category_id": form.template_category_id.data if current_app.config["FF_TEMPLATE_CATEGORY"] else None, } set_preview_data(preview_template_data, service_id) return redirect( @@ -953,6 +956,7 @@ def edit_service_template(service_id, template_id): # noqa: C901 TODO: remove t "process_type": form.process_type.data, "reply_to_text": template["reply_to_text"], "folder": template["folder"], + "template_category_id": form.template_category_id.data if current_app.config["FF_TEMPLATE_CATEGORY"] else None, } set_preview_data(new_template_data, service_id, template_id) diff --git a/tests/app/main/views/test_templates.py b/tests/app/main/views/test_templates.py index c4fa5b0b3e..99f790483c 100644 --- a/tests/app/main/views/test_templates.py +++ b/tests/app/main/views/test_templates.py @@ -1967,7 +1967,7 @@ def test_preview_has_correct_back_link( assert page.select(".back-link")[0]["href"] == expected_back_url -def test_preview_should_update_and_redirect_on_save(client_request, mock_update_service_template, fake_uuid, mocker): +def test_preview_should_update_and_redirect_on_save(client_request, mock_update_service_template, fake_uuid, mocker, app_): preview_data = { "name": "test name", "content": "test content", @@ -1975,6 +1975,7 @@ def test_preview_should_update_and_redirect_on_save(client_request, mock_update_ "template_type": "email", "process_type": DEFAULT_PROCESS_TYPE, "id": fake_uuid, + "template_category_id": DEFAULT_TEMPLATE_CATEGORY_LOW if app_.config["FF_TEMPLATE_CATEGORY"] else None, } mocker.patch( "app.main.views.templates.get_preview_data", @@ -1996,11 +1997,18 @@ def test_preview_should_update_and_redirect_on_save(client_request, mock_update_ ), ) mock_update_service_template.assert_called_with( - fake_uuid, "test name", "email", "test content", SERVICE_ONE_ID, "test subject", DEFAULT_PROCESS_TYPE + fake_uuid, + "test name", + "email", + "test content", + SERVICE_ONE_ID, + "test subject", + DEFAULT_PROCESS_TYPE, + DEFAULT_TEMPLATE_CATEGORY_LOW if app_.config["FF_TEMPLATE_CATEGORY"] else None, ) -def test_preview_should_create_and_redirect_on_save(client_request, mock_create_service_template, fake_uuid, mocker): +def test_preview_should_create_and_redirect_on_save(client_request, mock_create_service_template, fake_uuid, mocker, app_): preview_data = { "name": "test name", "content": "test content", @@ -2008,6 +2016,7 @@ def test_preview_should_create_and_redirect_on_save(client_request, mock_create_ "template_type": "email", "process_type": DEFAULT_PROCESS_TYPE, "folder": None, + "template_category_id": DEFAULT_TEMPLATE_CATEGORY_LOW if app_.config["FF_TEMPLATE_CATEGORY"] else None, } mocker.patch( "app.main.views.templates.get_preview_data", @@ -2028,7 +2037,14 @@ def test_preview_should_create_and_redirect_on_save(client_request, mock_create_ ), ) mock_create_service_template.assert_called_with( - "test name", "email", "test content", SERVICE_ONE_ID, "test subject", DEFAULT_PROCESS_TYPE, None + "test name", + "email", + "test content", + SERVICE_ONE_ID, + "test subject", + DEFAULT_PROCESS_TYPE, + None, + DEFAULT_TEMPLATE_CATEGORY_LOW if app_.config["FF_TEMPLATE_CATEGORY"] else None, ) diff --git a/tests_cypress/cypress/Notify/Admin/Pages/TemplatesPage.js b/tests_cypress/cypress/Notify/Admin/Pages/TemplatesPage.js index aacc390d4a..2f3d0bb36b 100644 --- a/tests_cypress/cypress/Notify/Admin/Pages/TemplatesPage.js +++ b/tests_cypress/cypress/Notify/Admin/Pages/TemplatesPage.js @@ -1,3 +1,7 @@ +let CONSTANTS = { + USE_CATEGORY_PRIORITY: "__use_tc", +}; + // Parts of the page a user can interact with let Components = { // Template list page @@ -18,10 +22,12 @@ let Components = { TemplateCategories: () => cy.getByTestId('template-categories'), TemplateCategoryOther: () => cy.get('#template_category_other'), SaveTemplateButton: () => cy.get('button[type="submit"]').first(), + PreviewTemplateButton: () => cy.get('button[type="submit"]').eq(1), SelectedTemplateCategory: () => Components.TemplateCategories().find('input:checked').parent(), SelectedTemplateCategoryCollapsed: () => Components.TemplateCategoryButtonContainer().find('p'), TCExpandBytton: () => cy.getByTestId('tc_expand_button'), TemplatePriority: () => cy.getByTestId('process_type'), + DeleteTemplateButton: () => cy.contains('a', 'Delete this template'), }; // Actions users can take on the page @@ -47,6 +53,10 @@ let Actions = { Components.FlashMessage().should('contain', 'template saved'); } }, + PreviewTemplate: () => { + Components.PreviewTemplateButton().click(); + cy.contains('h1', 'Previewing template').should('be.visible'); + }, CreateTemplate: () => { Components.CreateTemplateButton().click(); cy.contains('h1', 'Will you send the message by email or text?').should('be.visible'); @@ -73,7 +83,9 @@ let Actions = { }, FillTemplateForm: (name, subject, content, category = null, priority = null) => { Components.TemplateName().type(name); - Components.TemplateSubject().type(subject); + if (subject) { + Components.TemplateSubject().type(subject); + } Components.TemplateContent().type(content); if (category) { Actions.SelectTemplateCategory(category); @@ -97,10 +109,17 @@ let Actions = { return cy.url().then((url) => { return url.split("/templates/")[1] }) + }, + DeleteTemplate: () => { + Components.DeleteTemplateButton().click(); + cy.get('.banner-dangerous').contains('Are you sure').should('be.visible'); + cy.get('button[name="delete"]').click(); + cy.url().should('contain', '/templates'); } } let TemplatesPage = { + CONSTANTS, Components, ...Actions }; diff --git a/tests_cypress/cypress/e2e/admin/template-categories.cy.js b/tests_cypress/cypress/e2e/admin/template-categories.cy.js index cb7b5d7609..21dc3939d5 100644 --- a/tests_cypress/cypress/e2e/admin/template-categories.cy.js +++ b/tests_cypress/cypress/e2e/admin/template-categories.cy.js @@ -141,6 +141,33 @@ describe("Template categories", () => { .should("be.visible"); }); + it("Template can be saved after being previewed", () => { + Page.SelectTemplate(template.name); + Page.EditCurrentTemplate(); + Page.ExpandTemplateCategories(); + Page.SelectTemplateCategory(categories.AUTOREPLY); + Page.PreviewTemplate(); + Page.SaveTemplate(); + }); + + it("Template can be created after being previewed", () => { + Page.CreateTemplate(); + Page.SelectTemplateType(template.type); + Page.Continue(); + + const randomString = Math.random().toString(36).substring(2, 15); + const subject = template.type === "email" ? "Subject" : null; + const name = `Testing template ${randomString}`; + Page.FillTemplateForm(name, subject, "content", categories.AUTOREPLY); + Page.PreviewTemplate(); + Page.SaveTemplate(); + + // remove the template + cy.visit(`/services/${config.Services.Cypress}/templates`); + Page.SelectTemplate(name); + Page.DeleteTemplate(); + }); + context("Other/specify", () => { it("Category label must be provided when “other” template category selected in order to save template ", () => { // Start with the authentication category diff --git a/tests_cypress/cypress/e2e/admin/template/create-template.cy.js b/tests_cypress/cypress/e2e/admin/template/create-template.cy.js index 6e33bf0de2..8dd7e54c82 100644 --- a/tests_cypress/cypress/e2e/admin/template/create-template.cy.js +++ b/tests_cypress/cypress/e2e/admin/template/create-template.cy.js @@ -123,7 +123,7 @@ describe("Create Template", () => { "Test Subject", "Test Content", "Alert", - "__use_tc", + TemplatesPage.CONSTANTS.USE_CATEGORY_PRIORITY, ); TemplatesPage.SaveTemplate();