|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// [#27581] Deleting a record releases the `sys_secret` row its `secret` field |
| 4 | +// named — pinned at the REST data door of a booted stack, on a provider-wired |
| 5 | +// boot (`bootStack` wires one LocalCryptoProvider, as `objectstack dev` / |
| 6 | +// `serve` do), counting the credential rows at the driver. |
| 7 | +// `packages/objectql/src/secret-field-write-commit.test.ts` drives each delete |
| 8 | +// path at the engine; this file holds the halves a client can reach: |
| 9 | +// |
| 10 | +// - `DELETE /data/:object/:id` and the bulk `deleteMany` door remove the |
| 11 | +// credential row each deleted record named, and no other; |
| 12 | +// - a handle another record still names is kept until the last one goes; |
| 13 | +// - an atomic `deleteMany` that rolls back keeps the record and its row. |
| 14 | + |
| 15 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 16 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 17 | +import { defineStack } from '@objectstack/spec'; |
| 18 | +import { ObjectSchema, Field } from '@objectstack/spec/data'; |
| 19 | + |
| 20 | +const VAULT = 'sfd_vault'; |
| 21 | + |
| 22 | +const sfdStack = defineStack({ |
| 23 | + manifest: { |
| 24 | + id: 'com.dogfood.secret-field-delete-release', namespace: 'sfd', version: '0.0.0', type: 'app', |
| 25 | + name: 'Secret field delete-release fixture', description: 'A secret field on a record that gets deleted.', |
| 26 | + }, |
| 27 | + objects: [ |
| 28 | + ObjectSchema.create({ |
| 29 | + name: VAULT, label: 'Vault', pluralLabel: 'Vaults', sharingModel: 'public_read_write', |
| 30 | + fields: { |
| 31 | + name: Field.text({ label: 'Name', required: true }), |
| 32 | + tier: Field.select({ label: 'Tier', options: [{ label: 'Gold', value: 'gold' }, { label: 'Silver', value: 'silver' }] }), |
| 33 | + token: Field.secret({ label: 'Token' }), |
| 34 | + }, |
| 35 | + }), |
| 36 | + ], |
| 37 | +} as never); |
| 38 | + |
| 39 | +type Answer = { status: number; json: any; text: string }; |
| 40 | + |
| 41 | +describe('[#27581] secret field: deleting a record releases its sys_secret row — over REST', () => { |
| 42 | + let stack: VerifyStack; |
| 43 | + let ql: any; |
| 44 | + let token = ''; |
| 45 | + |
| 46 | + const call = async (method: string, path: string, body?: unknown): Promise<Answer> => { |
| 47 | + const res = await stack.apiAs(token, method, path, body); |
| 48 | + const text = await res.text(); |
| 49 | + let json: any = {}; |
| 50 | + try { json = JSON.parse(text); } catch { /* not JSON */ } |
| 51 | + return { status: res.status, json, text }; |
| 52 | + }; |
| 53 | + /** The ids of every credential row the store holds, read at the driver (no engine read path between). */ |
| 54 | + const secretIds = async (): Promise<string[]> => |
| 55 | + (((await ql.getDriver('sys_secret').find('sys_secret', {})) ?? []) as Array<{ id: string }>).map((r) => String(r.id)); |
| 56 | + /** The handle the business row holds, as the driver stores it. */ |
| 57 | + const handleOf = async (id: string): Promise<string> => { |
| 58 | + const [row] = await ql.getDriver(VAULT).find(VAULT, { where: { id } }); |
| 59 | + return String(row?.token ?? '').slice('secret:'.length); |
| 60 | + }; |
| 61 | + const create = async (name: string, value: string, tier = 'gold') => { |
| 62 | + const res = await call('POST', `/data/${VAULT}`, { name, tier, token: value }); |
| 63 | + expect(res.status, res.text).toBe(201); |
| 64 | + const [row] = await stack.rows(VAULT, { name }); |
| 65 | + return String(row.id); |
| 66 | + }; |
| 67 | + |
| 68 | + beforeAll(async () => { |
| 69 | + stack = await bootStack(sfdStack); |
| 70 | + token = await stack.signIn(); |
| 71 | + ql = await stack.kernel.getServiceAsync('objectql'); |
| 72 | + }, 180_000); |
| 73 | + |
| 74 | + afterAll(async () => { |
| 75 | + await stack?.stop(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('DELETE by id removes the credential row the record named, and no other', async () => { |
| 79 | + const gone = await create('sfd-gone', 'sfd-gone-value'); |
| 80 | + const kept = await create('sfd-kept', 'sfd-kept-value'); |
| 81 | + const goneHandle = await handleOf(gone); |
| 82 | + const keptHandle = await handleOf(kept); |
| 83 | + expect(await secretIds()).toEqual(expect.arrayContaining([goneHandle, keptHandle])); |
| 84 | + const before = (await secretIds()).length; |
| 85 | + |
| 86 | + const res = await call('DELETE', `/data/${VAULT}/${gone}`); |
| 87 | + expect(res.status, res.text).toBe(200); |
| 88 | + expect(await stack.rows(VAULT, { name: 'sfd-gone' })).toHaveLength(0); |
| 89 | + const after = await secretIds(); |
| 90 | + expect(after).not.toContain(goneHandle); |
| 91 | + expect(after).toContain(keptHandle); |
| 92 | + expect(after.length).toBe(before - 1); |
| 93 | + expect(await ql.resolveSecretField(VAULT, kept, 'token')).toBe('sfd-kept-value'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('the bulk deleteMany door removes the credential row of every record it deletes', async () => { |
| 97 | + const a = await create('sfd-many-a', 'sfd-many-a-value'); |
| 98 | + const b = await create('sfd-many-b', 'sfd-many-b-value'); |
| 99 | + const handles = [await handleOf(a), await handleOf(b)]; |
| 100 | + const before = (await secretIds()).length; |
| 101 | + |
| 102 | + const res = await call('POST', `/data/${VAULT}/deleteMany`, { ids: [a, b] }); |
| 103 | + expect(res.status, res.text).toBe(200); |
| 104 | + expect(res.json).toMatchObject({ succeeded: 2, failed: 0 }); |
| 105 | + const after = await secretIds(); |
| 106 | + for (const h of handles) expect(after).not.toContain(h); |
| 107 | + expect(after.length).toBe(before - 2); |
| 108 | + }); |
| 109 | + |
| 110 | + it('a handle another record still names is kept until the last record that names it is deleted', async () => { |
| 111 | + const a = await create('sfd-shared-a', 'sfd-shared-a-value', 'silver'); |
| 112 | + const b = await create('sfd-shared-b', 'sfd-shared-b-value', 'silver'); |
| 113 | + // One predicate write seals ONE handle for every row it matches. |
| 114 | + await ql.update(VAULT, { token: 'sfd-shared-value' }, { where: { tier: 'silver' }, multi: true, context: { isSystem: true } }); |
| 115 | + const shared = await handleOf(a); |
| 116 | + expect(await handleOf(b)).toBe(shared); |
| 117 | + expect(await secretIds()).toContain(shared); |
| 118 | + |
| 119 | + const first = await call('DELETE', `/data/${VAULT}/${a}`); |
| 120 | + expect(first.status, first.text).toBe(200); |
| 121 | + expect(await secretIds()).toContain(shared); |
| 122 | + expect(await ql.resolveSecretField(VAULT, b, 'token')).toBe('sfd-shared-value'); |
| 123 | + |
| 124 | + const last = await call('DELETE', `/data/${VAULT}/${b}`); |
| 125 | + expect(last.status, last.text).toBe(200); |
| 126 | + expect(await secretIds()).not.toContain(shared); |
| 127 | + }); |
| 128 | + |
| 129 | + it('an atomic deleteMany that rolls back keeps the record and its credential row', async () => { |
| 130 | + const id = await create('sfd-rollback', 'sfd-rollback-value'); |
| 131 | + const handle = await handleOf(id); |
| 132 | + const before = await secretIds(); |
| 133 | + |
| 134 | + // The second id names no record, so the batch rolls the first delete back. |
| 135 | + const res = await call('POST', `/data/${VAULT}/deleteMany`, { ids: [id, 'sfd-no-such-record'], options: { atomic: true } }); |
| 136 | + expect(res.status, res.text).toBe(200); |
| 137 | + expect(res.json).toMatchObject({ success: false, succeeded: 0 }); |
| 138 | + expect(res.json.results.map((r: any) => [r.id, r.success, r.errors?.[0]?.code])).toEqual([ |
| 139 | + [id, false, 'ROLLED_BACK'], |
| 140 | + ['sfd-no-such-record', false, 'RECORD_NOT_FOUND'], |
| 141 | + ]); |
| 142 | + expect(await stack.rows(VAULT, { name: 'sfd-rollback' })).toHaveLength(1); |
| 143 | + expect(await secretIds()).toEqual(before); |
| 144 | + expect(await secretIds()).toContain(handle); |
| 145 | + expect(await ql.resolveSecretField(VAULT, id, 'token')).toBe('sfd-rollback-value'); |
| 146 | + }); |
| 147 | +}); |
0 commit comments