feat(backtests): separate saving from retry-safe run submission
This commit is contained in:
parent
5eb0985057
commit
9b9403e36b
18 changed files with 1200 additions and 226 deletions
|
|
@ -1,11 +1,14 @@
|
|||
import { readFileSync } from 'node:fs';
|
||||
import { expect, test } from 'bun:test';
|
||||
import { Hono } from 'hono';
|
||||
import { createHttpSecurity as createApiSecurity } from '@stock-bot/di';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { IServiceContainer } from '@stock-bot/services';
|
||||
import { createPipelineRoutes } from '../../apps/stock/web-api/src/routes/pipeline.routes';
|
||||
import { createBacktestV2Routes } from '../../apps/stock/web-api/src/routes/backtests-v2.routes';
|
||||
import { createRoutes } from '../../apps/stock/web-api/src/routes/create-routes';
|
||||
import { createWebSocketRoute } from '../../apps/stock/web-api/src/routes/websocket.routes';
|
||||
import { createHttpSecurity, type HttpSecurityOptions } from '../../libs/core/di/src/http/security';
|
||||
import { createHttpSecurity, getVerifiedHttpPrincipal, type HttpSecurityOptions } from '../../libs/core/di/src/http/security';
|
||||
import { redactLogValue } from '../../libs/core/logger/src/redact';
|
||||
import { parseReferenceDataResetRequest } from '../../libs/core/types/src/reference-data';
|
||||
import { unexpectedTrackedEnvironmentFiles } from '../audit-secrets';
|
||||
|
|
@ -56,6 +59,44 @@ test('HTTP security fails closed and insecure mode is loopback-development only'
|
|||
expect(createHttpSecurity({ disabled: true }).hostname).toBe('127.0.0.1');
|
||||
});
|
||||
|
||||
test('submission principals are stable server identities across bearer/cookie renewal and ignore spoofed headers', async () => {
|
||||
const f = fixture();
|
||||
f.app.post('/api/principal', context => context.json({ principal: getVerifiedHttpPrincipal(context.req.raw) }));
|
||||
const post = (headers: HeadersInit) => f.app.request('/api/principal', { method: 'POST', headers });
|
||||
expect(await (await post({ ...bearer(), 'X-Principal': 'service' })).json()).toEqual({ principal: 'operator' });
|
||||
expect(await (await post(bearer(options.serviceToken))).json()).toEqual({ principal: 'service' });
|
||||
const cookies: string[] = [];
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const session = await login(f.app);
|
||||
const Cookie = session.headers.get('set-cookie')!.split(';')[0]!;
|
||||
cookies.push(Cookie);
|
||||
expect(await (await post({ Cookie, Origin: 'http://ui.test', 'X-Principal': 'attacker' })).json()).toEqual({ principal: 'operator' });
|
||||
}
|
||||
expect(cookies[0]).not.toBe(cookies[1]);
|
||||
expect((await post(bearer(options.readToken))).status).toBe(403);
|
||||
expect((await post({ 'X-Principal': 'operator' })).status).toBe(401);
|
||||
expect(getVerifiedHttpPrincipal(new Request('http://api.test', { headers: { 'X-Principal': 'operator' } }))).toBeUndefined();
|
||||
});
|
||||
|
||||
test('keyed run routes fail closed without verified security and support trusted CORS preflight', async () => {
|
||||
const services = { logger: getLogger('submission-security'), postgres: {} } as IServiceContainer;
|
||||
const path = '/api/v2/backtests/00000000-0000-4000-8000-000000000001/runs';
|
||||
const request = { method: 'POST', headers: { 'Idempotency-Key': 'key', 'X-Principal': 'operator' }, body: '{}' };
|
||||
const bare = createBacktestV2Routes(services);
|
||||
expect((await bare.request(path, request)).status).toBe(401);
|
||||
const app = createRoutes(services, createApiSecurity(options));
|
||||
expect((await app.request(path, request)).status).toBe(401);
|
||||
expect((await app.request(path, { ...request, headers: { ...request.headers, ...bearer(options.readToken) } })).status).toBe(403);
|
||||
expect((await app.request(path, { ...request, headers: { ...bearer(), 'Idempotency-Key': '' } })).status).toBe(400);
|
||||
const preflight = await app.request(path, { method: 'OPTIONS', headers: {
|
||||
Origin: 'http://ui.test', 'Access-Control-Request-Method': 'POST',
|
||||
'Access-Control-Request-Headers': 'content-type,idempotency-key',
|
||||
} });
|
||||
expect(preflight.status).toBe(204);
|
||||
expect(preflight.headers.get('Access-Control-Allow-Headers')?.toLowerCase()).toContain('idempotency-key');
|
||||
expect(preflight.headers.get('Access-Control-Allow-Origin')).toBe('http://ui.test');
|
||||
});
|
||||
|
||||
test('anonymous/invalid access is rejected; readers cannot mutate; service/admin can', async () => {
|
||||
const f = fixture();
|
||||
expect((await f.app.request('/health')).status).toBe(200);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue