Files
kai 68fd9dfc15
release / which version (push) Successful in 2s
release / olares chart · sanity (push) Successful in 4s
release / app (push) Successful in 1m15s
release / tag and release (push) Successful in 3s
26.11.40: Anmelden per Mail-Link (Resend) (#41)
2026-11-07 18:11:45 +00:00

109 lines
5.7 KiB
TypeScript

import { readdir, readFile } from "node:fs/promises";
import path from "node:path";
import { expect, test, type Browser, type Page } from "@playwright/test";
// Anmelden per Link aus der Mail (26.11.40, Kai: Resend). Der Server schreibt jede
// Mail nach $LUNCH_DATA/mails (MAIL_TEST_DIR) statt sie zu senden.
test.describe.configure({ mode: "serial" });
const DIR = path.join(process.env.LUNCH_DATA ?? "", "mails");
const KAI = "kai@test.example";
async function mails(to: string): Promise<{ to: string; text: string; subject: string }[]> {
const files = (await readdir(DIR).catch(() => [])).sort();
const all = await Promise.all(files.map(async (f) => JSON.parse(await readFile(path.join(DIR, f), "utf8"))));
return all.filter((m) => m.to === to);
}
const linkIn = (text: string) => text.match(/https?:\/\/\S+\/anmelden\/mail#\S+/)![0];
async function fresh(browser: Browser): Promise<Page> {
const context = await browser.newContext({ ...test.info().project.use, storageState: undefined });
return context.newPage();
}
test("the admin sets up sending with Resend and sends a test mail", async ({ page }) => {
await page.goto("/mehr");
await expect(page.getByRole("link", { name: /Noch nicht eingerichtet/ })).toBeVisible();
await page.goto("/mehr/mail");
await expect(page.getByLabel("Server")).toHaveValue("smtp.resend.com");
await expect(page.getByLabel("Port", { exact: true })).toHaveValue("465");
await expect(page.getByLabel("Benutzer")).toHaveValue("resend");
await expect(page.getByLabel("Absender", { exact: true })).toHaveValue("hallo@foodplanhero.de");
await page.getByLabel("API-Schlüssel").fill("re_test_not_a_real_key");
await page.getByRole("button", { name: "Speichern" }).click();
await expect(page.getByText("Ein Schlüssel ist gespeichert.")).toBeVisible();
// Der Schlüssel kommt nie zurück in die Seite.
expect(await page.content()).not.toContain("re_test_not_a_real_key");
await page.getByLabel("Testmail an").fill(KAI);
await page.getByRole("button", { name: "Testmail schicken" }).click();
await expect(page.getByRole("status")).toContainText("Die Testmail ist unterwegs.");
expect((await mails(KAI)).at(-1)?.subject).toBe("Testmail von foodplanhero");
});
test("a person confirms the address through the first link", async ({ page }) => {
await page.goto("/mehr");
const section = page.locator("#mail");
await section.getByLabel("Deine Mailadresse").fill(KAI);
await section.getByRole("button", { name: "Bestätigung schicken" }).click();
await expect(section.getByRole("status")).toContainText(`Wartet auf Bestätigung: ${KAI}`);
const verify = (await mails(KAI)).at(-1)!;
expect(verify.subject).toBe("Bitte bestätige deine Adresse");
await page.goto(linkIn(verify.text));
await expect(page.getByRole("heading", { level: 1 })).toHaveText("Adresse bestätigen");
// Der Token verschwindet aus der Adresszeile.
expect(page.url()).not.toContain("#");
await page.getByRole("button", { name: "Bestätigen" }).click();
await expect(page.getByRole("heading", { level: 1 })).toHaveText("Deine Adresse ist bestätigt.");
await page.goto("/mehr");
await expect(page.locator("#mail")).toContainText(`Bestätigt: ${KAI}`);
});
test("signed out: a link by mail signs in once; unknown addresses get the same answer and no mail", async ({ browser }) => {
const page = await fresh(browser);
const sent = "Wenn die Adresse bei uns bestätigt ist, kommt gleich eine Mail.";
await page.goto("/anmelden");
await page.getByRole("button", { name: "Lieber einen Link per Mail?" }).click();
await page.getByLabel("Deine Mailadresse").fill("niemand@test.example");
await page.getByRole("button", { name: "Link schicken" }).click();
await expect(page.getByRole("status")).toContainText(sent);
expect(await mails("niemand@test.example")).toHaveLength(0);
const before = (await mails(KAI)).length;
await page.goto("/anmelden");
await page.getByRole("button", { name: "Lieber einen Link per Mail?" }).click();
await page.getByLabel("Deine Mailadresse").fill(KAI.toUpperCase());
await page.getByRole("button", { name: "Link schicken" }).click();
await expect(page.getByRole("status")).toContainText(sent);
await expect.poll(async () => (await mails(KAI)).length).toBe(before + 1);
const link = linkIn((await mails(KAI)).at(-1)!.text);
await page.goto(link);
await expect(page.getByRole("heading", { level: 1 })).toHaveText("Bei foodplanhero anmelden");
await page.getByRole("button", { name: "Anmelden" }).click();
await expect(page).toHaveURL(/\/$/);
// Per Mail angemeldet: nur anmelden — kein Gerätelink, keine Einladung, keine Adresse ändern.
await page.goto("/mehr");
await expect(page.getByText("Du bist per Mail-Link angemeldet.").first()).toBeVisible();
const status = await page.evaluate(async () =>
Promise.all(
[
["/api/household/invitations", "POST", { kind: "device" }],
["/api/household/invitations", "POST", { kind: "person", name: "Gast" }],
["/api/me/email", "PUT", { email: "neu@test.example" }],
["/api/household/kid-devices", "POST", { label: "Tablet", kind: "kids" }],
].map(async ([url, method, body]) => {
const res = await fetch(url as string, { method: method as string, headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
return `${res.status} ${(await res.json()).error}`;
}),
),
);
expect(status).toEqual(["403 mailSession", "403 mailSession", "403 mailSession", "403 mailSession"]);
// Derselbe Link ein zweites Mal: verbraucht.
const again = await fresh(browser);
await again.goto(link);
await again.getByRole("button", { name: "Anmelden" }).click();
await expect(again.getByRole("heading", { level: 1 })).toHaveText("Dieser Link gilt nicht mehr");
});