import { test, expect } from "@playwright/test" test.describe("Authentication System", () => { test("Redirect to login when not authenticated", async ({ page }) => { // Try to access admin page without login await page.goto("http://localhost:5173/admin/dashboard") // Should redirect to login await expect(page).toHaveURL(/login/) console.log("✅ Not authenticated users redirected to login") }) test("Login with valid credentials", async ({ page }) => { // Go to login page await page.goto("http://localhost:5173/login") // Fill login form await page.fill("input[type='text']", "admin") await page.fill("input[type='password']", "password") // Submit await page.click("button[type='submit']") // Should redirect to dashboard await page.waitForURL(/admin\/dashboard/, { timeout: 5000 }) console.log("✅ Login successful") // Check if dashboard loads const dashboard = await page.locator("h1, .dashboard-title").first().isVisible().catch(() => false) console.log("✅ Dashboard visible after login") }) test("Access admin pages after login", async ({ page }) => { // Login first await page.goto("http://localhost:5173/login") await page.fill("input[type='text']", "admin") await page.fill("input[type='password']", "password") await page.click("button[type='submit']") await page.waitForURL(/admin\/dashboard/) // Check sidebar is visible const sidebar = await page.locator(".sidebar").isVisible() expect(sidebar).toBeTruthy() console.log("✅ Sidebar visible") // Check user info in sidebar const userInfo = await page.locator(".user-info").isVisible() expect(userInfo).toBeTruthy() console.log("✅ User info displayed in sidebar") // Check logout button const logoutBtn = await page.locator(".logout-btn").isVisible() expect(logoutBtn).toBeTruthy() console.log("✅ Logout button visible") // Navigate to Orders page await page.click("text=Orders") await page.waitForURL(/admin\/orders/) console.log("✅ Can navigate to Orders page") // Navigate to Products page await page.click("text=Products") await page.waitForURL(/admin\/products/) console.log("✅ Can navigate to Products page") }) test("Logout functionality", async ({ page }) => { // Login first await page.goto("http://localhost:5173/login") await page.fill("input[type='text']", "admin") await page.fill("input[type='password']", "password") await page.click("button[type='submit']") await page.waitForURL(/admin\/dashboard/) // Register dialog handler BEFORE clicking logout (timing-critical) page.once("dialog", async (dialog) => { console.log(`Dialog: ${dialog.message()}`) await dialog.accept() }) // Click logout await page.click(".logout-btn") // Should redirect to login await page.waitForURL(/login/, { timeout: 5000 }) console.log("✅ Logout successful") }) test("Session persistence", async ({ page, context }) => { // Login await page.goto("http://localhost:5173/login") await page.fill("input[type='text']", "admin") await page.fill("input[type='password']", "password") await page.click("button[type='submit']") await page.waitForURL(/admin\/dashboard/) // Check localStorage const token = await page.evaluate(() => localStorage.getItem("auth-token")) expect(token).toBeTruthy() console.log("✅ Auth token stored in localStorage") // Open new page in same context (shares storage) const page2 = await context.newPage() await page2.goto("http://localhost:5173/admin/dashboard") // Should be authenticated await expect(page2).toHaveURL(/admin\/dashboard/) console.log("✅ Session persists across pages") await page2.close() }) })