feat(api): GET/POST /api/v1/yachts
Add yacht list + create routes, export RouteHandler type and inner handlers so tests can invoke them directly with a mock AuthContext. New tests/helpers/route-tester.ts provides makeMockCtx/makeMockRequest reusable by subsequent Task 3.x routes.
This commit is contained in:
42
tests/helpers/route-tester.ts
Normal file
42
tests/helpers/route-tester.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
Reference in New Issue
Block a user