Skip to content

Commit

Permalink
update middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jgunnCO committed Apr 5, 2024
1 parent 3222fc1 commit 7148aa4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
7 changes: 5 additions & 2 deletions packages/admin/src/middleware.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const authenticatedPaths = [
'/scheme/:path*',
'/scheme-list/:path*',
'/super-admin-dashboard/:path*',
].map((path) => new URLPattern({ pathname: path }));
].map((pathname) => new URLPattern({ pathname }));

const isAuthenticatedPath = (pathname: string) =>
authenticatedPaths.some((authenticatedPath) =>
Expand All @@ -103,7 +103,10 @@ export async function middleware(req: NextRequest) {
const rewriteUrl = req.url;
let res = NextResponse.rewrite(rewriteUrl);
logRequest(req, res);

console.log({
pathname: req.nextUrl.pathname,
isAuthenticatedPath: isAuthenticatedPath(req.nextUrl.pathname),
});
if (isAuthenticatedPath(req.nextUrl.pathname)) {
await csrfMiddleware(req, res);
res = await authenticateRequest(req, res);
Expand Down
41 changes: 30 additions & 11 deletions packages/admin/src/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import '@testing-library/jest-dom';
import { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies';
// eslint-disable-next-line @next/next/no-server-import-in-page
import { NextRequest, NextResponse } from 'next/server';
import { NextURL } from 'next/dist/server/web/next-url';
import { middleware } from './middleware.page';
import { isAdminSessionValid } from './services/UserService';
import { getLoginUrl } from './utils/general';
Expand All @@ -12,15 +13,29 @@ jest.mock('./services/UserService', () => ({
isAdminSessionValid: jest.fn(),
}));

jest.mock('next/server', () => ({
...jest.requireActual('next/server'),
URLPattern: jest.fn().mockImplementation(() => ({
test: jest.fn(),
})),
}));
let cookieStore = {},
headerStore = {};

const getMockRequest = (url: string) =>
({
cookies: {
get: (key) => cookieStore[key],
getAll: () =>
Object.entries(cookieStore).map(([name, value]) => ({ name, value })),
set: (name, value) => (cookieStore[name] = { name, value }),
},
headers: {
get: (key) => headerStore[key],
entries: () => [],
set: (key, value) => (headerStore[key] = value),
},
url,
nextUrl: new NextURL(url),
method: 'GET',
} as unknown as NextRequest);

describe('middleware', () => {
const req = new NextRequest('http://localhost:3000/dashboard');
const req = getMockRequest('http://localhost:3000/dashboard');

beforeEach(() => {
process.env.MAX_COOKIE_AGE = '21600';
Expand All @@ -29,6 +44,9 @@ describe('middleware', () => {
process.env.V2_LOGIN_URL = 'http://localhost:8082/login';
process.env.FEATURE_ADVERT_BUILDER = 'enabled';
process.env.VALIDATE_USER_ROLES_IN_MIDDLEWARE = 'true';
jest.clearAllMocks();
cookieStore = {};
headerStore = {};
});

it('Should redirect to the logout page when the user is not authorized', async () => {
Expand All @@ -37,6 +55,7 @@ describe('middleware', () => {
const result = await middleware(req);

expect(result).toBeInstanceOf(NextResponse);
console.log(result);

// basePath from nextjs config does not apply to jest tests, thus no subpaths
expect(result.headers.get('Location')).toStrictEqual(expectedUrl);
Expand Down Expand Up @@ -76,14 +95,14 @@ describe('middleware', () => {
const result = await middleware(req);

expect(result.headers.get('x-middleware-rewrite')).toStrictEqual(
'http://localhost:3000/apply/test/destination'
'http://localhost:3000/dashboard'
);
});
});

describe('middleware', () => {
const req = new NextRequest(
'http://localhost:3000/apply/admin/scheme/1/advert/129744d5-0746-403f-8a5f-a8c9558bc4e3/grantDetails/1'
const req = getMockRequest(
'http://localhost:3000/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', async () => {
Expand All @@ -94,7 +113,7 @@ describe('middleware', () => {

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'
'http://localhost:3000/scheme/1/advert/129744d5-0746-403f-8a5f-a8c9558bc4e3/grantDetails/1'
);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/admin/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/applicant/src/middleware.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ const authenticatedPaths = [
'/sign-in-details',
'/api/redirect-from-find',
'/mandatory-questions/:path*',
].map((path) => new URLPattern({ pathname: path }));
].map((pathname) => new URLPattern({ pathname }));

const isAuthenticatedPath = (pathname: string) =>
authenticatedPaths.some((authenticatedPath) =>
Expand Down

0 comments on commit 7148aa4

Please sign in to comment.