226 lines
8.3 KiB
TypeScript
226 lines
8.3 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
import { ConfigManager } from '@stock-bot/config';
|
|
import { ServiceContainerBuilder } from '@stock-bot/di';
|
|
import { getLogger, setLoggerConfig } from '@stock-bot/logger';
|
|
import { MongoDBClient } from '@stock-bot/mongodb';
|
|
import { assertEnvironmentSafety, stockAppSchema, type StockAppConfig } from '@stock-bot/stock-config';
|
|
|
|
setLoggerConfig({ logLevel: 'fatal', logConsole: false });
|
|
|
|
function makeConfig(overrides: {
|
|
environment?: 'development' | 'qa' | 'test' | 'production';
|
|
pgHost?: string;
|
|
pgPort?: number;
|
|
pgDatabase?: string;
|
|
pgPassword?: string;
|
|
mongoUri?: string;
|
|
mongoReadUri?: string;
|
|
mongoReadOnly?: boolean;
|
|
mongoDatabase?: string;
|
|
dragonflyPassword?: string;
|
|
} = {}): StockAppConfig {
|
|
return stockAppSchema.parse({
|
|
name: 'stock-bot',
|
|
version: '1.0.0',
|
|
environment: overrides.environment ?? 'development',
|
|
service: {
|
|
name: 'stock-bot',
|
|
port: 2002,
|
|
host: '127.0.0.1',
|
|
},
|
|
log: {
|
|
level: 'info',
|
|
format: 'json',
|
|
},
|
|
database: {
|
|
postgres: {
|
|
enabled: true,
|
|
host: overrides.pgHost ?? 'localhost',
|
|
port: overrides.pgPort ?? 5433,
|
|
database: overrides.pgDatabase ?? 'trading_bot_dev',
|
|
user: 'dev_user',
|
|
password: overrides.pgPassword ?? 'dev_pass',
|
|
ssl: false,
|
|
},
|
|
mongodb: {
|
|
enabled: true,
|
|
uri:
|
|
overrides.mongoUri ??
|
|
'mongodb://dev_admin:dev_mongo_pass@localhost:27018/stock_dev?authSource=admin',
|
|
readUri:
|
|
overrides.mongoReadUri ??
|
|
'mongodb://studio_stock_bot_read:secret@localhost:27017/stock?authSource=stock',
|
|
readOnly: overrides.mongoReadOnly ?? false,
|
|
database: overrides.mongoDatabase ?? 'stock_dev',
|
|
},
|
|
questdb: {
|
|
enabled: false,
|
|
host: 'localhost',
|
|
ilpPort: 9010,
|
|
httpPort: 9100,
|
|
pgPort: 8813,
|
|
},
|
|
dragonfly: {
|
|
enabled: true,
|
|
host: 'localhost',
|
|
port: 6380,
|
|
password: overrides.dragonflyPassword,
|
|
},
|
|
},
|
|
providers: {
|
|
yahoo: { name: 'yahoo', enabled: false, priority: 1, timeout: 30000, baseUrl: 'https://query1.finance.yahoo.com' },
|
|
qm: { name: 'qm', enabled: false, priority: 2, username: '', password: '', baseUrl: 'https://app.quotemedia.com/quotetools', webmasterId: '' },
|
|
ib: { name: 'ib', enabled: false, priority: 3, gateway: { host: 'localhost', port: 5000, clientId: 1 }, marketDataType: 'delayed' },
|
|
eod: { name: 'eod', enabled: false, priority: 4, apiKey: '', baseUrl: 'https://eodhistoricaldata.com/api', tier: 'free' },
|
|
},
|
|
features: {},
|
|
});
|
|
}
|
|
|
|
describe('Environment safety & multi-env isolation', () => {
|
|
test('ConfigManager detects qa environment from NODE_ENV=qa', () => {
|
|
const prev = process.env.NODE_ENV;
|
|
try {
|
|
process.env.NODE_ENV = 'qa';
|
|
const manager = new ConfigManager({ loaders: [] });
|
|
expect(manager.getEnvironment()).toBe('qa');
|
|
} finally {
|
|
process.env.NODE_ENV = prev;
|
|
}
|
|
});
|
|
|
|
test('blocks development from targeting QA PostgreSQL (5432/trading_bot)', () => {
|
|
const cfg = makeConfig({
|
|
environment: 'development',
|
|
pgPort: 5432,
|
|
pgDatabase: 'trading_bot',
|
|
});
|
|
expect(() => assertEnvironmentSafety(cfg, 'dataPipeline', {})).toThrow(
|
|
/development environment cannot target QA PostgreSQL/
|
|
);
|
|
});
|
|
|
|
test('blocks development dataIngestion from writing to QA MongoDB (27017/stock)', () => {
|
|
const cfg = makeConfig({
|
|
environment: 'development',
|
|
mongoUri: 'mongodb://trading_admin:trading_mongo_dev@localhost:27017/stock?authSource=admin',
|
|
mongoDatabase: 'stock',
|
|
});
|
|
expect(() => assertEnvironmentSafety(cfg, 'dataIngestion', {})).toThrow(
|
|
/cannot open a writable MongoDB connection to QA MongoDB/
|
|
);
|
|
});
|
|
|
|
test('allows development dataIngestion on dev MongoDB (27018/stock_dev) and dev Postgres (5433)', () => {
|
|
const cfg = makeConfig({ environment: 'development' });
|
|
expect(() => assertEnvironmentSafety(cfg, 'dataIngestion', {})).not.toThrow();
|
|
});
|
|
|
|
test('allows development dataPipeline to read QA MongoDB via readUri while writing dev Postgres', () => {
|
|
const cfg = makeConfig({ environment: 'development' });
|
|
expect(() => assertEnvironmentSafety(cfg, 'dataPipeline', {})).not.toThrow();
|
|
});
|
|
|
|
test('blocks qa environment from targeting *_dev databases', () => {
|
|
const cfg = makeConfig({
|
|
environment: 'qa',
|
|
pgPort: 5433,
|
|
pgDatabase: 'trading_bot_dev',
|
|
});
|
|
expect(() => assertEnvironmentSafety(cfg, 'dataPipeline', {})).toThrow(
|
|
/qa environment cannot target dev PostgreSQL/
|
|
);
|
|
});
|
|
|
|
test('blocks production from using dev/placeholder credentials or disabling auth', () => {
|
|
const withDevPass = makeConfig({
|
|
environment: 'production',
|
|
pgHost: 'pg.prod.internal',
|
|
pgDatabase: 'trading_bot_prod',
|
|
pgPassword: 'trading_pass_dev',
|
|
mongoUri: 'mongodb://prod:strongpass@mongo.prod.internal:27017/stock',
|
|
});
|
|
expect(() => assertEnvironmentSafety(withDevPass, 'webApi', {})).toThrow(
|
|
/default dev\/qa credential/
|
|
);
|
|
|
|
const withSafeSecrets = makeConfig({
|
|
environment: 'production',
|
|
pgHost: 'pg.prod.internal',
|
|
pgDatabase: 'trading_bot_prod',
|
|
pgPassword: 'super-strong-prod-password',
|
|
mongoUri: 'mongodb://prod:strongpass@mongo.prod.internal:27017/stock',
|
|
});
|
|
expect(() =>
|
|
assertEnvironmentSafety(withSafeSecrets, 'webApi', { STOCK_BOT_AUTH_DISABLED: 'true' })
|
|
).toThrow(/STOCK_BOT_AUTH_DISABLED=true is forbidden/);
|
|
expect(() => assertEnvironmentSafety(withSafeSecrets, 'webApi', {})).not.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('MongoDBClient read-only enforcement', () => {
|
|
const logger = getLogger('mongo-readonly-test');
|
|
const roClient = new MongoDBClient(
|
|
{
|
|
host: 'localhost',
|
|
port: 27017,
|
|
database: 'stock',
|
|
readOnly: true,
|
|
},
|
|
logger
|
|
);
|
|
|
|
test('rejects all write and schema-mutation operations when readOnly=true', async () => {
|
|
expect(roClient.isReadOnly()).toBe(true);
|
|
await expect(
|
|
roClient.insertOne('qmSymbols', { source: 'test' } as never)
|
|
).rejects.toThrow(/read-only mode/);
|
|
await expect(roClient.insertMany('qmSymbols', [{ a: 1 }])).rejects.toThrow(/read-only mode/);
|
|
await expect(roClient.updateOne('qmSymbols', {}, { $set: { a: 1 } })).rejects.toThrow(
|
|
/read-only mode/
|
|
);
|
|
await expect(roClient.updateMany('qmSymbols', {}, { $set: { a: 1 } })).rejects.toThrow(
|
|
/read-only mode/
|
|
);
|
|
await expect(roClient.deleteOne('qmSymbols', {})).rejects.toThrow(/read-only mode/);
|
|
await expect(roClient.deleteMany('qmSymbols', {})).rejects.toThrow(/read-only mode/);
|
|
await expect(roClient.batchUpsert('qmSymbols', [], 'symbol')).rejects.toThrow(/read-only mode/);
|
|
await expect(roClient.createIndex('qmSymbols', { symbol: 1 })).rejects.toThrow(/read-only mode/);
|
|
await expect(roClient.dropIndex('qmSymbols', 'symbol_1')).rejects.toThrow(/read-only mode/);
|
|
await expect(roClient.createCollection('tmp')).rejects.toThrow(/read-only mode/);
|
|
await expect(roClient.dropCollection('tmp')).rejects.toThrow(/read-only mode/);
|
|
await expect(
|
|
roClient.aggregate('qmSymbols', [{ $match: {} }, { $out: 'evil' }])
|
|
).rejects.toThrow(/aggregation stage "\$out" is forbidden/);
|
|
await expect(
|
|
roClient.aggregate('qmSymbols', [{ $merge: { into: 'evil' } }])
|
|
).rejects.toThrow(/aggregation stage "\$merge" is forbidden/);
|
|
});
|
|
|
|
test('ServiceContainerBuilder resolves readUri and read-only mode when mongoReadOnly=true', async () => {
|
|
const cfg = makeConfig({ environment: 'development' });
|
|
const { container, shutdown } = await new ServiceContainerBuilder()
|
|
.withConfig(cfg)
|
|
.withOptions({
|
|
enableMongoDB: true,
|
|
mongoReadOnly: true,
|
|
enablePostgres: false,
|
|
enableQuestDB: false,
|
|
enableCache: false,
|
|
enableQueue: false,
|
|
enableBrowser: false,
|
|
enableProxy: false,
|
|
skipInitialization: true,
|
|
})
|
|
.buildOwned();
|
|
|
|
try {
|
|
const mongo = container.resolve('mongoClient');
|
|
expect(mongo).not.toBeNull();
|
|
expect(mongo!.isReadOnly()).toBe(true);
|
|
expect(mongo!.getDefaultDatabase()).toBe('stock');
|
|
} finally {
|
|
await shutdown();
|
|
}
|
|
});
|
|
});
|