Skip to content

Commit 4e34bfd

Browse files
committed
CCM-10424 Error page, removed preview error message and added playwright test
1 parent 7210ab9 commit 4e34bfd

File tree

15 files changed

+207
-3
lines changed

15 files changed

+207
-3
lines changed

frontend/src/__tests__/app/client-id-and-campaign-id-required/__snapshots__/page.test.tsx.snap renamed to frontend/src/__tests__/app/create-letter-template/client-id-and-campaign-id-required/__snapshots__/page.test.tsx.snap

File renamed without changes.

frontend/src/__tests__/app/client-id-and-campaign-id-required/page.test.tsx renamed to frontend/src/__tests__/app/create-letter-template/client-id-and-campaign-id-required/page.test.tsx

File renamed without changes.

frontend/src/__tests__/app/create-letter-template/page.test.tsx

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,102 @@
22
* @jest-environment node
33
*/
44
import CreateLetterTemplatePage from '@app/create-letter-template/page';
5+
import { getSessionServer } from '@utils/amplify-utils';
6+
import { fetchClient } from '@utils/server-features';
7+
import { redirect, RedirectType } from 'next/navigation';
8+
9+
jest.mock('next/navigation');
10+
jest.mock('@utils/amplify-utils');
11+
jest.mock('@utils/server-features');
12+
13+
const mockGetSessionServer = jest.mocked(getSessionServer);
14+
const mockFetchClient = jest.mocked(fetchClient);
515

616
describe('CreateLetterTemplatePage', () => {
717
beforeEach(() => {
818
jest.resetAllMocks();
919
});
1020

1121
it('should render CreateLetterTemplatePage', async () => {
22+
mockGetSessionServer.mockResolvedValueOnce({
23+
accessToken: 'mocktoken',
24+
clientId: 'client1',
25+
userSub: 'sub',
26+
});
27+
mockFetchClient.mockResolvedValueOnce({
28+
data: {
29+
campaignId: 'campaign2',
30+
features: {},
31+
},
32+
});
33+
1234
const page = await CreateLetterTemplatePage();
1335

1436
expect(page).toMatchSnapshot();
1537
});
38+
39+
it('should check client ID and campaign ID', async () => {
40+
mockGetSessionServer.mockResolvedValueOnce({
41+
accessToken: 'mocktoken',
42+
clientId: 'client1',
43+
userSub: 'sub',
44+
});
45+
mockFetchClient.mockResolvedValueOnce({
46+
data: {
47+
campaignId: 'campaign2',
48+
features: {},
49+
},
50+
});
51+
52+
await CreateLetterTemplatePage();
53+
54+
expect(mockGetSessionServer).toHaveBeenCalled();
55+
expect(mockFetchClient).toHaveBeenCalled();
56+
});
57+
58+
it('should redirect to error page when client ID is not present', async () => {
59+
const mockRedirect = jest.mocked(redirect);
60+
61+
mockGetSessionServer.mockResolvedValueOnce({
62+
accessToken: 'mocktoken',
63+
clientId: undefined,
64+
userSub: 'sub',
65+
});
66+
mockFetchClient.mockResolvedValueOnce({
67+
data: {
68+
campaignId: 'campaign2',
69+
features: {},
70+
},
71+
});
72+
73+
await CreateLetterTemplatePage();
74+
75+
expect(mockRedirect).toHaveBeenCalledWith(
76+
'/create-letter-template/client-id-and-campaign-id-required',
77+
RedirectType.push
78+
);
79+
});
80+
81+
it('should redirect to error page when campaign ID is not present', async () => {
82+
const mockRedirect = jest.mocked(redirect);
83+
84+
mockGetSessionServer.mockResolvedValueOnce({
85+
accessToken: 'mocktoken',
86+
clientId: 'client2',
87+
userSub: 'sub',
88+
});
89+
mockFetchClient.mockResolvedValueOnce({
90+
data: {
91+
campaignId: undefined,
92+
features: {},
93+
},
94+
});
95+
96+
await CreateLetterTemplatePage();
97+
98+
expect(mockRedirect).toHaveBeenCalledWith(
99+
'/create-letter-template/client-id-and-campaign-id-required',
100+
RedirectType.push
101+
);
102+
});
16103
});

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ jest.mock('next/navigation');
66

77
jest.mock('@utils/amplify-utils');
88

9+
beforeEach(() => {
10+
jest.resetAllMocks();
11+
});
12+
913
test('submit form - validation error', async () => {
1014
const response = await chooseTemplateAction(
1115
{},

frontend/src/__tests__/middleware.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe('middleware function', () => {
3535

3636
getTokenMock.mockResolvedValueOnce({
3737
accessToken: undefined,
38+
clientId: undefined,
3839
userSub: undefined,
3940
});
4041

@@ -53,6 +54,7 @@ describe('middleware function', () => {
5354
it('if request path is protected, and access token is obtained, respond with CSP', async () => {
5455
getTokenMock.mockResolvedValueOnce({
5556
accessToken: 'token',
57+
clientId: 'client1',
5658
userSub: 'sub',
5759
});
5860

frontend/src/__tests__/utils/form-actions.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('form-actions', () => {
3131
jest.resetAllMocks();
3232
authIdTokenServerMock.mockResolvedValueOnce({
3333
accessToken: 'token',
34+
clientId: 'client1',
3435
userSub: 'sub',
3536
});
3637
});
@@ -94,6 +95,7 @@ describe('form-actions', () => {
9495
authIdTokenServerMock.mockReset();
9596
authIdTokenServerMock.mockResolvedValueOnce({
9697
accessToken: undefined,
98+
clientId: undefined,
9799
userSub: undefined,
98100
});
99101

@@ -257,6 +259,7 @@ describe('form-actions', () => {
257259
authIdTokenServerMock.mockReset();
258260
authIdTokenServerMock.mockResolvedValueOnce({
259261
accessToken: undefined,
262+
clientId: undefined,
260263
userSub: undefined,
261264
});
262265

@@ -348,6 +351,7 @@ describe('form-actions', () => {
348351
authIdTokenServerMock.mockReset();
349352
authIdTokenServerMock.mockResolvedValueOnce({
350353
accessToken: undefined,
354+
clientId: undefined,
351355
userSub: undefined,
352356
});
353357

@@ -414,6 +418,7 @@ describe('form-actions', () => {
414418
authIdTokenServerMock.mockReset();
415419
authIdTokenServerMock.mockResolvedValueOnce({
416420
accessToken: undefined,
421+
clientId: undefined,
417422
userSub: undefined,
418423
});
419424

@@ -462,6 +467,7 @@ describe('form-actions', () => {
462467
authIdTokenServerMock.mockReset();
463468
authIdTokenServerMock.mockResolvedValueOnce({
464469
accessToken: undefined,
470+
clientId: undefined,
465471
userSub: undefined,
466472
});
467473

@@ -553,6 +559,7 @@ describe('form-actions', () => {
553559
authIdTokenServerMock.mockReset();
554560
authIdTokenServerMock.mockResolvedValueOnce({
555561
accessToken: undefined,
562+
clientId: undefined,
556563
userSub: undefined,
557564
});
558565

@@ -600,6 +607,7 @@ describe('form-actions', () => {
600607
authIdTokenServerMock.mockReset();
601608
authIdTokenServerMock.mockResolvedValueOnce({
602609
accessToken: undefined,
610+
clientId: undefined,
603611
userSub: undefined,
604612
});
605613

@@ -665,6 +673,7 @@ describe('form-actions', () => {
665673
authIdTokenServerMock.mockReset();
666674
authIdTokenServerMock.mockResolvedValueOnce({
667675
accessToken: undefined,
676+
clientId: undefined,
668677
userSub: undefined,
669678
});
670679

frontend/src/__tests__/utils/server-features.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('serverIsFeatureEnabled', () => {
1919
it('should return false when no accessToken', async () => {
2020
getSessionServerMock.mockResolvedValueOnce({
2121
accessToken: undefined,
22+
clientId: undefined,
2223
userSub: undefined,
2324
});
2425

@@ -30,6 +31,7 @@ describe('serverIsFeatureEnabled', () => {
3031
it('should return false when no client', async () => {
3132
getSessionServerMock.mockResolvedValueOnce({
3233
accessToken: 'token',
34+
clientId: 'client1',
3335
userSub: 'user',
3436
});
3537

@@ -49,6 +51,7 @@ describe('serverIsFeatureEnabled', () => {
4951
it('returns false if fetching configuration fails unexpectedly', async () => {
5052
getSessionServerMock.mockResolvedValueOnce({
5153
accessToken: 'token',
54+
clientId: 'client1',
5255
userSub: 'user',
5356
});
5457

@@ -74,6 +77,7 @@ describe('serverIsFeatureEnabled', () => {
7477

7578
getSessionServerMock.mockResolvedValueOnce({
7679
accessToken: 'token',
80+
clientId: 'client1',
7781
userSub: 'user',
7882
});
7983

@@ -95,6 +99,7 @@ describe('serverIsFeatureEnabled', () => {
9599

96100
getSessionServerMock.mockResolvedValueOnce({
97101
accessToken: 'token',
102+
clientId: 'client1',
98103
userSub: 'user',
99104
});
100105

frontend/src/app/create-letter-template/page.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { CreateLetterTemplate } from 'nhs-notify-web-template-management-utils';
22
import { LetterTemplateForm } from '@forms/LetterTemplateForm/LetterTemplateForm';
3+
import { getSessionServer } from '@utils/amplify-utils';
4+
import { redirect, RedirectType } from 'next/navigation';
5+
import { fetchClient } from '@utils/server-features';
36

47
const CreateLetterTemplatePage = async () => {
58
const initialState: CreateLetterTemplate = {
@@ -9,6 +12,25 @@ const CreateLetterTemplatePage = async () => {
912
language: 'en',
1013
};
1114

15+
const sessionServer = await getSessionServer();
16+
const { accessToken, clientId } = sessionServer;
17+
18+
if (!accessToken || !clientId)
19+
return redirect(
20+
'/create-letter-template/client-id-and-campaign-id-required',
21+
RedirectType.push
22+
);
23+
24+
const clientConfiguration = await fetchClient(accessToken);
25+
26+
const campaignId = clientConfiguration?.data?.campaignId;
27+
28+
if (!clientConfiguration || clientConfiguration.error || !campaignId)
29+
return redirect(
30+
'/create-letter-template/client-id-and-campaign-id-required',
31+
RedirectType.push
32+
);
33+
1234
return <LetterTemplateForm initialState={initialState} />;
1335
};
1436

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function chooseTemplateAction(
2727
};
2828
}
2929

30-
redirect(
30+
return redirect(
3131
`/create-${templateTypeToUrlTextMappings(parsedForm.data.templateType)}-template`,
3232
RedirectType.push
3333
);

frontend/src/content/content.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ const previewLetterTemplate = {
264264
'The personalisation fields in your files are missing or do not match.',
265265
validationErrorAction:
266266
'Check that the personalisation fields in your template file match the fields in your test personalisation file',
267+
missingClientOrCampaignIdError: 'You cannot proof this letter template.',
268+
missingClientOrCampaignIdErrorAction: 'To get access, contact your onboarding manager and give them this error message: letter template missing client ID and campaign ID.',
267269
preSubmissionText: previewLetterPreSubmissionText,
268270
};
269271

0 commit comments

Comments
 (0)