From 36c1ffa7be8cf79d7b8b53f7007943fc2f0c549a Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Tue, 11 Aug 2020 15:44:17 +0100 Subject: [PATCH] New template button takes user to new template page for all services that only allow sending one type of notifications --- app/assets/javascripts/templateFolderForm.js | 11 +++--- app/main/views/templates.py | 7 ++++ app/templates/views/templates/_move_to.html | 2 +- app/utils.py | 2 + tests/javascripts/templateFolderForm.test.js | 39 ++++++++++++++++++-- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/templateFolderForm.js b/app/assets/javascripts/templateFolderForm.js index a9f2a4bfd..c7d804f4e 100644 --- a/app/assets/javascripts/templateFolderForm.js +++ b/app/assets/javascripts/templateFolderForm.js @@ -153,19 +153,20 @@ return changed; }; - this.$broadcastService = (document.querySelector('div[id=add_new_template_form]')).getAttribute("data-broadcast") + this.$singleNotificationChannel = (document.querySelector('div[id=add_new_template_form]')).getAttribute("data-channel"); + this.$singleChannelService = (document.querySelector('div[id=add_new_template_form]')).getAttribute("data-service"); this.actionButtonClicked = function(event) { event.preventDefault(); this.currentState = $(event.currentTarget).val(); - if (event.currentTarget.value === 'add-new-template' && this.$broadcastService) { - return window.location = "/services/" + this.$broadcastService + "/templates/add-broadcast"; + if (event.currentTarget.value === 'add-new-template' && this.$singleNotificationChannel) { + window.location = "/services/" + this.$singleChannelService + "/templates/add-" + this.$singleNotificationChannel; } else { if (this.stateChanged()) { this.render(); - }; - }; + } + } }; this.selectionStatus = { diff --git a/app/main/views/templates.py b/app/main/views/templates.py index fcd394f4e..384abda56 100644 --- a/app/main/views/templates.py +++ b/app/main/views/templates.py @@ -31,6 +31,7 @@ from app.models.service import Service from app.models.template_list import TemplateList, TemplateLists from app.template_previews import TemplatePreview, get_page_count_for_letter from app.utils import ( + NOTIFICATION_TYPES, get_template, should_skip_template_page, user_has_permissions, @@ -130,6 +131,11 @@ def choose_template(service_id, template_type='all', template_folder_id=None): ) option_hints = {template_folder_id: 'current folder'} + single_notification_channel = None + notification_channels = list(set(current_service.permissions).intersection(NOTIFICATION_TYPES)) + if len(notification_channels) == 1: + single_notification_channel = notification_channels[0] + if request.method == 'POST' and templates_and_folders_form.validate_on_submit(): if not current_user.has_permissions('manage_templates'): abort(403) @@ -168,6 +174,7 @@ def choose_template(service_id, template_type='all', template_folder_id=None): templates_and_folders_form=templates_and_folders_form, move_to_children=templates_and_folders_form.move_to.children(), user_has_template_folder_permission=user_has_template_folder_permission, + single_notification_channel=single_notification_channel, option_hints=option_hints ) diff --git a/app/templates/views/templates/_move_to.html b/app/templates/views/templates/_move_to.html index 09a69e186..103054d69 100644 --- a/app/templates/views/templates/_move_to.html +++ b/app/templates/views/templates/_move_to.html @@ -28,7 +28,7 @@ {{ page_footer('Add new folder', button_name='operation', button_value='add-new-folder') }} -
+
{{ radios(templates_and_folders_form.add_template_by_template_type) }}
diff --git a/app/utils.py b/app/utils.py index 39ec1f771..c22b4c4a8 100644 --- a/app/utils.py +++ b/app/utils.py @@ -60,6 +60,8 @@ FAILURE_STATUSES = ['failed', 'temporary-failure', 'permanent-failure', 'technical-failure', 'virus-scan-failed', 'validation-failed'] REQUESTED_STATUSES = SENDING_STATUSES + DELIVERED_STATUSES + FAILURE_STATUSES +NOTIFICATION_TYPES = ["sms", "email", "letter", "broadcast"] + with open('{}/email_domains.txt'.format( os.path.dirname(os.path.realpath(__file__)) diff --git a/tests/javascripts/templateFolderForm.test.js b/tests/javascripts/templateFolderForm.test.js index cf880c4bb..0e6612374 100644 --- a/tests/javascripts/templateFolderForm.test.js +++ b/tests/javascripts/templateFolderForm.test.js @@ -1,6 +1,6 @@ const helpers = require('./support/helpers'); -function setFixtures (hierarchy) { +function setFixtures (hierarchy, newTemplateDataModules = "") { const foldersCheckboxesHTML = function (filter) { let count = 0; @@ -27,7 +27,7 @@ function setFixtures (hierarchy) { }(); - function controlsHTML () { + function controlsHTML (newTemplateDataModules) { return `
@@ -78,7 +78,7 @@ function setFixtures (hierarchy) {
-
+
@@ -127,7 +127,7 @@ function setFixtures (hierarchy) { document.body.innerHTML = `
${helpers.templatesAndFoldersCheckboxes(hierarchy)} - ${controlsHTML()} + ${controlsHTML(newTemplateDataModules)}
`; }; @@ -309,6 +309,37 @@ describe('TemplateFolderForm', () => { }); + describe("Click 'New template' for single channel service", () => { + test("should redirect to new template page", () => { + setFixtures(hierarchy, "data-channel='sms' data-service='123'") + templateFolderForm = document.querySelector('form[data-module=template-folder-form]'); + + // start module + window.GOVUK.modules.start(); + + formControls = templateFolderForm.querySelector('#sticky_template_forms'); + + // reset sticky JS mocks called when the module starts + resetStickyMocks(); + // add listener for url change + const descriptor1 = Object.getOwnPropertyDescriptor(window, 'location'); + delete window.location + + const mockCallback = jest.fn(x => {}); + + Object.defineProperty(window, 'location', { + set: mockCallback + }); + // click + helpers.triggerEvent(formControls.querySelector('[value=add-new-template]'), 'click'); + // expect url to change + expect(mockCallback).toHaveBeenCalledWith("/services/123/templates/add-sms") + + setFixtures(hierarchy) + resetStickyMocks() + }); + }) + describe("Clicking 'New template'", () => { beforeEach(() => {