T-7: M0.3a — Harden dev Mongo safety: env-safety throws on unsafe data-pipeline QA reads, scheduled jobs default off, read-only bypass tests
This commit is contained in:
parent
abbab4b5a8
commit
0e5ebbd62f
9 changed files with 425 additions and 29 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { describe, expect, mock, test } from 'bun:test';
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { getLogger, setLoggerConfig } from '@stock-bot/logger';
|
||||
import { MongoDBClient } from '@stock-bot/mongodb';
|
||||
|
||||
|
|
@ -277,6 +278,95 @@ describe('MongoDBClient read-only mode (offline)', () => {
|
|||
expect(iterated).toEqual([{ _id: 'AAPL', count: 5 }]);
|
||||
});
|
||||
|
||||
test('builds aggregation stream from guarded iteration without exposing pinned driver cursor and preserves safe event listeners', async () => {
|
||||
const logger = getLogger('mongodb-read-only-stream-test');
|
||||
// Unconnected real driver client — zero sockets opened
|
||||
const realMongoClient = new MongoClient('mongodb://127.0.0.1:1/stock');
|
||||
const realDb = realMongoClient.db('stock');
|
||||
const realCollection = realDb.collection('eodPrices');
|
||||
const realCursor = realCollection.aggregate([{ $match: { symbol: 'AAPL' } }]);
|
||||
|
||||
// Confirm the pinned driver's raw stream exposes the unwrapped cursor via `_cursor`
|
||||
const rawStream = realCursor.stream() as unknown as Record<string, unknown>;
|
||||
expect(rawStream._cursor).toBe(realCursor);
|
||||
|
||||
// Stub next() and close() on the real AggregationCursor so no network I/O occurs
|
||||
const docs = [
|
||||
{ _id: 'AAPL', close: 200 },
|
||||
{ _id: 'MSFT', close: 400 },
|
||||
];
|
||||
let index = 0;
|
||||
const closeMock = mock(async () => {});
|
||||
realCursor.next = mock(async () => (index < docs.length ? docs[index++] : null)) as never;
|
||||
realCursor.close = closeMock as never;
|
||||
|
||||
const fakeDb = {
|
||||
databaseName: 'stock',
|
||||
collection: mock(() => ({
|
||||
collectionName: 'eodPrices',
|
||||
aggregate: mock(() => realCursor),
|
||||
})),
|
||||
};
|
||||
|
||||
const client = new MongoDBClient(
|
||||
{ host: '127.0.0.1', port: 1, database: 'stock', readOnly: true },
|
||||
logger
|
||||
);
|
||||
Object.assign(client, {
|
||||
client: { db: mock(() => fakeDb) },
|
||||
db: fakeDb,
|
||||
isConnected: true,
|
||||
});
|
||||
|
||||
const wrappedCursor = client
|
||||
.getDatabase()
|
||||
.collection('eodPrices')
|
||||
.aggregate([{ $match: { symbol: 'AAPL' } }]);
|
||||
|
||||
// Safe EventEmitter methods work and return the wrapped cursor (never raw realCursor)
|
||||
let closeEvents = 0;
|
||||
let onceEvents = 0;
|
||||
const onReturn = wrappedCursor.on('close', () => {
|
||||
closeEvents++;
|
||||
});
|
||||
const onceReturn = wrappedCursor.once('close', () => {
|
||||
onceEvents++;
|
||||
});
|
||||
expect(onReturn).toBe(wrappedCursor);
|
||||
expect(onceReturn).toBe(wrappedCursor);
|
||||
realCursor.emit('close');
|
||||
realCursor.emit('close');
|
||||
expect(closeEvents).toBe(2);
|
||||
expect(onceEvents).toBe(1);
|
||||
|
||||
// Wrapped stream must not expose the raw AggregationCursor or MongoClient
|
||||
const guardedStream = wrappedCursor.stream({
|
||||
transform: doc => ({ symbol: doc._id, doubled: Number(doc.close) * 2 }),
|
||||
});
|
||||
const streamRecord = guardedStream as unknown as Record<PropertyKey, unknown>;
|
||||
expect(streamRecord._cursor).toBeUndefined();
|
||||
for (const key of Reflect.ownKeys(guardedStream)) {
|
||||
expect(streamRecord[key]).not.toBe(realCursor);
|
||||
expect(streamRecord[key]).not.toBe(realMongoClient);
|
||||
}
|
||||
|
||||
const streamed: unknown[] = [];
|
||||
for await (const chunk of guardedStream) {
|
||||
streamed.push(chunk);
|
||||
}
|
||||
expect(streamed).toEqual([
|
||||
{ symbol: 'AAPL', doubled: 400 },
|
||||
{ symbol: 'MSFT', doubled: 800 },
|
||||
]);
|
||||
|
||||
// Destroying a guarded stream closes the underlying cursor
|
||||
index = 0;
|
||||
const destroyStream = wrappedCursor.stream();
|
||||
destroyStream.destroy();
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
expect(closeMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('passes through safe read operations on client and proxies', async () => {
|
||||
const { client, dbStub, collectionStub } = createStubbedClient(true);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue