Repository navigation
Expand file tree
/
Copy pathcloud-connection-plugin.test.ts
More file actions
270 lines (246 loc) · 13.7 KB
/
Copy pathcloud-connection-plugin.test.ts
File metadata and controls
270 lines (246 loc) · 13.7 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* CloudConnectionPlugin — route mounting + mode behavior (cloud ADR-0008 Phase 1).
*
* Exercises the consolidated /api/v1/cloud-connection/* surface in both
* resolution modes:
* - multi-tenant: env-registry hostname lookup + per-env kernel session
* - single-environment: fixed env id + host-kernel auth session
*/
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
import { CloudConnectionPlugin, createCloudConnectionPlugin } from './cloud-connection-plugin.js';
import { bindCatalogFromTables } from './__tests__/security-catalog.testkit.js';
type Handler = (c: any) => Promise<Response | any>;
function makeRawApp() {
const routes = new Map<string, Handler>();
return {
routes,
get: (path: string, h: Handler) => routes.set(`GET ${path}`, h),
post: (path: string, h: Handler) => routes.set(`POST ${path}`, h),
};
}
function makeCtx(opts: {
rawApp: ReturnType<typeof makeRawApp>;
services?: Record<string, any>;
}) {
const hooks = new Map<string, (...args: any[]) => any>();
return {
ctx: {
hook: (event: string, handler: (...args: any[]) => any) => hooks.set(event, handler),
getService: (name: string) => {
if (name === 'http-server') return { getRawApp: () => opts.rawApp };
const svc = opts.services?.[name];
if (svc === undefined) throw new Error(`service ${name} not registered`);
return svc;
},
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
},
fireKernelReady: async () => { await hooks.get('kernel:ready')?.(); },
};
}
function makeC(url: string, body?: any) {
const json = vi.fn((payload: any, status?: number) => ({ payload, status: status ?? 200 }));
return {
req: {
url,
raw: new Request(url),
json: async () => body ?? {},
},
json,
};
}
/** The organization the sessions act in, and the one {@link setupAccessEngine}'s grant is owned by. */
const ORG = 'o1';
const sessionAuth = (userId?: string) => ({
api: { getSession: async () => (userId ? { user: { id: userId }, session: { activeOrganizationId: ORG } } : null) },
});
/**
* [#24148] A data engine under which `userId` holds `setup.access` — what
* `/status` serves its full answer behind. Real permission-set rows, read by
* the shared resolver through `find`. The grant is owned by {@link ORG}, the
* organization the session acts in: a grant applies only in its own
* organization.
*/
const setupAccessEngine = (userId: string) => {
const rows: Record<string, any[]> = {
sys_user: [{ id: userId, email: `${userId}@example.test` }],
sys_user_permission_set: [{ id: 'ups_1', user_id: userId, permission_set_id: 'ps_1', permission_set: 'setup_admin', organization_id: ORG }],
sys_permission_set: [{ id: 'ps_1', name: 'setup_admin', system_permissions: ['setup.access'] }],
};
return bindCatalogFromTables({ find: async (object: string) => rows[object] ?? [] }, rows);
};
/** [#24148] What `/status` serves a caller without `setup.access`. */
const narrowed = (data: { bound: boolean; runtimeId: string | null }) => ({ ...data, provider: 'objectstack-cloud' });
beforeEach(() => {
delete process.env.OS_ENVIRONMENT_ID;
});
afterEach(() => {
vi.unstubAllGlobals();
delete process.env.OS_ENVIRONMENT_ID;
});
describe('CloudConnectionPlugin — mounting', () => {
it('mounts all 8 routes on kernel:ready', async () => {
const rawApp = makeRawApp();
const { ctx, fireKernelReady } = makeCtx({ rawApp });
const plugin = new CloudConnectionPlugin({ controlPlaneUrl: 'http://cloud.test' });
await plugin.start(ctx as any);
await fireKernelReady();
const keys = [...rawApp.routes.keys()];
expect(keys).toEqual(expect.arrayContaining([
'GET /api/v1/cloud-connection/status',
'POST /api/v1/cloud-connection/bind/start',
'POST /api/v1/cloud-connection/bind/poll',
'POST /api/v1/cloud-connection/unbind',
'POST /api/v1/cloud-connection/install',
'GET /api/v1/cloud-connection/installation',
'GET /api/v1/cloud-connection/installed',
'GET /api/v1/cloud-connection/org-packages',
]));
expect(keys).toHaveLength(8);
});
it('warns and mounts nothing without an http-server', async () => {
const hooks = new Map<string, any>();
const warn = vi.fn();
const ctx = {
hook: (e: string, h: any) => hooks.set(e, h),
getService: () => { throw new Error('nope'); },
logger: { warn },
};
await createCloudConnectionPlugin().start(ctx as any);
await hooks.get('kernel:ready')?.();
expect(warn).toHaveBeenCalledWith(expect.stringContaining('http-server unavailable'));
});
});
describe('multi-tenant mode (env-registry + per-env kernel auth)', () => {
const ENV = 'env-123';
function mtServices(userId?: string) {
return {
'env-registry': { resolveByHostname: async () => ({ environmentId: ENV }) },
'kernel-manager': { getOrCreate: async () => ({ getServiceAsync: async () => sessionAuth(userId) }) },
};
}
it('status falls back to implicit binding from the service key when the control plane is down', async () => {
const rawApp = makeRawApp();
const { ctx, fireKernelReady } = makeCtx({ rawApp, services: mtServices() });
vi.stubGlobal('fetch', vi.fn(async () => { throw new Error('ECONNREFUSED'); }));
await new CloudConnectionPlugin({ controlPlaneUrl: 'http://cloud.test', controlPlaneApiKey: 'svc-key' }).start(ctx as any);
await fireKernelReady();
const res = await rawApp.routes.get('GET /api/v1/cloud-connection/status')!(makeC('https://t1.example.com/api/v1/cloud-connection/status'));
expect(res.payload).toEqual({ success: true, data: narrowed({ runtimeId: null, bound: true }) });
});
it('status 404s for an unknown hostname', async () => {
const rawApp = makeRawApp();
const { ctx, fireKernelReady } = makeCtx({
rawApp,
services: { 'env-registry': { resolveByHostname: async () => null } },
});
await new CloudConnectionPlugin({ controlPlaneUrl: 'http://cloud.test' }).start(ctx as any);
await fireKernelReady();
const res = await rawApp.routes.get('GET /api/v1/cloud-connection/status')!(makeC('https://nope.example.com/x'));
expect(res.status).toBe(404);
expect(res.payload.error.code).toBe('ENVIRONMENT_NOT_FOUND');
});
it('install rejects unauthenticated callers with 401 (no control-plane call)', async () => {
const rawApp = makeRawApp();
const { ctx, fireKernelReady } = makeCtx({ rawApp, services: mtServices(undefined) });
const fetchSpy = vi.fn();
vi.stubGlobal('fetch', fetchSpy);
await new CloudConnectionPlugin({ controlPlaneUrl: 'http://cloud.test', controlPlaneApiKey: 'svc-key' }).start(ctx as any);
await fireKernelReady();
const res = await rawApp.routes.get('POST /api/v1/cloud-connection/install')!(
makeC('https://t1.example.com/api/v1/cloud-connection/install', { package_id: 'pkg_1' }),
);
expect(res.status).toBe(401);
expect(fetchSpy).not.toHaveBeenCalled();
});
it('install forwards to the control plane install action for an authenticated session', async () => {
const rawApp = makeRawApp();
const { ctx, fireKernelReady } = makeCtx({ rawApp, services: mtServices('user-1') });
const fetchSpy = vi.fn(async (_url: string, _init?: RequestInit) => new Response(JSON.stringify({ success: true, data: { installed: true } }), { status: 200 }));
vi.stubGlobal('fetch', fetchSpy);
await new CloudConnectionPlugin({ controlPlaneUrl: 'http://cloud.test', controlPlaneApiKey: 'svc-key' }).start(ctx as any);
await fireKernelReady();
const res = await rawApp.routes.get('POST /api/v1/cloud-connection/install')!(
makeC('https://t1.example.com/api/v1/cloud-connection/install', { package_id: 'pkg_1', seed_sample_data: true }),
);
expect(res.payload.success).toBe(true);
const [url, init] = fetchSpy.mock.calls[0]!;
expect(url).toBe('http://cloud.test/api/v1/actions/sys_package/install_package');
expect(JSON.parse((init as any).body)).toEqual({ recordId: 'pkg_1', params: { environment_id: ENV, seed_sample_data: true } });
expect((init as any).headers.Authorization).toBe('Bearer svc-key');
});
it('installed degrades to an empty disconnected list without cloud credentials', async () => {
const rawApp = makeRawApp();
const { ctx, fireKernelReady } = makeCtx({ rawApp, services: mtServices('user-1') });
await new CloudConnectionPlugin({ controlPlaneUrl: '', controlPlaneApiKey: '' }).start(ctx as any);
await fireKernelReady();
const res = await rawApp.routes.get('GET /api/v1/cloud-connection/installed')!(
makeC('https://t1.example.com/api/v1/cloud-connection/installed'),
);
expect(res.payload).toEqual({ success: true, data: { packages: [], total: 0, connected: false } });
});
it('org-packages forwards environment_id and unwraps items', async () => {
const rawApp = makeRawApp();
const { ctx, fireKernelReady } = makeCtx({ rawApp, services: mtServices('user-1') });
const items = [{ id: 'pkg_a', manifest_id: 'com.acme.crm' }];
const fetchSpy = vi.fn(async (_url: string, _init?: RequestInit) => new Response(JSON.stringify({ success: true, data: { items } }), { status: 200 }));
vi.stubGlobal('fetch', fetchSpy);
await new CloudConnectionPlugin({ controlPlaneUrl: 'http://cloud.test', controlPlaneApiKey: 'svc-key' }).start(ctx as any);
await fireKernelReady();
const res = await rawApp.routes.get('GET /api/v1/cloud-connection/org-packages')!(
makeC('https://t1.example.com/api/v1/cloud-connection/org-packages'),
);
expect(String(fetchSpy.mock.calls[0]![0])).toBe(`http://cloud.test/api/v1/cloud/org-packages?environment_id=${ENV}`);
expect(res.payload.data).toEqual({ items, total: 1, connected: true });
});
});
describe('single-environment mode (host auth, fixed env id)', () => {
it('status reports bound:false (200, not 404) before the runtime is bound', async () => {
const rawApp = makeRawApp();
const { ctx, fireKernelReady } = makeCtx({ rawApp });
await new CloudConnectionPlugin({ singleEnvironment: true, controlPlaneUrl: 'http://cloud.test' }).start(ctx as any);
await fireKernelReady();
const res = await rawApp.routes.get('GET /api/v1/cloud-connection/status')!(makeC('http://localhost:3000/x'));
expect(res.status).toBe(200);
expect(res.payload.data).toEqual(narrowed({ runtimeId: null, bound: false }));
});
it('uses the configured environment id + the host kernel auth session', async () => {
const rawApp = makeRawApp();
const { ctx, fireKernelReady } = makeCtx({ rawApp, services: { auth: sessionAuth('admin-1') } });
const fetchSpy = vi.fn(async (_url: string, _init?: RequestInit) => new Response(JSON.stringify({ success: true, data: { packages: [{ id: 'p1' }] } }), { status: 200 }));
vi.stubGlobal('fetch', fetchSpy);
await new CloudConnectionPlugin({
singleEnvironment: true,
environmentId: 'env-ee-1',
controlPlaneUrl: 'http://cloud.test',
controlPlaneApiKey: 'svc-key',
}).start(ctx as any);
await fireKernelReady();
const res = await rawApp.routes.get('GET /api/v1/cloud-connection/installed')!(makeC('http://localhost:3000/api/v1/cloud-connection/installed'));
expect(String(fetchSpy.mock.calls[0]![0])).toContain('/api/v1/cloud/environments/env-ee-1/packages');
expect(res.payload.data.total).toBe(1);
});
it("ignores the CLI's local sentinel env ids (env_local / proj_local)", async () => {
process.env.OS_ENVIRONMENT_ID = 'env_local';
const rawApp = makeRawApp();
// A caller holding `setup.access`, who is served the environment id.
const { ctx, fireKernelReady } = makeCtx({ rawApp, services: { auth: sessionAuth('admin-1'), objectql: setupAccessEngine('admin-1') } });
await new CloudConnectionPlugin({ singleEnvironment: true, controlPlaneUrl: 'http://cloud.test' }).start(ctx as any);
await fireKernelReady();
const res = await rawApp.routes.get('GET /api/v1/cloud-connection/status')!(makeC('http://localhost:3000/x'));
expect(res.status).toBe(200);
// Treated as "no cloud environment", not presented to the control plane.
expect(res.payload.data.environmentId).toBeNull();
expect(res.payload.data.bound).toBe(false);
});
it('falls back to OS_ENVIRONMENT_ID when no config id is given', async () => {
process.env.OS_ENVIRONMENT_ID = 'env-from-env-var';
const rawApp = makeRawApp();
const { ctx, fireKernelReady } = makeCtx({ rawApp, services: { auth: sessionAuth('admin-1'), objectql: setupAccessEngine('admin-1') } });
vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ success: true, data: {} }), { status: 200 })));
await new CloudConnectionPlugin({ singleEnvironment: true, controlPlaneUrl: 'http://cloud.test', controlPlaneApiKey: 'k' }).start(ctx as any);
await fireKernelReady();
const res = await rawApp.routes.get('GET /api/v1/cloud-connection/status')!(makeC('http://localhost:3000/x'));
expect(res.payload.data.environmentId).toBe('env-from-env-var');
});
});