Skip to content

Commit

Permalink
mock isAdminSessionValidcall
Browse files Browse the repository at this point in the history
  • Loading branch information
john-tco committed Sep 6, 2023
1 parent 5e93f1e commit b2d4f54
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/admin/src/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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);

Expand All @@ -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');
Expand All @@ -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'
Expand All @@ -58,21 +62,21 @@ 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(
'http://localhost:3000/apply/admin/scheme/1/advert/129744d5-0746-403f-8a5f-a8c9558bc4e3/grantDetails/1'
);
});

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(
Expand Down

0 comments on commit b2d4f54

Please sign in to comment.