* fix terminal rendering and preview detection * Fix bot comments * fix: protect terminal preview URL probe --------- Co-authored-by: Konstantin Zolin <zolin_ka@vk.com> Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
87 lines
2.8 KiB
JavaScript
87 lines
2.8 KiB
JavaScript
import { describe, it, expect, vi } from 'vitest';
|
|
import express from 'express';
|
|
import request from 'supertest';
|
|
import { registerAuthAndAccessRoutes, registerCommonRequestMiddleware, registerServerStatusRoutes } from './core-routes.js';
|
|
|
|
describe('core-routes', () => {
|
|
it('should call gracefulShutdown with exitProcess: true on /api/system/shutdown', async () => {
|
|
const app = express();
|
|
let shutdownOpts = null;
|
|
const dependencies = {
|
|
gracefulShutdown: vi.fn(async (opts) => {
|
|
shutdownOpts = opts;
|
|
}),
|
|
getHealthSnapshot: () => ({ status: 'ok' }),
|
|
openchamberVersion: '1.0.0',
|
|
runtimeName: 'test',
|
|
express,
|
|
};
|
|
|
|
registerServerStatusRoutes(app, dependencies);
|
|
|
|
await request(app).post('/api/system/shutdown');
|
|
|
|
expect(dependencies.gracefulShutdown).toHaveBeenCalled();
|
|
expect(shutdownOpts).toEqual({ exitProcess: true });
|
|
});
|
|
|
|
it('should parse JSON bodies for snippet config routes', async () => {
|
|
const app = express();
|
|
registerCommonRequestMiddleware(app, { express });
|
|
app.post('/api/config/snippets/example', (req, res) => {
|
|
res.json({ body: req.body });
|
|
});
|
|
|
|
const response = await request(app)
|
|
.post('/api/config/snippets/example')
|
|
.send({ content: 'Snippet body' })
|
|
.expect(200);
|
|
|
|
expect(response.body).toEqual({ body: { content: 'Snippet body' } });
|
|
});
|
|
|
|
it('should require API auth before probing loopback preview URLs', async () => {
|
|
const app = express();
|
|
const originalFetch = globalThis.fetch;
|
|
const fetchMock = vi.fn();
|
|
globalThis.fetch = fetchMock;
|
|
|
|
registerAuthAndAccessRoutes(app, {
|
|
express,
|
|
tunnelAuthController: {
|
|
classifyRequestScope: () => 'local',
|
|
requireTunnelSession: vi.fn(),
|
|
getTunnelSessionFromRequest: vi.fn(),
|
|
clearTunnelSessionCookie: vi.fn(),
|
|
exchangeBootstrapToken: vi.fn(),
|
|
},
|
|
uiAuthController: {
|
|
requireAuth: (_req, res) => res.status(401).json({ error: 'Unauthorized' }),
|
|
handleSessionStatus: vi.fn(),
|
|
handleSessionCreate: vi.fn(),
|
|
handlePasskeyStatus: vi.fn(),
|
|
handlePasskeyAuthenticationOptions: vi.fn(),
|
|
handlePasskeyAuthenticationVerify: vi.fn(),
|
|
handlePasskeyRegistrationOptions: vi.fn(),
|
|
handlePasskeyRegistrationVerify: vi.fn(),
|
|
handlePasskeyList: vi.fn(),
|
|
handlePasskeyRevoke: vi.fn(),
|
|
handleResetAuth: vi.fn(),
|
|
},
|
|
readSettingsFromDiskMigrated: vi.fn(async () => ({})),
|
|
normalizeTunnelSessionTtlMs: vi.fn(),
|
|
});
|
|
|
|
try {
|
|
await request(app)
|
|
.post('/api/system/probe-url')
|
|
.send({ url: 'http://127.0.0.1:5173/' })
|
|
.expect(401);
|
|
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
});
|