import type { ExecutionContext } from '@nestjs/common'; import { InspectionCreateGuard } from './inspection-create.guard'; function contextOf(user: { id: string } | undefined): ExecutionContext { return { switchToHttp: () => ({ getRequest: () => ({ user }) }), } as unknown as ExecutionContext; } describe('InspectionCreateGuard', () => { it('asks for inspections:create for the signed-in person', async () => { const hasAny = jest.fn().mockResolvedValue(true); const guard = new InspectionCreateGuard({ hasAny } as never); await expect(guard.canActivate(contextOf({ id: 'u1' }))).resolves.toBe( true, ); expect(hasAny).toHaveBeenCalledWith('u1', ['inspections:create']); }); it('denies a person without the right and a request without a person', async () => { const hasAny = jest.fn().mockResolvedValue(false); const guard = new InspectionCreateGuard({ hasAny } as never); await expect(guard.canActivate(contextOf({ id: 'u1' }))).resolves.toBe( false, ); await expect(guard.canActivate(contextOf(undefined))).resolves.toBe(false); expect(hasAny).toHaveBeenCalledTimes(1); }); });