Files
sunrise/tests/server-config.test.ts
T

34 lines
1.6 KiB
TypeScript

import assert from 'node:assert/strict';
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import test from 'node:test';
import { loadServerConfig } from '../server/config.ts';
test('server and Mongo verifier can load the documented JSON configuration', () => {
const example = new URL('../.config.example.json', import.meta.url);
const config = loadServerConfig(example);
assert.equal(config.port, 3000);
assert.equal(config.visitStore, 'mongo');
assert.equal(config.mongoDatabase, 'sunrise');
assert.ok(config.allowedOrigins.includes('http://127.0.0.1:3000'));
});
test('missing or invalid configuration fails without exposing its contents', () => {
const directory = mkdtempSync(join(tmpdir(), 'sunrise-config-'));
const path = join(directory, '.config.json');
try {
assert.throws(() => loadServerConfig(path), /Cannot read/);
writeFileSync(path, '{"mongoUri":"secret",');
assert.throws(() => loadServerConfig(path), /^Error: Invalid JSON in .config.json$/);
const example = JSON.parse(readFileSync(new URL('../.config.example.json', import.meta.url), 'utf8'));
for (const override of [{ port: '3000' }, { port: 65536 }, { host: '' },
{ allowedOrigins: 'https://example.com' }, { visitStore: 'invalid' }, { mongoUri: null }]) {
writeFileSync(path, JSON.stringify({ ...example, ...override }));
assert.throws(() => loadServerConfig(path), /Invalid .config.json field/);
}
} finally {
rmSync(directory, { recursive: true, force: true });
}
});