Skip to content

Commit

Permalink
Fix/tc preview (#1951)
Browse files Browse the repository at this point in the history
* fix(templates): update the preview logic so it correctly translates the template category value and passes it to the API when saving

* fix(tests): update preview tests

* feat(ui tests): add new scenarios to cover preview path

* fix(tests): handle both when `FF_TEMPLATE_CATEGORY` is on and off

* fix(create template): pass the template_category_id when previewing while creating a new template

* task: try to disable xray in the PR review env

* temporary: remove XRAY code to see if review env works

* experiment: try using single quotes to init xray

* task: disable xray in DEV/TEST

* task: remove xray

* task: re-enable xray

* chore: formatting

* task: disable xray again

* task: undo things related to xray. fixed it in another PR (#1952)

* test(template_categories): add scenario for creating + previewing a template before saving

* chore: formatting

* fix(tests): have the ui test remove the template it creates

---------

Co-authored-by: Jumana B <[email protected]>
  • Loading branch information
andrewleith and jzbahrai authored Sep 23, 2024
1 parent 1454a26 commit 9127f01
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
8 changes: 6 additions & 2 deletions app/main/views/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"]

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down
24 changes: 20 additions & 4 deletions tests/app/main/views/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1967,14 +1967,15 @@ 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",
"subject": "test subject",
"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",
Expand All @@ -1996,18 +1997,26 @@ 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",
"subject": "test subject",
"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",
Expand All @@ -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,
)


Expand Down
21 changes: 20 additions & 1 deletion tests_cypress/cypress/Notify/Admin/Pages/TemplatesPage.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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
};
Expand Down
27 changes: 27 additions & 0 deletions tests_cypress/cypress/e2e/admin/template-categories.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe("Create Template", () => {
"Test Subject",
"Test Content",
"Alert",
"__use_tc",
TemplatesPage.CONSTANTS.USE_CATEGORY_PRIORITY,
);
TemplatesPage.SaveTemplate();

Expand Down

0 comments on commit 9127f01

Please sign in to comment.