198 lines
8.0 KiB
TypeScript
198 lines
8.0 KiB
TypeScript
import { test, expect } from "@playwright/test";
|
||
import { readFile } from "node:fs/promises";
|
||
import { signInAs, resetDb, BASE } from "./dev";
|
||
|
||
/**
|
||
* Event photography (J/K slice): a coordinator creates an event and assigns two
|
||
* photographers; the first creates a folder and uploads an edited JPEG plus a
|
||
* RAW original; the second — not the uploader — downloads the untouched bytes.
|
||
* An unassigned volunteer cannot open the event at all.
|
||
*/
|
||
|
||
// A real 32×24 JPEG, so the editor path sees a normal browser image.
|
||
const PHOTO = Buffer.from(
|
||
"/9j/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAAYACADASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAcICf/EABgQAQADAQAAAAAAAAAAAAAAAAAXZaTi/8QAFQEBAQAAAAAAAAAAAAAAAAAACAf/xAAbEQEAAQUBAAAAAAAAAAAAAAAAAQMEFFKh0f/aAAwDAQACEQMRAD8AuYAvrq5kinQtc5eyFrnL2XmDca9j0ccyhtyW6ABDI5mQA6BNf//Z",
|
||
"base64",
|
||
);
|
||
const PHOTO_NAME = "photo.jpg";
|
||
const RAW = Buffer.from("II*\u0000fake-cr3-bytes-for-download-verification");
|
||
|
||
let eventUrl = "";
|
||
|
||
test.describe("event photo sharing", () => {
|
||
test.beforeAll(async ({ browser }) => {
|
||
const context = await browser.newContext();
|
||
const page = await context.newPage();
|
||
await resetDb(page);
|
||
await context.close();
|
||
});
|
||
|
||
test("a coordinator creates an event and assigns two photographers", async ({
|
||
page,
|
||
}) => {
|
||
await page.setViewportSize({ width: 1200, height: 900 });
|
||
await signInAs(page, "admin@glenloch.local");
|
||
await page.goto(`${BASE}/events`);
|
||
|
||
await page.getByLabel("Title").fill("E2E photo day");
|
||
await page.getByLabel("Location").fill("Main campus");
|
||
await page.getByRole("button", { name: "Create event" }).click();
|
||
|
||
await expect(page).toHaveURL(/\/events\/[^/]+$/);
|
||
eventUrl = page.url().replace(/\/$/, "");
|
||
await expect(
|
||
page.getByRole("heading", { name: "E2E photo day" }),
|
||
).toBeVisible();
|
||
|
||
const teamMember = page.getByLabel("Team member");
|
||
await teamMember.selectOption({
|
||
label: "Avery Kim (avery@glenloch.local)",
|
||
});
|
||
await page.getByRole("button", { name: "Assign" }).click();
|
||
await expect(page.getByText("Avery Kim").first()).toBeVisible();
|
||
|
||
await teamMember.selectOption({ label: "Sam Okafor (sam@glenloch.local)" });
|
||
await page.getByRole("button", { name: "Assign" }).click();
|
||
await expect(page.getByText("Sam Okafor").first()).toBeVisible();
|
||
});
|
||
|
||
test("an assigned photographer creates a folder and uploads files", async ({
|
||
page,
|
||
}) => {
|
||
await page.setViewportSize({ width: 1200, height: 900 });
|
||
await signInAs(page, "avery@glenloch.local");
|
||
await page.goto(eventUrl);
|
||
|
||
await page.getByLabel("New folder").fill("Originals");
|
||
await page.getByRole("button", { name: "Create folder" }).click();
|
||
await page.getByRole("link", { name: /Originals/ }).click();
|
||
|
||
await page.getByLabel("Choose photos to upload").setInputFiles([
|
||
{ name: PHOTO_NAME, mimeType: "image/jpeg", buffer: PHOTO },
|
||
{ name: "raw.CR3", mimeType: "application/octet-stream", buffer: RAW },
|
||
]);
|
||
await page.getByRole("button", { name: "Upload 2" }).click();
|
||
|
||
await expect(page.getByText("2 uploaded")).toBeVisible({ timeout: 30_000 });
|
||
await expect(page.getByText(PHOTO_NAME)).toBeVisible();
|
||
await expect(page.getByText("raw.CR3")).toBeVisible();
|
||
});
|
||
|
||
test("another assigned photographer downloads the original bytes", async ({
|
||
page,
|
||
}) => {
|
||
await page.setViewportSize({ width: 1200, height: 900 });
|
||
await signInAs(page, "sam@glenloch.local");
|
||
await page.goto(eventUrl);
|
||
await page.getByRole("link", { name: /Originals/ }).click();
|
||
|
||
const [download] = await Promise.all([
|
||
page.waitForEvent("download"),
|
||
page.getByRole("link", { name: "Download" }).first().click(),
|
||
]);
|
||
const downloadPath = await download.path();
|
||
expect(downloadPath).toBeTruthy();
|
||
const bytes = await readFile(downloadPath!);
|
||
expect(bytes.equals(PHOTO)).toBe(true);
|
||
});
|
||
|
||
test("a photographer downloads every photo as one ZIP", async ({ page }) => {
|
||
await page.setViewportSize({ width: 1200, height: 900 });
|
||
await signInAs(page, "sam@glenloch.local");
|
||
await page.goto(eventUrl);
|
||
|
||
const [download] = await Promise.all([
|
||
page.waitForEvent("download"),
|
||
page.getByRole("link", { name: /Download all \(2\)/ }).click(),
|
||
]);
|
||
expect(download.suggestedFilename()).toBe("E2E photo day photos.zip");
|
||
const downloadPath = await download.path();
|
||
expect(downloadPath).toBeTruthy();
|
||
|
||
const zip = await readFile(downloadPath!);
|
||
expect(zip.subarray(0, 4).toString("binary")).toBe("PK\x03\x04");
|
||
// Store-only entries keep the original bytes and folder paths in the ZIP.
|
||
expect(zip.includes(PHOTO)).toBe(true);
|
||
expect(zip.includes(RAW)).toBe(true);
|
||
expect(zip.includes(Buffer.from(`Originals/${PHOTO_NAME}`))).toBe(true);
|
||
expect(zip.includes(Buffer.from("Originals/raw.CR3"))).toBe(true);
|
||
});
|
||
|
||
test("a photographer views a photo and saves a full-resolution edited JPEG", async ({
|
||
page,
|
||
}) => {
|
||
await page.setViewportSize({ width: 1200, height: 900 });
|
||
await signInAs(page, "sam@glenloch.local");
|
||
await page.goto(eventUrl);
|
||
await page.getByRole("link", { name: /Originals/ }).click();
|
||
|
||
await page
|
||
.getByRole("button", { name: `View and edit ${PHOTO_NAME}` })
|
||
.click();
|
||
await expect(page.getByRole("dialog")).toBeVisible();
|
||
await expect(page.getByRole("heading", { name: "Edit photo" })).toBeVisible();
|
||
|
||
await page.getByLabel("Exposure").focus();
|
||
await page.getByLabel("Exposure").press("ArrowRight");
|
||
await page.getByRole("button", { name: "Save edited JPEG" }).click();
|
||
await expect(page.getByText("Full-resolution JPEG saved.")).toBeVisible({
|
||
timeout: 30_000,
|
||
});
|
||
|
||
const [download] = await Promise.all([
|
||
page.waitForEvent("download"),
|
||
page.getByRole("link", { name: "Download edited JPEG" }).click(),
|
||
]);
|
||
expect(download.suggestedFilename()).toBe("photo-edited.jpg");
|
||
const edited = await readFile((await download.path())!);
|
||
expect(edited.subarray(0, 3).toString("hex")).toBe("ffd8ff");
|
||
expect(edited.equals(PHOTO)).toBe(false);
|
||
|
||
await page.getByRole("button", { name: "Close photo editor" }).click();
|
||
await expect(page.getByText("Edited", { exact: true })).toBeVisible();
|
||
await expect(page.locator(`img[alt="${PHOTO_NAME}"]`)).toHaveAttribute(
|
||
"src",
|
||
/variant=edited/,
|
||
);
|
||
|
||
// The event-wide ZIP now carries the generated JPEG beside its original.
|
||
await page.goto(eventUrl);
|
||
const [zipDownload] = await Promise.all([
|
||
page.waitForEvent("download"),
|
||
page.getByRole("link", { name: /Download all \(2\)/ }).click(),
|
||
]);
|
||
const zip = await readFile((await zipDownload.path())!);
|
||
expect(zip.includes(edited)).toBe(true);
|
||
expect(zip.includes(Buffer.from("Originals/photo-edited.jpg"))).toBe(true);
|
||
});
|
||
|
||
test("an unassigned volunteer cannot open the event", async ({ page }) => {
|
||
await page.setViewportSize({ width: 1200, height: 900 });
|
||
await signInAs(page, "jordan@glenloch.local");
|
||
const response = await page.goto(eventUrl);
|
||
expect(response?.status()).toBe(404);
|
||
});
|
||
|
||
test("a manager deletes the event and its photos", async ({ page }) => {
|
||
await page.setViewportSize({ width: 1200, height: 900 });
|
||
await signInAs(page, "admin@glenloch.local");
|
||
await page.goto(eventUrl);
|
||
await page.getByRole("link", { name: /Originals/ }).click();
|
||
const photoHref = await page
|
||
.getByRole("link", { name: "Download" })
|
||
.first()
|
||
.getAttribute("href");
|
||
expect(photoHref).toBeTruthy();
|
||
|
||
await page.goto(eventUrl);
|
||
page.once("dialog", (dialog) => dialog.accept());
|
||
await page.getByRole("button", { name: "Delete event" }).click();
|
||
await expect(page).toHaveURL(`${BASE}/events`);
|
||
|
||
const gone = await page.goto(eventUrl);
|
||
expect(gone?.status()).toBe(404);
|
||
const photoGone = await page.request.get(`${BASE}${photoHref}`);
|
||
expect(photoGone.status()).toBe(404);
|
||
});
|
||
});
|