crosscode adopt can only work out the church's address from an events.<address> domain, and the Hub never sent --address, so adopting a site like events-demo.crosscode.co.za failed on the server. The adopt form now asks for the address (required for such domains), the Hub refuses to plan the move without it, and it goes into the job's arguments. Link existing: the short name field and its error say it's a name like `hope`, not a folder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
347 lines
24 KiB
JavaScript
347 lines
24 KiB
JavaScript
// Servers and the agent (2.1.0, crosscode-platform docs/AGENT-1.0-PLAN.md): joining, signed agent
|
|
// requests, jobs (signed, config sealed to the server, one at a time, missed when late), reports
|
|
// and what follows (installs, updates, batches, uninstall + export), the port list and alerts.
|
|
jest.mock('../src/config/db', () => require('./fakeDb').createFakeDb());
|
|
const mockMails = [];
|
|
jest.mock('../src/utils/mail', () => ({ sendMail: jest.fn(async m => { mockMails.push(m); }), smtpConfigured: async () => true }));
|
|
|
|
process.env.HUB_ENCRYPTION_KEY = 'test-encryption-key-test-encryption-key';
|
|
process.env.FRONTEND_URL = 'https://hub.test';
|
|
|
|
const { agent } = require('@crosscode/contracts');
|
|
const signing = require('@crosscode/contracts/signing');
|
|
const db = require('../src/config/db');
|
|
const servers = require('../src/utils/servers');
|
|
const jobs = require('../src/utils/jobs');
|
|
const { encrypt } = require('../src/utils/crypto');
|
|
const { generateSecret, codeAt, STEP } = require('../src/utils/totp');
|
|
|
|
const hubKey = signing.generateEd25519();
|
|
process.env.HUB_JOB_SIGNING_KEY = hubKey.privateKeyPem;
|
|
global.fetch = jest.fn(async () => { throw new Error('no network in tests'); });
|
|
|
|
const NOW = new Date('2026-11-20T08:00:00.000Z');
|
|
const H = 60 * 60 * 1000;
|
|
const keys = { ed: signing.generateEd25519(), x: signing.generateX25519() };
|
|
const totpSecret = generateSecret();
|
|
const admin = { id: 'admin-1', name: 'Joshua', totpEnabled: true, totpSecret: encrypt(totpSecret) };
|
|
const totpNow = () => codeAt(totpSecret, Math.floor(Date.now() / 1000 / STEP));
|
|
|
|
async function joinServer(mode = 'church', name = 'church-1') {
|
|
const t = await servers.createJoinToken({ name, mode }, NOW);
|
|
const reply = await servers.join({ agentApiVersion: 1, token: t.token, mode, hostname: name, agentVersion: '1.0.0', publicKeys: { ed25519: keys.ed.publicKey, x25519: keys.x.publicKey } }, NOW);
|
|
const server = await db.server.findUnique({ where: { id: reply.serverId } });
|
|
return { reply, server, token: t.token };
|
|
}
|
|
|
|
async function seed() {
|
|
for (const k of Object.keys(db.tables)) db.tables[k].length = 0;
|
|
mockMails.length = 0;
|
|
servers.resetJobKeyForTests();
|
|
await db.setting.create({ data: { key: 'alert_email', value: 'joshua@crosscode.test' } });
|
|
await db.customer.create({ data: { id: 'cust-1', name: 'Hope Church', billingCycle: 'none' } });
|
|
await db.release.create({ data: { id: 'r1', product: 'events', version: '2.3.0', notes: '- New things', releasedAt: NOW } });
|
|
await db.release.create({ data: { id: 'r2', product: 'events', version: '2.4.0', notes: '- Newer things', releasedAt: NOW } });
|
|
}
|
|
|
|
const signedReport = (job, status, extra = {}) => ({
|
|
schema: 'crosscode.agent-report/1', jobId: job.id, serverId: job.serverId, type: job.type, status,
|
|
startedAt: NOW.toISOString(), finishedAt: NOW.toISOString(), steps: [{ name: 'step', startedAt: NOW.toISOString(), finishedAt: NOW.toISOString(), outcome: status === 'succeeded' ? 'ok' : 'failed' }],
|
|
result: {}, log: ['==> done'], ...extra,
|
|
});
|
|
|
|
beforeEach(seed);
|
|
|
|
describe('joining', () => {
|
|
test('a one-time token joins a server once and returns the pinned job key', async () => {
|
|
const { reply, token } = await joinServer();
|
|
expect(agent.joinResponseSchema.safeParse(reply).success).toBe(true);
|
|
expect(reply.hubJobKey).toBe(hubKey.publicKey);
|
|
await expect(servers.join({ agentApiVersion: 1, token, mode: 'church', hostname: 'x', agentVersion: '1.0.0', publicKeys: { ed25519: keys.ed.publicKey, x25519: keys.x.publicKey } }, NOW)).rejects.toMatchObject({ code: 'BAD_TOKEN' });
|
|
});
|
|
|
|
test('an expired token or the wrong mode is refused', async () => {
|
|
const t = await servers.createJoinToken({ name: 'late', mode: 'church' }, NOW);
|
|
const body = { agentApiVersion: 1, token: t.token, mode: 'church', hostname: 'x', agentVersion: '1.0.0', publicKeys: { ed25519: keys.ed.publicKey, x25519: keys.x.publicKey } };
|
|
await expect(servers.join(body, new Date(NOW.getTime() + 2 * H))).rejects.toMatchObject({ code: 'BAD_TOKEN' });
|
|
await expect(servers.join({ ...body, mode: 'central' }, NOW)).rejects.toMatchObject({ code: 'WRONG_MODE' });
|
|
});
|
|
|
|
test('the install command carries the token and --central', () => {
|
|
expect(servers.joinCommand('tok', 'central', 'https://hub.test')).toMatch(/install\.sh \| sudo bash -s -- --hub https:\/\/hub\.test --token tok --central$/);
|
|
});
|
|
});
|
|
|
|
describe('signed agent requests', () => {
|
|
const call = async (server, headers, raw) => {
|
|
const req = { headers, rawBody: raw };
|
|
let status = 200;
|
|
let body = null;
|
|
const res = { status(s) { status = s; return this; }, json(b) { body = b; return this; } };
|
|
let passed = false;
|
|
await servers.requireAgent(req, res, () => { passed = true; });
|
|
return { passed, status, body, req };
|
|
};
|
|
|
|
test('the server\'s key over the exact body passes; anything else is refused', async () => {
|
|
const { server } = await joinServer();
|
|
const raw = JSON.stringify({ agentApiVersion: 1, agentVersion: '1.0.0', mode: 'church', runningJobId: null });
|
|
const h = signing.signAgentRequest({ serverId: server.id, privateKeyPem: keys.ed.privateKeyPem, rawBody: raw });
|
|
expect((await call(server, h, raw)).passed).toBe(true);
|
|
expect(await call(server, h, `${raw} `)).toMatchObject({ passed: false, status: 401 });
|
|
const other = signing.signAgentRequest({ serverId: server.id, privateKeyPem: signing.generateEd25519().privateKeyPem, rawBody: raw });
|
|
expect(await call(server, other, raw)).toMatchObject({ passed: false, status: 401 });
|
|
expect(await call(server, { ...h, [agent.AGENT_HEADERS.server]: 'srv_unknown000' }, raw)).toMatchObject({ passed: false, status: 404 });
|
|
});
|
|
});
|
|
|
|
describe('jobs', () => {
|
|
test('a new install: licence, two ports, an install job the agent can verify and open', async () => {
|
|
const { server } = await joinServer();
|
|
await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback', DB_ADMIN_URL: 'postgresql://admin:pw@10.0.0.5/postgres' });
|
|
const { deployment } = await jobs.newInstall({ customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'events.hope.co.za', adminName: 'Jane', adminEmail: 'jane@hope.test' }, admin, NOW);
|
|
expect(deployment).toMatchObject({ shortName: 'hope', webPort: 20000, apiPort: 20001, status: 'installing' });
|
|
|
|
const polled = await jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.0.0', mode: 'church', runningJobId: null }, NOW);
|
|
expect(polled.jobs).toHaveLength(1);
|
|
const job = polled.jobs[0];
|
|
expect(signing.verifyJob(job, hubKey.publicKey)).toBe(true);
|
|
expect(agent.agentJobSchema.safeParse(job).success).toBe(true);
|
|
expect(job.args).toMatchObject({ church: 'hope', versions: { events: 'events@2.4.0' }, ports: { events: { web: 20000, api: 20001 } } });
|
|
const config = signing.openConfig(job.config, keys.x.privateKeyPem);
|
|
expect(config.server).toMatchObject({ TRUST_PROXY: 'loopback', DB_ADMIN_URL: 'postgresql://admin:pw@10.0.0.5/postgres' });
|
|
expect(config.install.events).toMatchObject({ HUB_ADMIN_EMAIL: 'jane@hope.test' });
|
|
expect(config.install.events.HUB_LICENCE_KEY).toMatch(/^CEH-/);
|
|
expect(JSON.stringify(job)).not.toContain('pw@');
|
|
|
|
// One at a time: nothing else while it runs.
|
|
expect((await jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.0.0', mode: 'church', runningJobId: null }, NOW)).jobs).toEqual([]);
|
|
});
|
|
|
|
test('a new install is refused while required server settings are missing, or the agent is offline', async () => {
|
|
const { server } = await joinServer();
|
|
await expect(jobs.newInstall({ customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'events.hope.co.za' }, admin, NOW)).rejects.toMatchObject({ code: 'CONFIG_MISSING', message: expect.stringContaining('TRUST_PROXY') });
|
|
await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' });
|
|
await expect(jobs.newInstall({ customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'events.hope.co.za' }, admin, new Date(NOW.getTime() + H))).rejects.toMatchObject({ code: 'AGENT_OFFLINE' });
|
|
});
|
|
|
|
test('a successful install waits for the proxy, asks the central server to check the ports, and sends the FastPanel email', async () => {
|
|
const { server } = await joinServer();
|
|
const central = (await joinServer('central', 'central')).server;
|
|
await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' });
|
|
const { deployment } = await jobs.newInstall({ customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'events.hope.co.za' }, admin, NOW);
|
|
const [job] = (await jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.0.0', mode: 'church', runningJobId: null }, NOW)).jobs;
|
|
await jobs.receiveReport(server, signedReport(job, 'succeeded', { result: { versions: { events: 'events@2.4.0' }, key: 'nope' } }), NOW);
|
|
expect(await db.deployment.findUnique({ where: { id: deployment.id } })).toMatchObject({ status: 'waiting_for_proxy', version: '2.4.0' });
|
|
expect(await db.job.findFirst({ where: { serverId: central.id, type: 'check-ports' } })).toMatchObject({ args: { ports: [20000, 20001] } });
|
|
const mail = mockMails.find(m => m.subject.includes('Installed: hope'));
|
|
expect(mail.text).toContain('/api → http://127.0.0.1:20001');
|
|
expect(mail.text).toContain('everything else → http://127.0.0.1:20000');
|
|
});
|
|
|
|
test('a failed install frees its ports again', async () => {
|
|
const { server } = await joinServer();
|
|
await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' });
|
|
const { deployment } = await jobs.newInstall({ customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'events.hope.co.za' }, admin, NOW);
|
|
const [job] = (await jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.0.0', mode: 'church', runningJobId: null }, NOW)).jobs;
|
|
await jobs.receiveReport(server, signedReport(job, 'failed'), NOW);
|
|
expect(await db.deployment.findUnique({ where: { id: deployment.id } })).toMatchObject({ status: 'failed' });
|
|
expect(await db.port.count({ where: { port: { in: [20000, 20001] } } })).toBe(0);
|
|
});
|
|
|
|
test('a job not picked up in its window is missed: nothing runs late', async () => {
|
|
const { server } = await joinServer();
|
|
const job = await jobs.makeJob(db, { serverId: server.id, type: 'status', args: {}, notBefore: NOW });
|
|
await jobs.tick(new Date(NOW.getTime() + 16 * 60 * 1000));
|
|
expect(await db.job.findUnique({ where: { id: job.id } })).toMatchObject({ status: 'missed' });
|
|
expect((await jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.0.0', mode: 'church', runningJobId: null }, new Date(NOW.getTime() + 16 * 60 * 1000))).jobs).toEqual([]);
|
|
expect(mockMails.some(m => m.subject.includes('Missed job'))).toBe(true);
|
|
});
|
|
|
|
test('a job isn\'t handed out before its time', async () => {
|
|
const { server } = await joinServer();
|
|
await jobs.makeJob(db, { serverId: server.id, type: 'status', args: {}, notBefore: new Date(NOW.getTime() + H) });
|
|
expect((await jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.0.0', mode: 'church', runningJobId: null }, NOW)).jobs).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('scheduled updates (§8)', () => {
|
|
async function liveDeployments(n) {
|
|
const { server } = await joinServer();
|
|
await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' });
|
|
const out = [];
|
|
for (let i = 0; i < n; i++) {
|
|
const install = await db.install.create({ data: { id: `inst-${i}`, customerId: 'cust-1', product: 'events', name: `Church ${i}`, url: `https://events.c${i}.co.za`, secret: 'x', version: '2.3.0', adminContact: { name: `Admin ${i}`, email: `admin${i}@c.test` } } });
|
|
out.push(await db.deployment.create({ data: { id: `dep-${i}`, serverId: server.id, customerId: 'cust-1', installId: install.id, product: 'events', shortName: `c${i}`, domain: `events.c${i}.co.za`, webPort: 20000 + 2 * i, apiPort: 20001 + 2 * i, status: 'live' } }));
|
|
}
|
|
return { server, deployments: out };
|
|
}
|
|
|
|
test('at least 4 hours ahead; emergency needs 15 minutes, a reason and 2FA', async () => {
|
|
const { deployments } = await liveDeployments(1);
|
|
const ids = [deployments[0].id];
|
|
await expect(jobs.scheduleUpdate({ deploymentIds: ids, version: '2.4.0', at: new Date(NOW.getTime() + 3 * H) }, admin, NOW)).rejects.toMatchObject({ code: 'TOO_SOON' });
|
|
await expect(jobs.scheduleUpdate({ deploymentIds: ids, version: '2.4.0', at: new Date(NOW.getTime() + 20 * 60 * 1000), emergency: true, totp: totpNow() }, admin, NOW)).rejects.toMatchObject({ status: 400, message: expect.stringContaining('reason') });
|
|
await expect(jobs.scheduleUpdate({ deploymentIds: ids, version: '2.4.0', at: new Date(NOW.getTime() + 20 * 60 * 1000), emergency: true, reason: 'Security fix', totp: '000000' }, admin, NOW)).rejects.toMatchObject({ code: 'BAD_TOTP' });
|
|
const ok = await jobs.scheduleUpdate({ deploymentIds: ids, version: '2.4.0', at: new Date(NOW.getTime() + 20 * 60 * 1000), emergency: true, reason: 'Security fix', totp: totpNow() }, admin, NOW);
|
|
expect(ok.jobs[0]).toMatchObject({ emergency: true, reason: 'Security fix', args: { versions: ['events@2.4.0'] } });
|
|
});
|
|
|
|
test('the church gets an email with what\'s new and a banner straight away', async () => {
|
|
const { deployments } = await liveDeployments(1);
|
|
const at = new Date(NOW.getTime() + 5 * H);
|
|
const { jobs: [job] } = await jobs.scheduleUpdate({ deploymentIds: [deployments[0].id], version: '2.4.0', at }, admin, NOW);
|
|
expect(mockMails.find(m => m.to === 'admin0@c.test')?.text).toContain('Newer things');
|
|
const notice = await db.notice.findUnique({ where: { id: job.noticeId } });
|
|
expect(notice).toMatchObject({ allInstalls: false, installIds: ['inst-0'], showBanner: true });
|
|
});
|
|
|
|
test('a batch runs one at a time and stops at the first failure; the rest are told it\'s postponed', async () => {
|
|
const { server, deployments } = await liveDeployments(3);
|
|
const at = new Date(NOW.getTime() + 5 * H);
|
|
const { jobs: made, batchId } = await jobs.scheduleUpdate({ deploymentIds: deployments.map(d => d.id), version: '2.4.0', at }, admin, NOW);
|
|
expect(batchId).toMatch(/^batch_/);
|
|
expect(made.map(j => j.status)).toEqual(['queued', 'scheduled', 'scheduled']);
|
|
|
|
const poll = when => jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.0.0', mode: 'church', runningJobId: null }, when);
|
|
const first = (await poll(at)).jobs[0];
|
|
await jobs.receiveReport(server, signedReport(first, 'succeeded'), at);
|
|
expect((await db.job.findUnique({ where: { id: made[1].id } })).status).toBe('queued');
|
|
|
|
const second = (await poll(at)).jobs[0];
|
|
expect(second.id).toBe(made[1].id);
|
|
mockMails.length = 0;
|
|
await jobs.receiveReport(server, signedReport(second, 'rolled-back'), at);
|
|
expect((await db.job.findUnique({ where: { id: made[1].id } })).status).toBe('rolled_back');
|
|
expect((await db.job.findUnique({ where: { id: made[2].id } })).status).toBe('cancelled');
|
|
expect(mockMails.find(m => m.to === 'admin1@c.test')?.subject).toContain('didn\'t go through');
|
|
expect(mockMails.find(m => m.to === 'admin2@c.test')?.subject).toContain('postponed');
|
|
expect(mockMails.some(m => m.subject.includes('Batch stopped'))).toBe(true);
|
|
});
|
|
|
|
test('uninstall: 2FA and the short name; the export goes first and its key is never stored', async () => {
|
|
const { server, deployments } = await liveDeployments(1);
|
|
const d = deployments[0];
|
|
await expect(jobs.scheduleUninstall({ deploymentId: d.id, totp: totpNow(), confirm: 'wrong' }, admin, NOW)).rejects.toMatchObject({ code: 'CONFIRM' });
|
|
const at = new Date(NOW.getTime() + 5 * H);
|
|
const { exportJob, uninstallJob } = await jobs.scheduleUninstall({ deploymentId: d.id, totp: totpNow(), confirm: 'c0', at }, admin, NOW);
|
|
expect(uninstallJob.status).toBe('scheduled');
|
|
|
|
const job = (await jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.0.0', mode: 'church', runningJobId: null }, at)).jobs[0];
|
|
expect(job.id).toBe(exportJob.id);
|
|
await jobs.receiveExport(server, job.args.exportId, Buffer.from('encrypted bytes'));
|
|
await jobs.receiveReport(server, signedReport(job, 'succeeded', { result: { key: 'SECRET-KEY-abc', encryptedBytes: 15 } }), at);
|
|
const stored = await db.job.findUnique({ where: { id: job.id } });
|
|
expect(JSON.stringify(stored)).not.toContain('SECRET-KEY-abc');
|
|
expect(mockMails.find(m => m.to === 'admin0@c.test' && m.subject.includes('data'))?.text).toContain(`/download/${job.args.exportId}#SECRET-KEY-abc`);
|
|
expect((await db.job.findUnique({ where: { id: uninstallJob.id } })).status).toBe('queued');
|
|
|
|
const un = (await jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.0.0', mode: 'church', runningJobId: null }, at)).jobs[0];
|
|
expect(un.args).toEqual({ church: 'c0' });
|
|
await jobs.receiveReport(server, signedReport(un, 'succeeded'), at);
|
|
expect(await db.deployment.findUnique({ where: { id: d.id } })).toMatchObject({ status: 'removed' });
|
|
expect((await db.install.findUnique({ where: { id: 'inst-0' } })).releasedAt).toEqual(at);
|
|
expect(await db.job.findFirst({ where: { type: 'purge', deploymentId: d.id } })).toBeTruthy();
|
|
});
|
|
});
|
|
|
|
describe('ports and alerts', () => {
|
|
test('ports are unique, and a released one is reused only after 30 days', async () => {
|
|
const { server } = await joinServer();
|
|
await db.port.create({ data: { port: 20000, role: 'taken' } });
|
|
await db.port.create({ data: { port: 20001, role: 'web', releasedAt: new Date(NOW.getTime() - 10 * 24 * H) } });
|
|
await db.port.create({ data: { port: 20002, role: 'api', releasedAt: new Date(NOW.getTime() - 40 * 24 * H) } });
|
|
expect(await servers.allocatePorts(db, server.id, NOW)).toEqual({ web: 20002, api: 20003 });
|
|
});
|
|
|
|
test('server problems: silent agent, full disk, frpc down, the central bind address', () => {
|
|
const base = { mode: 'church', lastSeenAt: NOW, lastStats: { disks: [{ mount: '/', totalBytes: 100, usedBytes: 90 }], frpcRunning: false } };
|
|
expect(servers.serverProblems(base, [], [], NOW).map(p => p.type)).toEqual(['disk:/', 'frpc_down']);
|
|
expect(servers.serverProblems({ ...base, lastSeenAt: new Date(NOW.getTime() - 6 * 60 * 1000) }, [], [], NOW)[0].type).toBe('agent_silent');
|
|
const central = { mode: 'central', lastSeenAt: NOW, lastStats: { disks: [], central: { proxyBindAddr: null, frpsRunning: true } } };
|
|
expect(servers.serverProblems(central, [], [], NOW).map(p => p.type)).toEqual(['proxy_bind_addr']);
|
|
const ram = Array.from({ length: 5 }, () => ({ ramUsedPercent: 95 }));
|
|
expect(servers.serverProblems({ ...base, lastStats: { disks: [] } }, ram, [], NOW).map(p => p.type)).toEqual(['ram_high']);
|
|
});
|
|
});
|
|
|
|
describe('reusing a short name (found in the VM test)', () => {
|
|
test('after a failed install the same short name can be used again', async () => {
|
|
const { server } = await joinServer();
|
|
await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' });
|
|
const first = await jobs.newInstall({ customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'events.hope.co.za' }, admin, NOW);
|
|
const [job] = (await jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.0.0', mode: 'church', runningJobId: null }, NOW)).jobs;
|
|
await jobs.receiveReport(server, signedReport(job, 'failed'), NOW);
|
|
const again = await jobs.newInstall({ customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'events.hope.co.za' }, admin, NOW);
|
|
expect(again.deployment.id).not.toBe(first.deployment.id);
|
|
expect(await db.deployment.findUnique({ where: { id: first.deployment.id } })).toBeNull();
|
|
expect(again.deployment).toMatchObject({ webPort: 20000, apiPort: 20001 });
|
|
});
|
|
|
|
test('a live install keeps its name', async () => {
|
|
const { server } = await joinServer();
|
|
await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' });
|
|
await jobs.newInstall({ customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'events.hope.co.za' }, admin, NOW);
|
|
await expect(jobs.newInstall({ customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'events.other.co.za' }, admin, NOW)).rejects.toMatchObject({ code: 'SHORT_NAME_TAKEN' });
|
|
});
|
|
});
|
|
|
|
describe('settings for servers (found in the VM test)', () => {
|
|
test('install defaults, the port range and the central IP are saved', async () => {
|
|
const { setSettings } = require('../src/utils/settings');
|
|
await setSettings({ install_defaults: '{"HUB_URL":"https://hub.test","SENTRY_DSN":"x"}', port_range_start: '21000', port_range_end: '21100', central_server_ip: '203.0.113.10' });
|
|
expect(await servers.installDefaults()).toEqual({ HUB_URL: 'https://hub.test', SENTRY_DSN: 'x' });
|
|
expect(await servers.portRange()).toEqual({ from: 21000, to: 21100 });
|
|
});
|
|
});
|
|
|
|
describe('banners reach a site the Hub installed (found in the VM test)', () => {
|
|
test('an update to a deployment registered through its licence gets a banner', async () => {
|
|
const { server } = await joinServer();
|
|
await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' });
|
|
await db.release.create({ data: { product: 'events', version: '2.4.0', notes: 'New', releasedAt: NOW } });
|
|
// A Hub install: the deployment knows its licence, not yet its install.
|
|
await db.licence.create({ data: { id: 'lic-9', customerId: 'cust-1', product: 'events', key: 'CEH-TEST' } });
|
|
await db.install.create({ data: { id: 'inst-9', customerId: 'cust-1', licenceId: 'lic-9', product: 'events', name: 'Hope', url: 'https://events.hope.test', secret: 'x', version: '2.3.0' } });
|
|
const d = await db.deployment.create({ data: { id: 'dep-9', serverId: server.id, customerId: 'cust-1', licenceId: 'lic-9', product: 'events', shortName: 'hope', domain: 'events.hope.test', webPort: 20000, apiPort: 20001, status: 'live', version: '2.3.0' } });
|
|
const r = await jobs.scheduleUpdate({ deploymentIds: [d.id], version: '2.4.0', at: new Date(NOW.getTime() + 5 * H) }, admin, NOW);
|
|
const job = await db.job.findUnique({ where: { id: r.jobs[0].id } });
|
|
expect(job.noticeId).toBeTruthy();
|
|
const notice = await db.notice.findUnique({ where: { id: job.noticeId } });
|
|
expect(notice).toMatchObject({ showBanner: true, installIds: ['inst-9'] });
|
|
expect((await db.deployment.findUnique({ where: { id: d.id } })).installId).toBe('inst-9');
|
|
});
|
|
});
|
|
|
|
describe('adopting an existing install', () => {
|
|
async function linked(domain) {
|
|
const { server } = await joinServer();
|
|
await db.release.create({ data: { product: 'events', version: '2.3.0', releasedAt: NOW } });
|
|
await db.install.create({ data: { id: `inst-${domain}`, customerId: 'cust-1', product: 'events', name: 'Demo', url: `https://${domain}`, secret: 'x', version: '2.1.1' } });
|
|
return jobs.linkDeployment({ serverId: server.id, installId: `inst-${domain}`, shortName: 'cross-events-demo', webPort: 3100, apiPort: 5100 }, admin);
|
|
}
|
|
const at = new Date(NOW.getTime() + 5 * H);
|
|
|
|
test('a link takes a name, not a folder', async () => {
|
|
const { server } = await joinServer();
|
|
await db.install.create({ data: { id: 'inst-p', customerId: 'cust-1', product: 'events', name: 'P', url: 'https://events.p.test', secret: 'x', version: '2.1.1' } });
|
|
await expect(jobs.linkDeployment({ serverId: server.id, installId: 'inst-p', shortName: '/var/www/cross-events-demo', webPort: 3100, apiPort: 5100 }, admin))
|
|
.rejects.toMatchObject({ status: 400, message: expect.stringContaining('not a folder') });
|
|
});
|
|
|
|
test('the old folder and the church address go to crosscode adopt', async () => {
|
|
const d = await linked('events-demo.crosscode.co.za');
|
|
const job = await jobs.scheduleAdopt({ deploymentId: d.id, eventsDir: '/var/www/cross-events-demo', address: 'crosscode.co.za', at }, admin, NOW);
|
|
expect(job.args).toMatchObject({ church: 'cross-events-demo', eventsDir: '/var/www/cross-events-demo', address: 'crosscode.co.za' });
|
|
expect(agent.jobArgsSchemas.adopt.safeParse(job.args).success).toBe(true);
|
|
});
|
|
|
|
test('a domain without "events." needs the address; events.<address> works it out', async () => {
|
|
const d = await linked('events-demo.crosscode.co.za');
|
|
await expect(jobs.scheduleAdopt({ deploymentId: d.id, eventsDir: '/var/www/cross-events-demo', at }, admin, NOW)).rejects.toMatchObject({ status: 400, code: 'ADDRESS_NEEDED' });
|
|
await db.deployment.update({ where: { id: d.id }, data: { domain: 'events.hope.co.za' } });
|
|
const job = await jobs.scheduleAdopt({ deploymentId: d.id, eventsDir: '/var/www/hope-events', at }, admin, NOW);
|
|
expect(job.args.address).toBe('hope.co.za');
|
|
});
|
|
});
|