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
d9a9dd23dd
commit
7cf940afce
14 changed files with 407 additions and 26 deletions
|
|
@ -44,7 +44,7 @@ export const queueConfigSchema = z.object({
|
|||
}),
|
||||
workers: z.number().default(1),
|
||||
concurrency: z.number().default(1),
|
||||
enableScheduledJobs: z.boolean().default(true),
|
||||
enableScheduledJobs: z.boolean().default(false),
|
||||
defaultJobOptions: z
|
||||
.object({
|
||||
attempts: z.number().default(3),
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { readFileSync } from 'fs';
|
|||
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
|
||||
import { ConfigLoaderError } from '../src/errors';
|
||||
import { EnvLoader } from '../src/loaders/env.loader';
|
||||
import { queueConfigSchema } from '../src/schemas';
|
||||
|
||||
// Mock fs module
|
||||
mock.module('fs', () => ({
|
||||
|
|
@ -251,6 +252,22 @@ describe('EnvLoader', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('should map QUEUE_ENABLE_SCHEDULED_JOBS=true while defaulting unset to false in schema', () => {
|
||||
loader = new EnvLoader();
|
||||
const unsetConfig = loader.load();
|
||||
expect((unsetConfig.queue as Record<string, unknown> | undefined)?.enableScheduledJobs).toBeUndefined();
|
||||
expect(queueConfigSchema.parse({ redis: {}, ...(unsetConfig.queue as object) }).enableScheduledJobs).toBe(
|
||||
false
|
||||
);
|
||||
|
||||
process.env.QUEUE_ENABLE_SCHEDULED_JOBS = 'true';
|
||||
const optedInConfig = loader.load();
|
||||
expect((optedInConfig.queue as Record<string, unknown> | undefined)?.enableScheduledJobs).toBe(true);
|
||||
expect(
|
||||
queueConfigSchema.parse({ redis: {}, ...(optedInConfig.queue as object) }).enableScheduledJobs
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('should not apply provider mappings when prefix is set', () => {
|
||||
process.env.APP_WEBSHARE_API_KEY = 'key';
|
||||
|
||||
|
|
|
|||
|
|
@ -214,7 +214,7 @@ describe('Config Schemas', () => {
|
|||
},
|
||||
workers: 1,
|
||||
concurrency: 1,
|
||||
enableScheduledJobs: true,
|
||||
enableScheduledJobs: false,
|
||||
defaultJobOptions: {
|
||||
attempts: 3,
|
||||
backoff: {
|
||||
|
|
@ -253,6 +253,12 @@ describe('Config Schemas', () => {
|
|||
|
||||
const config = queueConfigSchema.parse(input);
|
||||
expect(config).toEqual(input);
|
||||
|
||||
const optedIn = queueConfigSchema.parse({
|
||||
redis: {},
|
||||
enableScheduledJobs: true,
|
||||
});
|
||||
expect(optedIn.enableScheduledJobs).toBe(true);
|
||||
});
|
||||
|
||||
it('should validate backoff type', () => {
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ export const queueConfigSchema = z.object({
|
|||
enabled: z.boolean().optional().default(true),
|
||||
workers: z.number().optional().default(1),
|
||||
concurrency: z.number().optional().default(1),
|
||||
enableScheduledJobs: z.boolean().optional().default(true),
|
||||
enableScheduledJobs: z.boolean().optional().default(false),
|
||||
defaultJobOptions: z
|
||||
.object({
|
||||
attempts: z.number().default(3),
|
||||
|
|
|
|||
|
|
@ -209,7 +209,7 @@ export class ServiceContainerBuilder {
|
|||
enabled: true,
|
||||
workers: 1,
|
||||
concurrency: 1,
|
||||
enableScheduledJobs: true,
|
||||
enableScheduledJobs: false,
|
||||
defaultJobOptions: {
|
||||
attempts: 3,
|
||||
backoff: { type: 'exponential' as const, delay: 1000 },
|
||||
|
|
|
|||
|
|
@ -441,7 +441,7 @@ export class ServiceApplication {
|
|||
|
||||
// Initialize scheduled jobs if enabled in both serviceConfig and runtime queue config
|
||||
const scheduledJobsEnabled =
|
||||
this.serviceConfig.enableScheduledJobs && (this.config.queue?.enableScheduledJobs ?? true);
|
||||
this.serviceConfig.enableScheduledJobs && (this.config.queue?.enableScheduledJobs ?? false);
|
||||
if (scheduledJobsEnabled) {
|
||||
await this.initializeScheduledJobs();
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -366,7 +366,18 @@ describe('ServiceApplication', () => {
|
|||
|
||||
const jobContainerFactory = mock(async () => containerWithJobs);
|
||||
|
||||
app = new ServiceApplication(mockConfig, serviceConfig);
|
||||
const configWithScheduledJobs = {
|
||||
...mockConfig,
|
||||
queue: {
|
||||
enabled: true,
|
||||
redis: { host: 'localhost', port: 6379, db: 1 },
|
||||
workers: 1,
|
||||
concurrency: 1,
|
||||
enableScheduledJobs: true,
|
||||
},
|
||||
};
|
||||
|
||||
app = new ServiceApplication(configWithScheduledJobs as any, serviceConfig);
|
||||
|
||||
await app.start(jobContainerFactory, mockRouteFactory);
|
||||
|
||||
|
|
@ -382,6 +393,56 @@ describe('ServiceApplication', () => {
|
|||
expect(mockQueueManager.startAllWorkers).toHaveBeenCalled();
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('Scheduled jobs created', { totalJobs: 1 });
|
||||
});
|
||||
|
||||
it('should skip scheduled jobs and still start workers when queue.enableScheduledJobs is omitted or false', async () => {
|
||||
const serviceConfig: ServiceApplicationConfig = {
|
||||
serviceName: 'test-service',
|
||||
enableScheduledJobs: true,
|
||||
};
|
||||
|
||||
for (const runtimeConfig of [
|
||||
mockConfig,
|
||||
{
|
||||
...mockConfig,
|
||||
queue: {
|
||||
enabled: true,
|
||||
redis: { host: 'localhost', port: 6379, db: 1 },
|
||||
workers: 1,
|
||||
concurrency: 1,
|
||||
enableScheduledJobs: false,
|
||||
},
|
||||
},
|
||||
]) {
|
||||
const mockQueue = {
|
||||
addScheduledJob: mock(() => Promise.resolve()),
|
||||
};
|
||||
|
||||
const mockQueueManager = {
|
||||
getQueue: mock(() => mockQueue),
|
||||
startAllWorkers: mock(() => {}),
|
||||
shutdown: mock(() => Promise.resolve()),
|
||||
};
|
||||
|
||||
const containerWithJobs = {
|
||||
resolve: mock((name: string) => {
|
||||
if (name === 'serviceContainer') {
|
||||
return { test: 'container' };
|
||||
}
|
||||
if (name === 'queueManager') {
|
||||
return mockQueueManager;
|
||||
}
|
||||
return null;
|
||||
}),
|
||||
};
|
||||
|
||||
app = new ServiceApplication(runtimeConfig as any, serviceConfig);
|
||||
await app.start(async () => containerWithJobs as any, mockRouteFactory);
|
||||
|
||||
expect(mockQueueManager.getQueue).not.toHaveBeenCalled();
|
||||
expect(mockQueue.addScheduledJob).not.toHaveBeenCalled();
|
||||
expect(mockQueueManager.startAllWorkers).toHaveBeenCalledTimes(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('stop method', () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue