contracts: hub.storageRequestSchema and its reply, a sample and JSON Schemas. Website: an Owner or Admin asks for more 5 GB steps under Media library (signed to the Hub); the heartbeat reports the licence's storage limit, so the Hub emails the church at 80% and when full. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
75 lines
3.8 KiB
TypeScript
75 lines
3.8 KiB
TypeScript
import { after, describe, it } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import crypto from 'node:crypto';
|
|
import http from 'node:http';
|
|
import type { AddressInfo } from 'node:net';
|
|
import express from 'express';
|
|
import request from 'supertest';
|
|
import { hub } from '@crosscode/contracts';
|
|
import { hashToken } from '../src/lib/crypto.js';
|
|
import { stats } from '../src/hub/client.js';
|
|
import { harness, SITE } from './support/harness.js';
|
|
|
|
const SECRET = 'install-secret-0123456789abcdef0123456789';
|
|
const closers: (() => Promise<unknown>)[] = [];
|
|
after(async () => { for (const c of closers) await c(); });
|
|
|
|
/** The Hub: checks the install's signature as installApi.js does, and records what it's asked. */
|
|
async function fakeHub() {
|
|
const asked: { installId: string; body: unknown }[] = [];
|
|
const app = express();
|
|
app.post('/api/installs/storage-request', express.text({ type: () => true }), (req, res) => {
|
|
const ts = String(req.get('X-Hub-Timestamp'));
|
|
const expected = crypto.createHmac('sha256', SECRET).update(`${ts}.${req.body}`).digest('hex');
|
|
if (req.get('X-Hub-Signature') !== expected) return res.status(401).json({ code: 'BAD_SIGNATURE', message: 'no' });
|
|
const body = JSON.parse(req.body);
|
|
if (!hub.storageRequestSchema.safeParse(body).success) return res.status(400).json({ code: 'INVALID_REQUEST', message: 'no' });
|
|
asked.push({ installId: String(req.get('X-Hub-Install')), body });
|
|
return res.status(201).json({ requestedAt: new Date().toISOString(), steps: body.steps, alreadyRequested: asked.length > 1 });
|
|
});
|
|
const server = http.createServer(app);
|
|
await new Promise<void>((r) => server.listen(0, '127.0.0.1', () => r()));
|
|
closers.push(() => new Promise((r) => server.close(r)));
|
|
return { url: `http://127.0.0.1:${(server.address() as AddressInfo).port}`, asked };
|
|
}
|
|
|
|
async function site(role: 'owner' | 'editor') {
|
|
const fake = await fakeHub();
|
|
const h = await harness({ env: { HUB_URL: fake.url } });
|
|
await h.ctx.settings.set('hub_install_secret', SECRET);
|
|
const id = '00000000-0000-4000-8000-000000000001';
|
|
await h.db.websiteUser.create({ data: { id, name: 'Pastor Sam', email: 'sam@hope.test', role } });
|
|
await h.db.session.create({ data: { tokenHash: hashToken('t'), userId: id, expiresAt: new Date(Date.now() + 864e5) } });
|
|
const go = (m: 'get' | 'post', p: string, b?: object) => {
|
|
const r = request(h.app)[m](`/api/v1/admin${p}`).set('Cookie', 'ccw_session=t').set('Origin', SITE);
|
|
return b ? r.send(b) : r;
|
|
};
|
|
return { ...h, fake, go };
|
|
}
|
|
|
|
describe('Get more storage (decision 12)', () => {
|
|
it('asks the Hub, signed, with who asked and what is used; shows the request open until the limit goes up', async () => {
|
|
const { go, fake, db } = await site('owner');
|
|
await db.mediaAsset.create({ data: { filename: 'a.jpg', mime: 'image/jpeg', bytes: 4_500_000_000 } });
|
|
const r = await go('post', '/storage-request', { steps: 2 });
|
|
assert.equal(r.status, 200);
|
|
assert.equal(fake.asked.length, 1);
|
|
assert.deepEqual(fake.asked[0]!.body, { protocolVersion: 2, steps: 2, usedBytes: 4_500_000_000, limitBytes: 5 * 1024 ** 3, requestedBy: 'Pastor Sam' });
|
|
const media = (await go('get', '/media')).body;
|
|
assert.equal(media.request.steps, 2);
|
|
assert.equal((await go('post', '/storage-request', { steps: 11 })).status, 400);
|
|
});
|
|
|
|
it('is an Owner\'s or Admin\'s to ask (it costs the church)', async () => {
|
|
const { go } = await site('editor');
|
|
assert.equal((await go('post', '/storage-request', { steps: 1 })).status, 403);
|
|
});
|
|
|
|
it('reports the licence\'s limit in the heartbeat, so the Hub can email at 80% and 100%', async () => {
|
|
const { ctx } = await site('owner');
|
|
const s = await stats(ctx);
|
|
assert.equal(s.storage.includedBytes, 5 * 1024 ** 3);
|
|
assert.ok(hub.websiteStatsSchema.safeParse(s).success);
|
|
});
|
|
});
|