// Installs made from the Hub register by themselves with HUB_LICENCE_KEY (AGENT-1.0-PLAN ยง7). const mockSettings = new Map(); jest.mock('../src/config/db', () => ({ appSetting: { findMany: jest.fn(async ({ where }) => [...mockSettings].filter(([key]) => where.key.in.includes(key)).map(([key, value]) => ({ key, value }))), findUnique: jest.fn(async ({ where }) => (mockSettings.has(where.key) ? { key: where.key, value: mockSettings.get(where.key) } : null)), upsert: jest.fn(async ({ where, create }) => { mockSettings.set(where.key, create.value); }), deleteMany: jest.fn(async ({ where }) => { mockSettings.delete(where.key); }), }, })); jest.mock('../src/hub/report', () => ({ APP_VERSION: '2.3.0', installInfo: async () => ({ name: 'Hope', url: 'https://events.hope.co.za', version: '2.3.0' }) })); jest.mock('../src/hub/publicKey', () => ({ hubPublicKey: () => 'pem' })); jest.mock('../src/hub/licence', () => ({ verifyToken: () => ({ installId: 'inst_1' }), clearLicenceCache: () => {} })); const hub = require('../src/hub/client'); const reply = (status, body) => ({ ok: status < 400, status, json: async () => body }); const noSleep = async () => {}; describe('registering with HUB_LICENCE_KEY', () => { let bodies; beforeEach(() => { mockSettings.clear(); bodies = []; process.env.HUB_LICENCE_KEY = 'CEH-7K2M-9QXA-P4RT-WD8C'; process.env.HUB_ADMIN_NAME = 'Jane Admin'; process.env.HUB_ADMIN_EMAIL = 'jane@hope.co.za'; global.fetch = jest.fn(async (_url, opts) => { bodies.push(JSON.parse(opts.body)); return reply(201, { installId: 'inst_1', installSecret: 's3cret', licence: 'hel1.x.y', heartbeatIntervalMinutes: 15 }); }); }); afterEach(() => { delete global.fetch; delete process.env.HUB_LICENCE_KEY; delete process.env.HUB_ADMIN_NAME; delete process.env.HUB_ADMIN_EMAIL; }); test('registers once, with the admin contact, and stores the install', async () => { expect(await hub.connectFromEnv({ sleep: noSleep })).toEqual({ connected: true }); expect(bodies[0]).toMatchObject({ licenceKey: 'CEH-7K2M-9QXA-P4RT-WD8C', install: { adminContact: { name: 'Jane Admin', email: 'jane@hope.co.za' } } }); expect(mockSettings.get('hub_install_id')).toBe('inst_1'); expect(await hub.connectFromEnv({ sleep: noSleep })).toEqual({ connected: false, reason: 'already' }); expect(global.fetch).toHaveBeenCalledTimes(1); }); test('does nothing without the key', async () => { delete process.env.HUB_LICENCE_KEY; expect(await hub.connectFromEnv({ sleep: noSleep })).toEqual({ connected: false, reason: 'no_key' }); expect(global.fetch).not.toHaveBeenCalled(); }); test('a refused key isn\'t tried again; an unreachable Hub is', async () => { global.fetch = jest.fn(async () => reply(404, { code: 'LICENCE_NOT_FOUND', message: 'Unknown key' })); expect(await hub.connectFromEnv({ sleep: noSleep })).toEqual({ connected: false, reason: 'LICENCE_NOT_FOUND' }); expect(global.fetch).toHaveBeenCalledTimes(1); global.fetch = jest.fn(async () => { throw new Error('ECONNREFUSED'); }); expect(await hub.connectFromEnv({ attempts: 3, sleep: noSleep })).toEqual({ connected: false, reason: 'unreachable' }); expect(global.fetch).toHaveBeenCalledTimes(3); }); });