Skip to content

Commit d982ffd

Browse files
committed
CCM-8744: revert force refresh, csrf refactor, sync
1 parent 769e08f commit d982ffd

File tree

12 files changed

+191
-205
lines changed

12 files changed

+191
-205
lines changed

frontend/src/__tests__/components/forms/PreviewLetterTemplate/PreviewLetterTemplate.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe('Preview letter form renders', () => {
5353
currentVersion: '4C728B7D-A028-4BA2-B180-A63CDD2AE1E9',
5454
virusScanStatus: VirusScanStatus.PENDING,
5555
},
56+
testDataCsv: undefined,
5657
},
5758
})}
5859
/>

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

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -756,43 +756,6 @@ exports[`Preview letter form renders matches snapshot when navigating from manag
756756
</div>
757757
</div>
758758
</div>
759-
<div
760-
class="nhsuk-grid-row preview__row"
761-
>
762-
<div
763-
class="nhsuk-grid-column-one-third preview__col"
764-
>
765-
<div
766-
class="preview__col_heading"
767-
>
768-
Test personalisation file
769-
</div>
770-
</div>
771-
<div
772-
class="nhsuk-grid-column-two-thirds col"
773-
>
774-
<div
775-
class="container"
776-
>
777-
<svg
778-
class="icon"
779-
fill="none"
780-
height="26"
781-
viewBox="0 0 20 26"
782-
width="20"
783-
xmlns="http://www.w3.org/2000/svg"
784-
>
785-
<path
786-
d="M0 0V26H20V6.59375L19.7188 6.28125L13.7188 0.28125L13.4062 0H0ZM2 2H12V8H18V24H2V2ZM14 3.4375L16.5625 6H14V3.4375Z"
787-
fill="#4C6272"
788-
/>
789-
</svg>
790-
<p
791-
class="text"
792-
/>
793-
</div>
794-
</div>
795-
</div>
796759
</div>
797760
</div>
798761
<form

frontend/src/__tests__/components/molecules/NHSNotifyFormWrapper.test.tsx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import {
44
csrfServerAction,
55
} from '@molecules/NHSNotifyFormWrapper/NHSNotifyFormWrapper';
66
import { render } from '@testing-library/react';
7-
import { verifyCsrfTokenFull } from '@utils/csrf-utils';
7+
import { redirect } from 'next/navigation';
8+
import { verifyFormCsrfToken } from '@utils/csrf-utils';
9+
10+
jest.mock('next/navigation');
811

912
jest.mock('@utils/csrf-utils', () => ({
10-
verifyCsrfTokenFull: jest.fn(),
13+
verifyFormCsrfToken: jest.fn(),
1114
}));
1215

1316
test('Renders back button', () => {
@@ -27,8 +30,9 @@ describe('csrfServerAction', () => {
2730
expect(action).toEqual('/action');
2831
});
2932

30-
test('server action', async () => {
31-
const mockAction = jest.fn(() => 'response');
33+
test('server action with valid csrf check', async () => {
34+
jest.mocked(verifyFormCsrfToken).mockResolvedValueOnce(true);
35+
const mockAction = jest.fn();
3236
const action = csrfServerAction(mockAction);
3337

3438
if (typeof action === 'string') {
@@ -38,7 +42,25 @@ describe('csrfServerAction', () => {
3842
const mockFormData = mockDeep<FormData>();
3943
await action(mockFormData);
4044

41-
expect(verifyCsrfTokenFull).toHaveBeenCalledWith(mockFormData);
45+
expect(verifyFormCsrfToken).toHaveBeenCalledWith(mockFormData);
4246
expect(mockAction).toHaveBeenCalledWith(mockFormData);
4347
});
48+
49+
test('server action with failed csrf check', async () => {
50+
jest.mocked(verifyFormCsrfToken).mockResolvedValueOnce(false);
51+
52+
const mockAction = jest.fn();
53+
const action = csrfServerAction(mockAction);
54+
55+
if (typeof action === 'string') {
56+
throw new TypeError('Expected server action');
57+
}
58+
59+
const mockFormData = mockDeep<FormData>();
60+
await action(mockFormData);
61+
62+
expect(verifyFormCsrfToken).toHaveBeenCalledWith(mockFormData);
63+
expect(redirect).toHaveBeenCalledWith('/auth/signout');
64+
expect(mockAction).not.toHaveBeenCalled();
65+
});
4466
});

frontend/src/__tests__/middleware.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('middleware function', () => {
3535

3636
const response = await middleware(request);
3737

38-
expect(getTokenMock).toHaveBeenCalledWith({ forceRefresh: true });
38+
expect(getTokenMock).toHaveBeenCalled();
3939

4040
expect(response.status).toBe(307);
4141
expect(response.headers.get('location')).toBe(

frontend/src/__tests__/utils/amplify-utils.test.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/**
22
* @jest-environment node
33
*/
4-
import { getAccessTokenServer } from '@utils/amplify-utils';
4+
import { sign } from 'jsonwebtoken';
55
import { fetchAuthSession } from 'aws-amplify/auth/server';
6+
import { getAccessTokenServer, getSessionId } from '../../utils/amplify-utils';
67

78
jest.mock('aws-amplify/auth/server');
89
jest.mock('@aws-amplify/adapter-nextjs/api');
@@ -19,24 +20,22 @@ const fetchAuthSessionMock = jest.mocked(fetchAuthSession);
1920

2021
describe('amplify-utils', () => {
2122
test('getAccessTokenServer - should return the auth token', async () => {
22-
fetchAuthSessionMock.mockResolvedValue({
23+
fetchAuthSessionMock.mockResolvedValueOnce({
2324
tokens: {
2425
accessToken: {
25-
toString: () => 'mockSub',
26-
payload: {
27-
sub: 'mockSub',
28-
},
26+
toString: () => 'mockToken',
27+
payload: {},
2928
},
3029
},
3130
});
3231

3332
const result = await getAccessTokenServer();
3433

35-
expect(result).toEqual('mockSub');
34+
expect(result).toEqual('mockToken');
3635
});
3736

3837
test('getAccessTokenServer - should return undefined when no auth session', async () => {
39-
fetchAuthSessionMock.mockResolvedValue({});
38+
fetchAuthSessionMock.mockResolvedValueOnce({});
4039

4140
const result = await getAccessTokenServer();
4241

@@ -52,4 +51,46 @@ describe('amplify-utils', () => {
5251

5352
expect(result).toBeUndefined();
5453
});
54+
55+
describe('getSessionId', () => {
56+
test('returns void when access token not found', async () => {
57+
fetchAuthSessionMock.mockResolvedValueOnce({});
58+
59+
await expect(getSessionId()).resolves.toBeUndefined();
60+
});
61+
62+
test('errors when session ID not found', async () => {
63+
fetchAuthSessionMock.mockResolvedValueOnce({
64+
tokens: {
65+
accessToken: {
66+
toString: () => sign({}, 'key'),
67+
payload: {},
68+
},
69+
},
70+
});
71+
72+
await expect(getSessionId()).resolves.toBeUndefined();
73+
});
74+
75+
test('returns session id', async () => {
76+
fetchAuthSessionMock.mockResolvedValueOnce({
77+
tokens: {
78+
accessToken: {
79+
toString: () =>
80+
sign(
81+
{
82+
origin_jti: 'jti',
83+
},
84+
'key'
85+
),
86+
payload: {},
87+
},
88+
},
89+
});
90+
91+
const sessionId = await getSessionId();
92+
93+
expect(sessionId).toEqual('jti');
94+
});
95+
});
5596
});

0 commit comments

Comments
 (0)