|
| 1 | +import { |
| 2 | + ExecutionContext, |
| 3 | + ForbiddenException, |
| 4 | + InternalServerErrorException, |
| 5 | +} from '@nestjs/common' |
| 6 | + |
| 7 | +import { |
| 8 | + CaseIndictmentRulingDecision, |
| 9 | + CaseTransition, |
| 10 | + CaseType, |
| 11 | +} from '@island.is/judicial-system/types' |
| 12 | + |
| 13 | +import { CaseTransitionGuard } from '../caseTransition.guard' |
| 14 | + |
| 15 | +describe('CaseTransitionGuard', () => { |
| 16 | + let guard: CaseTransitionGuard |
| 17 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 18 | + let mockRequest: jest.Mock |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + guard = new CaseTransitionGuard() |
| 22 | + mockRequest = jest.fn() |
| 23 | + }) |
| 24 | + |
| 25 | + const mockExecutionContext = (requestMock: unknown): ExecutionContext => |
| 26 | + ({ |
| 27 | + switchToHttp: () => ({ |
| 28 | + getRequest: () => requestMock, |
| 29 | + }), |
| 30 | + } as unknown as ExecutionContext) |
| 31 | + |
| 32 | + const createMockCase = ( |
| 33 | + type: CaseType, |
| 34 | + rulingDecision: unknown, |
| 35 | + judgeId: string, |
| 36 | + ) => ({ |
| 37 | + type, |
| 38 | + indictmentRulingDecision: rulingDecision, |
| 39 | + judgeId, |
| 40 | + }) |
| 41 | + |
| 42 | + it('should activate when the judge is the assigned judge', () => { |
| 43 | + const mockCase = createMockCase( |
| 44 | + CaseType.INDICTMENT, |
| 45 | + CaseIndictmentRulingDecision.RULING, |
| 46 | + 'judgeId', |
| 47 | + ) |
| 48 | + const context = mockExecutionContext({ |
| 49 | + body: { transition: CaseTransition.COMPLETE }, |
| 50 | + case: mockCase, |
| 51 | + user: { id: 'judgeId' }, |
| 52 | + }) |
| 53 | + |
| 54 | + const result = guard.canActivate(context) |
| 55 | + |
| 56 | + expect(result).toBe(true) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should not activate when the user is not the assigned judge', () => { |
| 60 | + const mockCase = createMockCase( |
| 61 | + CaseType.INDICTMENT, |
| 62 | + CaseIndictmentRulingDecision.RULING, |
| 63 | + 'judgeId', |
| 64 | + ) |
| 65 | + const context = mockExecutionContext({ |
| 66 | + body: { transition: CaseTransition.COMPLETE }, |
| 67 | + case: mockCase, |
| 68 | + user: { id: 'differentJudgeId' }, |
| 69 | + }) |
| 70 | + |
| 71 | + expect(() => guard.canActivate(context)).toThrow(ForbiddenException) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should activate using the default rule for transitions not in the rule map', () => { |
| 75 | + const mockCase = createMockCase(CaseType.CUSTODY, null, 'someId') |
| 76 | + const context = mockExecutionContext({ |
| 77 | + body: { transition: CaseTransition.SUBMIT }, |
| 78 | + case: mockCase, |
| 79 | + user: { id: 'someId' }, |
| 80 | + }) |
| 81 | + |
| 82 | + const result = guard.canActivate(context) |
| 83 | + |
| 84 | + expect(result).toBe(true) |
| 85 | + }) |
| 86 | + |
| 87 | + it('should throw InternalServerErrorException when case or user is missing', () => { |
| 88 | + const context = mockExecutionContext({ |
| 89 | + body: { transition: CaseTransition.COMPLETE }, |
| 90 | + case: null, |
| 91 | + user: null, |
| 92 | + }) |
| 93 | + |
| 94 | + expect(() => guard.canActivate(context)).toThrow( |
| 95 | + InternalServerErrorException, |
| 96 | + ) |
| 97 | + }) |
| 98 | +}) |
0 commit comments