Files
recipe-app/backend/src/test-utils/security-test-helpers.ts
T
Nils-Johan Gynther 1db30c9b6f
Test Suite / test (24.15.0) (push) Has been cancelled
test(security): add and refactor api security/idor coverage
2026-05-11 16:40:16 +02:00

27 lines
800 B
TypeScript

import { ExecutionContext } from '@nestjs/common';
import { ROLES_KEY } from '../auth/decorators/roles.decorator';
export type MockHttpContextOptions = {
handler: Function;
clazz: Function;
user?: unknown;
};
export type AdminHandler = [string, Function];
export function mockHttpContext(options: MockHttpContextOptions): ExecutionContext {
return {
getClass: () => options.clazz,
getHandler: () => options.handler,
switchToHttp: () => ({
getRequest: () => ({ user: options.user }),
getResponse: () => ({}),
getNext: () => undefined,
}),
} as unknown as ExecutionContext;
}
export function getRolesMetadata(handler: Function): string[] | undefined {
return Reflect.getMetadata(ROLES_KEY, handler) as string[] | undefined;
}