27 lines
800 B
TypeScript
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;
|
|
}
|