// Rôle : vérifie l'envoi d'une toolbox par lien chiffré, sa réception dans un navigateur isolé et les limites d'envoi. import { expect, test } from "@playwright/test"; import { Buffer } from "node:buffer"; import { readFile } from "node:fs/promises"; import { fileURLToPath, URL } from "node:url"; const fixture = JSON.parse(await readFile(new URL("./fixtures/toolbox-export.json", import.meta.url), "utf8")); const coverPath = fileURLToPath(new URL("../website/public/static/img/toolbox-icons/sword.webp", import.meta.url)); const cover = `data:image/webp;base64,${(await readFile(coverPath)).toString("base64")}`; async function importToolbox(page, name) { await page.goto("/#/toolboxes"); await page.getByText("Importer une toolbox").locator("input[type='file']").setInputFiles({ name: "share.json", mimeType: "application/json", buffer: Buffer.from(JSON.stringify({ ...fixture, toolbox: { ...fixture.toolbox, name, customCover: cover } })) }); await expect(page.getByRole("link", { name: `Ouvrir ${name}` }).first()).toBeVisible(); } function shareDialog(page, name) { return page.getByRole("dialog", { name: `Envoyer « ${name} »` }); } async function createShare(page, name, { singleUse = true } = {}) { await page.getByRole("button", { name: `Envoyer ${name}`, exact: true }).click(); const dialog = shareDialog(page, name); const option = dialog.getByRole("checkbox", { name: /Supprimer après la première utilisation/ }); await expect(option).toBeChecked(); if (!singleUse) await option.uncheck(); await dialog.getByRole("button", { name: "Créer le lien" }).click(); const link = dialog.getByLabel("Lien à transmettre"); await expect(link).toHaveValue(/\/#\/receive\/[A-Za-z0-9_-]{22}#[A-Za-z0-9_-]{43}$/); return { dialog, link: await link.inputValue() }; } // Chaque destinataire a son propre contexte : ni stockage local, ni quotas, ni toolboxes partagés avec l'expéditeur. async function openAsRecipient(browser, link) { const context = await browser.newContext(); const page = await context.newPage(); await page.goto(link); return { context, page }; } async function expectImported(page, name) { await expect(page).toHaveURL(/#\/toolbox\/[^/]+$/); await expect(page.getByRole("heading", { name, exact: true })).toBeVisible(); const notepad = page.locator("[data-module-type='notepad']"); await expect(notepad.getByText("Imported fixture note")).toBeVisible(); } test("a single-use link imports the toolbox once in another browser", async ({ page, browser }) => { await importToolbox(page, "Envoi unique"); const { dialog, link } = await createShare(page, "Envoi unique"); await expect(dialog.getByText("Il ne fonctionne qu’une fois.")).toBeVisible(); const first = await openAsRecipient(browser, link); await expect(first.page.getByText("Ce lien ne fonctionne qu’une fois")).toBeVisible(); await first.page.getByRole("button", { name: "Recevoir et importer" }).click(); await expectImported(first.page, "Envoi unique"); await first.page.goto("/#/toolboxes"); await expect(first.page.getByRole("link", { name: "Ouvrir Envoi unique" }).first().locator("img")).toHaveAttribute("src", /^data:image\/webp;base64,/); await first.context.close(); const second = await openAsRecipient(browser, link); await expect(second.page.getByRole("alert")).toContainText("a déjà été utilisé"); await second.context.close(); // Le lien consommé libère sa place : la toolbox peut être renvoyée. await dialog.getByRole("button", { name: "Fermer" }).last().click(); await page.getByRole("button", { name: "Envoyer Envoi unique", exact: true }).click(); await expect(shareDialog(page, "Envoi unique").getByRole("button", { name: "Créer le lien" })).toBeVisible(); }); test("a reusable link imports twice, is shown again instead of a new one, and can be revoked", async ({ page, browser }) => { await importToolbox(page, "Envoi partagé"); const { dialog, link } = await createShare(page, "Envoi partagé", { singleUse: false }); await expect(dialog.getByText("Il peut être utilisé plusieurs fois.")).toBeVisible(); for (let index = 0; index < 2; index += 1) { const recipient = await openAsRecipient(browser, link); await expect(recipient.page.getByRole("heading", { name: "Envoi partagé" })).toBeVisible(); await expect(recipient.page.getByText("Bloc Note")).toBeVisible(); await recipient.page.getByRole("button", { name: "Importer dans mes toolboxes" }).click(); await expectImported(recipient.page, "Envoi partagé"); await recipient.context.close(); } await dialog.getByRole("button", { name: "Fermer" }).last().click(); await page.getByRole("button", { name: "Envoyer Envoi partagé", exact: true }).click(); const reopened = shareDialog(page, "Envoi partagé"); await expect(reopened.getByText("Cette toolbox a déjà un lien valide.")).toBeVisible(); await expect(reopened.getByLabel("Lien à transmettre")).toHaveValue(link); await reopened.getByRole("button", { name: "Révoquer" }).click(); await expect(reopened.getByRole("button", { name: "Créer le lien" })).toBeVisible(); const late = await openAsRecipient(browser, link); await expect(late.page.getByRole("alert")).toContainText("a été révoqué"); await late.context.close(); }); test("a missing or altered key is refused before anything is imported", async ({ page, browser }) => { await importToolbox(page, "Envoi altéré"); const { link } = await createShare(page, "Envoi altéré", { singleUse: false }); const [base, key] = [link.slice(0, link.lastIndexOf("#")), link.slice(link.lastIndexOf("#") + 1)]; const altered = `${base}#${key[0] === "A" ? "B" : "A"}${key.slice(1)}`; const recipient = await openAsRecipient(browser, altered); await expect(recipient.page.getByRole("alert")).toContainText("a été modifié"); await expect(recipient.page.getByRole("button", { name: "Importer dans mes toolboxes" })).toHaveCount(0); await recipient.page.goto(base); await expect(recipient.page.getByRole("alert")).toContainText("la clé de déchiffrement manque"); await recipient.context.close(); }); test("this browser allows two shares per day", async ({ page }) => { await importToolbox(page, "Quota un"); await importToolbox(page, "Quota deux"); await importToolbox(page, "Quota trois"); const first = await createShare(page, "Quota un", { singleUse: false }); await first.dialog.getByRole("button", { name: "Révoquer" }).click(); await first.dialog.getByRole("button", { name: "Fermer" }).last().click(); const second = await createShare(page, "Quota deux", { singleUse: false }); await second.dialog.getByRole("button", { name: "Fermer" }).last().click(); await page.getByRole("button", { name: "Envoyer Quota trois", exact: true }).click(); const dialog = shareDialog(page, "Quota trois"); await expect(dialog.getByText(/limite de 2 envois par 24 heures/)).toBeVisible(); await expect(dialog.getByRole("button", { name: "Créer le lien" })).toHaveCount(0); await expect(dialog.getByText("Quota deux")).toBeVisible(); });