Adds front-end set-up
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import type { AuthResponse, User } from '@/api/types';
|
||||
|
||||
/** Base URL the ApiClient targets; mocks must match the absolute URL. */
|
||||
export const API_BASE = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:5000';
|
||||
|
||||
export const TEST_CREDENTIALS = {
|
||||
email: 'owner@example.com',
|
||||
password: 'Password123!',
|
||||
};
|
||||
|
||||
export const mockUser: User = {
|
||||
id: '11111111-1111-1111-1111-111111111111',
|
||||
email: TEST_CREDENTIALS.email,
|
||||
name: 'Test Owner',
|
||||
role: 'Owner',
|
||||
isActive: true,
|
||||
};
|
||||
|
||||
export function makeAuthResponse(overrides: Partial<AuthResponse> = {}): AuthResponse {
|
||||
return {
|
||||
accessToken: 'mock-access-token',
|
||||
// Fixed timestamp keeps fixtures deterministic.
|
||||
expiresAt: '2099-01-01T00:00:00.000Z',
|
||||
user: mockUser,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import type { LoginRequest, ProblemDetails } from '@/api/types';
|
||||
import { API_BASE, makeAuthResponse, TEST_CREDENTIALS } from './fixtures';
|
||||
|
||||
function problem(status: number, title: string, detail?: string): ProblemDetails {
|
||||
return {
|
||||
type: 'about:blank',
|
||||
title,
|
||||
status,
|
||||
detail,
|
||||
traceId: '00-mock-trace-00',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Default auth mocks aligned with the real backend contract:
|
||||
* POST /api/v1/auth/login, /refresh, /revoke. Tests override these per case
|
||||
* via `server.use(...)` to simulate failures and 401 flows.
|
||||
*/
|
||||
export const authHandlers = [
|
||||
http.post(`${API_BASE}/api/v1/auth/login`, async ({ request }) => {
|
||||
const body = (await request.json()) as LoginRequest;
|
||||
if (body.email === TEST_CREDENTIALS.email && body.password === TEST_CREDENTIALS.password) {
|
||||
return HttpResponse.json(makeAuthResponse());
|
||||
}
|
||||
return HttpResponse.json(problem(401, 'Invalid email or password'), { status: 401 });
|
||||
}),
|
||||
|
||||
// By default refresh succeeds (simulates a valid httpOnly cookie present).
|
||||
http.post(`${API_BASE}/api/v1/auth/refresh`, () => {
|
||||
return HttpResponse.json(makeAuthResponse());
|
||||
}),
|
||||
|
||||
http.post(`${API_BASE}/api/v1/auth/revoke`, () => {
|
||||
return new HttpResponse(null, { status: 204 });
|
||||
}),
|
||||
];
|
||||
@@ -0,0 +1,5 @@
|
||||
import { setupWorker } from 'msw/browser';
|
||||
import { handlers } from './index';
|
||||
|
||||
/** MSW worker for development in the browser. */
|
||||
export const worker = setupWorker(...handlers);
|
||||
@@ -0,0 +1,11 @@
|
||||
import { authHandlers } from './auth/handlers';
|
||||
import { userHandlers } from './users/handlers';
|
||||
import { setupHandlers } from './setup/handlers';
|
||||
|
||||
/** All default MSW handlers, composed from feature folders (Q3-B). */
|
||||
export const handlers = [...authHandlers, ...userHandlers, ...setupHandlers];
|
||||
|
||||
export { authHandlers } from './auth/handlers';
|
||||
export { userHandlers } from './users/handlers';
|
||||
export { setupHandlers } from './setup/handlers';
|
||||
export * from './auth/fixtures';
|
||||
@@ -0,0 +1,5 @@
|
||||
import { setupServer } from 'msw/node';
|
||||
import { handlers } from './index';
|
||||
|
||||
/** MSW server for the Node/jsdom test environment. */
|
||||
export const server = setupServer(...handlers);
|
||||
@@ -0,0 +1,10 @@
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import type { SetupStatus } from '@/api/types';
|
||||
import { API_BASE } from '../auth/fixtures';
|
||||
|
||||
/** Setup status mock (used by public bootstrap guards). */
|
||||
export const setupHandlers = [
|
||||
http.get(`${API_BASE}/Setup/status`, () =>
|
||||
HttpResponse.json<SetupStatus>({ initialized: true }),
|
||||
),
|
||||
];
|
||||
@@ -0,0 +1,17 @@
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import type { User } from '@/api/types';
|
||||
import { API_BASE, mockUser } from '../auth/fixtures';
|
||||
|
||||
const mockUsers: User[] = [
|
||||
mockUser,
|
||||
{
|
||||
id: '22222222-2222-2222-2222-222222222222',
|
||||
email: 'admin@example.com',
|
||||
name: 'Admin User',
|
||||
role: 'Administrator',
|
||||
isActive: true,
|
||||
},
|
||||
];
|
||||
|
||||
/** Placeholder user-management mocks; expanded in Unit 5. */
|
||||
export const userHandlers = [http.get(`${API_BASE}/Users`, () => HttpResponse.json(mockUsers))];
|
||||
Reference in New Issue
Block a user