Files
MikrotikManager/tests/e2e/ipsec-cert.spec.ts

66 lines
4.4 KiB
TypeScript

import { test, expect } from "@playwright/test"
for (const byName of [false, true]) {
test(`IPsec: экспорт ${byName ? "по имени" : "по identity"} сохраняет контекст и пароль архива`, async ({ page }) => {
const client = { id: "1", rosId: "*P/1", serverId: "1", serverName: "Роутер", name: "alice", authMethod: "certificate", kind: "cert", certName: byName ? "alice-cert" : undefined, online: false, disabled: false, managed: true }
let attempts = 0
let release: (() => void) | undefined
const gate = new Promise<void>(resolve => { release = resolve })
let confirmedPassphrase = ""
await page.route("**/health", route => route.fulfill({ json: { ok: true } }))
await page.route("**/api/**", async route => {
const path = new URL(route.request().url()).pathname
if (route.request().method() === "POST") {
attempts++
expect(decodeURIComponent(path)).toBe(byName ? "/api/ipsec/certs/1/export" : "/api/ipsec/users/1/*P/1/cert")
const body = route.request().postDataJSON()
if (byName) expect(body.name).toBe("alice-cert")
expect(body.passphrase.length).toBeGreaterThanOrEqual(8)
if (attempts === 1) { await gate; return route.fulfill({ json: { user: "stale", filename: "stale.p12", contentB64: "YQ==", passphrase: body.passphrase } }) }
if (attempts === 2) return route.fulfill({ status: 403, json: { error: "Экспорт запрещён тест" } })
if (attempts === 3) return route.fulfill({ json: { user: "alice", filename: "alice.p12", contentB64: "!invalid!", passphrase: body.passphrase } })
if (attempts === 5) return route.fulfill({ status: 500, json: { error: "Повторный экспорт не выполнен" } })
confirmedPassphrase = body.passphrase
return route.fulfill({ json: { user: "alice", filename: "alice.p12", contentB64: "YQ==", passphrase: body.passphrase } })
}
return route.fulfill({ json: path === "/api/auth/config" ? { required: false }
: path === "/api/ipsec" ? { servers: [], clients: [client] }
: path === "/api/servers" ? [{ id: 1, name: "Роутер", host: "192.0.2.1", enabled: true, type: "exit-node", country: "RU", status: "online" }] : {} })
})
await page.goto("/ipsec")
const open = page.getByRole("button", { name: "Скачать сертификат", exact: true })
await open.click()
const sheet = page.getByRole("dialog")
await expect(sheet.getByRole("status")).toContainText("Загрузка сертификата")
await expect.poll(() => attempts).toBe(1)
await page.keyboard.press("Escape")
await expect(sheet).toHaveCount(0)
await open.click()
await expect(sheet.getByRole("alert")).toContainText("Экспорт запрещён тест")
const oldResponse = page.waitForResponse(r => r.request().method() === "POST")
release!()
await (await oldResponse).finished()
await page.waitForTimeout(100)
await expect(sheet.getByRole("alert")).toContainText("Экспорт запрещён тест")
await expect(sheet.getByText("stale.p12", { exact: false })).toHaveCount(0)
const retry = sheet.getByRole("button", { name: "Повторить экспорт" })
await retry.evaluate(button => { (button as HTMLButtonElement).click(); (button as HTMLButtonElement).click() })
await expect.poll(() => attempts).toBe(3)
await expect(sheet.getByRole("alert")).toContainText("Некорректный ответ экспорта")
await retry.click()
const password = sheet.getByLabel("Пароль архива .p12")
await expect(password).toHaveValue(confirmedPassphrase)
await expect(password).toHaveAttribute("readonly", "")
const original = confirmedPassphrase
await sheet.getByRole("button", { name: "Сгенерировать и перекачать" }).click()
await expect(sheet.getByRole("alert")).toContainText("Повторный экспорт не выполнен")
await expect(password).toHaveValue(original)
await expect(sheet.getByRole("button", { name: /alice.p12/ })).toBeEnabled()
await sheet.getByRole("button", { name: "Сгенерировать и перекачать" }).click()
await expect.poll(() => attempts).toBe(6)
await expect(password).not.toHaveValue(original)
await expect(password).toHaveValue(confirmedPassphrase)
await expect(sheet.getByRole("alert")).toHaveCount(0)
})
}