Skip to content

Commit 46940f2

Browse files
CCM-13415: Validate unsupported personalisation in digital templates
1 parent 55ad915 commit 46940f2

File tree

15 files changed

+181
-55
lines changed

15 files changed

+181
-55
lines changed

frontend/src/__tests__/components/forms/EmailTemplateForm/__snapshots__/EmailTemplateForm.test.tsx.snap

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,6 @@ exports[`Client-side validation triggers 1`] = `
237237
<li>
238238
((nhsNumber))
239239
</li>
240-
<li>
241-
((date))
242-
</li>
243240
</ul>
244241
<p>
245242
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -985,9 +982,6 @@ exports[`renders page one error 1`] = `
985982
<li>
986983
((nhsNumber))
987984
</li>
988-
<li>
989-
((date))
990-
</li>
991985
</ul>
992986
<p>
993987
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -1761,9 +1755,6 @@ exports[`renders page with multiple errors 1`] = `
17611755
<li>
17621756
((nhsNumber))
17631757
</li>
1764-
<li>
1765-
((date))
1766-
</li>
17671758
</ul>
17681759
<p>
17691760
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -2464,9 +2455,6 @@ exports[`renders page with preloaded field values 1`] = `
24642455
<li>
24652456
((nhsNumber))
24662457
</li>
2467-
<li>
2468-
((date))
2469-
</li>
24702458
</ul>
24712459
<p>
24722460
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -3160,9 +3148,6 @@ exports[`renders page without back link for initial state with id - edit mode 1`
31603148
<li>
31613149
((nhsNumber))
31623150
</li>
3163-
<li>
3164-
((date))
3165-
</li>
31663151
</ul>
31673152
<p>
31683153
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.

frontend/src/__tests__/components/forms/EmailTemplateForm/server-action.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ describe('CreateEmailTemplate server actions', () => {
9191
});
9292
});
9393

94+
it('create-email-template - should return response when when template message contains unsupported personalisation', async () => {
95+
const response = await processFormActions(
96+
initialState,
97+
getMockFormData({
98+
'form-id': 'create-email-template',
99+
emailTemplateName: 'template-name',
100+
emailTemplateSubjectLine: 'template-subject-line',
101+
emailTemplateMessage: 'a template message containing ((date))',
102+
})
103+
);
104+
105+
expect(response).toEqual({
106+
...initialState,
107+
errorState: {
108+
formErrors: [],
109+
fieldErrors: {
110+
emailTemplateMessage: ['Template message contains invalid personalisation fields'],
111+
},
112+
},
113+
});
114+
});
115+
94116
test('should save the template and redirect', async () => {
95117
saveTemplateMock.mockResolvedValue({
96118
...initialState,

frontend/src/__tests__/components/forms/NhsAppTemplateForm/__snapshots__/NhsAppTemplateForm.test.tsx.snap

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,6 @@ exports[`Client-side validation triggers 1`] = `
228228
<li>
229229
((nhsNumber))
230230
</li>
231-
<li>
232-
((date))
233-
</li>
234231
</ul>
235232
<p>
236233
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -912,9 +909,6 @@ exports[`renders page 1`] = `
912909
<li>
913910
((nhsNumber))
914911
</li>
915-
<li>
916-
((date))
917-
</li>
918912
</ul>
919913
<p>
920914
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -1636,9 +1630,6 @@ exports[`renders page one error 1`] = `
16361630
<li>
16371631
((nhsNumber))
16381632
</li>
1639-
<li>
1640-
((date))
1641-
</li>
16421633
</ul>
16431634
<p>
16441635
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -2374,9 +2365,6 @@ exports[`renders page with multiple errors 1`] = `
23742365
<li>
23752366
((nhsNumber))
23762367
</li>
2377-
<li>
2378-
((date))
2379-
</li>
23802368
</ul>
23812369
<p>
23822370
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -3058,9 +3046,6 @@ exports[`renders page with preloaded field values 1`] = `
30583046
<li>
30593047
((nhsNumber))
30603048
</li>
3061-
<li>
3062-
((date))
3063-
</li>
30643049
</ul>
30653050
<p>
30663051
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -3736,9 +3721,6 @@ exports[`renders page without back link for initial state with id - edit mode 1`
37363721
<li>
37373722
((nhsNumber))
37383723
</li>
3739-
<li>
3740-
((date))
3741-
</li>
37423724
</ul>
37433725
<p>
37443726
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.

frontend/src/__tests__/components/forms/NhsAppTemplateForm/server-action.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,28 @@ describe('CreateNHSAppTemplate server actions', () => {
9494
});
9595
});
9696

97+
it('create-nhs-app-template - should return response when when template message contains unsupported personalisation', async () => {
98+
const response = await processFormActions(
99+
initialState,
100+
getMockFormData({
101+
'form-id': 'create-nhs-app-template',
102+
nhsAppTemplateName: 'template-name',
103+
nhsAppTemplateMessage:
104+
'a template message containing ((date))',
105+
})
106+
);
107+
108+
expect(response).toEqual({
109+
...initialState,
110+
errorState: {
111+
formErrors: [],
112+
fieldErrors: {
113+
nhsAppTemplateMessage: ['Template message contains invalid personalisation fields'],
114+
},
115+
},
116+
});
117+
});
118+
97119
it('create-nhs-app-template - should return response when when template message contains link with angle brackets', async () => {
98120
const response = await processFormActions(
99121
initialState,

frontend/src/__tests__/components/forms/SmsTemplateForm/__snapshots__/SmsTemplateForm.test.tsx.snap

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,6 @@ exports[`CreateSmsTemplate component Client-side validation triggers 1`] = `
290290
<li>
291291
((nhsNumber))
292292
</li>
293-
<li>
294-
((date))
295-
</li>
296293
</ul>
297294
<p>
298295
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -743,9 +740,6 @@ exports[`CreateSmsTemplate component renders page one error 1`] = `
743740
<li>
744741
((nhsNumber))
745742
</li>
746-
<li>
747-
((date))
748-
</li>
749743
</ul>
750744
<p>
751745
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -1160,9 +1154,6 @@ exports[`CreateSmsTemplate component renders page with back link if initial stat
11601154
<li>
11611155
((nhsNumber))
11621156
</li>
1163-
<li>
1164-
((date))
1165-
</li>
11661157
</ul>
11671158
<p>
11681159
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -1627,9 +1618,6 @@ exports[`CreateSmsTemplate component renders page with multiple errors 1`] = `
16271618
<li>
16281619
((nhsNumber))
16291620
</li>
1630-
<li>
1631-
((date))
1632-
</li>
16331621
</ul>
16341622
<p>
16351623
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.
@@ -2038,9 +2026,6 @@ exports[`CreateSmsTemplate component renders page with no back link if initial s
20382026
<li>
20392027
((nhsNumber))
20402028
</li>
2041-
<li>
2042-
((date))
2043-
</li>
20442029
</ul>
20452030
<p>
20462031
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.

frontend/src/__tests__/components/forms/SmsTemplateForm/server-action.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,28 @@ describe('CreateSmsTemplate server actions', () => {
8888
});
8989
});
9090

91+
it('create-sms-template - should return response when when template message contains unsupported personalisation', async () => {
92+
const response = await processFormActions(
93+
initialState,
94+
getMockFormData({
95+
'form-id': 'create-sms-template',
96+
smsTemplateName: 'template-name',
97+
smsTemplateMessage:
98+
'a template message containing ((date))',
99+
})
100+
);
101+
102+
expect(response).toEqual({
103+
...initialState,
104+
errorState: {
105+
formErrors: [],
106+
fieldErrors: {
107+
smsTemplateMessage: ['Template message contains invalid personalisation fields'],
108+
},
109+
},
110+
});
111+
});
112+
91113
test('should save the template and redirect', async () => {
92114
saveTemplateMock.mockResolvedValue({
93115
...initialState,

frontend/src/__tests__/components/molecules/__snapshots__/Personalisation.test.tsx.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ exports[`Personalisation component matches snapshot 1`] = `
6666
<li>
6767
((nhsNumber))
6868
</li>
69-
<li>
70-
((date))
71-
</li>
7269
</ul>
7370
<p>
7471
Make sure your personalisation fields exactly match the PDS personalisation fields. This includes using the correct order of upper and lower case letters.

frontend/src/components/forms/EmailTemplateForm/server-action.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { z } from 'zod';
77
import { createTemplate, saveTemplate } from '@utils/form-actions';
88
import { redirect, RedirectType } from 'next/navigation';
9-
import { MAX_EMAIL_CHARACTER_LENGTH } from '@utils/constants';
9+
import { MAX_EMAIL_CHARACTER_LENGTH, INVALID_PERSONALISATION_FIELDS } from '@utils/constants';
1010
import content from '@content/content';
1111

1212
const {
@@ -30,6 +30,11 @@ export const $EmailTemplateFormSchema = z.object({
3030
})
3131
.refine((templateMessage) => !templateMessage.includes('http://'), {
3232
message: form.emailTemplateMessage.error.insecureLink,
33+
})
34+
.refine((templateMessage) => INVALID_PERSONALISATION_FIELDS.some(
35+
(field) => !templateMessage.includes(`((${field}))`)
36+
), {
37+
message: form.emailTemplateMessage.error.invalidPersonalisation,
3338
}),
3439
});
3540

frontend/src/components/forms/NhsAppTemplateForm/server-action.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { z } from 'zod';
88
import { saveTemplate, createTemplate } from '@utils/form-actions';
99
import { redirect, RedirectType } from 'next/navigation';
10+
import { INVALID_PERSONALISATION_FIELDS } from '@utils/constants';
1011
import content from '@content/content';
1112

1213
const {
@@ -49,7 +50,12 @@ export const $CreateNhsAppTemplateSchema = z.object({
4950
.refine(
5051
(templateMessage) => !hasInvalidCharactersInLinks(templateMessage),
5152
{ message: form.nhsAppTemplateMessage.error.invalidUrlCharacter }
52-
),
53+
)
54+
.refine((templateMessage) => INVALID_PERSONALISATION_FIELDS.some(
55+
(field) => !templateMessage.includes(`((${field}))`)
56+
), {
57+
message: form.nhsAppTemplateMessage.error.invalidPersonalisation,
58+
}),
5359
});
5460

5561
export async function processFormActions(

frontend/src/components/forms/SmsTemplateForm/server-action.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { z } from 'zod';
77
import { saveTemplate, createTemplate } from '@utils/form-actions';
88
import { redirect, RedirectType } from 'next/navigation';
9-
import { MAX_SMS_CHARACTER_LENGTH } from '@utils/constants';
9+
import { MAX_SMS_CHARACTER_LENGTH, INVALID_PERSONALISATION_FIELDS } from '@utils/constants';
1010
import content from '@content/content';
1111

1212
const {
@@ -27,6 +27,11 @@ export const $CreateSmsTemplateSchema = z.object({
2727
})
2828
.refine((templateMessage) => !templateMessage.includes('http://'), {
2929
message: form.smsTemplateMessage.error.insecureLink,
30+
})
31+
.refine((templateMessage) => INVALID_PERSONALISATION_FIELDS.some(
32+
(field) => !templateMessage.includes(`((${field}))`)
33+
), {
34+
message: form.smsTemplateMessage.error.invalidPersonalisation,
3035
}),
3136
});
3237

0 commit comments

Comments
 (0)