Files
pn-new-crm/tests/helpers/route-tester.ts

43 lines
1.3 KiB
TypeScript
Raw Normal View History

/**
* Helper to invoke route inner-handlers directly, bypassing withAuth.
* Route files must export the inner handler (in addition to the withAuth-wrapped HTTP method).
*/
import { NextRequest } from 'next/server';
import type { AuthContext } from '@/lib/api/helpers';
import type { RolePermissions } from '@/lib/db/schema/users';
export interface MockCtxOptions {
portId: string;
isSuperAdmin?: boolean;
permissions?: RolePermissions | null;
userId?: string;
}
export function makeMockCtx(opts: MockCtxOptions): AuthContext {
return {
userId: opts.userId ?? 'test-user',
portId: opts.portId,
portSlug: 'test-port',
isSuperAdmin: opts.isSuperAdmin ?? false,
permissions: opts.permissions ?? null,
user: { email: 'test@example.com', name: 'Test User' },
ipAddress: '127.0.0.1',
userAgent: 'vitest/1.0',
};
}
export function makeMockRequest(
method: string,
url: string,
opts: { body?: unknown; headers?: Record<string, string> } = {},
): NextRequest {
const init: { method: string; headers: Record<string, string>; body?: string } = {
method,
headers: { 'content-type': 'application/json', ...(opts.headers ?? {}) },
};
if (opts.body !== undefined) {
init.body = JSON.stringify(opts.body);
}
return new NextRequest(url, init);
}