|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// A file field's declared `accept` / `maxSize` refusal, end to end through the |
| 4 | +// real stack: storage service's copy-on-claim before-hook |
| 5 | +// (`assertFileConstraints`, `FileConstraintError`) → ObjectQL triggerHooks → |
| 6 | +// REST `mapDataError` → HTTP body. |
| 7 | +// |
| 8 | +// ── What this pins ────────────────────────────────────────────────────────── |
| 9 | +// |
| 10 | +// The refusal is a verdict about the CALLER's own file — wrong type, too big — |
| 11 | +// so it answers `400 ERR_FILE_CONSTRAINT` with a sentence naming the field and |
| 12 | +// the constraint. Before `FileConstraintError` declared its `status`, the |
| 13 | +// declared-status passthrough never fired and the same refusal left through the |
| 14 | +// unclassified-fault terminal as `500 INTERNAL_ERROR` with the sentence |
| 15 | +// withheld: enforcement held, the wire contract did not. |
| 16 | +// |
| 17 | +// ── Why each assertion is load-bearing ────────────────────────────────────── |
| 18 | +// |
| 19 | +// `code` AND `status` (ADR-0112). A bare status cannot tell this verdict |
| 20 | +// apart from any other 400 the data door gives. |
| 21 | +// |
| 22 | +// The READ-BACK, not the response. A write that answers 400 and lands anyway |
| 23 | +// satisfies a response-only assertion perfectly, so every refusal re-reads |
| 24 | +// the object's rows and the `sys_file` ownership columns under a system |
| 25 | +// context. |
| 26 | +// |
| 27 | +// The SMALL-PDF CONTROL, on the same field. Without it the refusal cases pass |
| 28 | +// just as well when every file write is refused for an unrelated reason. |
| 29 | +// |
| 30 | +// The sentence is asserted by its NAMED SUBJECTS only — the field and the |
| 31 | +// constraint — never its full wording: the field and the constraint reach the |
| 32 | +// wire through the message alone (the declared-status passthrough forwards |
| 33 | +// `status`, the registered `code` and the bounded message, and no other member |
| 34 | +// of the thrown error). |
| 35 | + |
| 36 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 37 | +import { mkdtempSync } from 'node:fs'; |
| 38 | +import { join } from 'node:path'; |
| 39 | +import { tmpdir } from 'node:os'; |
| 40 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 41 | +import { defineStack } from '@objectstack/spec'; |
| 42 | +import { ObjectSchema, Field, RAW_FILE_VALUES_CONTEXT_KEY } from '@objectstack/spec/data'; |
| 43 | +import { StorageServicePlugin } from '@objectstack/service-storage'; |
| 44 | + |
| 45 | +const SYS = { isSystem: true } as const; |
| 46 | +const OBJECT = 'ffc_doc'; |
| 47 | + |
| 48 | +const FfcDoc = ObjectSchema.create({ |
| 49 | + name: OBJECT, |
| 50 | + label: 'Constrained Document', |
| 51 | + fields: { |
| 52 | + name: Field.text({ label: 'Name' }), |
| 53 | + doc: Field.file({ label: 'Document', accept: ['.pdf'], maxSize: 10 }), |
| 54 | + }, |
| 55 | +}); |
| 56 | + |
| 57 | +const ffcStack = defineStack({ |
| 58 | + manifest: { |
| 59 | + id: 'com.dogfood.file-field-constraint-refusal', |
| 60 | + namespace: 'ffc', |
| 61 | + version: '0.0.0', |
| 62 | + type: 'app', |
| 63 | + name: 'File Field Constraint Refusal Fixture', |
| 64 | + description: 'A file field declaring accept and maxSize, written through the data API.', |
| 65 | + }, |
| 66 | + objects: [FfcDoc], |
| 67 | +}); |
| 68 | + |
| 69 | +/** The three `sys_file` rows the cases reference — one per verdict. */ |
| 70 | +const FILES = { |
| 71 | + wrongType: { id: 'ffc_file_txt', name: 'notes.txt', mime_type: 'text/plain', size: 6 }, |
| 72 | + oversize: { id: 'ffc_file_big', name: 'big.pdf', mime_type: 'application/pdf', size: 5005 }, |
| 73 | + smallPdf: { id: 'ffc_file_ok', name: 'small.pdf', mime_type: 'application/pdf', size: 6 }, |
| 74 | + smallPdf2: { id: 'ffc_file_ok2', name: 'small2.pdf', mime_type: 'application/pdf', size: 6 }, |
| 75 | +} as const; |
| 76 | + |
| 77 | +describe('a file field\'s accept / maxSize refusal answers 400 ERR_FILE_CONSTRAINT', () => { |
| 78 | + let stack: VerifyStack; |
| 79 | + let token: string; |
| 80 | + let ql: any; |
| 81 | + |
| 82 | + beforeAll(async () => { |
| 83 | + const rootDir = mkdtempSync(join(tmpdir(), 'ffc-pin-')); |
| 84 | + stack = await bootStack(ffcStack, { |
| 85 | + extraPlugins: [ |
| 86 | + new StorageServicePlugin({ adapter: 'local', local: { rootDir }, bindToSettings: false }), |
| 87 | + ], |
| 88 | + }); |
| 89 | + token = await stack.signIn(); |
| 90 | + ql = await stack.kernel.getServiceAsync('objectql'); |
| 91 | + |
| 92 | + for (const f of Object.values(FILES)) { |
| 93 | + await ql.insert( |
| 94 | + 'sys_file', |
| 95 | + { ...f, key: `user/${f.id}`, status: 'committed', scope: 'user' }, |
| 96 | + { context: SYS }, |
| 97 | + ); |
| 98 | + } |
| 99 | + }, 60_000); |
| 100 | + |
| 101 | + afterAll(async () => { |
| 102 | + await stack?.stop(); |
| 103 | + }); |
| 104 | + |
| 105 | + const write = async (method: 'POST' | 'PATCH', path: string, data: Record<string, unknown>) => { |
| 106 | + const res = await stack.apiAs(token, method, path, data); |
| 107 | + return { status: res.status, body: (await res.json()) as any }; |
| 108 | + }; |
| 109 | + |
| 110 | + /** The rows named `name`, read above every access layer. */ |
| 111 | + const rowsNamed = async (name: string) => |
| 112 | + ql.find(OBJECT, { where: { name }, context: { ...SYS, [RAW_FILE_VALUES_CONTEXT_KEY]: true } }); |
| 113 | + |
| 114 | + /** The `sys_file` ownership columns, read above every access layer. */ |
| 115 | + const owner = async (id: string) => { |
| 116 | + const row = await ql.findOne('sys_file', { where: { id }, context: SYS }); |
| 117 | + return { ref_object: row?.ref_object ?? null, ref_id: row?.ref_id ?? null, ref_field: row?.ref_field ?? null }; |
| 118 | + }; |
| 119 | + const UNOWNED = { ref_object: null, ref_id: null, ref_field: null }; |
| 120 | + |
| 121 | + let controlId: string; |
| 122 | + |
| 123 | + it('a file outside the accept list answers 400 ERR_FILE_CONSTRAINT naming the field and the accept list, and writes no row', async () => { |
| 124 | + const r = await write('POST', `/data/${OBJECT}`, { name: 'wrong type', doc: FILES.wrongType.id }); |
| 125 | + |
| 126 | + expect(r.status, JSON.stringify(r.body)).toBe(400); |
| 127 | + expect(r.body.code).toBe('ERR_FILE_CONSTRAINT'); |
| 128 | + expect(r.body.error).toContain("'doc'"); |
| 129 | + expect(r.body.error).toContain('accept list'); |
| 130 | + expect(r.body.object).toBe(OBJECT); |
| 131 | + |
| 132 | + expect(await rowsNamed('wrong type')).toHaveLength(0); |
| 133 | + expect(await owner(FILES.wrongType.id)).toEqual(UNOWNED); |
| 134 | + }); |
| 135 | + |
| 136 | + it('a file over maxSize answers 400 ERR_FILE_CONSTRAINT naming the field and the maximum size, and writes no row', async () => { |
| 137 | + const r = await write('POST', `/data/${OBJECT}`, { name: 'oversize', doc: FILES.oversize.id }); |
| 138 | + |
| 139 | + expect(r.status, JSON.stringify(r.body)).toBe(400); |
| 140 | + expect(r.body.code).toBe('ERR_FILE_CONSTRAINT'); |
| 141 | + expect(r.body.error).toContain("'doc'"); |
| 142 | + expect(r.body.error).toContain('maximum size'); |
| 143 | + expect(r.body.object).toBe(OBJECT); |
| 144 | + |
| 145 | + expect(await rowsNamed('oversize')).toHaveLength(0); |
| 146 | + expect(await owner(FILES.oversize.id)).toEqual(UNOWNED); |
| 147 | + }); |
| 148 | + |
| 149 | + it('control: a 6-byte PDF satisfies both declarations, answers 201, and is claimed by the row', async () => { |
| 150 | + const r = await write('POST', `/data/${OBJECT}`, { name: 'control', doc: FILES.smallPdf.id }); |
| 151 | + |
| 152 | + expect(r.status, JSON.stringify(r.body)).toBe(201); |
| 153 | + controlId = r.body.record?.id ?? r.body.id; |
| 154 | + expect(controlId).toBeTruthy(); |
| 155 | + |
| 156 | + const stored = await rowsNamed('control'); |
| 157 | + expect(stored).toHaveLength(1); |
| 158 | + expect(stored[0].doc).toBe(FILES.smallPdf.id); |
| 159 | + expect(await owner(FILES.smallPdf.id)).toEqual({ ref_object: OBJECT, ref_id: controlId, ref_field: 'doc' }); |
| 160 | + }); |
| 161 | + |
| 162 | + it('an UPDATE onto an oversize file is refused the same way and leaves the record and both files as they were', async () => { |
| 163 | + const r = await write('PATCH', `/data/${OBJECT}/${controlId}`, { doc: FILES.oversize.id }); |
| 164 | + |
| 165 | + expect(r.status, JSON.stringify(r.body)).toBe(400); |
| 166 | + expect(r.body.code).toBe('ERR_FILE_CONSTRAINT'); |
| 167 | + expect(r.body.error).toContain("'doc'"); |
| 168 | + expect(r.body.error).toContain('maximum size'); |
| 169 | + |
| 170 | + const stored = await rowsNamed('control'); |
| 171 | + expect(stored).toHaveLength(1); |
| 172 | + expect(stored[0].doc).toBe(FILES.smallPdf.id); |
| 173 | + expect(await owner(FILES.smallPdf.id)).toEqual({ ref_object: OBJECT, ref_id: controlId, ref_field: 'doc' }); |
| 174 | + expect(await owner(FILES.oversize.id)).toEqual(UNOWNED); |
| 175 | + }); |
| 176 | + |
| 177 | + it('control: an UPDATE onto a permitted file still lands', async () => { |
| 178 | + const r = await write('PATCH', `/data/${OBJECT}/${controlId}`, { doc: FILES.smallPdf2.id }); |
| 179 | + |
| 180 | + expect(r.status, JSON.stringify(r.body)).toBe(200); |
| 181 | + const stored = await rowsNamed('control'); |
| 182 | + expect(stored[0].doc).toBe(FILES.smallPdf2.id); |
| 183 | + }); |
| 184 | +}); |
0 commit comments