63 lines
4.5 KiB
TypeScript
63 lines
4.5 KiB
TypeScript
import { test, expect } from "@playwright/test"
|
|
|
|
for (const kind of ["user", "server", "peer", "cert"] as const) {
|
|
test(`IPsec: удаление ${kind} сохраняет подтверждение и защищает повтор`, async ({ page }) => {
|
|
const peer = { id: "peer", rosId: "*P/1", serverId: "1", serverName: "Роутер", name: "peer-test", managed: true, disabled: false }
|
|
const cert = { name: "cert-test", managed: true, role: "client" }
|
|
const client = { id: "client", rosId: "*C/1", serverId: "1", serverName: "Роутер", name: "alice", authMethod: "certificate", online: false, disabled: false, managed: true }
|
|
let attempts = 0
|
|
let release: (() => void) | undefined
|
|
const gate = new Promise<void>(resolve => { release = resolve })
|
|
const canForce = kind === "peer" || kind === "cert"
|
|
await page.route("**/health", route => route.fulfill({ json: { ok: true } }))
|
|
await page.route("**/api/**", async route => {
|
|
const url = new URL(route.request().url())
|
|
const path = url.pathname
|
|
if (route.request().method() === "DELETE") {
|
|
attempts++
|
|
expect(decodeURIComponent(path)).toBe(kind === "user" ? "/api/ipsec/users/1/*C/1" : kind === "server" ? "/api/ipsec/server/1" : kind === "peer" ? "/api/ipsec/peers/1/*P/1" : "/api/ipsec/certs/1")
|
|
if (kind === "cert") expect(route.request().postDataJSON()).toEqual({ name: "cert-test" })
|
|
expect(url.searchParams.get("force")).toBe(attempts === 5 && canForce ? "true" : null)
|
|
if (attempts === 1) { await gate; return route.fulfill({ status: 403, json: { error: "Удаление запрещено тест" } }) }
|
|
if (attempts === 2) return route.fulfill({ json: {} })
|
|
if (attempts === 3) return route.fulfill({ json: { ok: false } })
|
|
if (attempts === 4 && canForce) return route.fulfill({ status: 409, json: { error: "Объект используется тест" } })
|
|
return route.fulfill({ json: { ok: true } })
|
|
}
|
|
return route.fulfill({ json: path === "/api/auth/config" ? { required: false }
|
|
: path === "/api/ipsec" ? { servers: [{ serverId: "1", serverName: "Роутер", initialized: true, ike2Ready: true, natRuleManaged: true, clientsTotal: 1, clientsOnline: 0, peers: [peer], certs: [cert] }], 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")
|
|
if (kind !== "user") await page.getByRole("tab", { name: "Сервер", exact: true }).click()
|
|
const label = { user: "Удалить клиента", server: "Удалить IKEv2-сервер", peer: "Удалить peer", cert: "Удалить сертификат" }[kind]
|
|
await page.getByRole("button", { name: label, exact: true }).click()
|
|
const dialog = page.getByRole("alertdialog")
|
|
const remove = dialog.getByRole("button", { name: "Удалить", exact: true })
|
|
await remove.evaluate(button => { (button as HTMLButtonElement).click(); (button as HTMLButtonElement).click() })
|
|
await expect.poll(() => attempts).toBe(1)
|
|
await expect(dialog.getByRole("status")).toContainText("Ожидание результата")
|
|
await expect(dialog.getByRole("button", { name: "Отмена" })).toBeDisabled()
|
|
await page.keyboard.press("Escape")
|
|
await expect(dialog).toBeVisible()
|
|
release!()
|
|
await expect(dialog.getByRole("alert")).toContainText("Удаление запрещено тест")
|
|
for (let i = 2; i <= 3; i++) {
|
|
await remove.click()
|
|
await expect.poll(() => attempts).toBe(i)
|
|
await expect(dialog.getByRole("alert")).toContainText("Сервер не подтвердил удаление")
|
|
await expect(page.getByText("Удалено", { exact: true })).toHaveCount(0)
|
|
}
|
|
await remove.click()
|
|
if (canForce) {
|
|
await expect(dialog.getByRole("alert")).toContainText("Объект используется тест")
|
|
await dialog.getByRole("button", { name: "Удалить принудительно", exact: true }).evaluate(button => { (button as HTMLButtonElement).click(); (button as HTMLButtonElement).click() })
|
|
}
|
|
await expect(dialog).toHaveCount(0)
|
|
expect(attempts).toBe(canForce ? 5 : 4)
|
|
await page.getByRole("button", { name: label, exact: true }).click()
|
|
await expect(dialog.getByRole("alert")).toHaveCount(0)
|
|
await expect(dialog.getByRole("button", { name: "Удалить принудительно" })).toHaveCount(0)
|
|
})
|
|
}
|