Files

59 lines
3.9 KiB
TypeScript

import { test, expect } from "@playwright/test"
test("IPsec: peer сохраняет поля и ждёт подтверждения API", async ({ page }) => {
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 (route.request().method() === "PATCH") {
attempts++
expect(decodeURIComponent(path)).toBe("/api/ipsec/peers/1/*P/1")
expect(route.request().postDataJSON()).toMatchObject({ name: "updated", address: "192.0.2.0/24", profile: "test-profile", passive: true, disabled: true })
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 } })
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: 0, clientsOnline: 0, peers: [{ id: "p", rosId: "*P/1", serverId: "1", serverName: "Роутер", name: "peer-test", managed: true, disabled: false, passive: false }] }], clients: [] }
: path === "/api/servers" ? [{ id: 1, name: "Роутер", 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: "Изменить peer", exact: true }).click()
const sheet = page.getByRole("dialog")
await sheet.getByLabel("Имя", { exact: true }).fill("updated")
await sheet.getByLabel("Адрес", { exact: true }).fill("192.0.2.0/24")
await sheet.getByLabel("Profile", { exact: true }).fill("test-profile")
await sheet.getByRole("switch", { name: "Passive" }).click()
await sheet.getByRole("switch", { name: "Отключён" }).click()
const submit = sheet.getByRole("button", { name: "Сохранить", exact: true })
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("Имя", { exact: true })).toBeDisabled()
await expect(sheet.getByRole("switch", { name: "Passive" })).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")).toContainText("Редактирование запрещено тест")
await expect(sheet.getByLabel("Имя", { exact: true })).toHaveValue("updated")
await expect(sheet.getByLabel("Profile", { exact: true })).toHaveValue("test-profile")
for (let attempt = 2; attempt <= 3; attempt++) {
await submit.click()
await expect.poll(() => attempts).toBe(attempt)
await expect(sheet.getByRole("alert")).toContainText("Сервер не подтвердил сохранение peer")
await expect(page.getByText("Peer обновлён", { exact: true })).toHaveCount(0)
}
await submit.click()
await expect(sheet).toHaveCount(0)
expect(attempts).toBe(4)
await page.getByRole("button", { name: "Изменить peer", exact: true }).click()
await expect(sheet.getByRole("alert")).toHaveCount(0)
await expect(sheet.getByLabel("Имя", { exact: true })).toHaveValue("peer-test")
})