mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 05:26:47 +00:00
724c88c201
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
/**
|
|
* Auth helpers for integration tests.
|
|
*
|
|
* Provides utilities to generate JWTs and authenticate supertest requests
|
|
* using the fixed test JWT_SECRET from TEST_CONFIG.
|
|
*/
|
|
import { TEST_CONFIG } from './test-config';
|
|
|
|
import jwt from 'jsonwebtoken';
|
|
|
|
/** Signs a JWT for the given user ID using the test secret. */
|
|
export function generateToken(userId: number, extraClaims: Record<string, unknown> = {}): string {
|
|
return jwt.sign({ id: userId, ...extraClaims }, TEST_CONFIG.JWT_SECRET, { algorithm: 'HS256', expiresIn: '1h' });
|
|
}
|
|
|
|
/**
|
|
* Returns a cookie string suitable for supertest:
|
|
* request(app).get('/api/...').set('Cookie', authCookie(userId))
|
|
*/
|
|
export function authCookie(userId: number): string {
|
|
return `trek_session=${generateToken(userId)}`;
|
|
}
|
|
|
|
/**
|
|
* Returns an Authorization header object suitable for supertest:
|
|
* request(app).get('/api/...').set(authHeader(userId))
|
|
*/
|
|
export function authHeader(userId: number): Record<string, string> {
|
|
return { Authorization: `Bearer ${generateToken(userId)}` };
|
|
}
|