SPB Git

Repository Public

helpers.ts 3.1 KB 48 lines typescript

1import type { BrowserContext, CDPSession, Page } from "@playwright/test"2import { expect } from "@playwright/test"3import { ORIGIN, OUT, readState, writeState } from "./env"4import { purgeLeftovers, trackMailboxes, type AdminCall } from "./mailboxes"56export interface Authn {7  cdp: CDPSession8  id: string9}10/** Authentificateur de plateforme virtuel (CTAP2, clé résidente, vérification de l'utilisateur toujours réussie). */11export async function addAuthenticator(context: BrowserContext, page: Page): Promise<Authn> {12  const cdp = await context.newCDPSession(page)13  await cdp.send("WebAuthn.enable")14  const { authenticatorId } = await cdp.send("WebAuthn.addVirtualAuthenticator", {15    options: { protocol: "ctap2", transport: "internal", hasResidentKey: true, hasUserVerification: true, isUserVerified: true, automaticPresenceSimulation: true },16  })17  return { cdp, id: authenticatorId }18}19export async function exportCredential(a: Authn) {20  const { credentials } = await a.cdp.send("WebAuthn.getCredentials", { authenticatorId: a.id })21  const c = credentials[0]22  writeState({ credential: { credentialId: c.credentialId, isResidentCredential: c.isResidentCredential, rpId: c.rpId ?? "localhost", privateKey: c.privateKey, userHandle: c.userHandle ?? "", signCount: c.signCount } })23}24export async function importCredential(a: Authn, bump = 1000) {25  const c = readState().credential26  if (!c) throw new Error("no credential exported by the desktop project")27  // compteur de signatures en avance sur l'authentificateur d'origine (sinon le serveur y verrait un clone, à juste titre)28  await a.cdp.send("WebAuthn.addCredential", { authenticatorId: a.id, credential: { ...c, signCount: c.signCount + bump } })29}30export async function signIn(page: Page) {31  await page.goto("/")32  await page.getByRole("button", { name: "Se connecter avec une clé d'accès" }).click()33  await expect(page.locator("#sidebar")).toBeAttached()34}35export const list = (page: Page) => page.getByRole("listbox", { name: /^Conversations/ })36export const row = (page: Page, subject: string | RegExp) => list(page).getByRole("option", { name: subject })37export const toastText = (page: Page, text: string | RegExp) => page.locator(".toast").filter({ hasText: text })3839/** Appel de l'API d'administration avec la session de la page (hors de la page : en-têtes Origin et anti-CSRF posés ici). */40export const adminCall = (page: Page): AdminCall => async (method, path, body) => {41  const r = await page.context().request.fetch(path, { method, headers: { origin: ORIGIN, "x-spmail": "1" }, ...(body === undefined ? {} : { data: body }) })42  return { status: r.status(), body: await r.json().catch(() => null) }43}44/** Dès qu'une session existe : purge ce qu'un parcours précédent de CETTE suite a laissé (registre sous OUT) — rien d'autre. */45export const purgeLeftoverMailboxes = (page: Page) => purgeLeftovers(OUT, adminCall(page))46/** Inscrit les boîtes que le parcours va créer : le démontage (ou le lancement suivant) purge exactement celles-là. */47export const track = (...addresses: string[]) => trackMailboxes(OUT, ...addresses)48
Files main
Menu

Theme