103 lines
7.9 KiB
TypeScript
103 lines
7.9 KiB
TypeScript
import { test, expect } from "@playwright/test"
|
|
|
|
for (const width of [1280, 320]) for (const shared of [false, true]) {
|
|
test(`IPsec: сохранить сертификат при удалении identity shared=${shared} ${width}px`, async ({ page }) => {
|
|
await page.setViewportSize({ width, height: 1000 })
|
|
const client = (n: number, serverId = "1") => ({ id: serverId + ":identity:*" + n, rosId: "identity:*" + n, serverId, serverName: "Роутер", name: "Клиент " + n, kind: "cert", certName: "shared-cert", authMethod: "certificate", online: false, disabled: false, managed: true })
|
|
let attempts = 0, deleted = false
|
|
await page.route("**/health", r => r.fulfill({ json: { ok: true } }))
|
|
await page.route("**/api/**", r => {
|
|
const url = new URL(r.request().url())
|
|
if (r.request().method() === "DELETE") {
|
|
attempts++
|
|
expect(decodeURIComponent(url.pathname)).toBe("/api/ipsec/users/1/identity:*0")
|
|
expect(url.searchParams.get("removeCertificate")).toBe("false")
|
|
if (attempts === 1) return r.fulfill({ status: 403, json: { error: "Нет доступа тест" } })
|
|
deleted = true
|
|
return r.fulfill({ json: { ok: true } })
|
|
}
|
|
const rows = [...(deleted ? [] : [client(0)]), client(1, shared ? "1" : "2")]
|
|
return r.fulfill({ json: url.pathname === "/api/ipsec" ? { servers: [{ serverId: "1", serverName: "Роутер", initialized: true, ike2Ready: true, natRuleManaged: false, clientsTotal: rows.filter(c => c.serverId === "1").length, clientsOnline: 0 }, { serverId: "2", serverName: "Другой", initialized: false, ike2Ready: false, natRuleManaged: false, clientsTotal: shared ? 0 : 1, clientsOnline: 0 }], clients: rows }
|
|
: url.pathname === "/api/servers" ? [1,2].map(id => ({ id, name: "Роутер " + id, host: "192.0.2." + id, enabled: true, type: "exit-node", country: "RU", status: "online" })) : url.pathname === "/api/auth/config" ? { required: false } : {} })
|
|
})
|
|
await page.goto("/ipsec?server=1")
|
|
const row = page.locator("tr[data-row-id]").filter({ has: page.getByRole("button", { name: "Клиент 0", exact: true, includeHidden: true }) })
|
|
await row.getByRole("button", { name: "Удалить клиента", exact: true }).click()
|
|
const dialog = page.getByRole("alertdialog"), checkbox = dialog.getByRole("checkbox", { name: "Удалить также клиентский сертификат" })
|
|
if (shared) { await expect(checkbox).not.toBeChecked(); await expect(checkbox).toBeDisabled(); await expect(dialog).toContainText("используется другими identity") }
|
|
else { await expect(checkbox).toBeChecked(); await expect(checkbox).toBeEnabled(); await checkbox.uncheck() }
|
|
await expect(dialog).toContainText("Сертификат останется на роутере")
|
|
expect(await dialog.evaluate(e => e.scrollWidth <= e.clientWidth)).toBe(true)
|
|
await dialog.getByRole("button", { name: "Удалить", exact: true }).click()
|
|
await expect(dialog.getByRole("alert")).toContainText("Нет доступа тест")
|
|
await expect(checkbox).not.toBeChecked()
|
|
await expect(row).toHaveCount(1)
|
|
await dialog.getByRole("button", { name: "Удалить", exact: true }).click()
|
|
await expect(dialog).toHaveCount(0)
|
|
await expect(row).toHaveCount(0)
|
|
expect(attempts).toBe(2)
|
|
if (shared) await expect(page.getByRole("button", { name: "Клиент 1", exact: true })).toBeVisible()
|
|
})
|
|
}
|
|
|
|
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)
|
|
})
|
|
}
|