@serfab/cadre-host 0.12.0 → 1.0.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 +6 -4
- package/dist/bin/host.js +9 -1
- package/dist/bin/host.js.map +1 -1
- package/dist/donation/__tests__/fake-orchestrator.js +1 -1
- package/dist/donation/__tests__/fake-orchestrator.js.map +1 -1
- package/dist/donation/donation-service.d.ts +7 -2
- package/dist/donation/donation-service.js +16 -2
- package/dist/donation/donation-service.js.map +1 -1
- package/dist/donation/donation-supervisor.d.ts +12 -2
- package/dist/donation/donation-supervisor.js +25 -2
- package/dist/donation/donation-supervisor.js.map +1 -1
- package/dist/donation/types.d.ts +2 -1
- package/dist/donation/types.js.map +1 -1
- package/dist/installer/index.d.ts +9 -1
- package/dist/installer/index.js +5 -1
- package/dist/installer/index.js.map +1 -1
- package/dist/orchestrator/host-process-orchestrator.d.ts +71 -11
- package/dist/orchestrator/host-process-orchestrator.js +173 -75
- package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
- package/dist/orchestrator/port-allocator.d.ts +44 -2
- package/dist/orchestrator/port-allocator.js +63 -5
- package/dist/orchestrator/port-allocator.js.map +1 -1
- package/dist/orchestrator/port-probe.d.ts +15 -0
- package/dist/orchestrator/port-probe.js +22 -0
- package/dist/orchestrator/port-probe.js.map +1 -0
- package/dist/orchestrator/types.d.ts +13 -1
- package/dist/orchestrator/types.js.map +1 -1
- package/dist/server/routes/bootstrap-node-validation.d.ts +115 -0
- package/dist/server/routes/bootstrap-node-validation.js +197 -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/server.d.ts +4 -1
- package/dist/server/server.js +36 -2
- package/dist/server/server.js.map +1 -1
- package/dist/ui/assets/{index-C-iks41M.js → index-CEoNRwM_.js} +3 -3
- package/dist/ui/assets/{index-C-iks41M.js.map → index-CEoNRwM_.js.map} +1 -1
- package/dist/ui/index.html +1 -1
- package/package.json +5 -4
- package/src/__tests__/orchestrator-env-scrub.test.ts +22 -0
- package/src/__tests__/orchestrator-node-identity.test.ts +4 -4
- package/src/__tests__/orchestrator-owner.test.ts +45 -2
- package/src/__tests__/orchestrator-ports.test.ts +172 -0
- package/src/__tests__/orchestrator.test.ts +1062 -951
- package/src/bin/host.ts +9 -1
- package/src/donation/__tests__/donation-service.test.ts +94 -7
- package/src/donation/__tests__/donation-supervisor.test.ts +50 -12
- package/src/donation/__tests__/fake-orchestrator.test.ts +168 -168
- package/src/donation/__tests__/fake-orchestrator.ts +1 -1
- package/src/donation/donation-service.ts +22 -3
- package/src/donation/donation-supervisor.ts +25 -2
- package/src/donation/types.ts +2 -1
- package/src/installer/__tests__/installer.smoke.test.ts +263 -218
- package/src/installer/index.ts +327 -314
- package/src/orchestrator/host-process-orchestrator.ts +1250 -1138
- package/src/orchestrator/port-allocator.ts +148 -91
- package/src/orchestrator/port-probe.ts +31 -0
- package/src/orchestrator/types.ts +13 -1
- package/src/server/__tests__/bootstrap-node-validation.test.ts +138 -0
- package/src/server/__tests__/grants-route.test.ts +394 -263
- package/src/server/__tests__/nodes-route.test.ts +2 -2
- package/src/server/__tests__/publishers.test.ts +1 -1
- package/src/server/__tests__/status-route.test.ts +1 -1
- package/src/server/routes/bootstrap-node-validation.ts +206 -0
- package/src/server/routes/grants.ts +9 -26
- package/src/server/routes/provision-request-validation.ts +105 -0
- package/src/server/server.ts +157 -122
|
@@ -1,263 +1,394 @@
|
|
|
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
|
|
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 {
|
|
26
|
-
import {
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
async
|
|
47
|
-
async
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
async
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
let
|
|
56
|
-
let
|
|
57
|
-
let
|
|
58
|
-
let
|
|
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
|
-
const
|
|
97
|
-
expect(
|
|
98
|
-
|
|
99
|
-
expect(donation.
|
|
100
|
-
expect(donation.
|
|
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
|
-
expect(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
expect(
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
const
|
|
170
|
-
expect(
|
|
171
|
-
expect(
|
|
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
|
-
const
|
|
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
|
-
expect(
|
|
249
|
-
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
const
|
|
261
|
-
expect(
|
|
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 type { FastifyInstance } 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 { buildFastify } from '../server.js';
|
|
26
|
+
import { DonationService } from '../../donation/donation-service.js';
|
|
27
|
+
import { DonationStore } from '../../donation/donation-store.js';
|
|
28
|
+
import { GrantService } from '../../donation/grant-service.js';
|
|
29
|
+
import { GrantStore } from '../../donation/grant-store.js';
|
|
30
|
+
|
|
31
|
+
class FakeOrchestrator implements Orchestrator {
|
|
32
|
+
stopped: string[] = [];
|
|
33
|
+
removed: string[] = [];
|
|
34
|
+
private counter = 0;
|
|
35
|
+
async createContainer(_request: OrchestratorCreateRequest): Promise<OrchestratorCreateResult> {
|
|
36
|
+
const n = ++this.counter;
|
|
37
|
+
return {
|
|
38
|
+
dockerId: `dock_${n}`,
|
|
39
|
+
healthEndpoint: `http://127.0.0.1:${9000 + n}/health`,
|
|
40
|
+
metricsEndpoint: `http://127.0.0.1:${9000 + n}/metrics`,
|
|
41
|
+
seedEndpoint: `http://127.0.0.1:${9000 + n}/seed`,
|
|
42
|
+
seedToken: `seed-token-${n}`,
|
|
43
|
+
p2pPort: 4000 + n,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
async stopContainer(id: string): Promise<void> { this.stopped.push(id); }
|
|
47
|
+
async removeContainer(id: string): Promise<void> { this.removed.push(id); }
|
|
48
|
+
async getStats(): Promise<OrchestratorStats> {
|
|
49
|
+
return { cpuPercent: 0, memoryBytes: 0, networkRxBytes: 0, networkTxBytes: 0 };
|
|
50
|
+
}
|
|
51
|
+
async isRunning(): Promise<boolean> { return true; }
|
|
52
|
+
async getLogs(): Promise<string> { return ''; }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let tmpRoot: string;
|
|
56
|
+
let app: FastifyInstance;
|
|
57
|
+
let grants: GrantService;
|
|
58
|
+
let store: DonationStore;
|
|
59
|
+
let token: string;
|
|
60
|
+
const originalFetch = globalThis.fetch;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A well-formed 32-byte base64url owner key. `DonationService.provision`
|
|
64
|
+
* shape-checks the pins, so a placeholder string is a 400 rather than a fixture.
|
|
65
|
+
*/
|
|
66
|
+
const OWNER_KEY = Buffer.alloc(32, 7).toString('base64url');
|
|
67
|
+
|
|
68
|
+
const body = {
|
|
69
|
+
partyId: 'party-P',
|
|
70
|
+
bootstrapNodes: ['/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWA9hbnKrRnPRSPTRkzXqTHzGE8YpJ3JHZmQ5tGwLRTMmp'],
|
|
71
|
+
ownerKeys: [OWNER_KEY],
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const bearer = (t: string) => ({ authorization: `Bearer ${t}` });
|
|
75
|
+
|
|
76
|
+
beforeEach(() => {
|
|
77
|
+
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-grants-svc-route-'));
|
|
78
|
+
grants = new GrantService({ store: new GrantStore(join(tmpRoot, 'grants')) });
|
|
79
|
+
token = grants.issue({ label: 'Alice', maxNodes: 2 }).token;
|
|
80
|
+
store = new DonationStore(join(tmpRoot, 'donations'));
|
|
81
|
+
const donations = new DonationService({ orchestrator: new FakeOrchestrator(), grants, store });
|
|
82
|
+
// The production constructor, so these tests run with the same body parsing.
|
|
83
|
+
app = buildFastify();
|
|
84
|
+
registerErrorHandler(app);
|
|
85
|
+
registerGrantsRoutes(app, { donations, grants });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
afterEach(async () => {
|
|
89
|
+
globalThis.fetch = originalFetch;
|
|
90
|
+
await app.close();
|
|
91
|
+
try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('POST /grants', () => {
|
|
95
|
+
it('provisions a donated node → 201, awaiting_seed, seedToken redacted', async () => {
|
|
96
|
+
const res = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
97
|
+
expect(res.statusCode).toBe(201);
|
|
98
|
+
const donation = (res.json() as { data: { donation: Record<string, unknown> } }).data.donation;
|
|
99
|
+
expect(donation.status).toBe('awaiting_seed');
|
|
100
|
+
expect(donation.partyId).toBe('party-P');
|
|
101
|
+
expect(donation.seedToken).toBeUndefined();
|
|
102
|
+
expect(donation.seedEndpoint).toBeUndefined();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('rejects a missing bearer → 401', async () => {
|
|
106
|
+
const res = await app.inject({ method: 'POST', url: '/grants', payload: body });
|
|
107
|
+
expect(res.statusCode).toBe(401);
|
|
108
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('unauthorized');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('rejects an unknown bearer → 401', async () => {
|
|
112
|
+
const res = await app.inject({ method: 'POST', url: '/grants', headers: bearer('nope'), payload: body });
|
|
113
|
+
expect(res.statusCode).toBe(401);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('rejects a missing partyId → 400 invalid_request', async () => {
|
|
117
|
+
const res = await app.inject({
|
|
118
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
119
|
+
payload: { bootstrapNodes: body.bootstrapNodes, ownerKeys: body.ownerKeys },
|
|
120
|
+
});
|
|
121
|
+
expect(res.statusCode).toBe(400);
|
|
122
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('invalid_request');
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('rejects empty ownerKeys → 400 invalid_request', async () => {
|
|
126
|
+
const res = await app.inject({
|
|
127
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
128
|
+
payload: { ...body, ownerKeys: [] },
|
|
129
|
+
});
|
|
130
|
+
expect(res.statusCode).toBe(400);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// A typo'd pin used to be answered 201 and only fail when the spawned child
|
|
134
|
+
// tried to decode it at boot — by which point the caller had no way to act on it.
|
|
135
|
+
it('rejects a malformed ownerKeys entry → 400 invalid_request, naming the bad key', async () => {
|
|
136
|
+
const res = await app.inject({
|
|
137
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
138
|
+
payload: { ...body, ownerKeys: [OWNER_KEY, 'this-is-not-a-key'] },
|
|
139
|
+
});
|
|
140
|
+
expect(res.statusCode).toBe(400);
|
|
141
|
+
const error = (res.json() as { error: { code: string; message: string } }).error;
|
|
142
|
+
expect(error.code).toBe('invalid_request');
|
|
143
|
+
expect(error.message).toContain('this-is-not-a-key');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// Element types are the route's job: the service's shape check assumes strings,
|
|
147
|
+
// so a number here would come back as an internal `.trim is not a function`.
|
|
148
|
+
it('rejects a non-string ownerKeys entry → 400 naming the field, not a decoder error', async () => {
|
|
149
|
+
const res = await app.inject({
|
|
150
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
151
|
+
payload: { ...body, ownerKeys: [42] },
|
|
152
|
+
});
|
|
153
|
+
expect(res.statusCode).toBe(400);
|
|
154
|
+
const error = (res.json() as { error: { code: string; message: string } }).error;
|
|
155
|
+
expect(error.code).toBe('invalid_request');
|
|
156
|
+
expect(error.message).toBe('ownerKeys must be an array of strings');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// The address rule the whole `bootstrap-node-validation.ts` module exists for,
|
|
160
|
+
// at the route boundary. Its accept/reject table is pinned in
|
|
161
|
+
// `bootstrap-node-validation.test.ts`; what matters here is that a bad address
|
|
162
|
+
// is a 400 naming the entry, and that no child was spawned on the way.
|
|
163
|
+
it('rejects an unusable bootstrapNodes entry → 400 invalid_request, naming the bad address', async () => {
|
|
164
|
+
const res = await app.inject({
|
|
165
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
166
|
+
payload: { ...body, bootstrapNodes: [...body.bootstrapNodes, 'not-an-address'] },
|
|
167
|
+
});
|
|
168
|
+
expect(res.statusCode).toBe(400);
|
|
169
|
+
const error = (res.json() as { error: { code: string; message: string } }).error;
|
|
170
|
+
expect(error.code).toBe('invalid_request');
|
|
171
|
+
expect(error.message).toContain('not-an-address');
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('provisions nothing for any of the unusable-address shapes', async () => {
|
|
175
|
+
const bad: unknown[] = [
|
|
176
|
+
[42],
|
|
177
|
+
['not-an-address'],
|
|
178
|
+
// Parses, but names no peer: the child would start with zero bootstrap
|
|
179
|
+
// peers and never reach the requester's cadre.
|
|
180
|
+
['/ip4/127.0.0.1/tcp/4001'],
|
|
181
|
+
// Names a peer id that does not decode: the child dies constructing libp2p.
|
|
182
|
+
['/ip4/127.0.0.1/tcp/4001/p2p/12D3KooReq'],
|
|
183
|
+
// Names a decodable peer but no place to dial it: bootstrap keeps it, libp2p
|
|
184
|
+
// finds no transport for it, and the node is alone just the same.
|
|
185
|
+
['/p2p/12D3KooWA9hbnKrRnPRSPTRkzXqTHzGE8YpJ3JHZmQ5tGwLRTMmp'],
|
|
186
|
+
[''],
|
|
187
|
+
];
|
|
188
|
+
for (const bootstrapNodes of bad) {
|
|
189
|
+
const res = await app.inject({
|
|
190
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
191
|
+
payload: { ...body, bootstrapNodes },
|
|
192
|
+
});
|
|
193
|
+
expect(res.statusCode, `expected 400 for ${JSON.stringify(bootstrapNodes)}`).toBe(400);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const list = await app.inject({ method: 'GET', url: '/grants', headers: bearer(token) });
|
|
197
|
+
expect((list.json() as { data: { donations: unknown[] } }).data.donations).toHaveLength(0);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// A phone has no address to give, so it sends none and dials the lent node itself:
|
|
201
|
+
// leaving the field out, or sending an empty list, provisions a node with no
|
|
202
|
+
// bootstrap peers rather than being rejected.
|
|
203
|
+
it('provisions with bootstrapNodes absent or empty → 201, recording an empty list', async () => {
|
|
204
|
+
const { bootstrapNodes: _bootstrapNodes, ...withoutBootstrap } = body;
|
|
205
|
+
for (const payload of [withoutBootstrap, { ...body, bootstrapNodes: [] }]) {
|
|
206
|
+
const res = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload });
|
|
207
|
+
expect(res.statusCode, `expected 201 for ${JSON.stringify(payload)}`).toBe(201);
|
|
208
|
+
const donation = (res.json() as { data: { donation: { id: string; bootstrapNodes?: string[] } } }).data.donation;
|
|
209
|
+
expect(donation.bootstrapNodes).toEqual([]);
|
|
210
|
+
expect(store.get(donation.id)?.bootstrapNodes).toEqual([]);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('rejects a non-string partyId → 400, rather than stringifying it into the child config', async () => {
|
|
215
|
+
for (const partyId of [42, { id: 'party-P' }, ' ']) {
|
|
216
|
+
const res = await app.inject({
|
|
217
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
218
|
+
payload: { ...body, partyId },
|
|
219
|
+
});
|
|
220
|
+
expect(res.statusCode, `expected 400 for partyId ${JSON.stringify(partyId)}`).toBe(400);
|
|
221
|
+
expect((res.json() as { error: { message: string } }).error.message).toBe('partyId is required');
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('rejects an unknown profile → 400', async () => {
|
|
226
|
+
const res = await app.inject({
|
|
227
|
+
method: 'POST', url: '/grants', headers: bearer(token),
|
|
228
|
+
payload: { ...body, profile: 'archive' },
|
|
229
|
+
});
|
|
230
|
+
expect(res.statusCode).toBe(400);
|
|
231
|
+
expect((res.json() as { error: { message: string } }).error.message)
|
|
232
|
+
.toBe('profile must be "storage" or "transaction"');
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('enforces the grant quota → 429 quota_exceeded once maxNodes is reached', async () => {
|
|
236
|
+
const g = grants.issue({ label: 'Bob', maxNodes: 1 }).token;
|
|
237
|
+
const first = await app.inject({ method: 'POST', url: '/grants', headers: bearer(g), payload: body });
|
|
238
|
+
expect(first.statusCode).toBe(201);
|
|
239
|
+
const second = await app.inject({ method: 'POST', url: '/grants', headers: bearer(g), payload: body });
|
|
240
|
+
expect(second.statusCode).toBe(429);
|
|
241
|
+
expect((second.json() as { error: { code: string } }).error.code).toBe('quota_exceeded');
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
describe('GET /grants auth', () => {
|
|
246
|
+
it('rejects an unknown bearer → 401 unauthorized (code matches the status)', async () => {
|
|
247
|
+
const res = await app.inject({ method: 'GET', url: '/grants', headers: bearer('nope') });
|
|
248
|
+
expect(res.statusCode).toBe(401);
|
|
249
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('unauthorized');
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe('GET /grants (ownership isolation)', () => {
|
|
254
|
+
it('lists only the donations the presented grant authorized', async () => {
|
|
255
|
+
const other = grants.issue({ label: 'Carol' }).token;
|
|
256
|
+
await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
257
|
+
await app.inject({ method: 'POST', url: '/grants', headers: bearer(other), payload: body });
|
|
258
|
+
|
|
259
|
+
const mine = await app.inject({ method: 'GET', url: '/grants', headers: bearer(token) });
|
|
260
|
+
const donations = (mine.json() as { data: { donations: Array<{ grantToken: string }> } }).data.donations;
|
|
261
|
+
expect(donations).toHaveLength(1);
|
|
262
|
+
expect(donations[0].grantToken).toBe(token);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('a foreign grant cannot read another grant’s donation → 404', async () => {
|
|
266
|
+
const other = grants.issue({ label: 'Carol' }).token;
|
|
267
|
+
const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
268
|
+
const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
|
|
269
|
+
|
|
270
|
+
const mine = await app.inject({ method: 'GET', url: `/grants/${id}`, headers: bearer(token) });
|
|
271
|
+
expect(mine.statusCode).toBe(200);
|
|
272
|
+
|
|
273
|
+
const foreign = await app.inject({ method: 'GET', url: `/grants/${id}`, headers: bearer(other) });
|
|
274
|
+
expect(foreign.statusCode).toBe(404);
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
describe('PUT /grants/:id/seed validation', () => {
|
|
279
|
+
it('rejects a missing seed body → 400 invalid_request', async () => {
|
|
280
|
+
const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
281
|
+
const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
|
|
282
|
+
const res = await app.inject({ method: 'PUT', url: `/grants/${id}/seed`, headers: bearer(token), payload: {} });
|
|
283
|
+
expect(res.statusCode).toBe(400);
|
|
284
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('invalid_request');
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it('answers 409 invalid_state when the loan ends while the seed is in flight', async () => {
|
|
288
|
+
const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
289
|
+
const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
|
|
290
|
+
|
|
291
|
+
// The borrower's DELETE lands inside the seed window: the node accepts the
|
|
292
|
+
// seed, but by the time it answers the loan is over. The ending wins, so the
|
|
293
|
+
// route must NOT report a 200 that implies a live, freshly seeded node.
|
|
294
|
+
globalThis.fetch = (async () => {
|
|
295
|
+
await app.inject({ method: 'DELETE', url: `/grants/${id}`, headers: bearer(token) });
|
|
296
|
+
return { ok: true, json: async () => ({ success: true, peersAdded: 2 }) } as unknown as Response;
|
|
297
|
+
}) as typeof globalThis.fetch;
|
|
298
|
+
|
|
299
|
+
const res = await app.inject({
|
|
300
|
+
method: 'PUT', url: `/grants/${id}/seed`, headers: bearer(token), payload: { seed: 'encoded-seed' },
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
expect(res.statusCode).toBe(409);
|
|
304
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('invalid_state');
|
|
305
|
+
const after = await app.inject({ method: 'GET', url: `/grants/${id}`, headers: bearer(token) });
|
|
306
|
+
expect((after.json() as { data: { donation: { status: string } } }).data.donation.status)
|
|
307
|
+
.toBe('terminated');
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it('answers 404 not_found when the record vanishes while the seed is in flight', async () => {
|
|
311
|
+
const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
312
|
+
const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
|
|
313
|
+
|
|
314
|
+
// The row is gone by the time the node answers — the other half of the
|
|
315
|
+
// abandon mapping, which has no status to report.
|
|
316
|
+
globalThis.fetch = (async () => {
|
|
317
|
+
store.remove(id);
|
|
318
|
+
return { ok: true, json: async () => ({ success: true, peersAdded: 2 }) } as unknown as Response;
|
|
319
|
+
}) as typeof globalThis.fetch;
|
|
320
|
+
|
|
321
|
+
const res = await app.inject({
|
|
322
|
+
method: 'PUT', url: `/grants/${id}/seed`, headers: bearer(token), payload: { seed: 'encoded-seed' },
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
expect(res.statusCode).toBe(404);
|
|
326
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('not_found');
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
describe('DELETE /grants/:id', () => {
|
|
331
|
+
it('terminates a donation and drops it from the live tally', async () => {
|
|
332
|
+
const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
333
|
+
const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
|
|
334
|
+
|
|
335
|
+
const del = await app.inject({ method: 'DELETE', url: `/grants/${id}`, headers: bearer(token) });
|
|
336
|
+
expect(del.statusCode).toBe(200);
|
|
337
|
+
|
|
338
|
+
const one = await app.inject({ method: 'GET', url: `/grants/${id}`, headers: bearer(token) });
|
|
339
|
+
expect((one.json() as { data: { donation: { status: string } } }).data.donation.status).toBe('terminated');
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
// The phone app sent `content-type: application/json` on its body-less DELETE, and
|
|
343
|
+
// Fastify's default parser answered 400 FST_ERR_CTP_EMPTY_JSON_BODY — so the loan,
|
|
344
|
+
// and the host's node, kept running.
|
|
345
|
+
it('ends the loan when the DELETE declares JSON but sends no body', async () => {
|
|
346
|
+
const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
|
|
347
|
+
const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
|
|
348
|
+
|
|
349
|
+
const del = await app.inject({
|
|
350
|
+
method: 'DELETE', url: `/grants/${id}`,
|
|
351
|
+
headers: { ...bearer(token), 'content-type': 'application/json' },
|
|
352
|
+
});
|
|
353
|
+
expect(del.statusCode).toBe(200);
|
|
354
|
+
expect(store.get(id)?.status).toBe('terminated');
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
describe('JSON body parsing (buildFastify)', () => {
|
|
359
|
+
const json = (t: string) => ({ ...bearer(t), 'content-type': 'application/json' });
|
|
360
|
+
|
|
361
|
+
it('treats a declared-JSON empty body as no body, so the route answers its own 400', async () => {
|
|
362
|
+
const res = await app.inject({ method: 'POST', url: '/grants', headers: json(token) });
|
|
363
|
+
expect(res.statusCode).toBe(400);
|
|
364
|
+
const error = (res.json() as { error: { code: string; message: string } }).error;
|
|
365
|
+
expect(error.code).toBe('invalid_request');
|
|
366
|
+
expect(error.message).toBe('partyId is required');
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it('still rejects a non-empty malformed body → 400 FST_ERR_CTP_INVALID_JSON_BODY', async () => {
|
|
370
|
+
const res = await app.inject({ method: 'POST', url: '/grants', headers: json(token), payload: '{"partyId":' });
|
|
371
|
+
expect(res.statusCode).toBe(400);
|
|
372
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('FST_ERR_CTP_INVALID_JSON_BODY');
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it('still refuses a prototype-poisoning body, as Fastify\'s own parser does', async () => {
|
|
376
|
+
const res = await app.inject({
|
|
377
|
+
method: 'POST', url: '/grants', headers: json(token),
|
|
378
|
+
payload: '{"partyId":"party-P","__proto__":{"polluted":true}}',
|
|
379
|
+
});
|
|
380
|
+
expect(res.statusCode).toBe(400);
|
|
381
|
+
expect((res.json() as { error: { code: string } }).error.code).toBe('FST_ERR_CTP_INVALID_JSON_BODY');
|
|
382
|
+
const list = await app.inject({ method: 'GET', url: '/grants', headers: bearer(token) });
|
|
383
|
+
expect((list.json() as { data: { donations: unknown[] } }).data.donations).toHaveLength(0);
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
it('parses a well-formed body with a charset parameter on the content type', async () => {
|
|
387
|
+
const res = await app.inject({
|
|
388
|
+
method: 'POST', url: '/grants',
|
|
389
|
+
headers: { ...bearer(token), 'content-type': 'application/json; charset=utf-8' },
|
|
390
|
+
payload: JSON.stringify(body),
|
|
391
|
+
});
|
|
392
|
+
expect(res.statusCode).toBe(201);
|
|
393
|
+
});
|
|
394
|
+
});
|