|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// An approver reassigns only their own slot. A reassign that neither the |
| 4 | +// system nor the administrator override admits moves the caller's own slot |
| 5 | +// and no other, through the door the console calls |
| 6 | +// (`POST /approvals/requests/:id/reassign`), on a real boot: |
| 7 | +// |
| 8 | +// ⭐ a slot holder naming another approver's slot as `from` is refused |
| 9 | +// `403 FORBIDDEN`, and nothing moved: the slate is unchanged and no |
| 10 | +// `reassign` action was recorded; |
| 11 | +// ⭐ CONTROL: the same holder naming their own slot moves it; |
| 12 | +// ⭐ the administrator override still moves another approver's slot. |
| 13 | +// |
| 14 | +// The system caller has no HTTP door; its leg is pinned beside the service |
| 15 | +// (`plugin-approvals` `approval-service.test.ts`, same rule). |
| 16 | + |
| 17 | +import { describe, it, expect } from 'vitest'; |
| 18 | +import { bootStack } from '@objectstack/verify'; |
| 19 | +import { ApprovalsServicePlugin } from '@objectstack/plugin-approvals'; |
| 20 | +import { RecordChangeTriggerPlugin } from '@objectstack/trigger-record-change'; |
| 21 | +import { MessagingServicePlugin } from '@objectstack/service-messaging'; |
| 22 | +import { |
| 23 | + reassignOwnSlotStack, |
| 24 | + reassignOwnSlotSecurity, |
| 25 | + FIRST_APPROVER_EMAIL, |
| 26 | + SECOND_APPROVER_EMAIL, |
| 27 | +} from './fixtures/reassign-own-slot-fixture.js'; |
| 28 | + |
| 29 | +const SYS = { context: { isSystem: true } } as const; |
| 30 | +const SUBMITTER = 'reassign-own-slot-submitter@example.com'; |
| 31 | +const MEMBER = 'reassign-own-slot-member@example.com'; |
| 32 | +const OTHER_MEMBER = 'reassign-own-slot-other@example.com'; |
| 33 | + |
| 34 | +interface Row { |
| 35 | + id: string; |
| 36 | + object_name?: string; |
| 37 | + pending_approvers?: string[]; |
| 38 | +} |
| 39 | + |
| 40 | +describe('an approver reassigns only their own slot', () => { |
| 41 | + it( |
| 42 | + 'refuses another approver\'s slot, moves the caller\'s own, and leaves the override unchanged', |
| 43 | + async () => { |
| 44 | + const stack = await bootStack(reassignOwnSlotStack as unknown as Parameters<typeof bootStack>[0], { |
| 45 | + automation: true, |
| 46 | + security: reassignOwnSlotSecurity(), |
| 47 | + extraPlugins: [ |
| 48 | + new MessagingServicePlugin({ reliableDelivery: false }), |
| 49 | + new RecordChangeTriggerPlugin(), |
| 50 | + new ApprovalsServicePlugin(), |
| 51 | + ], |
| 52 | + }); |
| 53 | + try { |
| 54 | + const ql: any = await stack.kernel.getServiceAsync('objectql'); |
| 55 | + const idOf = async (email: string): Promise<string> => { |
| 56 | + const u = await ql.findOne('sys_user', { where: { email }, context: SYS.context }); |
| 57 | + return String(u?.id ?? ''); |
| 58 | + }; |
| 59 | + // The dev admin first, so the sign-ups below are plain members. |
| 60 | + const adminToken = await stack.signIn(); |
| 61 | + const submitterToken = await stack.signUp(SUBMITTER); |
| 62 | + const firstToken = await stack.signUp(FIRST_APPROVER_EMAIL); |
| 63 | + await stack.signUp(SECOND_APPROVER_EMAIL); |
| 64 | + await stack.signUp(MEMBER); |
| 65 | + await stack.signUp(OTHER_MEMBER); |
| 66 | + const memberId = await idOf(MEMBER); |
| 67 | + const otherMemberId = await idOf(OTHER_MEMBER); |
| 68 | + expect(memberId).not.toBe(''); |
| 69 | + expect(otherMemberId).not.toBe(''); |
| 70 | + |
| 71 | + const created = await stack.apiAs(submitterToken, 'POST', '/data/reassign_own_slot', { name: 'two approvers' }); |
| 72 | + expect(created.status).toBe(201); |
| 73 | + const pendingRes = await stack.apiAs(adminToken, 'GET', '/approvals/requests?status=pending'); |
| 74 | + expect(pendingRes.status).toBe(200); |
| 75 | + const open = ((await pendingRes.json()) as { data: Row[] }).data |
| 76 | + .find((r) => r.object_name === 'reassign_own_slot'); |
| 77 | + expect(open?.pending_approvers, 'precondition: the request is open on both named approvers') |
| 78 | + .toEqual([FIRST_APPROVER_EMAIL, SECOND_APPROVER_EMAIL]); |
| 79 | + const requestId = open!.id; |
| 80 | + |
| 81 | + const reassign = (token: string, body: Record<string, unknown>) => |
| 82 | + stack.apiAs(token, 'POST', `/approvals/requests/${requestId}/reassign`, body); |
| 83 | + const slate = async (): Promise<string> => { |
| 84 | + const row = await ql.findOne('sys_approval_request', { where: { id: requestId }, context: SYS.context }); |
| 85 | + return String(row?.pending_approvers ?? ''); |
| 86 | + }; |
| 87 | + const moves = async (): Promise<unknown[]> => |
| 88 | + (await ql.find('sys_approval_action', { where: { request_id: requestId }, context: SYS.context })) |
| 89 | + .filter((a: any) => a.action === 'reassign'); |
| 90 | + |
| 91 | + // ⭐ 1 — another approver's slot is refused, and nothing moved. |
| 92 | + const refused = await reassign(firstToken, { from: SECOND_APPROVER_EMAIL, to: memberId }); |
| 93 | + expect(refused.status).toBe(403); |
| 94 | + expect(((await refused.json()) as { code?: string }).code).toBe('FORBIDDEN'); |
| 95 | + expect(await slate()).toBe(`${FIRST_APPROVER_EMAIL},${SECOND_APPROVER_EMAIL}`); |
| 96 | + expect(await moves()).toEqual([]); |
| 97 | + |
| 98 | + // ⭐ 2 — CONTROL: the same holder's own slot moves. |
| 99 | + const own = await reassign(firstToken, { from: FIRST_APPROVER_EMAIL, to: memberId }); |
| 100 | + expect(own.status).toBe(200); |
| 101 | + expect(((await own.json()) as { request?: Row }).request?.pending_approvers) |
| 102 | + .toEqual([memberId, SECOND_APPROVER_EMAIL]); |
| 103 | + expect(await moves()).toHaveLength(1); |
| 104 | + |
| 105 | + // ⭐ 3 — the administrator override still moves another approver's slot. |
| 106 | + const override = await reassign(adminToken, { from: SECOND_APPROVER_EMAIL, to: otherMemberId }); |
| 107 | + expect(override.status).toBe(200); |
| 108 | + expect(((await override.json()) as { request?: Row }).request?.pending_approvers) |
| 109 | + .toEqual([memberId, otherMemberId]); |
| 110 | + expect(await moves()).toHaveLength(2); |
| 111 | + } finally { |
| 112 | + await stack.stop(); |
| 113 | + } |
| 114 | + }, |
| 115 | + 120_000, |
| 116 | + ); |
| 117 | +}); |
0 commit comments