From b2d4f54818a2782dab0472316c2643ef91789731 Mon Sep 17 00:00:00 2001 From: john-tco Date: Wed, 6 Sep 2023 15:01:47 +0100 Subject: [PATCH] mock isAdminSessionValidcall --- packages/admin/src/middleware.test.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/admin/src/middleware.test.ts b/packages/admin/src/middleware.test.ts index 3a5821982..07d5474ab 100644 --- a/packages/admin/src/middleware.test.ts +++ b/packages/admin/src/middleware.test.ts @@ -7,6 +7,10 @@ import { middleware } from './middleware.page'; // eslint-disable-next-line @next/next/no-server-import-in-page import { NextRequest, NextResponse } from 'next/server'; +jest.mock('./services/UserService', () => ({ + isAdminSessionValid: () => true, +})); + describe('middleware', () => { const req = new NextRequest('http://localhost:3000/apply/test/destination'); @@ -17,8 +21,8 @@ describe('middleware', () => { process.env.V2_LOGIN_URL = 'http://localhost:8082/login'; }); - it('Should redirect to the logout page when the user is not authorized', () => { - const result = middleware(req); + it('Should redirect to the logout page when the user is not authorized', async () => { + const result = await middleware(req); expect(result).toBeInstanceOf(NextResponse); @@ -28,12 +32,12 @@ describe('middleware', () => { ); }); - it('Should reset auth cookie correctly when the user is authorised', () => { + it('Should reset auth cookie correctly when the user is authorised', async () => { // Just set any expiry. If response expiry is different, cookie was updated req.cookies.set('session_id', 'session_id_value', { maxAge: 60 }); const reqCookie = req.cookies.getWithOptions('session_id'); - const result = middleware(req); + const result = await middleware(req); const cookieOptions = result.cookies.getWithOptions('session_id').options; expect(result.cookies.get('session_id')).toStrictEqual('session_id_value'); @@ -43,9 +47,9 @@ describe('middleware', () => { expect(cookieOptions.Expires).not.toStrictEqual(reqCookie.options.Expires); }); - it('Should redirect to the original requests URL when we are authorised', () => { + it('Should redirect to the original requests URL when we are authorised', async () => { req.cookies.set('session_id', 'session_id_value', { maxAge: 60 }); - const result = middleware(req); + const result = await middleware(req); expect(result.headers.get('x-middleware-rewrite')).toStrictEqual( 'http://localhost:3000/apply/test/destination' @@ -58,10 +62,10 @@ describe('middleware', () => { 'http://localhost:3000/apply/admin/scheme/1/advert/129744d5-0746-403f-8a5f-a8c9558bc4e3/grantDetails/1' ); - it('Should allow the user to access the advert builder pages if the feature is enabled', () => { + it('Should allow the user to access the advert builder pages if the feature is enabled', async () => { req.cookies.set('session_id', 'session_id_value', { maxAge: 60 }); process.env.FEATURE_ADVERT_BUILDER = 'enabled'; - const result = middleware(req); + const result = await middleware(req); expect(result).toBeInstanceOf(NextResponse); expect(result.headers.get('x-middleware-rewrite')).toStrictEqual( @@ -69,10 +73,10 @@ describe('middleware', () => { ); }); - it('Should not allow the user to access the advert builder pages if the feature is enabled', () => { + it('Should not allow the user to access the advert builder pages if the feature is enabled', async () => { req.cookies.set('session_id', 'session_id_value', { maxAge: 60 }); process.env.FEATURE_ADVERT_BUILDER = 'disabled'; - const result = middleware(req); + const result = await middleware(req); expect(result).toBeInstanceOf(NextResponse); expect(result.headers.get('location')).toStrictEqual(