SPB Git

Repository Public

approvals.spec.ts 8.9 KB 137 lines typescript

1// Approbations : une clé dont la politique d'envoi externe est « avec approbation » demande trois envois vers une adresse2// traitée comme externe (crochet de recette : rien ne sort réellement). File d'attente et pastille de la navigation,3// approbation (clé d'accès exigée, reprise transparente), rejet, demande déjà traitée ailleurs (409), historique.4// Un seul message est réellement remis (celui qui est approuvé).5import type { Page } from "@playwright/test"6import crypto from "node:crypto"7import { api, apiKey, axeBoth, clearReauth, confirmReauth, expect, grantReauth, noOverflow, openAdmin, st, test, toastText } from "./fixtures"89const card = (page: Page, outboundId: string) => page.locator(`[data-testid="approval-card"][data-outbound-id="${outboundId}"]`)10const outboundStatus = async (token: string, id: string): Promise<string> => (await apiKey(token, "GET", `/messages/outbound/${id}`)).body.status1112test("approbations : file d'attente, approbation avec clé d'accès, rejet, demande déjà traitée, historique", async ({ page, watch }) => {13  test.setTimeout(240_000)14  // attendus : 403 (réauthentification exigée avant l'approbation, puis reprise) et 409 (demande déjà traitée ailleurs)15  watch.allow.push(/status of 403/, /status of 409/)16  const tag = `${st.run}-${crypto.randomBytes(2).toString("hex")}`17  const keyName = `web-approbation-${tag}`1819  // ---------------------------------------------------------------- préparation par l'API : une clé « avec approbation », trois demandes20  const addresses = (await api("GET", "/api/v1/addresses")).body.data as { id: string; address: string }[]21  const addrA = addresses.find((a) => a.address === st.addrA)22  expect(addrA, "adresse A de la recette").toBeTruthy()23  grantReauth()24  const created = await api("POST", "/api/admin/api-keys", { name: keyName, scopes: ["mail.send"], address_ids: [addrA!.id], external_send_policy: "approval" })25  expect(created.status).toBe(201)26  const token: string = created.body.token27  const ask = async (label: string) => {28    const subject = `Approbation ${tag} ${label}`29    const r = await apiKey(token, "POST", "/messages/send", { from: st.addrA, to: [{ email: st.addrExt }], cc: [{ email: st.addrB }], subject, text: `Corps du message ${label} <b>pas du HTML</b> ${tag}` })30    expect(r.status).toBe(202)31    expect(r.body.status).toBe("pending_approval")32    return { subject, id: r.body.id as string, approvalId: r.body.approval_id as string }33  }34  const toApprove = await ask("à approuver")35  const toReject = await ask("à rejeter")36  const stale = await ask("déjà traitée")37  // rien n'est parti : ni vers l'externe, ni vers le destinataire interne en copie38  expect(await outboundStatus(token, toApprove.id)).toBe("pending_approval")3940  // ---------------------------------------------------------------- file d'attente41  await openAdmin(page, "approvals")42  const badge = page.getByTestId("nav-badge-approvals")43  await expect(badge).toBeVisible()44  const before = Number(await badge.textContent())45  expect(before).toBeGreaterThanOrEqual(3)46  await expect(page.getByTestId("approvals-tab-pending")).toHaveAttribute("aria-pressed", "true")47  for (const x of [toApprove, toReject, stale]) await expect(card(page, x.id)).toBeVisible()48  const c = card(page, toApprove.id)49  await expect(c).toHaveAttribute("data-approval-id", toApprove.approvalId)50  await expect(c.getByRole("heading", { level: 3, name: toApprove.subject })).toBeVisible()51  await expect(c).toContainText(keyName)52  await expect(c).toContainText(st.addrA)53  // les destinataires externes sont marqués, les internes aussi (jamais par la seule couleur)54  await expect(c.locator('li[data-external="true"]')).toHaveCount(1)55  await expect(c.locator('li[data-external="true"]')).toContainText(st.addrExt)56  await expect(c.locator('li[data-external="true"]')).toContainText("Externe")57  await expect(c.locator('li[data-external="false"]')).toContainText(st.addrB)58  await expect(c.locator('li[data-external="false"]')).toContainText("Interne")59  // l'aperçu est du texte : le balisage reçu est affiché tel quel, jamais interprété60  const preview = c.getByTestId("approval-preview")61  await expect(preview).toContainText(`Corps du message à approuver <b>pas du HTML</b> ${tag}`)62  await expect(preview.locator("b")).toHaveCount(0)63  await expect(c).toContainText("Expire")64  await axeBoth(page, "approbations-file")65  await noOverflow(page)6667  // ---------------------------------------------------------------- approbation : clé d'accès exigée, puis reprise transparente68  clearReauth()69  await c.getByRole("button", { name: `Approuver l'envoi « ${toApprove.subject} »` }).click()70  const confirmApprove = page.getByRole("alertdialog", { name: "Approuver cet envoi ?" })71  await expect(confirmApprove).toContainText(st.addrExt)72  await axeBoth(page, "approbations-confirmation")73  await confirmApprove.getByRole("button", { name: "Approuver et envoyer" }).click()74  await confirmReauth(page)75  await expect(toastText(page, "Envoi approuvé")).toBeVisible()76  await expect(card(page, toApprove.id)).toHaveCount(0)77  await expect.poll(() => outboundStatus(token, toApprove.id), { timeout: 30_000 }).not.toBe("pending_approval")78  expect(["queued", "sent", "delivered", "deferred"]).toContain(await outboundStatus(token, toApprove.id))79  await expect.poll(async () => Number((await badge.textContent().catch(() => "0")) ?? "0"), { timeout: 15_000 }).toBe(before - 1)8081  // ---------------------------------------------------------------- rejet : confirmation, sans clé d'accès82  clearReauth()83  await card(page, toReject.id).getByRole("button", { name: `Rejeter l'envoi « ${toReject.subject} »` }).click()84  const confirmReject = page.getByRole("alertdialog", { name: "Rejeter cet envoi ?" })85  await expect(confirmReject).toContainText(toReject.subject)86  await confirmReject.getByRole("button", { name: "Rejeter l'envoi", exact: true }).click()87  await expect(toastText(page, "Envoi rejeté")).toBeVisible()88  await expect(page.getByTestId("reauth-dialog")).toHaveCount(0)89  await expect(card(page, toReject.id)).toHaveCount(0)90  await expect.poll(() => outboundStatus(token, toReject.id), { timeout: 15_000 }).toBe("cancelled")9192  // ---------------------------------------------------------------- demande traitée ailleurs entre-temps : 409 not_pending, dit clairement, liste actualisée93  // (le dialogue de confirmation est ouvert d'abord : tant qu'il l'est, la relecture périodique de la file est suspendue)94  await card(page, stale.id).getByRole("button", { name: `Rejeter l'envoi « ${stale.subject} »` }).click()95  const confirmStale = page.getByRole("alertdialog", { name: "Rejeter cet envoi ?" })96  await expect(confirmStale).toContainText(stale.subject)97  const elsewhere = await api("POST", `/api/admin/approvals/${stale.approvalId}/reject`)98  expect(elsewhere.status).toBe(200)99  await confirmStale.getByRole("button", { name: "Rejeter l'envoi", exact: true }).click()100  await expect(toastText(page, "Cette demande n'est plus en attente")).toBeVisible()101  await expect(card(page, stale.id)).toHaveCount(0)102103  // ---------------------------------------------------------------- historique104  await page.getByTestId("approvals-tab-approved").click()105  const history = page.getByTestId("approvals-history")106  const approvedRow = history.locator(`tr[data-outbound-id="${toApprove.id}"]`)107  await expect(approvedRow).toBeVisible()108  await expect(approvedRow).toContainText("Approuvée")109  await expect(approvedRow).toContainText(keyName)110  await expect(approvedRow).toContainText(st.addrExt)111  await expect(history.locator(`tr[data-outbound-id="${toReject.id}"]`)).toHaveCount(0)112  await axeBoth(page, "approbations-historique")113  await noOverflow(page)114  await page.getByTestId("approvals-tab-rejected").click()115  await expect(page.getByTestId("approvals-tab-rejected")).toHaveAttribute("aria-pressed", "true")116  for (const x of [toReject, stale]) {117    const r = page.getByTestId("approvals-history").locator(`tr[data-outbound-id="${x.id}"]`)118    await expect(r).toBeVisible()119    await expect(r).toContainText("Rejetée")120    await expect(r).toContainText("cancelled")121  }122  await expect(page.getByTestId("approvals-history").locator(`tr[data-outbound-id="${toApprove.id}"]`)).toHaveCount(0)123  await page.getByTestId("approvals-tab-expired").click()124  await expect(page.getByTestId("approvals-count")).toBeVisible()125  await expect(page.locator(`[data-outbound-id="${toApprove.id}"]`)).toHaveCount(0)126127  // téléphone : fiches lisibles sans défilement horizontal128  await page.setViewportSize({ width: 390, height: 844 })129  await page.getByTestId("approvals-tab-rejected").click()130  await expect(page.getByTestId("approvals-history")).toBeVisible()131  await noOverflow(page)132133  // ménage : la clé de l'essai est révoquée134  const keys = (await api("GET", "/api/admin/api-keys")).body as { id: string; name: string }[]135  for (const k of keys.filter((x) => x.name === keyName)) expect((await api("DELETE", `/api/admin/api-keys/${k.id}`)).status).toBe(200)136})137
Files main
Menu

Theme