-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdatasource-admin-service.test.ts
More file actions
318 lines (289 loc) · 11.9 KB
/
Copy pathdatasource-admin-service.test.ts
File metadata and controls
318 lines (289 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect } from 'vitest';
import {
DatasourceAdminService,
type DatasourceAdminServiceConfig,
type StoredDatasource,
type ProbeInput,
} from '../datasource-admin-service.js';
/**
* In-memory harness: an editable record store + secret store, with probe and
* bound-object count stubbable per test. Records what was probed/written so
* tests can assert credentials never leak into the persisted record.
*/
function makeHarness(opts?: {
seed?: StoredDatasource[];
probe?: (input: ProbeInput) => Promise<{ ok: boolean; error?: string; latencyMs?: number }>;
boundCounts?: Record<string, number>;
}) {
// Flat list, not a name-keyed map: in production `listDatasourceRecords`
// merges artefact (code) records with runtime-store records, so the same
// name can legitimately appear twice (a runtime row shadowed by a code one).
const records: StoredDatasource[] = (opts?.seed ?? []).map((r) => ({ ...r }));
/** Resolve the effective record for a name (code wins over runtime). */
const findEffective = (n: string) =>
records.find((r) => r.name === n && r.origin !== 'runtime') ??
records.find((r) => r.name === n);
const secrets = new Map<string, { value: string; namespace?: string; key?: string }>();
let secretSeq = 0;
const probed: ProbeInput[] = [];
const registered: string[] = [];
const unregistered: string[] = [];
const removedSecrets: string[] = [];
const config: DatasourceAdminServiceConfig = {
probe: async (input) => {
probed.push(input);
return (opts?.probe ?? (async () => ({ ok: true, latencyMs: 3 })))(input);
},
listDatasourceRecords: async () => records.map((r) => ({ ...r })),
getDatasourceRecord: async (n) => {
const r = findEffective(n);
return r ? { ...r } : undefined;
},
putDatasourceRecord: async (record) => {
const idx = records.findIndex((r) => r.name === record.name && r.origin === 'runtime');
if (idx >= 0) records[idx] = { ...record };
else records.push({ ...record });
},
deleteDatasourceRecord: async (n) => {
const idx = records.findIndex((r) => r.name === n && r.origin === 'runtime');
if (idx >= 0) records.splice(idx, 1);
},
writeSecret: async (input, hint) => {
const ref = `sys_secret://datasource/${input.key ?? hint.name}#${++secretSeq}`;
secrets.set(ref, { value: input.value, namespace: input.namespace, key: input.key });
return ref;
},
removeSecret: async (ref) => {
removedSecrets.push(ref);
secrets.delete(ref);
},
countBoundObjects: async (n) => opts?.boundCounts?.[n] ?? 0,
registerPool: (record) => {
registered.push(record.name);
},
unregisterPool: (name) => {
unregistered.push(name);
},
};
const service = new DatasourceAdminService(config);
// Thin accessor over the flat record list, runtime-preferring (tests assert
// on the persisted runtime row, e.g. after create/update).
const store = {
get: (n: string) =>
records.find((r) => r.name === n && r.origin === 'runtime') ??
records.find((r) => r.name === n),
has: (n: string) => records.some((r) => r.name === n),
get size() {
return records.length;
},
};
return { service, store, secrets, probed, registered, unregistered, removedSecrets };
}
describe('listDatasources', () => {
it('reports origin + dedupes by name (code wins, flags shadowed runtime)', async () => {
const { service } = makeHarness({
seed: [
{ name: 'crm_primary', driver: 'sqlite', origin: 'code', definedIn: '@example/crm' },
{ name: 'crm_primary', driver: 'postgres', origin: 'runtime' },
{ name: 'reporting', driver: 'postgres', schemaMode: 'external', origin: 'runtime' },
],
});
const list = await service.listDatasources();
const crm = list.find((d) => d.name === 'crm_primary')!;
const reporting = list.find((d) => d.name === 'reporting')!;
expect(list).toHaveLength(2);
expect(crm.origin).toBe('code');
expect(crm.driver).toBe('sqlite'); // code wins over the runtime row
expect(crm.definedIn).toBe('@example/crm');
expect(crm.conflictsWithCode).toBe(true);
expect(reporting.origin).toBe('runtime');
expect(reporting.schemaMode).toBe('external');
expect(reporting.conflictsWithCode).toBeUndefined();
});
});
describe('testConnection', () => {
it('probes with the cleartext secret without persisting anything', async () => {
const { service, store, probed } = makeHarness();
const res = await service.testConnection(
{ name: 'tmp', driver: 'postgres', config: { host: 'db.internal' } },
{ value: 's3cret' },
);
expect(res.ok).toBe(true);
expect(probed[0].secret).toBe('s3cret');
expect(store.size).toBe(0); // nothing saved
});
it('returns ok:false when no driver is supplied', async () => {
const { service } = makeHarness();
const res = await service.testConnection({ name: 'x', driver: '' });
expect(res.ok).toBe(false);
expect(res.error).toMatch(/driver is required/i);
});
it('captures a thrown probe error as ok:false', async () => {
const { service } = makeHarness({
probe: async () => {
throw new Error('ECONNREFUSED');
},
});
const res = await service.testConnection({ name: 'x', driver: 'postgres' });
expect(res.ok).toBe(false);
expect(res.error).toMatch(/ECONNREFUSED/);
});
});
describe('createDatasource', () => {
it('persists a runtime record and stores the secret as an opaque ref only', async () => {
const { service, store, secrets } = makeHarness();
const summary = await service.createDatasource(
{
name: 'reporting',
driver: 'postgres',
schemaMode: 'external',
config: { host: 'db.internal', database: 'analytics' },
external: { allowWrites: false },
},
{ value: 'postgres://user:pw@db.internal/analytics' },
);
expect(summary.origin).toBe('runtime');
const rec = store.get('reporting')!;
expect(rec.origin).toBe('runtime');
// credential is referenced, never inlined
expect(rec.external?.credentialsRef).toBeTruthy();
expect(JSON.stringify(rec)).not.toContain('postgres://');
expect(JSON.stringify(rec)).not.toContain('pw@');
expect(secrets.size).toBe(1);
});
it('hot-registers the pool after create', async () => {
const { service, registered } = makeHarness();
await service.createDatasource({ name: 'reporting', driver: 'postgres' });
expect(registered).toContain('reporting');
});
it('rejects a name owned by a code-defined datasource', async () => {
const { service } = makeHarness({
seed: [{ name: 'crm_primary', driver: 'sqlite', origin: 'code' }],
});
await expect(
service.createDatasource({ name: 'crm_primary', driver: 'postgres' }),
).rejects.toThrow(/code-defined/i);
});
it('rejects a duplicate runtime name', async () => {
const { service } = makeHarness({
seed: [{ name: 'reporting', driver: 'postgres', origin: 'runtime' }],
});
await expect(
service.createDatasource({ name: 'reporting', driver: 'postgres' }),
).rejects.toThrow(/already exists/i);
});
it('rejects an invalid name', async () => {
const { service } = makeHarness();
await expect(
service.createDatasource({ name: 'Bad-Name', driver: 'postgres' }),
).rejects.toThrow(/must match/i);
});
});
describe('updateDatasource', () => {
it('patches a runtime record and rewraps the secret, removing the old ref', async () => {
const { service, store, secrets, removedSecrets } = makeHarness({
seed: [
{
name: 'reporting',
driver: 'postgres',
origin: 'runtime',
external: { credentialsRef: 'sys_secret://datasource/reporting#0' },
},
],
});
secrets.set('sys_secret://datasource/reporting#0', { value: 'old' });
const summary = await service.updateDatasource(
'reporting',
{ label: 'Reporting DB', active: false },
{ value: 'new-pw' },
);
expect(summary.label).toBe('Reporting DB');
expect(summary.active).toBe(false);
const rec = store.get('reporting')!;
expect(rec.external?.credentialsRef).not.toBe('sys_secret://datasource/reporting#0');
expect(removedSecrets).toContain('sys_secret://datasource/reporting#0');
});
it('preserves the existing credentialsRef when external is patched without a new secret', async () => {
const ref = 'sys_secret://datasource/reporting#0';
const { service, store } = makeHarness({
seed: [
{ name: 'reporting', driver: 'postgres', origin: 'runtime', external: { credentialsRef: ref } },
],
});
await service.updateDatasource('reporting', { external: { allowWrites: true } });
expect(store.get('reporting')!.external?.credentialsRef).toBe(ref);
});
it('rejects editing a code-defined datasource', async () => {
const { service } = makeHarness({
seed: [{ name: 'crm_primary', driver: 'sqlite', origin: 'code' }],
});
await expect(
service.updateDatasource('crm_primary', { label: 'x' }),
).rejects.toThrow(/code-defined/i);
});
it('rejects updating a missing datasource', async () => {
const { service } = makeHarness();
await expect(service.updateDatasource('nope', { label: 'x' })).rejects.toThrow(/not found/i);
});
});
describe('removeDatasource', () => {
it('removes a runtime record, its secret, and the pool', async () => {
const ref = 'sys_secret://datasource/reporting#0';
const { service, store, removedSecrets, unregistered } = makeHarness({
seed: [
{ name: 'reporting', driver: 'postgres', origin: 'runtime', external: { credentialsRef: ref } },
],
});
await service.removeDatasource('reporting');
expect(store.has('reporting')).toBe(false);
expect(removedSecrets).toContain(ref);
expect(unregistered).toContain('reporting');
});
it('refuses to remove while objects are still bound', async () => {
const { service, store } = makeHarness({
seed: [{ name: 'reporting', driver: 'postgres', origin: 'runtime' }],
boundCounts: { reporting: 3 },
});
await expect(service.removeDatasource('reporting')).rejects.toThrow(/3 object\(s\)/);
expect(store.has('reporting')).toBe(true);
});
it('refuses to remove a code-defined datasource', async () => {
const { service } = makeHarness({
seed: [{ name: 'crm_primary', driver: 'sqlite', origin: 'code' }],
});
await expect(service.removeDatasource('crm_primary')).rejects.toThrow(/code-defined/i);
});
it('rejects removing a missing datasource', async () => {
const { service } = makeHarness();
await expect(service.removeDatasource('nope')).rejects.toThrow(/not found/i);
});
});
describe('getDatasource', () => {
it('returns config + hasSecret with the credentialsRef stripped', async () => {
const { service } = makeHarness({
seed: [
{
name: 'pg',
driver: 'postgres',
origin: 'runtime',
config: { host: 'db', port: 5432, database: 'app' },
external: { credentialsRef: 'sys_secret://datasource/pg#1' },
},
],
});
const ds = await service.getDatasource('pg');
expect(ds).toMatchObject({ name: 'pg', driver: 'postgres', origin: 'runtime', hasSecret: true });
expect(ds!.config).toEqual({ host: 'db', port: 5432, database: 'app' });
// The opaque credential handle must never be returned.
expect(JSON.stringify(ds)).not.toContain('sys_secret');
expect(JSON.stringify(ds)).not.toContain('credentialsRef');
});
it('reports hasSecret:false and returns undefined for unknown names', async () => {
const { service } = makeHarness({
seed: [{ name: 'lite', driver: 'sqlite', origin: 'runtime', config: { filename: '/tmp/a.db' } }],
});
expect((await service.getDatasource('lite'))!.hasSecret).toBe(false);
expect(await service.getDatasource('missing')).toBeUndefined();
});
});