|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | +import { promises as fs } from "node:fs"; |
| 3 | + |
| 4 | +const ADMIN_USERNAME = process.env.E2E_ADMIN_USERNAME ?? "admin"; |
| 5 | +const ADMIN_PASSWORD = process.env.E2E_ADMIN_PASSWORD ?? "admin"; |
| 6 | + |
| 7 | +function toLocalOneShotUrl(issuedLink: string, baseURL: string): string { |
| 8 | + const parsed = new URL(issuedLink); |
| 9 | + const token = new URLSearchParams(parsed.hash.replace(/^#/, "")).get("token"); |
| 10 | + if (!token) { |
| 11 | + throw new Error(`Missing token in generated link: ${issuedLink}`); |
| 12 | + } |
| 13 | + return new URL(`/oneshot#token=${token}`, baseURL).toString(); |
| 14 | +} |
| 15 | + |
| 16 | +async function loginAsAdmin(page: import("@playwright/test").Page): Promise<void> { |
| 17 | + await page.goto("/login"); |
| 18 | + await page.getByTestId("login-username-input").fill(ADMIN_USERNAME); |
| 19 | + await page.getByTestId("login-password-input").fill(ADMIN_PASSWORD); |
| 20 | + await page.getByTestId("login-password-btn").click(); |
| 21 | + await expect(page).toHaveURL(/\/dashboard/, { timeout: 20_000 }); |
| 22 | + await page.goto("/admin"); |
| 23 | + await expect(page.getByRole("heading", { name: "Admin Panel" })).toBeVisible(); |
| 24 | +} |
| 25 | + |
| 26 | +async function generateOneShotLink( |
| 27 | + page: import("@playwright/test").Page, |
| 28 | + baseURL: string, |
| 29 | +): Promise<string> { |
| 30 | + await page.getByTestId("admin-generate-link").click(); |
| 31 | + const linkInput = page.getByTestId("admin-generated-link-input"); |
| 32 | + await expect(linkInput).toHaveValue(/#token=/, { timeout: 10_000 }); |
| 33 | + const issuedLink = await linkInput.inputValue(); |
| 34 | + return toLocalOneShotUrl(issuedLink, baseURL); |
| 35 | +} |
| 36 | + |
| 37 | +test.describe("OneShot E2E lifecycle", () => { |
| 38 | + test("full lifecycle: generate link, external upload, admin audit + download", async ({ |
| 39 | + page, |
| 40 | + browser, |
| 41 | + baseURL, |
| 42 | + }) => { |
| 43 | + test.skip(!baseURL, "Playwright baseURL is required for URL normalization."); |
| 44 | + await loginAsAdmin(page); |
| 45 | + const oneShotUrl = await generateOneShotLink(page, baseURL!); |
| 46 | + |
| 47 | + const fileName = "oneshot-e2e-upload.txt"; |
| 48 | + const fileContent = "oneshot e2e dummy payload"; |
| 49 | + |
| 50 | + const externalContext = await browser.newContext(); |
| 51 | + try { |
| 52 | + const externalPage = await externalContext.newPage(); |
| 53 | + await externalPage.goto(oneShotUrl); |
| 54 | + await expect(externalPage.getByLabel("Upload File")).toBeVisible(); |
| 55 | + await externalPage.setInputFiles('input[type="file"]', { |
| 56 | + name: fileName, |
| 57 | + mimeType: "text/plain", |
| 58 | + buffer: Buffer.from(fileContent, "utf8"), |
| 59 | + }); |
| 60 | + await externalPage.getByRole("button", { name: "Upload" }).click(); |
| 61 | + await expect(externalPage.getByText("Upload complete")).toBeVisible(); |
| 62 | + } finally { |
| 63 | + await externalContext.close(); |
| 64 | + } |
| 65 | + |
| 66 | + await page.reload(); |
| 67 | + await page.getByTestId("admin-audit-logs-tab").click(); |
| 68 | + const fileRow = page.locator("tr", { hasText: fileName }).first(); |
| 69 | + await expect(fileRow).toBeVisible({ timeout: 20_000 }); |
| 70 | + |
| 71 | + const downloadPromise = page.waitForEvent("download"); |
| 72 | + await fileRow.getByRole("button", { name: "Download" }).click(); |
| 73 | + const download = await downloadPromise; |
| 74 | + expect(download.suggestedFilename()).toBe(fileName); |
| 75 | + |
| 76 | + const outPath = test.info().outputPath(fileName); |
| 77 | + await download.saveAs(outPath); |
| 78 | + const downloaded = await fs.readFile(outPath, "utf8"); |
| 79 | + expect(downloaded).toBe(fileContent); |
| 80 | + }); |
| 81 | + |
| 82 | + test("ephemerality lockout: reused link shows expired state and blocks file selection", async ({ |
| 83 | + page, |
| 84 | + browser, |
| 85 | + baseURL, |
| 86 | + }) => { |
| 87 | + test.skip(!baseURL, "Playwright baseURL is required for URL normalization."); |
| 88 | + await loginAsAdmin(page); |
| 89 | + const oneShotUrl = await generateOneShotLink(page, baseURL!); |
| 90 | + |
| 91 | + const firstContext = await browser.newContext(); |
| 92 | + try { |
| 93 | + const firstPage = await firstContext.newPage(); |
| 94 | + await firstPage.goto(oneShotUrl); |
| 95 | + await expect(firstPage.getByLabel("Upload File")).toBeVisible(); |
| 96 | + await firstPage.setInputFiles('input[type="file"]', { |
| 97 | + name: "oneshot-first-use.txt", |
| 98 | + mimeType: "text/plain", |
| 99 | + buffer: Buffer.from("first upload", "utf8"), |
| 100 | + }); |
| 101 | + await firstPage.getByRole("button", { name: "Upload" }).click(); |
| 102 | + await expect(firstPage.getByText("Upload complete")).toBeVisible(); |
| 103 | + } finally { |
| 104 | + await firstContext.close(); |
| 105 | + } |
| 106 | + |
| 107 | + const secondContext = await browser.newContext(); |
| 108 | + try { |
| 109 | + const secondPage = await secondContext.newPage(); |
| 110 | + await secondPage.goto(oneShotUrl); |
| 111 | + await expect(secondPage.getByText("Link Expired or Invalid")).toBeVisible({ |
| 112 | + timeout: 15_000, |
| 113 | + }); |
| 114 | + await expect(secondPage.getByLabel("Upload File")).toBeDisabled(); |
| 115 | + } finally { |
| 116 | + await secondContext.close(); |
| 117 | + } |
| 118 | + }); |
| 119 | +}); |
0 commit comments