website refactor
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import { describe, test, beforeAll, afterAll } from 'vitest';
|
||||
import { routes } from '../../../apps/website/lib/routing/RouteConfig';
|
||||
import { WebsiteServerHarness } from '../harness/WebsiteServerHarness';
|
||||
import { ApiServerHarness } from '../harness/ApiServerHarness';
|
||||
import { HttpDiagnostics } from '../../shared/website/HttpDiagnostics';
|
||||
|
||||
const WEBSITE_BASE_URL = process.env.WEBSITE_BASE_URL || 'http://localhost:3000';
|
||||
const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:3101';
|
||||
const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:3001';
|
||||
|
||||
type AuthRole = 'unauth' | 'auth' | 'admin' | 'sponsor';
|
||||
|
||||
@@ -18,6 +19,7 @@ async function loginViaApi(role: AuthRole): Promise<string | null> {
|
||||
}[role];
|
||||
|
||||
try {
|
||||
console.log(`[RouteProtection] Attempting login for role ${role} at ${API_BASE_URL}/auth/login`);
|
||||
const res = await fetch(`${API_BASE_URL}/auth/login`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -25,38 +27,71 @@ async function loginViaApi(role: AuthRole): Promise<string | null> {
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
console.warn(`Login failed for role ${role}: ${res.status} ${res.statusText}`);
|
||||
console.warn(`[RouteProtection] Login failed for role ${role}: ${res.status} ${res.statusText}`);
|
||||
const body = await res.text();
|
||||
console.warn(`[RouteProtection] Login failure body: ${body}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const setCookie = res.headers.get('set-cookie') ?? '';
|
||||
console.log(`[RouteProtection] Login success. set-cookie: ${setCookie}`);
|
||||
const cookiePart = setCookie.split(';')[0] ?? '';
|
||||
return cookiePart.startsWith('gp_session=') ? cookiePart : null;
|
||||
} catch (e) {
|
||||
console.warn(`Could not connect to API at ${API_BASE_URL} for role ${role} login.`);
|
||||
console.warn(`[RouteProtection] Could not connect to API at ${API_BASE_URL} for role ${role} login: ${e.message}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
describe('Route Protection Matrix', () => {
|
||||
let harness: WebsiteServerHarness | null = null;
|
||||
let websiteHarness: WebsiteServerHarness | null = null;
|
||||
let apiHarness: ApiServerHarness | null = null;
|
||||
|
||||
beforeAll(async () => {
|
||||
if (WEBSITE_BASE_URL.includes('localhost')) {
|
||||
console.log(`[RouteProtection] beforeAll starting. WEBSITE_BASE_URL=${WEBSITE_BASE_URL}, API_BASE_URL=${API_BASE_URL}`);
|
||||
|
||||
// 1. Ensure API is running
|
||||
if (API_BASE_URL.includes('localhost')) {
|
||||
try {
|
||||
await fetch(WEBSITE_BASE_URL, { method: 'HEAD' });
|
||||
await fetch(`${API_BASE_URL}/health`);
|
||||
console.log(`[RouteProtection] API already running at ${API_BASE_URL}`);
|
||||
} catch (e) {
|
||||
harness = new WebsiteServerHarness({
|
||||
port: parseInt(new URL(WEBSITE_BASE_URL).port) || 3000,
|
||||
console.log(`[RouteProtection] Starting API server harness on ${API_BASE_URL}...`);
|
||||
apiHarness = new ApiServerHarness({
|
||||
port: parseInt(new URL(API_BASE_URL).port) || 3001,
|
||||
});
|
||||
await harness.start();
|
||||
await apiHarness.start();
|
||||
console.log(`[RouteProtection] API Harness started.`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 2. Ensure Website is running
|
||||
if (WEBSITE_BASE_URL.includes('localhost')) {
|
||||
try {
|
||||
console.log(`[RouteProtection] Checking if website is already running at ${WEBSITE_BASE_URL}`);
|
||||
await fetch(WEBSITE_BASE_URL, { method: 'HEAD' });
|
||||
console.log(`[RouteProtection] Website already running.`);
|
||||
} catch (e) {
|
||||
console.log(`[RouteProtection] Website not running, starting harness...`);
|
||||
websiteHarness = new WebsiteServerHarness({
|
||||
port: parseInt(new URL(WEBSITE_BASE_URL).port) || 3000,
|
||||
env: {
|
||||
API_BASE_URL: API_BASE_URL,
|
||||
NEXT_PUBLIC_API_BASE_URL: API_BASE_URL,
|
||||
},
|
||||
});
|
||||
await websiteHarness.start();
|
||||
console.log(`[RouteProtection] Website Harness started.`);
|
||||
}
|
||||
}
|
||||
}, 120000);
|
||||
|
||||
afterAll(async () => {
|
||||
if (harness) {
|
||||
await harness.stop();
|
||||
if (websiteHarness) {
|
||||
await websiteHarness.stop();
|
||||
}
|
||||
if (apiHarness) {
|
||||
await apiHarness.stop();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -73,19 +108,19 @@ describe('Route Protection Matrix', () => {
|
||||
{ role: 'unauth', path: routes.sponsor.dashboard, expectedStatus: [302, 307], expectedRedirect: routes.auth.login },
|
||||
|
||||
// Authenticated (Driver)
|
||||
{ role: 'auth', path: routes.public.home, expectedStatus: 200 },
|
||||
{ role: 'auth', path: routes.public.home, expectedStatus: [302, 307], expectedRedirect: routes.protected.dashboard },
|
||||
{ role: 'auth', path: routes.protected.dashboard, expectedStatus: 200 },
|
||||
{ role: 'auth', path: routes.admin.root, expectedStatus: [302, 307], expectedRedirect: routes.protected.dashboard },
|
||||
{ role: 'auth', path: routes.sponsor.dashboard, expectedStatus: [302, 307], expectedRedirect: routes.protected.dashboard },
|
||||
|
||||
// Admin
|
||||
{ role: 'admin', path: routes.public.home, expectedStatus: 200 },
|
||||
{ role: 'admin', path: routes.public.home, expectedStatus: [302, 307], expectedRedirect: routes.protected.dashboard },
|
||||
{ role: 'admin', path: routes.protected.dashboard, expectedStatus: 200 },
|
||||
{ role: 'admin', path: routes.admin.root, expectedStatus: 200 },
|
||||
{ role: 'admin', path: routes.sponsor.dashboard, expectedStatus: [302, 307], expectedRedirect: routes.admin.root },
|
||||
|
||||
// Sponsor
|
||||
{ role: 'sponsor', path: routes.public.home, expectedStatus: 200 },
|
||||
{ role: 'sponsor', path: routes.public.home, expectedStatus: [302, 307], expectedRedirect: routes.protected.dashboard },
|
||||
{ role: 'sponsor', path: routes.protected.dashboard, expectedStatus: 200 },
|
||||
{ role: 'sponsor', path: routes.admin.root, expectedStatus: [302, 307], expectedRedirect: routes.sponsor.dashboard },
|
||||
{ role: 'sponsor', path: routes.sponsor.dashboard, expectedStatus: 200 },
|
||||
@@ -123,7 +158,7 @@ describe('Route Protection Matrix', () => {
|
||||
status,
|
||||
location,
|
||||
html,
|
||||
serverLogs: harness?.getLogTail(60),
|
||||
serverLogs: websiteHarness?.getLogTail(60),
|
||||
};
|
||||
|
||||
const formatFailure = (extra: string) => HttpDiagnostics.formatHttpFailure({ ...failureContext, extra });
|
||||
|
||||
Reference in New Issue
Block a user