|
39 | 39 | import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
40 | 40 | import { ObjectQL } from '@objectstack/objectql'; |
41 | 41 | import { SqlDriver } from '@objectstack/driver-sql'; |
42 | | -import { bindApprovalLockHook } from './lifecycle-hooks.js'; |
| 42 | +import { bindApprovalLockHook, bindDetailRowLockHook } from './lifecycle-hooks.js'; |
43 | 43 | import type { FieldVisibilitySource } from './payload-redaction.js'; |
44 | 44 | import { installDefaultOrganization } from './default-organization.testkit.js'; |
45 | 45 |
|
@@ -192,3 +192,178 @@ describe('[#23618] the RECORD_LOCKED refusal names the record from what its writ |
192 | 192 | expect(vis.calls).toEqual([{ object: 'opportunity', userId: RESTRICTED, permissions: [`grant_${RESTRICTED}`] }]); |
193 | 193 | }); |
194 | 194 | }); |
| 195 | + |
| 196 | +/** |
| 197 | + * A locked record's refusal names it only by a field the writer may read. |
| 198 | + * |
| 199 | + * A column declared `internal: true`, or of a credential type, is one no |
| 200 | + * caller reads on any read path, so it never titles the refusal. When the |
| 201 | + * served-fields source cannot answer, the refusal names no title. A writer |
| 202 | + * refused a title still gets the same code, status and sentence, naming the |
| 203 | + * object's label. |
| 204 | + * |
| 205 | + * The stand-in source serves the writer every declared field (full |
| 206 | + * field-level access). |
| 207 | + */ |
| 208 | +describe('a locked record\'s refusal names it only by a field the writer may read', () => { |
| 209 | + const STORED = 'stored-value-7f3a'; |
| 210 | + const ORDINARY_TITLE = 'Ordinary title'; |
| 211 | + |
| 212 | + const fieldsOf = (extra: Record<string, unknown>) => ({ |
| 213 | + id: { name: 'id', type: 'text' as const, primaryKey: true }, |
| 214 | + ...extra, |
| 215 | + amount: { name: 'amount', type: 'number' as const }, |
| 216 | + }); |
| 217 | + |
| 218 | + /** An explicit title pointer at an `internal: true` text column. */ |
| 219 | + const INTERNAL_NAMED = { |
| 220 | + name: 'vault_entry', label: 'Vault Entry', nameField: 'code', |
| 221 | + fields: fieldsOf({ code: { name: 'code', type: 'text' as const, internal: true } }), |
| 222 | + }; |
| 223 | + /** No pointer: the derivation lands on a name-ish column, which is `internal: true`. */ |
| 224 | + const INTERNAL_DERIVED = { |
| 225 | + name: 'vault_token', label: 'Vault Token', |
| 226 | + fields: fieldsOf({ holder_name: { name: 'holder_name', type: 'text' as const, internal: true } }), |
| 227 | + }; |
| 228 | + /** An explicit title pointer at a credential-typed column. */ |
| 229 | + const CREDENTIAL_NAMED = { |
| 230 | + name: 'service_login', label: 'Service Login', nameField: 'passcode', |
| 231 | + fields: fieldsOf({ passcode: { name: 'passcode', type: 'password' as const } }), |
| 232 | + }; |
| 233 | + /** The control: an ordinary display field. */ |
| 234 | + const ORDINARY = { |
| 235 | + name: 'plain_deal', label: 'Plain Deal', nameField: 'name', |
| 236 | + fields: fieldsOf({ name: { name: 'name', type: 'text' as const } }), |
| 237 | + }; |
| 238 | + /** A line under a locked master whose display field is `internal: true`. */ |
| 239 | + const LINE = { |
| 240 | + name: 'vault_entry_line', label: 'Vault Entry Line', |
| 241 | + fields: { |
| 242 | + id: { name: 'id', type: 'text' as const, primaryKey: true }, |
| 243 | + qty: { name: 'qty', type: 'number' as const }, |
| 244 | + vault_entry: { name: 'vault_entry', type: 'master_detail' as const, reference: 'vault_entry' }, |
| 245 | + }, |
| 246 | + }; |
| 247 | + |
| 248 | + const OBJECTS = [INTERNAL_NAMED, INTERNAL_DERIVED, CREDENTIAL_NAMED, ORDINARY, LINE, APPROVAL_REQUEST]; |
| 249 | + |
| 250 | + type Mode = 'full' | 'throws' | 'none'; |
| 251 | + let mode: Mode; |
| 252 | + let engine: ObjectQL; |
| 253 | + const locked: Record<string, string> = {}; |
| 254 | + let lineId: string; |
| 255 | + |
| 256 | + /** Full field-level access for the writer, as the security service answers it. */ |
| 257 | + const source = (): FieldVisibilitySource | undefined => { |
| 258 | + if (mode === 'none') return undefined; |
| 259 | + const every = (object: string) => Object.keys((OBJECTS.find((o) => o.name === object) as any)?.fields ?? {}); |
| 260 | + return { |
| 261 | + async getReadableFields(object: string, context?: any) { |
| 262 | + if (mode === 'throws' && !context?.isSystem) throw new Error('field-visibility source unavailable'); |
| 263 | + return every(object); |
| 264 | + }, |
| 265 | + async getQueryableFields(object: string) { |
| 266 | + return every(object); |
| 267 | + }, |
| 268 | + }; |
| 269 | + }; |
| 270 | + |
| 271 | + afterEach(async () => { |
| 272 | + try { await engine?.destroy(); } catch { /* noop */ } |
| 273 | + }); |
| 274 | + |
| 275 | + beforeEach(async () => { |
| 276 | + mode = 'full'; |
| 277 | + engine = new ObjectQL(); |
| 278 | + engine.registerDriver(new SqlDriver({ |
| 279 | + client: 'better-sqlite3', |
| 280 | + connection: { filename: ':memory:' }, |
| 281 | + useNullAsDefault: true, |
| 282 | + }), true); |
| 283 | + await engine.init(); |
| 284 | + for (const o of OBJECTS) engine.registry.registerObject(o as any, 'com.objectstack.test.lock-title-withheld'); |
| 285 | + await engine.syncSchemas(); |
| 286 | + await installDefaultOrganization(engine); |
| 287 | + |
| 288 | + const seeds: Array<[string, Record<string, unknown>]> = [ |
| 289 | + ['vault_entry', { code: STORED, amount: 1 }], |
| 290 | + ['vault_token', { holder_name: STORED, amount: 1 }], |
| 291 | + ['service_login', { passcode: STORED, amount: 1 }], |
| 292 | + ['plain_deal', { name: ORDINARY_TITLE, amount: 1 }], |
| 293 | + ]; |
| 294 | + for (const [object, row] of seeds) { |
| 295 | + locked[object] = String((await engine.insert(object, row, { context: SYSTEM } as any)).id); |
| 296 | + await engine.insert('sys_approval_request', { |
| 297 | + object_name: object, record_id: locked[object], status: 'pending', flow_run_id: `run_${object}`, |
| 298 | + node_config_json: JSON.stringify({ lockRecord: true }), |
| 299 | + }, { context: SYSTEM } as any); |
| 300 | + } |
| 301 | + lineId = String((await engine.insert('vault_entry_line', { qty: 1, vault_entry: locked.vault_entry }, { context: SYSTEM } as any)).id); |
| 302 | + |
| 303 | + bindApprovalLockHook(engine as any, { warn: () => {}, info: () => {} }, source); |
| 304 | + bindDetailRowLockHook(engine as any, { warn: () => {}, info: () => {} }); |
| 305 | + }); |
| 306 | + |
| 307 | + async function refused(write: () => Promise<unknown>): Promise<any> { |
| 308 | + let thrown: any = null; |
| 309 | + try { await write(); } catch (e) { thrown = e; } |
| 310 | + expect(thrown, 'the write was not refused').not.toBeNull(); |
| 311 | + expect(thrown.code).toBe('RECORD_LOCKED'); |
| 312 | + expect(thrown.statusCode).toBe(409); |
| 313 | + return thrown; |
| 314 | + } |
| 315 | + |
| 316 | + const update = (object: string, options: Record<string, unknown> = { where: { id: locked[object] } }) => |
| 317 | + refused(() => engine.update(object, { amount: 2 }, { ...options, context: asUser(WRITER) } as any)); |
| 318 | + |
| 319 | + const expectUntitled = (thrown: any, label: string) => { |
| 320 | + const sentence = `This ${label} ${LOCKED_SENTENCE}`; |
| 321 | + expect(thrown.message).toBe(`RECORD_LOCKED: ${sentence}`); |
| 322 | + expect(thrown.userMessage).toBe(sentence); |
| 323 | + expect(`${thrown.message} ${thrown.userMessage}`).not.toContain(STORED); |
| 324 | + }; |
| 325 | + |
| 326 | + it('an internal display field named by the object gives no title — by-id update', async () => { |
| 327 | + expectUntitled(await update('vault_entry'), 'Vault Entry'); |
| 328 | + }); |
| 329 | + |
| 330 | + it('…nor on a predicate update, where the refusal is raised per matched row', async () => { |
| 331 | + expectUntitled(await update('vault_entry', { where: { amount: 1 }, multi: true }), 'Vault Entry'); |
| 332 | + }); |
| 333 | + |
| 334 | + it('an internal display field the object derives gives no title', async () => { |
| 335 | + expectUntitled(await update('vault_token'), 'Vault Token'); |
| 336 | + }); |
| 337 | + |
| 338 | + it('a credential-typed display field gives no title', async () => { |
| 339 | + expectUntitled(await update('service_login'), 'Service Login'); |
| 340 | + }); |
| 341 | + |
| 342 | + it('a line under a locked master is refused without the master\'s withheld title — update and delete', async () => { |
| 343 | + const opts = { where: { id: lineId }, context: asUser(WRITER) } as any; |
| 344 | + expectUntitled(await refused(() => engine.update('vault_entry_line', { qty: 3 }, opts)), 'Vault Entry'); |
| 345 | + expectUntitled(await refused(() => engine.delete('vault_entry_line', opts)), 'Vault Entry'); |
| 346 | + }); |
| 347 | + |
| 348 | + it('a served-fields source that cannot answer gives no title', async () => { |
| 349 | + mode = 'throws'; |
| 350 | + expectUntitled(await update('plain_deal'), 'Plain Deal'); |
| 351 | + }); |
| 352 | + |
| 353 | + it('control: an ordinary display field still names the record', async () => { |
| 354 | + const thrown = await update('plain_deal'); |
| 355 | + expect(thrown.message).toBe(`RECORD_LOCKED: Plain Deal '${ORDINARY_TITLE}' ${LOCKED_SENTENCE}`); |
| 356 | + }); |
| 357 | + |
| 358 | + it('control: with no served-fields source wired, an ordinary display field still names the record', async () => { |
| 359 | + mode = 'none'; |
| 360 | + const thrown = await update('plain_deal'); |
| 361 | + expect(thrown.message).toBe(`RECORD_LOCKED: Plain Deal '${ORDINARY_TITLE}' ${LOCKED_SENTENCE}`); |
| 362 | + }); |
| 363 | + |
| 364 | + it('with no served-fields source wired, a withheld display field still gives no title', async () => { |
| 365 | + mode = 'none'; |
| 366 | + expectUntitled(await update('vault_entry'), 'Vault Entry'); |
| 367 | + expectUntitled(await update('service_login'), 'Service Login'); |
| 368 | + }); |
| 369 | +}); |
0 commit comments