middleware fix wip

This commit is contained in:
2026-01-04 12:49:30 +01:00
parent 729d95cd73
commit 691e6e2c7e
10 changed files with 741 additions and 152 deletions

View File

@@ -0,0 +1,46 @@
import { test, expect } from '@playwright/test';
import { WebsiteRouteManager } from '../../shared/website/WebsiteRouteManager';
const WEBSITE_BASE_URL = process.env.PLAYWRIGHT_BASE_URL || 'http://localhost:3000';
test.describe('Debug Public Routes', () => {
let routeManager: WebsiteRouteManager;
test.beforeEach(() => {
routeManager = new WebsiteRouteManager();
});
test('debug public routes', async ({ page }) => {
const routes = routeManager.getWebsiteRouteInventory();
const publicRoutes = routes.filter(r => r.access === 'public').slice(0, 5);
console.log('Testing public routes:', publicRoutes);
for (const route of publicRoutes) {
const path = routeManager.resolvePathTemplate(route.pathTemplate, route.params);
const fullUrl = `${WEBSITE_BASE_URL}${path}`;
console.log(`\nTesting route: ${route.pathTemplate} -> ${path}`);
const response = await page.goto(fullUrl);
const status = response?.status();
const ok = response?.ok();
console.log(` URL: ${fullUrl}`);
console.log(` Status: ${status}`);
console.log(` OK: ${ok}`);
console.log(` Current URL: ${page.url()}`);
// Should load successfully or show 404 page
const passes = ok || status === 404;
console.log(` Passes: ${passes}`);
if (!passes) {
console.log(` ❌ FAILED: ${path} returned status ${status}`);
} else {
console.log(` ✅ PASSED: ${path}`);
}
}
});
});

View File

@@ -88,7 +88,10 @@ test.describe('Website Pages - TypeORM Integration', () => {
{
const auth = await WebsiteAuthManager.createAuthContext(browser, request, 'auth');
await auth.page.goto(`${WEBSITE_BASE_URL}${path}`);
expect(auth.page.url().includes('login')).toBeTruthy();
const finalUrl = auth.page.url();
console.log(`[DEBUG] Final URL: ${finalUrl}`);
console.log(`[DEBUG] Includes 'login': ${finalUrl.includes('login')}`);
expect(finalUrl.includes('login')).toBeTruthy();
await auth.context.close();
}

View File

@@ -27,34 +27,50 @@ export class WebsiteAuthManager {
const role = (typeof requestOrRole === 'string' ? requestOrRole : maybeRole) as AuthRole;
const request = typeof requestOrRole === 'string' ? null : requestOrRole;
const context = await browser.newContext({ baseURL });
if (request) {
const token = await WebsiteAuthManager.loginViaApi(request, apiBaseUrl, role);
// Critical: the website must receive `gp_session` so middleware can forward it.
// Playwright runs in its own container and accesses website via PLAYWRIGHT_BASE_URL
// The cookie domain must match the hostname in the URL that Playwright uses
const url = new URL(baseURL);
const domain = url.hostname; // "website" in Docker, "localhost" locally
// If using API login, create context with cookies pre-set
if (typeof requestOrRole !== 'string') {
const token = await WebsiteAuthManager.loginViaApi(requestOrRole, apiBaseUrl, role);
await context.addCookies([
{
name: 'gp_session',
value: token,
domain: domain,
path: '/',
httpOnly: true,
sameSite: 'Lax',
console.log(`[WebsiteAuthManager] Creating context with pre-set cookie, baseURL: ${baseURL}, token length: ${token.length}`);
// Create context with storage state that includes the cookie
// This is more reliable than adding cookies after context creation
const contextWithCookies = await browser.newContext({
baseURL,
storageState: {
cookies: [
{
name: 'gp_session',
value: token,
domain: new URL(baseURL).hostname,
path: '/',
expires: -1,
httpOnly: true,
secure: false,
sameSite: 'Lax',
},
],
origins: [],
},
]);
});
const page = await contextWithCookies.newPage();
// Verify cookies
const cookies = await contextWithCookies.cookies();
console.log(`[WebsiteAuthManager] Cookies in context:`, cookies);
return {
context: contextWithCookies,
page,
role,
};
}
// UI login path
const context = await browser.newContext({ baseURL });
const page = await context.newPage();
if (!request) {
await WebsiteAuthManager.loginViaUi(page, role);
}
await WebsiteAuthManager.loginViaUi(page, role);
return {
context,