mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
test(server): src/config takes fixed test values from the setup file, so suites stop mocking it
tests/setup.ts mocks src/config once with tests/helpers/test-config.ts. A suite that needs other values calls overrideTestConfig(), and the setup file restores the defaults after every case. The 191 per-file factories are gone; config.test.ts takes the real module through vi.unmock, and the few suites that signed tokens with their own literal secret read it from TEST_CONFIG.
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { TEST_CONFIG } from './test-db';
|
||||
import { TEST_CONFIG } from './test-config';
|
||||
|
||||
/** Signs a JWT for the given user ID using the test secret. */
|
||||
export function generateToken(userId: number, extraClaims: Record<string, unknown> = {}): string {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* The values `src/config` holds under test.
|
||||
*
|
||||
* tests/setup.ts mocks `src/config` with this object for every test file, so a
|
||||
* suite never reads or writes the key files under data/ and every suite signs
|
||||
* tokens with the same secret the code under test verifies them with. A suite
|
||||
* does not declare its own `vi.mock` of the config module (lint:test-mocks
|
||||
* counts them); one that needs a different value calls `overrideTestConfig`,
|
||||
* and the setup file puts the defaults back after every test.
|
||||
*
|
||||
* This file must not import application code: the setup file loads it before
|
||||
* any module under test, and an app import here would resolve against the
|
||||
* environment before the setup file has written it.
|
||||
*/
|
||||
import type * as RealConfig from '../../src/config';
|
||||
|
||||
type ConfigModule = typeof RealConfig;
|
||||
type ConfigValues = Omit<ConfigModule, 'updateJwtSecret'>;
|
||||
|
||||
const DEFAULTS: ConfigValues = {
|
||||
JWT_SECRET: 'test-jwt-secret-for-trek-testing-only',
|
||||
ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2',
|
||||
DEFAULT_LANGUAGE: 'en',
|
||||
SESSION_DURATION: '24h',
|
||||
SESSION_DURATION_MS: 86400000,
|
||||
SESSION_DURATION_SECONDS: 86400,
|
||||
SESSION_DURATION_REMEMBER: '30d',
|
||||
SESSION_DURATION_REMEMBER_MS: 2592000000,
|
||||
SESSION_DURATION_REMEMBER_SECONDS: 2592000,
|
||||
};
|
||||
|
||||
/**
|
||||
* The mocked module itself. Code under test reads its exports at call time,
|
||||
* so a change made by `overrideTestConfig` is seen by the next read.
|
||||
* `updateJwtSecret` stays inert, as it was in every per-file mock before: a
|
||||
* rotation inside one case must not change the secret the next case signs with.
|
||||
*/
|
||||
export const TEST_CONFIG: ConfigModule = {
|
||||
...DEFAULTS,
|
||||
updateJwtSecret: () => {},
|
||||
};
|
||||
|
||||
/**
|
||||
* Set config values for the current test. Returns a function that puts the
|
||||
* previous values back; the setup file also restores the defaults after every
|
||||
* test, so calling it is only needed to undo an override inside one case.
|
||||
*/
|
||||
export function overrideTestConfig(values: Partial<ConfigValues>): () => void {
|
||||
const previous: Partial<ConfigValues> = {};
|
||||
const target = TEST_CONFIG as unknown as Record<string, unknown>;
|
||||
for (const key of Object.keys(values) as (keyof ConfigValues)[]) {
|
||||
(previous as Record<string, unknown>)[key] = target[key];
|
||||
target[key] = values[key];
|
||||
}
|
||||
return () => {
|
||||
for (const key of Object.keys(previous)) target[key] = (previous as Record<string, unknown>)[key];
|
||||
};
|
||||
}
|
||||
|
||||
/** Put every value back to its default. Called by tests/setup.ts after each test. */
|
||||
export function resetTestConfig(): void {
|
||||
Object.assign(TEST_CONFIG, DEFAULTS);
|
||||
}
|
||||
@@ -10,7 +10,6 @@
|
||||
* const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock');
|
||||
* return buildDbMock(createSnapshotTestDb());
|
||||
* });
|
||||
* vi.mock('../../src/config', () => TEST_CONFIG);
|
||||
*
|
||||
* import { db as testDb } from '../../src/db/database';
|
||||
* import { resetTestDb, resetRateLimits } from '../helpers/test-db';
|
||||
@@ -188,15 +187,3 @@ export function resetRateLimits(app: INestApplication): Promise<void> {
|
||||
return ctrl.rl.reset();
|
||||
}
|
||||
|
||||
/** Fixed config mock — use with vi.mock('../../src/config', () => TEST_CONFIG) */
|
||||
export const TEST_CONFIG = {
|
||||
JWT_SECRET: 'test-jwt-secret-for-trek-testing-only',
|
||||
ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2',
|
||||
updateJwtSecret: () => {},
|
||||
SESSION_DURATION: '24h',
|
||||
SESSION_DURATION_MS: 86400000,
|
||||
SESSION_DURATION_SECONDS: 86400,
|
||||
SESSION_DURATION_REMEMBER: '30d',
|
||||
SESSION_DURATION_REMEMBER_MS: 2592000000,
|
||||
SESSION_DURATION_REMEMBER_SECONDS: 2592000,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user