Files
kokken/availability-playwright.spec.ts
Alex Polo e6a83da2ab Refactor API calls to use apiFetch for consistency and improved cookie handling
- Updated availability admin page to use apiFetch for fetching and modifying blocked dates.
- Enhanced menu generator page to ensure all expected arrays are initialized to avoid rendering issues.
- Added path to cookies in login and logout routes for better cookie management.
- Improved availability blocked API to log request headers and handle both date and id for deletion.
- Updated menu generator API to include tags in the response structure and ensure expected arrays are present.
- Enhanced middleware to handle authentication more robustly, including detailed logging and handling of various request types.
- Added Playwright test to verify that toggling availability does not log out the admin user.
- Created apiFetch utility to standardize API requests with credentials included.
2025-11-07 15:50:22 +01:00

71 lines
2.5 KiB
TypeScript

import { test, expect } from '@playwright/test'
test('availability toggle should not log out the admin (with network capture)', async ({ page }) => {
const networkLogs: string[] = []
page.on('request', req => {
const url = req.url()
if (url.includes('/api/availability/blocked') || url.includes('/_next/image') || url.includes('/_next/data')) {
networkLogs.push(`REQ -> ${req.method()} ${url} | headers: ${JSON.stringify(req.headers())}`)
}
})
page.on('response', async res => {
const url = res.url()
if (url.includes('/api/availability/blocked') || url.includes('/_next/image') || url.includes('/_next/data')) {
let body = ''
try {
body = await res.text()
} catch (e) {
body = `<could not read body: ${e}>`
}
networkLogs.push(`RES <- ${res.status()} ${url} | headers: ${JSON.stringify(res.headers())} | body: ${body.substring(0, 200)}`)
}
})
// Go to login and sign in
await page.goto('/login')
await page.fill('#username', 'brotha')
await page.fill('#password', 'makefood1')
await Promise.all([
page.waitForNavigation({ url: '**/admin', waitUntil: 'load' }),
page.click('button:has-text("Log ind")'),
])
// Confirm cookie set
const cookiesBefore = await page.context().cookies()
const hasAuthBefore = cookiesBefore.some(c => c.name === 'authToken')
expect(hasAuthBefore).toBeTruthy()
// Navigate to availability
await page.goto('/admin/availability')
await expect(page).toHaveURL(/\/admin\/availability/)
// Try to toggle: prefer existing "Åbn dag" button if present (safer), otherwise click first available day in calendar
const openButton = page.locator('button:has-text("Åbn dag")').first()
if (await openButton.count() > 0) {
await openButton.click()
} else {
// Fallback: click first clickable day in the calendar
const dayButton = page.locator('main button:not([disabled])').first()
await dayButton.click()
}
// Wait for any network activity to settle
await page.waitForTimeout(1000)
// Dump network logs for debugging
console.log('\n--- NETWORK LOGS ---')
for (const l of networkLogs) console.log(l)
console.log('--- END NETWORK LOGS ---\n')
// Ensure we are still on the admin availability page (not redirected to /login)
const current = page.url()
expect(current).toContain('/admin/availability')
// Confirm auth cookie still exists after the toggle
const cookiesAfter = await page.context().cookies()
const hasAuthAfter = cookiesAfter.some(c => c.name === 'authToken')
expect(hasAuthAfter).toBeTruthy()
})