@@ -10,6 +10,7 @@ import { cookies } from 'next/headers';
1010import { sign } from 'jsonwebtoken' ;
1111import type { ReadonlyRequestCookies } from 'next/dist/server/web/spec-extension/adapters/request-cookies' ;
1212import { BinaryLike , BinaryToTextEncoding } from 'node:crypto' ;
13+ import { getAccessTokenServer } from '@utils/amplify-utils' ;
1314
1415class MockHmac {
1516 constructor ( ) { }
@@ -35,6 +36,7 @@ jest.mock('node:crypto', () => ({
3536 createHmac : ( ) => new MockHmac ( ) ,
3637 randomBytes : ( ) => 'salt' ,
3738} ) ) ;
39+ jest . mock ( '@utils/amplify-utils' ) ;
3840
3941const OLD_ENV = { ...process . env } ;
4042
@@ -80,11 +82,7 @@ describe('getCsrfFormValue', () => {
8082
8183describe ( 'getSessionId' , ( ) => {
8284 test ( 'errors when access token not found' , async ( ) => {
83- jest . mocked ( cookies ) . mockReturnValue (
84- mockDeep < ReadonlyRequestCookies > ( {
85- getAll : ( ) => [ ] ,
86- } )
87- ) ;
85+ jest . mocked ( getAccessTokenServer ) . mockResolvedValue ( undefined ) ;
8886
8987 await expect ( ( ) => getSessionId ( ) ) . rejects . toThrow (
9088 'Could not get access token'
@@ -99,33 +97,15 @@ describe('getSessionId', () => {
9997 'key'
10098 ) ;
10199
102- jest . mocked ( cookies ) . mockReturnValue (
103- mockDeep < ReadonlyRequestCookies > ( {
104- getAll : ( ) => [
105- {
106- name : 'Cognito.123.accessToken' ,
107- value : mockEmptyJwt ,
108- } ,
109- ] ,
110- } )
111- ) ;
100+ jest . mocked ( getAccessTokenServer ) . mockResolvedValue ( mockEmptyJwt ) ;
112101
113102 await expect ( ( ) => getSessionId ( ) ) . rejects . toThrow (
114103 'Could not get session ID'
115104 ) ;
116105 } ) ;
117106
118107 test ( 'returns session id' , async ( ) => {
119- jest . mocked ( cookies ) . mockReturnValue (
120- mockDeep < ReadonlyRequestCookies > ( {
121- getAll : ( ) => [
122- {
123- name : 'Cognito.123.accessToken' ,
124- value : mockJwt ,
125- } ,
126- ] ,
127- } )
128- ) ;
108+ jest . mocked ( getAccessTokenServer ) . mockResolvedValue ( mockJwt ) ;
129109
130110 const sessionId = await getSessionId ( ) ;
131111
@@ -164,16 +144,7 @@ describe('verifyCsrfTokenFull', () => {
164144 test ( 'missing CSRF cookie' , async ( ) => {
165145 const formData = mockDeep < FormData > ( ) ;
166146
167- jest . mocked ( cookies ) . mockReturnValue (
168- mockDeep < ReadonlyRequestCookies > ( {
169- getAll : ( ) => [
170- {
171- name : 'Cognito.123.accessToken' ,
172- value : mockJwt ,
173- } ,
174- ] ,
175- } )
176- ) ;
147+ jest . mocked ( getAccessTokenServer ) . mockResolvedValue ( mockJwt ) ;
177148
178149 await expect ( ( ) => verifyCsrfTokenFull ( formData ) ) . rejects . toThrow (
179150 'missing CSRF cookie'
@@ -185,14 +156,9 @@ describe('verifyCsrfTokenFull', () => {
185156 get : ( ) => null ,
186157 } ) ;
187158
159+ jest . mocked ( getAccessTokenServer ) . mockResolvedValue ( mockJwt ) ;
188160 jest . mocked ( cookies ) . mockReturnValue (
189161 mockDeep < ReadonlyRequestCookies > ( {
190- getAll : ( ) => [
191- {
192- name : 'Cognito.123.accessToken' ,
193- value : mockJwt ,
194- } ,
195- ] ,
196162 get : ( _ : string ) => ( {
197163 name : 'csrf_token' ,
198164 value : 'hmac.salt' ,
@@ -210,14 +176,9 @@ describe('verifyCsrfTokenFull', () => {
210176 get : ( ) => 'hmac2.salt' ,
211177 } ) ;
212178
179+ jest . mocked ( getAccessTokenServer ) . mockResolvedValue ( mockJwt ) ;
213180 jest . mocked ( cookies ) . mockReturnValue (
214181 mockDeep < ReadonlyRequestCookies > ( {
215- getAll : ( ) => [
216- {
217- name : 'Cognito.123.accessToken' ,
218- value : mockJwt ,
219- } ,
220- ] ,
221182 get : ( _ : string ) => ( {
222183 name : 'csrf_token' ,
223184 value : 'hmac.salt' ,
@@ -235,14 +196,9 @@ describe('verifyCsrfTokenFull', () => {
235196 get : ( ) => 'hmac2.salt' ,
236197 } ) ;
237198
199+ jest . mocked ( getAccessTokenServer ) . mockResolvedValue ( mockJwt ) ;
238200 jest . mocked ( cookies ) . mockReturnValue (
239201 mockDeep < ReadonlyRequestCookies > ( {
240- getAll : ( ) => [
241- {
242- name : 'Cognito.123.accessToken' ,
243- value : mockJwt ,
244- } ,
245- ] ,
246202 get : ( _ : string ) => ( {
247203 name : 'csrf_token' ,
248204 value : 'hmac2.salt' ,
@@ -260,14 +216,9 @@ describe('verifyCsrfTokenFull', () => {
260216 get : ( ) => 'hmac.salt' ,
261217 } ) ;
262218
219+ jest . mocked ( getAccessTokenServer ) . mockResolvedValue ( mockJwt ) ;
263220 jest . mocked ( cookies ) . mockReturnValue (
264221 mockDeep < ReadonlyRequestCookies > ( {
265- getAll : ( ) => [
266- {
267- name : 'Cognito.123.accessToken' ,
268- value : mockJwt ,
269- } ,
270- ] ,
271222 get : ( _ : string ) => ( {
272223 name : 'csrf_token' ,
273224 value : 'hmac.salt' ,
0 commit comments