|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | +import { TestHelpers } from "../utils/test-helpers"; |
| 3 | + |
| 4 | +test.describe("Admin", () => { |
| 5 | + let testHelpers: TestHelpers; |
| 6 | + |
| 7 | + test.beforeEach(async ({ page }) => { |
| 8 | + testHelpers = new TestHelpers(page); |
| 9 | + }); |
| 10 | + |
| 11 | + test("should display admin dashboard", async ({ page }) => { |
| 12 | + await testHelpers.navigateTo("/admin"); |
| 13 | + |
| 14 | + // Should show admin content or redirect to login |
| 15 | + const adminContent = page |
| 16 | + .getByRole("main") |
| 17 | + .or(page.locator('[data-testid*="admin"]')); |
| 18 | + |
| 19 | + const loginPrompt = page.getByText(/sign in|login|unauthorized/i); |
| 20 | + |
| 21 | + // Either show admin content or login prompt |
| 22 | + const hasValidResponse = await Promise.race([ |
| 23 | + adminContent.isVisible(), |
| 24 | + loginPrompt.isVisible(), |
| 25 | + ]); |
| 26 | + |
| 27 | + expect(hasValidResponse).toBe(true); |
| 28 | + }); |
| 29 | + |
| 30 | + test("should show admin navigation when authorized", async ({ page }) => { |
| 31 | + await testHelpers.navigateTo("/admin"); |
| 32 | + |
| 33 | + // Look for admin navigation elements |
| 34 | + const adminNav = page |
| 35 | + .getByRole("navigation") |
| 36 | + .or(page.locator('[data-testid*="admin-nav"]')) |
| 37 | + .or(page.locator(".admin-nav, .sidebar")); |
| 38 | + |
| 39 | + if (await adminNav.isVisible()) { |
| 40 | + await expect(adminNav).toBeVisible(); |
| 41 | + |
| 42 | + // Should have admin-specific links |
| 43 | + const adminLinks = adminNav.getByRole("link", { |
| 44 | + name: /dashboard|artists|festivals|analytics|roles/i, |
| 45 | + }); |
| 46 | + if (await adminLinks.first().isVisible()) { |
| 47 | + await expect(adminLinks.first()).toBeVisible(); |
| 48 | + } |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + test("should navigate to artists management", async ({ page }) => { |
| 53 | + await testHelpers.navigateTo("/admin/artists"); |
| 54 | + |
| 55 | + // Should show artists management or auth prompt |
| 56 | + const artistsManagement = page.getByRole("main"); |
| 57 | + await expect(artistsManagement).toBeVisible(); |
| 58 | + |
| 59 | + // If authorized, should show artists table or management interface |
| 60 | + const artistsTable = page |
| 61 | + .getByRole("table") |
| 62 | + .or(page.locator('[data-testid*="artists"]')); |
| 63 | + |
| 64 | + if (await artistsTable.isVisible()) { |
| 65 | + await expect(artistsTable).toBeVisible(); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + test("should navigate to festivals management", async ({ page }) => { |
| 70 | + await testHelpers.navigateTo("/admin/festivals"); |
| 71 | + |
| 72 | + const festivalsContent = page.getByRole("main"); |
| 73 | + await expect(festivalsContent).toBeVisible(); |
| 74 | + |
| 75 | + // Look for festivals management interface |
| 76 | + const festivalsTable = page |
| 77 | + .getByRole("table") |
| 78 | + .or(page.locator('[data-testid*="festivals"]')); |
| 79 | + |
| 80 | + if (await festivalsTable.isVisible()) { |
| 81 | + await expect(festivalsTable).toBeVisible(); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + test("should navigate to analytics", async ({ page }) => { |
| 86 | + await testHelpers.navigateTo("/admin/analytics"); |
| 87 | + |
| 88 | + const analyticsContent = page.getByRole("main"); |
| 89 | + await expect(analyticsContent).toBeVisible(); |
| 90 | + |
| 91 | + // Look for analytics dashboard elements |
| 92 | + const analyticsElements = page |
| 93 | + .locator('[data-testid*="analytics"]') |
| 94 | + .or(page.locator(".chart, .metric, .stats")); |
| 95 | + |
| 96 | + if (await analyticsElements.first().isVisible()) { |
| 97 | + await expect(analyticsElements.first()).toBeVisible(); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + test("should navigate to admin roles", async ({ page }) => { |
| 102 | + await testHelpers.navigateTo("/admin/admins"); |
| 103 | + |
| 104 | + const rolesContent = page.getByRole("main"); |
| 105 | + await expect(rolesContent).toBeVisible(); |
| 106 | + |
| 107 | + // Look for roles management interface |
| 108 | + const rolesTable = page |
| 109 | + .getByRole("table") |
| 110 | + .or(page.locator('[data-testid*="roles"], [data-testid*="admins"]')); |
| 111 | + |
| 112 | + if (await rolesTable.isVisible()) { |
| 113 | + await expect(rolesTable).toBeVisible(); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + test("should show add artist functionality", async ({ page }) => { |
| 118 | + await testHelpers.navigateTo("/admin/artists"); |
| 119 | + |
| 120 | + // Look for add artist button |
| 121 | + const addButton = page |
| 122 | + .getByRole("button", { name: /add artist|new artist|create/i }) |
| 123 | + .or(page.locator('[data-testid*="add-artist"]')); |
| 124 | + |
| 125 | + if (await addButton.isVisible()) { |
| 126 | + await expect(addButton).toBeVisible(); |
| 127 | + |
| 128 | + // Click to open dialog |
| 129 | + await addButton.click(); |
| 130 | + |
| 131 | + // Should show add artist dialog |
| 132 | + const dialog = page.getByRole("dialog"); |
| 133 | + if (await dialog.isVisible()) { |
| 134 | + await expect(dialog).toBeVisible(); |
| 135 | + |
| 136 | + // Should have artist name input |
| 137 | + const nameInput = page |
| 138 | + .getByLabel(/name|title/i) |
| 139 | + .or(page.getByPlaceholder(/artist name/i)); |
| 140 | + |
| 141 | + if (await nameInput.isVisible()) { |
| 142 | + await expect(nameInput).toBeVisible(); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + test("should show edit artist functionality", async ({ page }) => { |
| 149 | + await testHelpers.navigateTo("/admin/artists"); |
| 150 | + |
| 151 | + // Look for edit buttons in artists table |
| 152 | + const editButtons = page |
| 153 | + .getByRole("button", { name: /edit/i }) |
| 154 | + .or(page.locator('[data-testid*="edit"]')); |
| 155 | + |
| 156 | + if (await editButtons.first().isVisible()) { |
| 157 | + await expect(editButtons.first()).toBeVisible(); |
| 158 | + |
| 159 | + // Click edit button |
| 160 | + await editButtons.first().click(); |
| 161 | + |
| 162 | + // Should show edit dialog |
| 163 | + const dialog = page.getByRole("dialog"); |
| 164 | + if (await dialog.isVisible()) { |
| 165 | + await expect(dialog).toBeVisible(); |
| 166 | + } |
| 167 | + } |
| 168 | + }); |
| 169 | + |
| 170 | + test("should show festival creation functionality", async ({ page }) => { |
| 171 | + await testHelpers.navigateTo("/admin/festivals"); |
| 172 | + |
| 173 | + // Look for create festival button |
| 174 | + const createButton = page |
| 175 | + .getByRole("button", { name: /add festival|new festival|create/i }) |
| 176 | + .or(page.locator('[data-testid*="add-festival"]')); |
| 177 | + |
| 178 | + if (await createButton.isVisible()) { |
| 179 | + await expect(createButton).toBeVisible(); |
| 180 | + |
| 181 | + await createButton.click(); |
| 182 | + |
| 183 | + // Should show festival creation dialog |
| 184 | + const dialog = page.getByRole("dialog"); |
| 185 | + if (await dialog.isVisible()) { |
| 186 | + await expect(dialog).toBeVisible(); |
| 187 | + } |
| 188 | + } |
| 189 | + }); |
| 190 | + |
| 191 | + test("should show CSV import functionality", async ({ page }) => { |
| 192 | + await testHelpers.navigateTo("/admin/artists"); |
| 193 | + |
| 194 | + // Look for import/CSV functionality |
| 195 | + const importButton = page |
| 196 | + .getByRole("button", { name: /import|csv|upload/i }) |
| 197 | + .or(page.locator('[data-testid*="import"]')); |
| 198 | + |
| 199 | + if (await importButton.isVisible()) { |
| 200 | + await expect(importButton).toBeVisible(); |
| 201 | + |
| 202 | + await importButton.click(); |
| 203 | + |
| 204 | + // Should show import dialog |
| 205 | + const dialog = page.getByRole("dialog"); |
| 206 | + if (await dialog.isVisible()) { |
| 207 | + await expect(dialog).toBeVisible(); |
| 208 | + |
| 209 | + // Should have file input |
| 210 | + const fileInput = page.locator('input[type="file"]'); |
| 211 | + if (await fileInput.isVisible()) { |
| 212 | + await expect(fileInput).toBeVisible(); |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + }); |
| 217 | + |
| 218 | + test("should handle unauthorized access gracefully", async ({ page }) => { |
| 219 | + // Test accessing admin without proper permissions |
| 220 | + await testHelpers.navigateTo("/admin"); |
| 221 | + |
| 222 | + const isAuth = await testHelpers.isAuthenticated(); |
| 223 | + if (!isAuth) { |
| 224 | + // Should redirect to login or show unauthorized message |
| 225 | + const unauthorizedContent = page |
| 226 | + .getByText(/sign in|login|unauthorized|access denied/i) |
| 227 | + .or(page.getByRole("button", { name: /sign in/i })); |
| 228 | + |
| 229 | + const hasAuthPrompt = await unauthorizedContent.isVisible(); |
| 230 | + expect(hasAuthPrompt).toBe(true); |
| 231 | + } |
| 232 | + }); |
| 233 | + |
| 234 | + test("should show data tables with proper headers", async ({ page }) => { |
| 235 | + await testHelpers.navigateTo("/admin/artists"); |
| 236 | + |
| 237 | + const table = page.getByRole("table"); |
| 238 | + |
| 239 | + if (await table.isVisible()) { |
| 240 | + await expect(table).toBeVisible(); |
| 241 | + |
| 242 | + // Should have table headers |
| 243 | + const headers = table.locator('thead th, [role="columnheader"]'); |
| 244 | + if (await headers.first().isVisible()) { |
| 245 | + const headerCount = await headers.count(); |
| 246 | + expect(headerCount).toBeGreaterThan(0); |
| 247 | + } |
| 248 | + } |
| 249 | + }); |
| 250 | + |
| 251 | + test("should support search/filter in admin tables", async ({ page }) => { |
| 252 | + await testHelpers.navigateTo("/admin/artists"); |
| 253 | + |
| 254 | + // Look for search functionality |
| 255 | + const searchInput = page |
| 256 | + .getByPlaceholder(/search/i) |
| 257 | + .or(page.getByLabel(/search/i)); |
| 258 | + |
| 259 | + if (await searchInput.isVisible()) { |
| 260 | + await expect(searchInput).toBeVisible(); |
| 261 | + |
| 262 | + // Try searching |
| 263 | + await searchInput.fill("test"); |
| 264 | + await testHelpers.waitForPageLoad(); |
| 265 | + |
| 266 | + // Table should still be visible |
| 267 | + const table = page.getByRole("table"); |
| 268 | + if (await table.isVisible()) { |
| 269 | + await expect(table).toBeVisible(); |
| 270 | + } |
| 271 | + } |
| 272 | + }); |
| 273 | +}); |
0 commit comments