mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 05:26:47 +00:00
6dda05f672
Eight files in services/ hold no state and touch no database: avatarUrl, conflictResult, demo, passwordPolicy, timezoneService, cookie, and the two crypto helpers. They are imported from 65 places, 62 of them already inside src/nest, so having them live in the legacy layer was pure inertia. They stay plain functions rather than becoming providers. Three importers sit outside the container and would have no way to reach an injectable one: db/migrations.ts runs before the app is built, and middleware/auth.ts and middleware/mfaPolicy.ts run beside it. Wrapping these in DI would buy ceremony and nothing else. mfaCrypto and apiKeyCrypto move together into common/crypto — they read the same ENCRYPTION_KEY, and the directory says so. services/ drops from 44 files to 36, 9,870 lines to 8,493. One trap worth recording: src/app-config and src/nest/app-config both exist. cookie.ts imported '../app-config' from services/, which resolved to the former; after the move the same string silently pointed at the latter, which has no readEnv. It is now explicit about which one it wants. Nothing else changes — no signatures, no behaviour, no tests rewritten beyond their import paths.
70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
|
|
import { cookieOptions } from '../../../src/nest/common/cookie';
|
|
import { SESSION_DURATION_MS, SESSION_DURATION_REMEMBER_MS } from '../../../src/config';
|
|
|
|
describe('cookieOptions', () => {
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
it('always sets httpOnly: true', () => {
|
|
expect(cookieOptions()).toHaveProperty('httpOnly', true);
|
|
});
|
|
|
|
it('always sets sameSite: lax', () => {
|
|
expect(cookieOptions()).toHaveProperty('sameSite', 'lax');
|
|
});
|
|
|
|
it('always sets path: /', () => {
|
|
expect(cookieOptions()).toHaveProperty('path', '/');
|
|
});
|
|
|
|
it('sets secure: false in test environment (COOKIE_SECURE=false from setup)', () => {
|
|
// setup.ts sets COOKIE_SECURE=false, so secure should be false
|
|
const opts = cookieOptions();
|
|
expect(opts.secure).toBe(false);
|
|
});
|
|
|
|
it('sets secure: true when NODE_ENV=production and COOKIE_SECURE is not false', () => {
|
|
vi.stubEnv('COOKIE_SECURE', 'true');
|
|
vi.stubEnv('NODE_ENV', 'production');
|
|
expect(cookieOptions().secure).toBe(true);
|
|
});
|
|
|
|
it('sets secure: false when COOKIE_SECURE=false even in production', () => {
|
|
vi.stubEnv('COOKIE_SECURE', 'false');
|
|
vi.stubEnv('NODE_ENV', 'production');
|
|
expect(cookieOptions().secure).toBe(false);
|
|
});
|
|
|
|
it('sets secure: true when FORCE_HTTPS=true', () => {
|
|
vi.stubEnv('COOKIE_SECURE', 'true');
|
|
vi.stubEnv('FORCE_HTTPS', 'true');
|
|
vi.stubEnv('NODE_ENV', 'development');
|
|
expect(cookieOptions().secure).toBe(true);
|
|
});
|
|
|
|
it('includes maxAge: 86400000 when clear is false (default)', () => {
|
|
expect(cookieOptions()).toHaveProperty('maxAge', 24 * 60 * 60 * 1000);
|
|
expect(cookieOptions(false)).toHaveProperty('maxAge', 24 * 60 * 60 * 1000);
|
|
});
|
|
|
|
it('omits maxAge when clear is true', () => {
|
|
const opts = cookieOptions(true);
|
|
expect(opts).not.toHaveProperty('maxAge');
|
|
});
|
|
|
|
it('keeps the default SESSION_DURATION maxAge when remember is undefined', () => {
|
|
expect(cookieOptions(false, undefined)).toHaveProperty('maxAge', SESSION_DURATION_MS);
|
|
});
|
|
|
|
it('uses the longer SESSION_DURATION_REMEMBER maxAge when remember is true', () => {
|
|
expect(cookieOptions(false, undefined, true)).toHaveProperty('maxAge', SESSION_DURATION_REMEMBER_MS);
|
|
});
|
|
|
|
it('omits maxAge (session cookie) when remember is false', () => {
|
|
expect(cookieOptions(false, undefined, false)).not.toHaveProperty('maxAge');
|
|
});
|
|
});
|