test: use private tester socket for processing controls
This commit is contained in:
1 parent
81889b58d5
commit
c78f48946a
1 file changed
+14
-5
@@ -1,12 +1,17 @@
|
|||||||
import test from 'node:test';
|
import test from 'node:test';
|
||||||
import assert from 'node:assert/strict';
|
import assert from 'node:assert/strict';
|
||||||
import { request } from 'node:http';
|
import { request } from 'node:http';
|
||||||
|
import { mkdtempSync, rmSync } from 'node:fs';
|
||||||
|
import { join } from 'node:path';
|
||||||
|
import { tmpdir } from 'node:os';
|
||||||
|
import { startTesterSocket, TESTER_ORIGIN } from '../src/tester-socket.mjs';
|
||||||
import { startServer } from '../src/serve.mjs';
|
import { startServer } from '../src/serve.mjs';
|
||||||
import { close } from './fixture.mjs';
|
import { close } from './fixture.mjs';
|
||||||
const GEN = '12345678-1234-4234-8234-123456789abc';
|
const GEN = '12345678-1234-4234-8234-123456789abc';
|
||||||
function http(server, path, { method = 'GET', headers = {}, body = '' } = {}) {
|
function http(server, path, { method = 'GET', headers = {}, body = '' } = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const req = request({ host: '127.0.0.1', port: server.address().port, path, method,
|
const address = server.address();
|
||||||
|
const req = request({ ...(typeof address === 'string' ? { socketPath: address } : { host: '127.0.0.1', port: address.port }), path, method,
|
||||||
headers: Object.fromEntries(Object.entries({ host: 'console.example', 'x-user': 'jason', ...headers }).filter(([, v]) => v !== undefined)) }, res => {
|
headers: Object.fromEntries(Object.entries({ host: 'console.example', 'x-user': 'jason', ...headers }).filter(([, v]) => v !== undefined)) }, res => {
|
||||||
let text = ''; res.on('data', c => { text += c; }); res.on('end', () => resolve({ status: res.statusCode, text, value: JSON.parse(text) }));
|
let text = ''; res.on('data', c => { text += c; }); res.on('end', () => resolve({ status: res.statusCode, text, value: JSON.parse(text) }));
|
||||||
}); req.on('error', reject); req.end(body);
|
}); req.on('error', reject); req.end(body);
|
||||||
@@ -21,9 +26,13 @@ async function fixture(t, { send = true, tester = false } = {}) {
|
|||||||
halt: async args => { calls.push(['halt', args]); state.halted = true; return { ...state }; },
|
halt: async args => { calls.push(['halt', args]); state.halted = true; return { ...state }; },
|
||||||
resume: async args => { calls.push(['resume', args]); state.halted = false; return { ...state }; },
|
resume: async args => { calls.push(['resume', args]); state.halted = false; return { ...state }; },
|
||||||
};
|
};
|
||||||
const server = await startServer({ port: 0, ...(tester ? { tester: true } : { preview: { host: 'console.example', header: 'x-user', user: 'jason' } }),
|
const root = tester ? mkdtempSync(join(tmpdir(), 'processing-web-')) : null;
|
||||||
send: send && !tester ? async () => {} : null, processing, log: () => {} });
|
const options = { processing, log: () => {} };
|
||||||
t.after(() => close(server)); return { server, calls, state, processing };
|
const server = tester ? await startTesterSocket({ dataRoot: root, ...options }) : await startServer({ port: 0,
|
||||||
|
preview: { host: 'console.example', header: 'x-user', user: 'jason' },
|
||||||
|
send: send ? async () => {} : null, ...options });
|
||||||
|
t.after(async () => { await close(server); if (root) rmSync(root, { recursive: true, force: true }); });
|
||||||
|
return { server, calls, state, processing };
|
||||||
}
|
}
|
||||||
test('read-only Console can read processing state but neither human control writes without --send', async t => {
|
test('read-only Console can read processing state but neither human control writes without --send', async t => {
|
||||||
const f = await fixture(t, { send: false });
|
const f = await fixture(t, { send: false });
|
||||||
@@ -56,7 +65,7 @@ test('processing write fences refuse cross-site, wrong origin, unnamed host, dup
|
|||||||
});
|
});
|
||||||
test('tester cannot invoke either control writer and failures never echo backend text', async t => {
|
test('tester cannot invoke either control writer and failures never echo backend text', async t => {
|
||||||
const f = await fixture(t, { tester: true });
|
const f = await fixture(t, { tester: true });
|
||||||
const headers = { host: `127.0.0.1:${f.server.address().port}` };
|
const headers = { host: new URL(TESTER_ORIGIN).host, origin: TESTER_ORIGIN, 'x-user': undefined };
|
||||||
for (const action of ['halt', 'resume']) assert.equal((await http(f.server, '/api/processing/' + action, { ...POST, headers: { ...POST.headers, ...headers } })).status, 403);
|
for (const action of ['halt', 'resume']) assert.equal((await http(f.server, '/api/processing/' + action, { ...POST, headers: { ...POST.headers, ...headers } })).status, 403);
|
||||||
assert.deepEqual(f.calls, []);
|
assert.deepEqual(f.calls, []);
|
||||||
const human = await fixture(t); human.processing.halt = async () => { throw Object.assign(Error('NEVER ECHO fixture path'), { code: 'outcome-unknown' }); };
|
const human = await fixture(t); human.processing.halt = async () => { throw Object.assign(Error('NEVER ECHO fixture path'), { code: 'outcome-unknown' }); };
|
||||||
|
|||||||
Reference in new issue
Block a user