@serfab/cadre-host 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +406 -296
- package/dist/bin/host.d.ts +11 -7
- package/dist/bin/host.js +40 -12
- package/dist/bin/host.js.map +1 -1
- package/dist/installer/config.d.ts +4 -4
- package/dist/installer/config.js +5 -39
- package/dist/installer/config.js.map +1 -1
- package/dist/installer/identity.d.ts +8 -1
- package/dist/installer/identity.js +8 -1
- package/dist/installer/identity.js.map +1 -1
- package/dist/orchestrator/host-process-orchestrator.d.ts +2 -2
- package/dist/orchestrator/host-process-orchestrator.js +14 -4
- package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
- package/dist/server/routes/bootstrap-node-validation.d.ts +96 -0
- package/dist/server/routes/bootstrap-node-validation.js +178 -0
- package/dist/server/routes/bootstrap-node-validation.js.map +1 -0
- package/dist/server/routes/grants.js +8 -25
- package/dist/server/routes/grants.js.map +1 -1
- package/dist/server/routes/provision-request-validation.d.ts +45 -0
- package/dist/server/routes/provision-request-validation.js +97 -0
- package/dist/server/routes/provision-request-validation.js.map +1 -0
- package/dist/server/settings-store.d.ts +1 -1
- package/dist/server/settings-store.js +5 -1
- package/dist/server/settings-store.js.map +1 -1
- package/package.json +5 -4
- package/src/__tests__/cli.smoke.test.ts +8 -3
- package/src/__tests__/global-setup.ts +9 -4
- package/src/__tests__/orchestrator-node-identity.test.ts +204 -204
- package/src/__tests__/orchestrator.test.ts +951 -951
- package/src/bin/__tests__/cli-reference.test.ts +167 -0
- package/src/bin/host.ts +1326 -1299
- package/src/donation/__tests__/donation-service.test.ts +1068 -1068
- package/src/donation/__tests__/donation-supervisor.test.ts +510 -510
- package/src/donation/__tests__/fake-orchestrator.test.ts +168 -168
- package/src/installer/__tests__/config.test.ts +134 -112
- package/src/installer/config.ts +5 -43
- package/src/installer/identity.ts +8 -1
- package/src/nat/__tests__/nat-service.test.ts +515 -497
- package/src/orchestrator/host-process-orchestrator.ts +1143 -1133
- package/src/server/__tests__/bootstrap-node-validation.test.ts +134 -0
- package/src/server/__tests__/grants-route.test.ts +326 -263
- package/src/server/__tests__/settings-route.test.ts +8 -7
- package/src/server/routes/bootstrap-node-validation.ts +187 -0
- package/src/server/routes/grants.ts +9 -26
- package/src/server/routes/provision-request-validation.ts +105 -0
- package/src/server/settings-store.ts +41 -37
|
@@ -1,263 +1,326 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Grantee-facing `/grants` route tests — the bearer-gated provisioning surface.
|
|
3
|
-
*
|
|
4
|
-
* Uses a fake orchestrator (no real children) so the focus is the HTTP contract:
|
|
5
|
-
* bearer auth, request validation, per-grant ownership isolation, the quota gate,
|
|
6
|
-
* and seedToken redaction. The live-node round-trips (peer/seed) are covered by
|
|
7
|
-
* the cross-package integration scenario.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import Fastify from 'fastify';
|
|
11
|
-
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
12
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
13
|
-
import { tmpdir } from 'node:os';
|
|
14
|
-
import { join } from 'node:path';
|
|
15
|
-
|
|
16
|
-
import type {
|
|
17
|
-
Orchestrator,
|
|
18
|
-
OrchestratorCreateRequest,
|
|
19
|
-
OrchestratorCreateResult,
|
|
20
|
-
OrchestratorStats,
|
|
21
|
-
} from '@serfab/cadre-provider';
|
|
22
|
-
|
|
23
|
-
import { registerErrorHandler } from '../error-handler.js';
|
|
24
|
-
import { registerGrantsRoutes } from '../routes/grants.js';
|
|
25
|
-
import { DonationService } from '../../donation/donation-service.js';
|
|
26
|
-
import { DonationStore } from '../../donation/donation-store.js';
|
|
27
|
-
import { GrantService } from '../../donation/grant-service.js';
|
|
28
|
-
import { GrantStore } from '../../donation/grant-store.js';
|
|
29
|
-
|
|
30
|
-
class FakeOrchestrator implements Orchestrator {
|
|
31
|
-
stopped: string[] = [];
|
|
32
|
-
removed: string[] = [];
|
|
33
|
-
private counter = 0;
|
|
34
|
-
async createContainer(_request: OrchestratorCreateRequest): Promise<OrchestratorCreateResult> {
|
|
35
|
-
const n = ++this.counter;
|
|
36
|
-
return {
|
|
37
|
-
dockerId: `dock_${n}`,
|
|
38
|
-
healthEndpoint: `http://127.0.0.1:${9000 + n}/health`,
|
|
39
|
-
metricsEndpoint: `http://127.0.0.1:${9000 + n}/metrics`,
|
|
40
|
-
seedEndpoint: `http://127.0.0.1:${9000 + n}/seed`,
|
|
41
|
-
seedToken: `seed-token-${n}`,
|
|
42
|
-
p2pPort: 4000 + n,
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
async stopContainer(id: string): Promise<void> { this.stopped.push(id); }
|
|
46
|
-
async removeContainer(id: string): Promise<void> { this.removed.push(id); }
|
|
47
|
-
async getStats(): Promise<OrchestratorStats> {
|
|
48
|
-
return { cpuPercent: 0, memoryBytes: 0, networkRxBytes: 0, networkTxBytes: 0 };
|
|
49
|
-
}
|
|
50
|
-
async isRunning(): Promise<boolean> { return true; }
|
|
51
|
-
async getLogs(): Promise<string> { return ''; }
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
let tmpRoot: string;
|
|
55
|
-
let app: ReturnType<typeof Fastify>;
|
|
56
|
-
let grants: GrantService;
|
|
57
|
-
let store: DonationStore;
|
|
58
|
-
let token: string;
|
|
59
|
-
const originalFetch = globalThis.fetch;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* A well-formed 32-byte base64url owner key. `DonationService.provision`
|
|
63
|
-
* shape-checks the pins, so a placeholder string is a 400 rather than a fixture.
|
|
64
|
-
*/
|
|
65
|
-
const OWNER_KEY = Buffer.alloc(32, 7).toString('base64url');
|
|
66
|
-
|
|
67
|
-
const body = {
|
|
68
|
-
partyId: 'party-P',
|
|
69
|
-
bootstrapNodes: ['/ip4/127.0.0.1/tcp/4001/p2p/
|
|
70
|
-
ownerKeys: [OWNER_KEY],
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const bearer = (t: string) => ({ authorization: `Bearer ${t}` });
|
|
74
|
-
|
|
75
|
-
beforeEach(() => {
|
|
76
|
-
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-grants-svc-route-'));
|
|
77
|
-
grants = new GrantService({ store: new GrantStore(join(tmpRoot, 'grants')) });
|
|
78
|
-
token = grants.issue({ label: 'Alice', maxNodes: 2 }).token;
|
|
79
|
-
store = new DonationStore(join(tmpRoot, 'donations'));
|
|
80
|
-
const donations = new DonationService({ orchestrator: new FakeOrchestrator(), grants, store });
|
|
81
|
-
app = Fastify();
|
|
82
|
-
registerErrorHandler(app);
|
|
83
|
-
registerGrantsRoutes(app, { donations, grants });
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
afterEach(async () => {
|
|
87
|
-
globalThis.fetch = originalFetch;
|
|
88
|
-
await app.close();
|
|
89
|
-
try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
describe('POST /grants', () => {
|
|
93
|
-
it('provisions a donated node → 201, awaiting_seed, seedToken redacted', async () => {
|
|
94
|
-
const res = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
95
|
-
expect(res.statusCode).toBe(201);
|
|
96
|
-
const donation = (res.json() as { data: { donation: Record<string, unknown> } }).data.donation;
|
|
97
|
-
expect(donation.status).toBe('awaiting_seed');
|
|
98
|
-
expect(donation.partyId).toBe('party-P');
|
|
99
|
-
expect(donation.seedToken).toBeUndefined();
|
|
100
|
-
expect(donation.seedEndpoint).toBeUndefined();
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('rejects a missing bearer → 401', async () => {
|
|
104
|
-
const res = await app.inject({ method: 'POST', url: '/grants', payload: body });
|
|
105
|
-
expect(res.statusCode).toBe(401);
|
|
106
|
-
expect((res.json() as { error: { code: string } }).error.code).toBe('unauthorized');
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('rejects an unknown bearer → 401', async () => {
|
|
110
|
-
const res = await app.inject({ method: 'POST', url: '/grants', headers: bearer('nope'), payload: body });
|
|
111
|
-
expect(res.statusCode).toBe(401);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it('rejects a missing partyId → 400 invalid_request', async () => {
|
|
115
|
-
const res = await app.inject({
|
|
116
|
-
method: 'POST', url: '/grants', headers: bearer(token),
|
|
117
|
-
payload: { bootstrapNodes: body.bootstrapNodes, ownerKeys: body.ownerKeys },
|
|
118
|
-
});
|
|
119
|
-
expect(res.statusCode).toBe(400);
|
|
120
|
-
expect((res.json() as { error: { code: string } }).error.code).toBe('invalid_request');
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('rejects empty ownerKeys → 400 invalid_request', async () => {
|
|
124
|
-
const res = await app.inject({
|
|
125
|
-
method: 'POST', url: '/grants', headers: bearer(token),
|
|
126
|
-
payload: { ...body, ownerKeys: [] },
|
|
127
|
-
});
|
|
128
|
-
expect(res.statusCode).toBe(400);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
// A typo'd pin used to be answered 201 and only fail when the spawned child
|
|
132
|
-
// tried to decode it at boot — by which point the caller had no way to act on it.
|
|
133
|
-
it('rejects a malformed ownerKeys entry → 400 invalid_request, naming the bad key', async () => {
|
|
134
|
-
const res = await app.inject({
|
|
135
|
-
method: 'POST', url: '/grants', headers: bearer(token),
|
|
136
|
-
payload: { ...body, ownerKeys: [OWNER_KEY, 'this-is-not-a-key'] },
|
|
137
|
-
});
|
|
138
|
-
expect(res.statusCode).toBe(400);
|
|
139
|
-
const error = (res.json() as { error: { code: string; message: string } }).error;
|
|
140
|
-
expect(error.code).toBe('invalid_request');
|
|
141
|
-
expect(error.message).toContain('this-is-not-a-key');
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
// Element types are the route's job: the service's shape check assumes strings,
|
|
145
|
-
// so a number here would come back as an internal `.trim is not a function`.
|
|
146
|
-
it('rejects a non-string ownerKeys entry → 400 naming the field, not a decoder error', async () => {
|
|
147
|
-
const res = await app.inject({
|
|
148
|
-
method: 'POST', url: '/grants', headers: bearer(token),
|
|
149
|
-
payload: { ...body, ownerKeys: [42] },
|
|
150
|
-
});
|
|
151
|
-
expect(res.statusCode).toBe(400);
|
|
152
|
-
const error = (res.json() as { error: { code: string; message: string } }).error;
|
|
153
|
-
expect(error.code).toBe('invalid_request');
|
|
154
|
-
expect(error.message).toBe('ownerKeys must be an array of strings');
|
|
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
|
-
const
|
|
196
|
-
expect(
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
expect(
|
|
226
|
-
expect((
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
expect(
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Grantee-facing `/grants` route tests — the bearer-gated provisioning surface.
|
|
3
|
+
*
|
|
4
|
+
* Uses a fake orchestrator (no real children) so the focus is the HTTP contract:
|
|
5
|
+
* bearer auth, request validation, per-grant ownership isolation, the quota gate,
|
|
6
|
+
* and seedToken redaction. The live-node round-trips (peer/seed) are covered by
|
|
7
|
+
* the cross-package integration scenario.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import Fastify from 'fastify';
|
|
11
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
12
|
+
import { mkdtempSync, rmSync } from 'node:fs';
|
|
13
|
+
import { tmpdir } from 'node:os';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
|
|
16
|
+
import type {
|
|
17
|
+
Orchestrator,
|
|
18
|
+
OrchestratorCreateRequest,
|
|
19
|
+
OrchestratorCreateResult,
|
|
20
|
+
OrchestratorStats,
|
|
21
|
+
} from '@serfab/cadre-provider';
|
|
22
|
+
|
|
23
|
+
import { registerErrorHandler } from '../error-handler.js';
|
|
24
|
+
import { registerGrantsRoutes } from '../routes/grants.js';
|
|
25
|
+
import { DonationService } from '../../donation/donation-service.js';
|
|
26
|
+
import { DonationStore } from '../../donation/donation-store.js';
|
|
27
|
+
import { GrantService } from '../../donation/grant-service.js';
|
|
28
|
+
import { GrantStore } from '../../donation/grant-store.js';
|
|
29
|
+
|
|
30
|
+
class FakeOrchestrator implements Orchestrator {
|
|
31
|
+
stopped: string[] = [];
|
|
32
|
+
removed: string[] = [];
|
|
33
|
+
private counter = 0;
|
|
34
|
+
async createContainer(_request: OrchestratorCreateRequest): Promise<OrchestratorCreateResult> {
|
|
35
|
+
const n = ++this.counter;
|
|
36
|
+
return {
|
|
37
|
+
dockerId: `dock_${n}`,
|
|
38
|
+
healthEndpoint: `http://127.0.0.1:${9000 + n}/health`,
|
|
39
|
+
metricsEndpoint: `http://127.0.0.1:${9000 + n}/metrics`,
|
|
40
|
+
seedEndpoint: `http://127.0.0.1:${9000 + n}/seed`,
|
|
41
|
+
seedToken: `seed-token-${n}`,
|
|
42
|
+
p2pPort: 4000 + n,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async stopContainer(id: string): Promise<void> { this.stopped.push(id); }
|
|
46
|
+
async removeContainer(id: string): Promise<void> { this.removed.push(id); }
|
|
47
|
+
async getStats(): Promise<OrchestratorStats> {
|
|
48
|
+
return { cpuPercent: 0, memoryBytes: 0, networkRxBytes: 0, networkTxBytes: 0 };
|
|
49
|
+
}
|
|
50
|
+
async isRunning(): Promise<boolean> { return true; }
|
|
51
|
+
async getLogs(): Promise<string> { return ''; }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let tmpRoot: string;
|
|
55
|
+
let app: ReturnType<typeof Fastify>;
|
|
56
|
+
let grants: GrantService;
|
|
57
|
+
let store: DonationStore;
|
|
58
|
+
let token: string;
|
|
59
|
+
const originalFetch = globalThis.fetch;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A well-formed 32-byte base64url owner key. `DonationService.provision`
|
|
63
|
+
* shape-checks the pins, so a placeholder string is a 400 rather than a fixture.
|
|
64
|
+
*/
|
|
65
|
+
const OWNER_KEY = Buffer.alloc(32, 7).toString('base64url');
|
|
66
|
+
|
|
67
|
+
const body = {
|
|
68
|
+
partyId: 'party-P',
|
|
69
|
+
bootstrapNodes: ['/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWA9hbnKrRnPRSPTRkzXqTHzGE8YpJ3JHZmQ5tGwLRTMmp'],
|
|
70
|
+
ownerKeys: [OWNER_KEY],
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const bearer = (t: string) => ({ authorization: `Bearer ${t}` });
|
|
74
|
+
|
|
75
|
+
beforeEach(() => {
|
|
76
|
+
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-grants-svc-route-'));
|
|
77
|
+
grants = new GrantService({ store: new GrantStore(join(tmpRoot, 'grants')) });
|
|
78
|
+
token = grants.issue({ label: 'Alice', maxNodes: 2 }).token;
|
|
79
|
+
store = new DonationStore(join(tmpRoot, 'donations'));
|
|
80
|
+
const donations = new DonationService({ orchestrator: new FakeOrchestrator(), grants, store });
|
|
81
|
+
app = Fastify();
|
|
82
|
+
registerErrorHandler(app);
|
|
83
|
+
registerGrantsRoutes(app, { donations, grants });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
afterEach(async () => {
|
|
87
|
+
globalThis.fetch = originalFetch;
|
|
88
|
+
await app.close();
|
|
89
|
+
try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('POST /grants', () => {
|
|
93
|
+
it('provisions a donated node → 201, awaiting_seed, seedToken redacted', async () => {
|
|
94
|
+
const res = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
95
|
+
expect(res.statusCode).toBe(201);
|
|
96
|
+
const donation = (res.json() as { data: { donation: Record<string, unknown> } }).data.donation;
|
|
97
|
+
expect(donation.status).toBe('awaiting_seed');
|
|
98
|
+
expect(donation.partyId).toBe('party-P');
|
|
99
|
+
expect(donation.seedToken).toBeUndefined();
|
|
100
|
+
expect(donation.seedEndpoint).toBeUndefined();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('rejects a missing bearer → 401', async () => {
|
|
104
|
+
const res = await app.inject({ method: 'POST', url: '/grants', payload: body });
|
|
105
|
+
expect(res.statusCode).toBe(401);
|
|
106
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('unauthorized');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('rejects an unknown bearer → 401', async () => {
|
|
110
|
+
const res = await app.inject({ method: 'POST', url: '/grants', headers: bearer('nope'), payload: body });
|
|
111
|
+
expect(res.statusCode).toBe(401);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('rejects a missing partyId → 400 invalid_request', async () => {
|
|
115
|
+
const res = await app.inject({
|
|
116
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
117
|
+
payload: { bootstrapNodes: body.bootstrapNodes, ownerKeys: body.ownerKeys },
|
|
118
|
+
});
|
|
119
|
+
expect(res.statusCode).toBe(400);
|
|
120
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('invalid_request');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('rejects empty ownerKeys → 400 invalid_request', async () => {
|
|
124
|
+
const res = await app.inject({
|
|
125
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
126
|
+
payload: { ...body, ownerKeys: [] },
|
|
127
|
+
});
|
|
128
|
+
expect(res.statusCode).toBe(400);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// A typo'd pin used to be answered 201 and only fail when the spawned child
|
|
132
|
+
// tried to decode it at boot — by which point the caller had no way to act on it.
|
|
133
|
+
it('rejects a malformed ownerKeys entry → 400 invalid_request, naming the bad key', async () => {
|
|
134
|
+
const res = await app.inject({
|
|
135
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
136
|
+
payload: { ...body, ownerKeys: [OWNER_KEY, 'this-is-not-a-key'] },
|
|
137
|
+
});
|
|
138
|
+
expect(res.statusCode).toBe(400);
|
|
139
|
+
const error = (res.json() as { error: { code: string; message: string } }).error;
|
|
140
|
+
expect(error.code).toBe('invalid_request');
|
|
141
|
+
expect(error.message).toContain('this-is-not-a-key');
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Element types are the route's job: the service's shape check assumes strings,
|
|
145
|
+
// so a number here would come back as an internal `.trim is not a function`.
|
|
146
|
+
it('rejects a non-string ownerKeys entry → 400 naming the field, not a decoder error', async () => {
|
|
147
|
+
const res = await app.inject({
|
|
148
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
149
|
+
payload: { ...body, ownerKeys: [42] },
|
|
150
|
+
});
|
|
151
|
+
expect(res.statusCode).toBe(400);
|
|
152
|
+
const error = (res.json() as { error: { code: string; message: string } }).error;
|
|
153
|
+
expect(error.code).toBe('invalid_request');
|
|
154
|
+
expect(error.message).toBe('ownerKeys must be an array of strings');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// The address rule the whole `bootstrap-node-validation.ts` module exists for,
|
|
158
|
+
// at the route boundary. Its accept/reject table is pinned in
|
|
159
|
+
// `bootstrap-node-validation.test.ts`; what matters here is that a bad address
|
|
160
|
+
// is a 400 naming the entry, and that no child was spawned on the way.
|
|
161
|
+
it('rejects an unusable bootstrapNodes entry → 400 invalid_request, naming the bad address', async () => {
|
|
162
|
+
const res = await app.inject({
|
|
163
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
164
|
+
payload: { ...body, bootstrapNodes: [...body.bootstrapNodes, 'not-an-address'] },
|
|
165
|
+
});
|
|
166
|
+
expect(res.statusCode).toBe(400);
|
|
167
|
+
const error = (res.json() as { error: { code: string; message: string } }).error;
|
|
168
|
+
expect(error.code).toBe('invalid_request');
|
|
169
|
+
expect(error.message).toContain('not-an-address');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('provisions nothing for any of the unusable-address shapes', async () => {
|
|
173
|
+
const bad: unknown[] = [
|
|
174
|
+
[42],
|
|
175
|
+
['not-an-address'],
|
|
176
|
+
// Parses, but names no peer: the child would start with zero bootstrap
|
|
177
|
+
// peers and never reach the requester's cadre.
|
|
178
|
+
['/ip4/127.0.0.1/tcp/4001'],
|
|
179
|
+
// Names a peer id that does not decode: the child dies constructing libp2p.
|
|
180
|
+
['/ip4/127.0.0.1/tcp/4001/p2p/12D3KooReq'],
|
|
181
|
+
// Names a decodable peer but no place to dial it: bootstrap keeps it, libp2p
|
|
182
|
+
// finds no transport for it, and the node is alone just the same.
|
|
183
|
+
['/p2p/12D3KooWA9hbnKrRnPRSPTRkzXqTHzGE8YpJ3JHZmQ5tGwLRTMmp'],
|
|
184
|
+
[''],
|
|
185
|
+
[],
|
|
186
|
+
];
|
|
187
|
+
for (const bootstrapNodes of bad) {
|
|
188
|
+
const res = await app.inject({
|
|
189
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
190
|
+
payload: { ...body, bootstrapNodes },
|
|
191
|
+
});
|
|
192
|
+
expect(res.statusCode, `expected 400 for ${JSON.stringify(bootstrapNodes)}`).toBe(400);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const list = await app.inject({ method: 'GET', url: '/grants', headers: bearer(token) });
|
|
196
|
+
expect((list.json() as { data: { donations: unknown[] } }).data.donations).toHaveLength(0);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('rejects a non-string partyId → 400, rather than stringifying it into the child config', async () => {
|
|
200
|
+
for (const partyId of [42, { id: 'party-P' }, ' ']) {
|
|
201
|
+
const res = await app.inject({
|
|
202
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
203
|
+
payload: { ...body, partyId },
|
|
204
|
+
});
|
|
205
|
+
expect(res.statusCode, `expected 400 for partyId ${JSON.stringify(partyId)}`).toBe(400);
|
|
206
|
+
expect((res.json() as { error: { message: string } }).error.message).toBe('partyId is required');
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('rejects an unknown profile → 400', async () => {
|
|
211
|
+
const res = await app.inject({
|
|
212
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
213
|
+
payload: { ...body, profile: 'archive' },
|
|
214
|
+
});
|
|
215
|
+
expect(res.statusCode).toBe(400);
|
|
216
|
+
expect((res.json() as { error: { message: string } }).error.message)
|
|
217
|
+
.toBe('profile must be "storage" or "transaction"');
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('enforces the grant quota → 429 quota_exceeded once maxNodes is reached', async () => {
|
|
221
|
+
const g = grants.issue({ label: 'Bob', maxNodes: 1 }).token;
|
|
222
|
+
const first = await app.inject({ method: 'POST', url: '/grants', headers: bearer(g), payload: body });
|
|
223
|
+
expect(first.statusCode).toBe(201);
|
|
224
|
+
const second = await app.inject({ method: 'POST', url: '/grants', headers: bearer(g), payload: body });
|
|
225
|
+
expect(second.statusCode).toBe(429);
|
|
226
|
+
expect((second.json() as { error: { code: string } }).error.code).toBe('quota_exceeded');
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
describe('GET /grants auth', () => {
|
|
231
|
+
it('rejects an unknown bearer → 401 unauthorized (code matches the status)', async () => {
|
|
232
|
+
const res = await app.inject({ method: 'GET', url: '/grants', headers: bearer('nope') });
|
|
233
|
+
expect(res.statusCode).toBe(401);
|
|
234
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('unauthorized');
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
describe('GET /grants (ownership isolation)', () => {
|
|
239
|
+
it('lists only the donations the presented grant authorized', async () => {
|
|
240
|
+
const other = grants.issue({ label: 'Carol' }).token;
|
|
241
|
+
await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
242
|
+
await app.inject({ method: 'POST', url: '/grants', headers: bearer(other), payload: body });
|
|
243
|
+
|
|
244
|
+
const mine = await app.inject({ method: 'GET', url: '/grants', headers: bearer(token) });
|
|
245
|
+
const donations = (mine.json() as { data: { donations: Array<{ grantToken: string }> } }).data.donations;
|
|
246
|
+
expect(donations).toHaveLength(1);
|
|
247
|
+
expect(donations[0].grantToken).toBe(token);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('a foreign grant cannot read another grant’s donation → 404', async () => {
|
|
251
|
+
const other = grants.issue({ label: 'Carol' }).token;
|
|
252
|
+
const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
253
|
+
const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
|
|
254
|
+
|
|
255
|
+
const mine = await app.inject({ method: 'GET', url: `/grants/${id}`, headers: bearer(token) });
|
|
256
|
+
expect(mine.statusCode).toBe(200);
|
|
257
|
+
|
|
258
|
+
const foreign = await app.inject({ method: 'GET', url: `/grants/${id}`, headers: bearer(other) });
|
|
259
|
+
expect(foreign.statusCode).toBe(404);
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
describe('PUT /grants/:id/seed validation', () => {
|
|
264
|
+
it('rejects a missing seed body → 400 invalid_request', async () => {
|
|
265
|
+
const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
266
|
+
const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
|
|
267
|
+
const res = await app.inject({ method: 'PUT', url: `/grants/${id}/seed`, headers: bearer(token), payload: {} });
|
|
268
|
+
expect(res.statusCode).toBe(400);
|
|
269
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('invalid_request');
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('answers 409 invalid_state when the loan ends while the seed is in flight', async () => {
|
|
273
|
+
const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
274
|
+
const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
|
|
275
|
+
|
|
276
|
+
// The borrower's DELETE lands inside the seed window: the node accepts the
|
|
277
|
+
// seed, but by the time it answers the loan is over. The ending wins, so the
|
|
278
|
+
// route must NOT report a 200 that implies a live, freshly seeded node.
|
|
279
|
+
globalThis.fetch = (async () => {
|
|
280
|
+
await app.inject({ method: 'DELETE', url: `/grants/${id}`, headers: bearer(token) });
|
|
281
|
+
return { ok: true, json: async () => ({ success: true, peersAdded: 2 }) } as unknown as Response;
|
|
282
|
+
}) as typeof globalThis.fetch;
|
|
283
|
+
|
|
284
|
+
const res = await app.inject({
|
|
285
|
+
method: 'PUT', url: `/grants/${id}/seed`, headers: bearer(token), payload: { seed: 'encoded-seed' },
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
expect(res.statusCode).toBe(409);
|
|
289
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('invalid_state');
|
|
290
|
+
const after = await app.inject({ method: 'GET', url: `/grants/${id}`, headers: bearer(token) });
|
|
291
|
+
expect((after.json() as { data: { donation: { status: string } } }).data.donation.status)
|
|
292
|
+
.toBe('terminated');
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('answers 404 not_found when the record vanishes while the seed is in flight', async () => {
|
|
296
|
+
const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
297
|
+
const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
|
|
298
|
+
|
|
299
|
+
// The row is gone by the time the node answers — the other half of the
|
|
300
|
+
// abandon mapping, which has no status to report.
|
|
301
|
+
globalThis.fetch = (async () => {
|
|
302
|
+
store.remove(id);
|
|
303
|
+
return { ok: true, json: async () => ({ success: true, peersAdded: 2 }) } as unknown as Response;
|
|
304
|
+
}) as typeof globalThis.fetch;
|
|
305
|
+
|
|
306
|
+
const res = await app.inject({
|
|
307
|
+
method: 'PUT', url: `/grants/${id}/seed`, headers: bearer(token), payload: { seed: 'encoded-seed' },
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
expect(res.statusCode).toBe(404);
|
|
311
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('not_found');
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
describe('DELETE /grants/:id', () => {
|
|
316
|
+
it('terminates a donation and drops it from the live tally', async () => {
|
|
317
|
+
const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
318
|
+
const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
|
|
319
|
+
|
|
320
|
+
const del = await app.inject({ method: 'DELETE', url: `/grants/${id}`, headers: bearer(token) });
|
|
321
|
+
expect(del.statusCode).toBe(200);
|
|
322
|
+
|
|
323
|
+
const one = await app.inject({ method: 'GET', url: `/grants/${id}`, headers: bearer(token) });
|
|
324
|
+
expect((one.json() as { data: { donation: { status: string } } }).data.donation.status).toBe('terminated');
|
|
325
|
+
});
|
|
326
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
1
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { tmpdir } from 'node:os';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
|
|
@@ -180,15 +180,16 @@ describe('/api/settings routes', () => {
|
|
|
180
180
|
expect(res.statusCode).toBe(400);
|
|
181
181
|
});
|
|
182
182
|
|
|
183
|
-
it('
|
|
184
|
-
writeV1Config(dataDir);
|
|
183
|
+
it('rejects a v1 host.config.json without rewriting it', async () => {
|
|
184
|
+
const path = writeV1Config(dataDir);
|
|
185
|
+
const raw = readFileSync(path, 'utf8');
|
|
185
186
|
app = Fastify();
|
|
186
187
|
registerErrorHandler(app);
|
|
187
188
|
registerSettingsRoutes(app, { settingsStore: new HostSettingsStore({ dataDir }), nat: fakeNat().svc });
|
|
188
189
|
const res = await app.inject({ method: 'GET', url: '/api/settings' });
|
|
189
|
-
expect(res.statusCode).toBe(
|
|
190
|
-
const body = res.json() as {
|
|
191
|
-
expect(body.
|
|
192
|
-
expect(
|
|
190
|
+
expect(res.statusCode).toBe(500);
|
|
191
|
+
const body = res.json() as { error: { message: string } };
|
|
192
|
+
expect(body.error.message).toMatch(/unsupported version=1/);
|
|
193
|
+
expect(readFileSync(path, 'utf8')).toBe(raw);
|
|
193
194
|
});
|
|
194
195
|
});
|