162 lines
4.5 KiB
JavaScript
162 lines
4.5 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {
|
|
attachPortalBlockedWordsRoute,
|
|
attachPortalImageMakeRuntimeConfigRoute,
|
|
} from './portal-config-routes.mjs';
|
|
|
|
function createRouterRecorder() {
|
|
const routes = new Map();
|
|
return {
|
|
routes,
|
|
get(path, handler) {
|
|
routes.set(path, handler);
|
|
},
|
|
};
|
|
}
|
|
|
|
function createResponseRecorder() {
|
|
return {
|
|
statusCode: 200,
|
|
body: undefined,
|
|
status(code) {
|
|
this.statusCode = code;
|
|
return this;
|
|
},
|
|
json(body) {
|
|
this.body = body;
|
|
return this;
|
|
},
|
|
};
|
|
}
|
|
|
|
function createRequest(authorization) {
|
|
return {
|
|
get(name) {
|
|
assert.equal(name, 'authorization');
|
|
return authorization;
|
|
},
|
|
};
|
|
}
|
|
|
|
test('portal config route module preserves each route inventory', () => {
|
|
const imageApi = createRouterRecorder();
|
|
attachPortalImageMakeRuntimeConfigRoute(imageApi);
|
|
assert.deepEqual([...imageApi.routes.keys()], ['/internal/image-make/runtime-config']);
|
|
|
|
const wordsApi = createRouterRecorder();
|
|
attachPortalBlockedWordsRoute(wordsApi);
|
|
assert.deepEqual([...wordsApi.routes.keys()], ['/config/blocked-words']);
|
|
});
|
|
|
|
test('GET /internal/image-make/runtime-config preserves unavailable response', async () => {
|
|
const api = createRouterRecorder();
|
|
attachPortalImageMakeRuntimeConfigRoute(api);
|
|
const res = createResponseRecorder();
|
|
|
|
await api.routes.get('/internal/image-make/runtime-config')(createRequest(), res);
|
|
|
|
assert.equal(res.statusCode, 503);
|
|
assert.deepEqual(res.body, { message: 'image_make 配置服务未启用' });
|
|
});
|
|
|
|
test('GET /internal/image-make/runtime-config preserves bearer authorization', async () => {
|
|
const api = createRouterRecorder();
|
|
let receivedToken = null;
|
|
attachPortalImageMakeRuntimeConfigRoute(api, {
|
|
getImageMakeAdminConfigService: () => ({
|
|
authorizeRuntimeRequest(token) {
|
|
receivedToken = token;
|
|
return false;
|
|
},
|
|
}),
|
|
});
|
|
const res = createResponseRecorder();
|
|
|
|
await api.routes.get('/internal/image-make/runtime-config')(
|
|
createRequest('Bearer runtime-token '),
|
|
res,
|
|
);
|
|
|
|
assert.equal(receivedToken, 'runtime-token');
|
|
assert.equal(res.statusCode, 401);
|
|
assert.deepEqual(res.body, { message: '未授权' });
|
|
});
|
|
|
|
test('GET /internal/image-make/runtime-config preserves invalid runtime response', async () => {
|
|
const api = createRouterRecorder();
|
|
attachPortalImageMakeRuntimeConfigRoute(api, {
|
|
getImageMakeAdminConfigService: () => ({
|
|
authorizeRuntimeRequest: () => true,
|
|
async getRuntimeConfig() {
|
|
return { ok: false, message: 'runtime incomplete' };
|
|
},
|
|
}),
|
|
});
|
|
const res = createResponseRecorder();
|
|
|
|
await api.routes.get('/internal/image-make/runtime-config')(createRequest('Bearer token'), res);
|
|
|
|
assert.equal(res.statusCode, 503);
|
|
assert.deepEqual(res.body, { message: 'runtime incomplete' });
|
|
});
|
|
|
|
test('GET /internal/image-make/runtime-config preserves successful runtime payload', async () => {
|
|
const api = createRouterRecorder();
|
|
const runtime = { ok: true, baseUrl: 'http://image-make.test', token: 'secret' };
|
|
attachPortalImageMakeRuntimeConfigRoute(api, {
|
|
getImageMakeAdminConfigService: () => ({
|
|
authorizeRuntimeRequest: (token) => token === 'valid-token',
|
|
async getRuntimeConfig() {
|
|
return runtime;
|
|
},
|
|
}),
|
|
});
|
|
const res = createResponseRecorder();
|
|
|
|
await api.routes.get('/internal/image-make/runtime-config')(
|
|
createRequest('Bearer valid-token'),
|
|
res,
|
|
);
|
|
|
|
assert.equal(res.statusCode, 200);
|
|
assert.equal(res.body, runtime);
|
|
});
|
|
|
|
test('GET /config/blocked-words waits for auth and preserves empty fallback', async () => {
|
|
const api = createRouterRecorder();
|
|
let ready = false;
|
|
attachPortalBlockedWordsRoute(api, {
|
|
waitForUserAuthReady: async () => {
|
|
ready = true;
|
|
},
|
|
getWordFilterService: () => {
|
|
assert.equal(ready, true);
|
|
return null;
|
|
},
|
|
});
|
|
const res = createResponseRecorder();
|
|
|
|
await api.routes.get('/config/blocked-words')({}, res);
|
|
|
|
assert.deepEqual(res.body, { words: [] });
|
|
});
|
|
|
|
test('GET /config/blocked-words preserves frontend word list', async () => {
|
|
const api = createRouterRecorder();
|
|
const words = ['alpha', 'beta'];
|
|
attachPortalBlockedWordsRoute(api, {
|
|
getWordFilterService: () => ({
|
|
async listAllForFrontend() {
|
|
return words;
|
|
},
|
|
}),
|
|
});
|
|
const res = createResponseRecorder();
|
|
|
|
await api.routes.get('/config/blocked-words')({}, res);
|
|
|
|
assert.equal(res.statusCode, 200);
|
|
assert.deepEqual(res.body, { words });
|
|
});
|