build-server-archive.mjs now requires LICENSE and THIRD_PARTY_NOTICES.md, so the installer fixture stages both and checks that they land next to service.js. The sidecar half of the legal route test gets the 60 s hook timeout the other full-server suites use.
117 lines
4.6 KiB
TypeScript
117 lines
4.6 KiB
TypeScript
/**
|
|
* GET /api/legal/license and GET /api/legal/third-party-notices (UF-059).
|
|
*
|
|
* Packaged builds read the files next to service.js; a source checkout reads
|
|
* them from the repository root.
|
|
*/
|
|
|
|
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import Fastify, { type FastifyInstance } from 'fastify';
|
|
import { buildLocalServer } from '../../src/local/index.js';
|
|
import { defaultLegalSearchDirs, legalRoutes } from '../../src/local/routes/legal.js';
|
|
import { injectWithAuth } from '../test-utils.js';
|
|
|
|
const REPO_ROOT = path.resolve(import.meta.dirname, '..', '..', '..', '..');
|
|
const ROUTES_DIR = path.resolve(import.meta.dirname, '..', '..', 'src', 'local', 'routes');
|
|
|
|
const temps: string[] = [];
|
|
function tempDir(): string {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-legal-'));
|
|
temps.push(dir);
|
|
return dir;
|
|
}
|
|
|
|
async function serverWith(searchDirs: string[]): Promise<FastifyInstance> {
|
|
const server = Fastify({ logger: false });
|
|
await server.register(legalRoutes, { searchDirs });
|
|
return server;
|
|
}
|
|
|
|
afterEach(() => {
|
|
for (const dir of temps.splice(0)) fs.rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
describe('legal routes', () => {
|
|
it('serves the copies next to service.js in a packaged build', async () => {
|
|
const resources = tempDir();
|
|
fs.writeFileSync(path.join(resources, 'LICENSE'), 'MIT License (packaged)\n');
|
|
fs.writeFileSync(path.join(resources, 'THIRD_PARTY_NOTICES.md'), '# Third-party notices (packaged)\n');
|
|
const server = await serverWith(defaultLegalSearchDirs(resources));
|
|
try {
|
|
const notices = await server.inject({ method: 'GET', url: '/api/legal/third-party-notices' });
|
|
expect(notices.statusCode).toBe(200);
|
|
expect(notices.headers['content-type']).toBe('text/markdown; charset=utf-8');
|
|
expect(notices.payload).toBe('# Third-party notices (packaged)\n');
|
|
|
|
const license = await server.inject({ method: 'GET', url: '/api/legal/license' });
|
|
expect(license.statusCode).toBe(200);
|
|
expect(license.headers['content-type']).toBe('text/plain; charset=utf-8');
|
|
expect(license.payload).toBe('MIT License (packaged)\n');
|
|
} finally {
|
|
await server.close();
|
|
}
|
|
});
|
|
|
|
it('falls back to the repository root when run from source', async () => {
|
|
expect(defaultLegalSearchDirs(ROUTES_DIR)).toEqual([ROUTES_DIR, REPO_ROOT]);
|
|
const server = await serverWith(defaultLegalSearchDirs(ROUTES_DIR));
|
|
try {
|
|
const notices = await server.inject({ method: 'GET', url: '/api/legal/third-party-notices' });
|
|
expect(notices.statusCode).toBe(200);
|
|
expect(notices.payload).toBe(fs.readFileSync(path.join(REPO_ROOT, 'THIRD_PARTY_NOTICES.md'), 'utf8'));
|
|
const license = await server.inject({ method: 'GET', url: '/api/legal/license' });
|
|
expect(license.payload).toBe(fs.readFileSync(path.join(REPO_ROOT, 'LICENSE'), 'utf8'));
|
|
} finally {
|
|
await server.close();
|
|
}
|
|
});
|
|
|
|
it('does not look outside the packaged folder when it is not in a source checkout', () => {
|
|
const resources = tempDir();
|
|
expect(defaultLegalSearchDirs(resources)).toEqual([resources]);
|
|
});
|
|
|
|
it('answers 404 when a file is missing', async () => {
|
|
const server = await serverWith([tempDir()]);
|
|
try {
|
|
const res = await server.inject({ method: 'GET', url: '/api/legal/third-party-notices' });
|
|
expect(res.statusCode).toBe(404);
|
|
expect(JSON.parse(res.payload).error).toContain('THIRD_PARTY_NOTICES.md');
|
|
} finally {
|
|
await server.close();
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('legal routes in the sidecar', () => {
|
|
let server: FastifyInstance;
|
|
let dataDir: string;
|
|
|
|
beforeAll(async () => {
|
|
// Exercise the real auth path: no loopback trust (vitest.setup.ts turns it on).
|
|
vi.stubEnv('WAGGLE_TRUST_LOCALHOST', '0');
|
|
dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-legal-server-'));
|
|
server = await buildLocalServer({ dataDir });
|
|
}, 60_000);
|
|
|
|
afterAll(async () => {
|
|
await server.close();
|
|
fs.rmSync(dataDir, { recursive: true, force: true });
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
it('serves the notices to an authenticated client', async () => {
|
|
const res = await injectWithAuth(server, { method: 'GET', url: '/api/legal/third-party-notices' });
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.payload).toContain('# Third-party notices');
|
|
});
|
|
|
|
it('requires the session token like every other API route', async () => {
|
|
const res = await server.inject({ method: 'GET', url: '/api/legal/third-party-notices' });
|
|
expect(res.statusCode).toBe(401);
|
|
});
|
|
});
|