Files
MikrotikManager/tests/e2e/firewall-toggle.spec.ts
T

49 lines
3.0 KiB
TypeScript

import { test, expect } from "@playwright/test"
for (const enabled of [true, false]) {
test(`Firewall: переключение правила ${enabled} требует подтверждения`, async ({ page }) => {
let attempts = 0
let release: (() => void) | undefined
const gate = new Promise<void>(resolve => { release = resolve })
let currentEnabled = enabled
const rule = { id: "rule-1", rosId: "*R/1", serverId: "1", serverName: "Роутер", family: "ip", table: "filter", chain: "forward", action: "accept", proto: "tcp", src: "", dst: "", port: "443", iface: "", comment: "Правило тест", enabled, hits: 1 }
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(path).toBe("/api/firewall/rules")
expect(route.request().postDataJSON()).toMatchObject({ serverId: "1", rosId: "*R/1", family: "ip", table: "filter", disabled: enabled })
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 } })
currentEnabled = !enabled
return route.fulfill({ json: { ok: true } })
}
return route.fulfill({ json: path === "/api/auth/config" ? { required: false }
: path === "/api/firewall/all" ? { rules: [{ ...rule, enabled: currentEnabled }], addressLists: [] }
: path === "/api/servers" ? [{ id: 1, name: "Роутер", host: "192.0.2.1", type: "exit-node", enabled: true, country: "RU", status: "online" }] : {} })
})
await page.goto("/firewall?server=1")
const row = page.locator("tr[data-row-id]")
await expect(row).toHaveCount(1)
const toggle = row.getByRole("switch")
await toggle.evaluate(element => { (element as HTMLElement).click(); (element as HTMLElement).click() })
await expect.poll(() => attempts).toBe(1)
await expect(toggle).toBeDisabled()
await expect(page.getByRole("status").filter({ hasText: "Ожидание изменения правила" })).toBeVisible()
release!()
await expect(page.getByRole("alert").filter({ hasText: "Изменение запрещено тест" })).toBeVisible()
for (let i = 2; i <= 3; i++) {
await toggle.click()
await expect.poll(() => attempts).toBe(i)
await expect(page.getByRole("alert").filter({ hasText: "Сервер не подтвердил изменение правила" })).toBeVisible()
await expect(toggle).toHaveAttribute("aria-checked", String(enabled))
}
await toggle.click()
await expect(toggle).toHaveAttribute("aria-checked", String(!enabled))
expect(attempts).toBe(4)
await expect(page.getByRole("alert").filter({ hasText: "Сервер не подтвердил" })).toHaveCount(0)
})
}