|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The create-side guard of `sys_file`, judged at the guard: the handler |
| 5 | + * {@link installSysFileStorageOwnedColumnsGuard} registers, called with the |
| 6 | + * hook contexts the engine builds. The real engine around it is |
| 7 | + * `sys-file-storage-owned-columns.integration.test.ts`. |
| 8 | + * |
| 9 | + * The rule pinned here is the uploader's: a stored file's uploader is the user |
| 10 | + * who created it, as an attachment's and a comment's author are. A non-system |
| 11 | + * create records the session's user as `owner_id` over whatever it carried, and |
| 12 | + * one whose session names no signed-in user is refused, as the attachment and |
| 13 | + * comment create hooks refuse theirs. System context keeps what it supplies, |
| 14 | + * and so does a call with no session at all (a bare kernel), which the |
| 15 | + * attachment hook passes the same way. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, it, expect, vi } from 'vitest'; |
| 19 | +import { installSysFileStorageOwnedColumnsGuard } from './sys-file-storage-owned-columns.js'; |
| 20 | + |
| 21 | +type Handler = (ctx: any) => void | Promise<void>; |
| 22 | + |
| 23 | +/** Install the guard on a stand-in engine and hand back its one handler. */ |
| 24 | +function installGuard() { |
| 25 | + const registered: Array<{ event: string; handler: Handler; options: { object: string } }> = []; |
| 26 | + const logger = { warn: vi.fn() }; |
| 27 | + installSysFileStorageOwnedColumnsGuard( |
| 28 | + { registerHook: (event, handler, options) => { registered.push({ event, handler, options }); } }, |
| 29 | + logger, |
| 30 | + ); |
| 31 | + expect(registered.map((r) => [r.event, r.options.object])).toEqual([['beforeInsert', 'sys_file']]); |
| 32 | + const run = async (session: unknown, data: Record<string, unknown>) => { |
| 33 | + await registered[0]!.handler({ object: 'sys_file', event: 'beforeInsert', session, input: { data } }); |
| 34 | + return data; |
| 35 | + }; |
| 36 | + return { run, logger }; |
| 37 | +} |
| 38 | + |
| 39 | +/** What a thrown refusal answers, reduced to the two members ADR-0112 makes assertable. */ |
| 40 | +async function refusalOf(write: Promise<unknown>): Promise<{ code: unknown; status: unknown } | null> { |
| 41 | + return write.then( |
| 42 | + () => null, |
| 43 | + (err: any) => ({ code: err?.code, status: err?.status }), |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +const CREATOR = 'u_creator'; |
| 48 | +const SOMEONE_ELSE = 'u_someone_else'; |
| 49 | + |
| 50 | +describe('a stored file\'s uploader is the user who created it (sys_file.owner_id on create)', () => { |
| 51 | + it('a non-system create records the session\'s user as the uploader, over an owner_id naming another user', async () => { |
| 52 | + const { run } = installGuard(); |
| 53 | + const row = await run({ userId: CREATOR }, { name: 'a.png', owner_id: SOMEONE_ELSE }); |
| 54 | + expect(row.owner_id).toBe(CREATOR); |
| 55 | + expect(row.name).toBe('a.png'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('a non-system create that names no uploader records the session\'s user', async () => { |
| 59 | + const { run } = installGuard(); |
| 60 | + expect((await run({ userId: CREATOR }, { name: 'a.png' })).owner_id).toBe(CREATOR); |
| 61 | + }); |
| 62 | + |
| 63 | + it.each([ |
| 64 | + ['a session that names no user (a service principal)', { actor: 'svc:flow:importer' }], |
| 65 | + ['a session that names only an organization', { organizationId: 'org_1' }], |
| 66 | + ['a session whose user id is empty', { userId: '' }], |
| 67 | + ])('a non-system create is refused 403 RECORD_NOT_ACCESSIBLE when there is no signed-in user to record: %s', async (_label, session) => { |
| 68 | + const { run, logger } = installGuard(); |
| 69 | + expect(await refusalOf(run(session, { name: 'a.png', owner_id: SOMEONE_ELSE, status: 'committed' }))).toEqual({ |
| 70 | + code: 'RECORD_NOT_ACCESSIBLE', |
| 71 | + status: 403, |
| 72 | + }); |
| 73 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('a system create keeps the uploader it supplies', async () => { |
| 77 | + const { run } = installGuard(); |
| 78 | + const row = await run({ userId: CREATOR, isSystem: true }, { name: 'a.png', owner_id: SOMEONE_ELSE }); |
| 79 | + expect(row.owner_id).toBe(SOMEONE_ELSE); |
| 80 | + }); |
| 81 | + |
| 82 | + it('a create with no session at all (a bare kernel) keeps the uploader it supplies, and still loses the storage-owned columns', async () => { |
| 83 | + const { run } = installGuard(); |
| 84 | + const row = await run(undefined, { name: 'a.png', owner_id: SOMEONE_ELSE, status: 'committed', key: 'k' }); |
| 85 | + expect(row.owner_id).toBe(SOMEONE_ELSE); |
| 86 | + expect(row.status).toBe('pending'); |
| 87 | + expect(row).not.toHaveProperty('key'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('the uploader is recorded beside the storage-owned strip, which still drops the caller\'s columns and starts the row pending', async () => { |
| 91 | + const { run, logger } = installGuard(); |
| 92 | + const row = await run({ userId: CREATOR }, { name: 'a.png', owner_id: SOMEONE_ELSE, size: 9, status: 'committed' }); |
| 93 | + expect(row).toEqual({ name: 'a.png', owner_id: CREATOR, status: 'pending' }); |
| 94 | + expect(logger.warn).toHaveBeenCalledTimes(1); |
| 95 | + expect(logger.warn.mock.calls[0]![1]).toMatchObject({ fields: ['size', 'status'] }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments