|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#26594 / ruling of record 6457467702] `GET /data/:object/:id/field-access` |
| 5 | + * — the field projection of ONE record, the answer a record's edit form reads. |
| 6 | + * |
| 7 | + * What the route owes, each pinned below: |
| 8 | + * |
| 9 | + * - the record is read FIRST, exactly as `GET /data/:object/:id` reads it, so |
| 10 | + * a refusal of that read (404 for an absent or unreadable row, 403) is this |
| 11 | + * route's answer too and the projection is never asked; |
| 12 | + * - the answer is the security service's `getRecordFieldAccess`, relayed in |
| 13 | + * the shared envelope, asked with the caller's object, record and context; |
| 14 | + * - a row that leaves the caller's sight between the two reads answers the |
| 15 | + * read path's own 404, never an empty projection; |
| 16 | + * - no service with the method → a loud 501, never an invented projection; |
| 17 | + * - a CLOSED, empty query-parameter set: any name is a located 400 and the |
| 18 | + * service is never called. |
| 19 | + * |
| 20 | + * The end-to-end half — the real security stack judging a `when` on a row each |
| 21 | + * side of its condition — is `record-field-access-when.dogfood.test.ts`. |
| 22 | + */ |
| 23 | + |
| 24 | +import { describe, it, expect, vi } from 'vitest'; |
| 25 | +import { RestServer } from './rest-server.js'; |
| 26 | +import { RECORD_FIELD_ACCESS_PARAMS } from './data-action-routes.js'; |
| 27 | +import { unknownQueryParamMessage } from './query-allowlist.js'; |
| 28 | + |
| 29 | +const TASK = { |
| 30 | + name: 'task', |
| 31 | + label: 'Task', |
| 32 | + fields: { |
| 33 | + id: { name: 'id', type: 'text', label: 'ID' }, |
| 34 | + title: { name: 'title', type: 'text', label: 'Title' }, |
| 35 | + amount: { name: 'amount', type: 'number', label: 'Amount' }, |
| 36 | + }, |
| 37 | +}; |
| 38 | +const CTX = { userId: 'u1', permissions: ['member_default'] }; |
| 39 | +const ACCESS = { object: 'task', recordId: 'r1', readable: ['id', 'title', 'amount'], editable: ['title', 'amount'] }; |
| 40 | + |
| 41 | +function mockServer() { |
| 42 | + const noop = () => { /* routes are driven directly, never through the adapter */ }; |
| 43 | + return { |
| 44 | + get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, |
| 45 | + listen: async () => { /* never started */ }, |
| 46 | + close: async () => { /* never started */ }, |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +function mockRes() { |
| 51 | + const res: any = { |
| 52 | + statusCode: 200, |
| 53 | + header: vi.fn(function (this: any) { return this; }), |
| 54 | + setHeader: vi.fn(function (this: any) { return this; }), |
| 55 | + json: vi.fn(function (this: any, body: any) { this._body = body; return this; }), |
| 56 | + status: vi.fn(function (this: any, code: number) { this.statusCode = code; return this; }), |
| 57 | + }; |
| 58 | + return res; |
| 59 | +} |
| 60 | + |
| 61 | +function boot(opts: { getData?: () => Promise<unknown>; security?: unknown } = {}) { |
| 62 | + const getData = vi.fn(opts.getData ?? (async () => ({ object: 'task', id: 'r1', record: { id: 'r1' } }))); |
| 63 | + const protocol: any = { |
| 64 | + getDiscovery: vi.fn().mockResolvedValue({ |
| 65 | + version: 'v0', routes: { data: '', metadata: '', ui: '', auth: '/auth' }, |
| 66 | + }), |
| 67 | + getMetaTypes: vi.fn().mockResolvedValue([]), |
| 68 | + getMetaItems: vi.fn().mockResolvedValue({ items: [TASK] }), |
| 69 | + getMetaItem: vi.fn().mockResolvedValue({ type: 'object', name: 'task', item: TASK }), |
| 70 | + getData, |
| 71 | + }; |
| 72 | + const rest = new RestServer(mockServer() as any, protocol as any, { api: { requireAuth: false } } as any); |
| 73 | + (rest as any).resolveExecCtx = async () => CTX; |
| 74 | + (rest as any).resolveSecurityService = async () => opts.security; |
| 75 | + rest.registerRoutes(); |
| 76 | + const route = (rest as any).getRoutes().find( |
| 77 | + (r: any) => r.method === 'GET' && r.path === '/api/v1/data/:object/:id/field-access', |
| 78 | + ); |
| 79 | + const call = async (query: Record<string, unknown> = {}, params = { object: 'task', id: 'r1' }) => { |
| 80 | + if (!route) throw new Error('route not registered: GET /api/v1/data/:object/:id/field-access'); |
| 81 | + const res = mockRes(); |
| 82 | + await route.handler({ method: 'GET', params, query, headers: {}, body: {} } as any, res); |
| 83 | + return { status: res.statusCode, body: res.json.mock.calls.at(-1)?.[0] }; |
| 84 | + }; |
| 85 | + return { getData, call }; |
| 86 | +} |
| 87 | + |
| 88 | +const recordNotFound = () => Object.assign(new Error('Record r1 not found in task'), { |
| 89 | + code: 'RECORD_NOT_FOUND', status: 404, object: 'task', |
| 90 | +}); |
| 91 | + |
| 92 | +describe('[#26594] GET /data/:object/:id/field-access', () => { |
| 93 | + it("relays the service's projection of this record, asked with the caller's object, record and context", async () => { |
| 94 | + const getRecordFieldAccess = vi.fn().mockResolvedValue(ACCESS); |
| 95 | + const { call, getData } = boot({ security: { getRecordFieldAccess } }); |
| 96 | + const answer = await call(); |
| 97 | + expect(answer).toEqual({ status: 200, body: { success: true, data: ACCESS } }); |
| 98 | + expect(getRecordFieldAccess).toHaveBeenCalledWith('task', 'r1', CTX); |
| 99 | + // The record read the GET record route makes, as that route makes it. |
| 100 | + expect(getData).toHaveBeenCalledWith({ object: 'task', id: 'r1', context: CTX }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('a record the read path refuses answers that refusal, and the projection is never asked', async () => { |
| 104 | + const getRecordFieldAccess = vi.fn().mockResolvedValue(ACCESS); |
| 105 | + const { call } = boot({ getData: async () => { throw recordNotFound(); }, security: { getRecordFieldAccess } }); |
| 106 | + const answer = await call(); |
| 107 | + expect(answer.status).toBe(404); |
| 108 | + expect(answer.body?.error?.code ?? answer.body?.code).toBe('RECORD_NOT_FOUND'); |
| 109 | + expect(getRecordFieldAccess).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + |
| 112 | + it("a row the service cannot see after the read answers the read path's own 404, not an empty projection", async () => { |
| 113 | + const viaService = await boot({ security: { getRecordFieldAccess: vi.fn().mockResolvedValue(null) } }).call(); |
| 114 | + const viaRead = await boot({ getData: async () => { throw recordNotFound(); }, security: {} }).call(); |
| 115 | + expect(viaService.status).toBe(404); |
| 116 | + expect(viaService).toEqual(viaRead); |
| 117 | + }); |
| 118 | + |
| 119 | + it('no security service with the method answers a loud 501, never an invented projection', async () => { |
| 120 | + for (const security of [undefined, {}]) { |
| 121 | + const answer = await boot({ security }).call(); |
| 122 | + expect(answer.status).toBe(501); |
| 123 | + expect(answer.body?.success).toBe(false); |
| 124 | + expect(answer.body?.error?.code).toBe('NOT_IMPLEMENTED'); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + it('takes no query parameter: a name is a located 400 and the service is never called', async () => { |
| 129 | + expect(RECORD_FIELD_ACCESS_PARAMS).toEqual([]); |
| 130 | + const getRecordFieldAccess = vi.fn().mockResolvedValue(ACCESS); |
| 131 | + const { call, getData } = boot({ security: { getRecordFieldAccess } }); |
| 132 | + const answer = await call({ fields: 'amount' }); |
| 133 | + expect(answer.status).toBe(400); |
| 134 | + expect(answer.body?.error?.code).toBe('VALIDATION_ERROR'); |
| 135 | + expect(answer.body?.error?.message).toBe(unknownQueryParamMessage(['fields'], [])); |
| 136 | + expect(getData).not.toHaveBeenCalled(); |
| 137 | + expect(getRecordFieldAccess).not.toHaveBeenCalled(); |
| 138 | + }); |
| 139 | +}); |
0 commit comments