Files
familysync/apps/api/tests/lib/bootGuards.test.ts
Lucas Berger 8154ba6f35
CI / changes (pull_request) Successful in 2s
CI / fast-checks (pull_request) Successful in 1m23s
CI / api (pull_request) Successful in 1m0s
CI / harness (pull_request) Successful in 3m54s
CI / security (pull_request) Successful in 40s
CI / gate (pull_request) Successful in 1s
style(16): apply prettier formatting to satisfy CI format:check
2026-06-13 09:29:02 -04:00

69 lines
2.3 KiB
TypeScript

/**
* assertNotDevBypassInProduction() — unit tests.
*
* Tests the three behavioral cases:
* 1. NODE_ENV='production' AND DEV_AUTH_BYPASS='true' → calls process.exit(1)
* 2. NODE_ENV='development' AND DEV_AUTH_BYPASS='true' → does NOT call process.exit
* 3. NODE_ENV='production' AND DEV_AUTH_BYPASS unset → does NOT call process.exit
*/
import { describe, it, expect, vi, afterEach } from 'vitest';
import { assertNotDevBypassInProduction } from '../../src/lib/bootGuards.js';
describe('assertNotDevBypassInProduction', () => {
const originalNodeEnv = process.env.NODE_ENV;
const originalBypassFlag = process.env.DEV_AUTH_BYPASS;
afterEach(() => {
// Restore env after each test
process.env.NODE_ENV = originalNodeEnv;
if (originalBypassFlag === undefined) {
delete process.env.DEV_AUTH_BYPASS;
} else {
process.env.DEV_AUTH_BYPASS = originalBypassFlag;
}
});
it('calls process.exit(1) when NODE_ENV=production and DEV_AUTH_BYPASS=true', () => {
process.env.NODE_ENV = 'production';
process.env.DEV_AUTH_BYPASS = 'true';
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => {
throw new Error('process.exit called');
}) as never);
expect(() => assertNotDevBypassInProduction()).toThrow('process.exit called');
expect(exitSpy).toHaveBeenCalledWith(1);
exitSpy.mockRestore();
});
it('does NOT call process.exit when NODE_ENV=development and DEV_AUTH_BYPASS=true', () => {
process.env.NODE_ENV = 'development';
process.env.DEV_AUTH_BYPASS = 'true';
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => {
throw new Error('process.exit called');
}) as never);
expect(() => assertNotDevBypassInProduction()).not.toThrow();
expect(exitSpy).not.toHaveBeenCalled();
exitSpy.mockRestore();
});
it('does NOT call process.exit when NODE_ENV=production and DEV_AUTH_BYPASS is unset', () => {
process.env.NODE_ENV = 'production';
delete process.env.DEV_AUTH_BYPASS;
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => {
throw new Error('process.exit called');
}) as never);
expect(() => assertNotDevBypassInProduction()).not.toThrow();
expect(exitSpy).not.toHaveBeenCalled();
exitSpy.mockRestore();
});
});