|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// [ADR-0111 D7] A share a non-system caller grants is always a MANUAL share, |
| 4 | +// pinned at the direct-grant door of the booted showcase: |
| 5 | +// `POST /api/v1/data/:object/:id/shares`. |
| 6 | +// |
| 7 | +// The door forwards the caller's `source` / `sourceId` to `SharingService.grant`, |
| 8 | +// and the service refuses, on its non-system path, an input whose `source` is |
| 9 | +// present and is not `'manual'` and an input that carries any `sourceId`: 400 |
| 10 | +// `VALIDATION_FAILED`, nothing written. The service-level pins (every refused |
| 11 | +// shape, nothing read, the system-path controls) live beside the service in |
| 12 | +// `plugin-sharing/src/sharing-service.test.ts`; this file holds the composed |
| 13 | +// door — the real REST route, the real service, the real engine — because only |
| 14 | +// the booted runtime has all three. |
| 15 | +// |
| 16 | +// Controls: no source and an explicit 'manual' are granted (and the manual |
| 17 | +// share revokes); the system context still writes a rule share with its |
| 18 | +// sourceId, and that share's revoke through the door is still CONFLICT. The |
| 19 | +// generic data door on `sys_record_share` itself is pinned as refused too. |
| 20 | + |
| 21 | +import { describe, it, expect, beforeAll } from 'vitest'; |
| 22 | +import { type VerifyStack } from '@objectstack/verify'; |
| 23 | +import type { ISharingService } from '@objectstack/spec/contracts'; |
| 24 | +import type { ExecutionContext } from '@objectstack/spec/kernel'; |
| 25 | +import { getSharedShowcase } from './shared-showcase.js'; |
| 26 | + |
| 27 | +const OBJECT = 'showcase_private_note'; |
| 28 | +const SYS = { isSystem: true } as const; |
| 29 | +const idOf = (b: any) => b?.id ?? b?.record?.id; |
| 30 | + |
| 31 | +describe('[ADR-0111 D7] a share granted directly by a non-system caller is always a manual share', () => { |
| 32 | + let stack: VerifyStack; |
| 33 | + let ql: any; |
| 34 | + let sharing: ISharingService; |
| 35 | + let ownerToken: string; |
| 36 | + let recipientId: string; |
| 37 | + let organizationId: string | undefined; |
| 38 | + |
| 39 | + const sharesOn = async (recordId: string) => |
| 40 | + ql.find('sys_record_share', { where: { object_name: OBJECT, record_id: recordId }, context: SYS }); |
| 41 | + |
| 42 | + const newNote = async (title: string): Promise<string> => { |
| 43 | + const res = await stack.apiAs(ownerToken, 'POST', `/data/${OBJECT}`, { title }); |
| 44 | + expect(res.status, await res.clone().text()).toBe(201); |
| 45 | + return String(idOf(await res.json())); |
| 46 | + }; |
| 47 | + |
| 48 | + const grant = (recordId: string, extra: Record<string, unknown>) => |
| 49 | + stack.apiAs(ownerToken, 'POST', `/data/${OBJECT}/${recordId}/shares`, { |
| 50 | + recipientId, accessLevel: 'read', ...extra, |
| 51 | + }); |
| 52 | + |
| 53 | + beforeAll(async () => { |
| 54 | + stack = await getSharedShowcase(); |
| 55 | + ql = await stack.kernel.getServiceAsync('objectql'); |
| 56 | + sharing = stack.kernel.getService<ISharingService>('sharing'); |
| 57 | + ownerToken = await stack.signUp('dsm-owner@verify.test'); |
| 58 | + await stack.signUp('dsm-recipient@verify.test'); |
| 59 | + const user = await ql.findOne('sys_user', { where: { email: 'dsm-recipient@verify.test' }, context: SYS }); |
| 60 | + recipientId = String(user?.id); |
| 61 | + }, 120_000); |
| 62 | + |
| 63 | + const refused: Array<[string, Record<string, unknown>]> = [ |
| 64 | + ["source 'rule' with a sourceId", { source: 'rule', sourceId: 'srule_dsm' }], |
| 65 | + ["source 'team'", { source: 'team' }], |
| 66 | + ['an unknown source string', { source: 'bogus' }], |
| 67 | + ['a sourceId with no source', { sourceId: 'srule_dsm' }], |
| 68 | + ['a snake_case source_id with no source', { source_id: 'srule_dsm' }], |
| 69 | + ["a sourceId with source 'manual'", { source: 'manual', sourceId: 'srule_dsm' }], |
| 70 | + ]; |
| 71 | + |
| 72 | + for (const [label, extra] of refused) { |
| 73 | + it(`refuses ${label}: 400 VALIDATION_FAILED, no share written`, async () => { |
| 74 | + const recordId = await newNote(`dsm refused ${label}`); |
| 75 | + const res = await grant(recordId, extra); |
| 76 | + const body = await res.json() as { error?: { code?: string } }; |
| 77 | + expect(res.status, JSON.stringify(body)).toBe(400); |
| 78 | + expect(body.error?.code).toBe('VALIDATION_FAILED'); |
| 79 | + expect(await sharesOn(recordId)).toHaveLength(0); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + it('CONTROL: no source is granted as a manual share, and the owner revokes it', async () => { |
| 84 | + const recordId = await newNote('dsm control no source'); |
| 85 | + const res = await grant(recordId, {}); |
| 86 | + expect(res.status, await res.clone().text()).toBe(201); |
| 87 | + const share = await res.json() as { id: string; source: string }; |
| 88 | + expect(share.source).toBe('manual'); |
| 89 | + const rows = await sharesOn(recordId); |
| 90 | + expect(rows).toHaveLength(1); |
| 91 | + expect(rows[0]).toMatchObject({ source: 'manual', recipient_id: recipientId }); |
| 92 | + organizationId = rows[0].organization_id ?? undefined; |
| 93 | + |
| 94 | + const revoke = await stack.apiAs(ownerToken, 'DELETE', `/data/${OBJECT}/${recordId}/shares/${share.id}`); |
| 95 | + expect(revoke.status, await revoke.clone().text()).toBe(204); |
| 96 | + expect(await sharesOn(recordId)).toHaveLength(0); |
| 97 | + }); |
| 98 | + |
| 99 | + it("CONTROL: an explicit 'manual' source is granted as a manual share", async () => { |
| 100 | + const recordId = await newNote('dsm control manual'); |
| 101 | + const res = await grant(recordId, { source: 'manual' }); |
| 102 | + expect(res.status, await res.clone().text()).toBe(201); |
| 103 | + const rows = await sharesOn(recordId); |
| 104 | + expect(rows).toHaveLength(1); |
| 105 | + expect(rows[0]).toMatchObject({ source: 'manual', source_id: null }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('CONTROL: the system context still writes a rule share, and its revoke through the door is still CONFLICT', async () => { |
| 109 | + const recordId = await newNote('dsm control system rule share'); |
| 110 | + const share = await sharing.grant( |
| 111 | + { object: OBJECT, recordId, recipientId, accessLevel: 'read', source: 'rule', sourceId: 'srule_dsm' }, |
| 112 | + { ...SYS, ...(organizationId ? { tenantId: organizationId } : {}) } as ExecutionContext, |
| 113 | + ); |
| 114 | + const rows = await sharesOn(recordId); |
| 115 | + expect(rows).toHaveLength(1); |
| 116 | + expect(rows[0]).toMatchObject({ source: 'rule', source_id: 'srule_dsm' }); |
| 117 | + |
| 118 | + const revoke = await stack.apiAs(ownerToken, 'DELETE', `/data/${OBJECT}/${recordId}/shares/${share.id}`); |
| 119 | + const body = await revoke.json() as { error?: { code?: string } }; |
| 120 | + expect(revoke.status, JSON.stringify(body)).toBe(409); |
| 121 | + expect(body.error?.code).toBe('CONFLICT'); |
| 122 | + expect(await sharesOn(recordId)).toHaveLength(1); |
| 123 | + }); |
| 124 | + |
| 125 | + it('the generic data door on sys_record_share refuses the caller\'s create and writes no share row', async () => { |
| 126 | + const recordId = await newNote('dsm generic door'); |
| 127 | + const res = await stack.apiAs(ownerToken, 'POST', '/data/sys_record_share', { |
| 128 | + object_name: OBJECT, record_id: recordId, recipient_type: 'user', recipient_id: recipientId, |
| 129 | + access_level: 'read', source: 'rule', source_id: 'srule_dsm', |
| 130 | + }); |
| 131 | + const text = await res.text(); |
| 132 | + // `enable.apiMethods` on `sys_record_share` grants no create (ADR-0103). |
| 133 | + expect(res.status, text).toBe(405); |
| 134 | + expect(text).toContain('OBJECT_API_METHOD_NOT_ALLOWED'); |
| 135 | + expect(await sharesOn(recordId)).toHaveLength(0); |
| 136 | + }); |
| 137 | +}); |
0 commit comments