Files

69 lines
4.6 KiB
TypeScript

import { test, expect } from "@playwright/test"
test("IPsec: инициализация сохраняет ввод, блокирует повтор и требует подтверждения", async ({ page }) => {
const summary = { serverId: "1", serverName: "IPsec роутер", initialized: false, ike2Ready: false, natRuleManaged: false, clientsTotal: 0, clientsOnline: 0, peers: [] }
let attempts = 0
let release: (() => void) | undefined
const gate = new Promise<void>(resolve => { release = resolve })
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 (path === "/api/ipsec/server/init") {
attempts++
expect(route.request().postDataJSON()).toMatchObject({ serverId: "1", serverEndpoint: "192.0.2.1", poolCidr: "10.78.0.0/24", dns: "1.1.1.1", createNatRule: false })
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: summary })
if (attempts === 4) return route.fulfill({ json: { ...summary, initialized: true, serverId: "2" } })
return route.fulfill({ status: 201, json: { ...summary, initialized: true, ike2Ready: true } })
}
return route.fulfill({ json: path === "/api/auth/config" ? { required: false }
: path === "/api/ipsec" ? { servers: [summary], clients: [] }
: path === "/api/servers" ? [{ id: 1, name: "IPsec роутер", host: "192.0.2.1", enabled: true, type: "exit-node", country: "RU", status: "online" }] : {} })
})
await page.goto("/ipsec")
await page.getByRole("tab", { name: "Сервер", exact: true }).click()
await page.getByRole("button", { name: "Инициализировать", exact: true }).click()
const sheet = page.getByRole("dialog", { name: "Инициализация IKEv2-сервера" })
const submit = sheet.getByRole("button", { name: "Инициализировать", exact: true })
await expect(sheet.getByLabel("Адрес сервера (CN/SAN)")).toHaveValue("192.0.2.1")
await expect(submit).toBeEnabled()
await sheet.getByLabel("Адрес сервера (CN/SAN)").fill("999.0.0.1")
await expect(submit).toBeDisabled()
await sheet.getByLabel("Адрес сервера (CN/SAN)").fill("vpn.example.com")
await expect(submit).toBeEnabled()
await sheet.getByLabel("Адрес сервера (CN/SAN)").fill("192.0.2.1")
await sheet.getByLabel("Подсеть клиентов").fill("10.78.0.0/24")
await sheet.getByLabel("DNS для клиентов").fill("1.1.1.1")
await sheet.getByRole("switch", { name: "Интернет клиентам (NAT)" }).click()
await submit.evaluate(button => { (button as HTMLButtonElement).click(); (button as HTMLButtonElement).click() })
await expect.poll(() => attempts).toBe(1)
await expect(sheet.getByRole("status")).toContainText("Ожидание результата")
await expect(sheet.getByLabel("Подсеть клиентов")).toBeDisabled()
await expect(sheet.getByLabel("Сервер", { exact: true })).toBeDisabled()
await expect(sheet.getByRole("switch")).toBeDisabled()
await expect(sheet.getByRole("button", { name: "Отмена" })).toBeDisabled()
await page.keyboard.press("Escape")
await page.mouse.click(10, 10)
await expect(sheet).toBeVisible()
release!()
await expect(sheet.getByRole("alert").filter({ hasText: "Инициализация запрещена тест" })).toBeVisible()
await expect(sheet.getByLabel("Подсеть клиентов")).toHaveValue("10.78.0.0/24")
await expect(sheet.getByLabel("DNS для клиентов")).toHaveValue("1.1.1.1")
for (let attempt = 2; attempt <= 4; attempt++) {
await submit.click()
await expect.poll(() => attempts).toBe(attempt)
await expect(sheet.getByRole("alert").filter({ hasText: "Сервер не подтвердил инициализацию IKEv2" })).toBeVisible()
await expect(page.getByText("IKEv2-сервер инициализирован", { exact: true })).toHaveCount(0)
}
await submit.click()
await expect(sheet).toHaveCount(0)
expect(attempts).toBe(5)
await page.getByRole("button", { name: "Инициализировать", exact: true }).click()
await expect(sheet.getByLabel("Подсеть клиентов")).toHaveValue("10.77.0.0/24")
await expect(sheet.getByText("Сервер не подтвердил инициализацию IKEv2", { exact: true })).toHaveCount(0)
})