Files

66 lines
4.2 KiB
TypeScript

import { test, expect } from "@playwright/test"
for (const mode of ["create", "dto", "edit"]) {
test(`GRE: сохранение ${mode} подтверждается API`, async ({ page }) => {
let attempts = 0
const remoteInnerIp = mode === "edit" ? "" : "10.0.0.2/30"
let release: (() => void) | undefined
const gate = new Promise<void>(resolve => { release = resolve })
const tunnel = { id: "*T/1", name: "gre-test", serverId: "1", localAddress: "192.0.2.1", remoteAddress: "192.0.2.2", localInnerIp: "10.0.0.1/30", remoteInnerIp: "10.0.0.2/30", poolId: "pool", ipsec: null, mtu: 1476, keepaliveInterval: 10, keepaliveRetries: 3, dscp: "inherit", clampTcpMss: true, allowFastPath: true, comment: "", enabled: true, status: "up" }
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() === (mode === "edit" ? "PATCH" : "POST")) {
attempts++
expect(path).toBe("/api/gre/tunnels")
expect(route.request().postDataJSON()).toMatchObject({ serverId: "1", name: "gre-test", remoteAddress: "192.0.2.9", remoteInnerIp, localInnerIp: mode === "edit" ? "" : "fd00:151::1/64", comment: "сохранённый ввод" })
if (mode === "edit") expect(route.request().postDataJSON().rosId).toBe("*T/1")
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: mode === "edit" ? { ok: false } : { ...tunnel, serverId: "2" } })
return route.fulfill({ json: mode === "dto" ? { ...tunnel, remoteAddress: "192.0.2.9" } : mode === "edit" ? { ok: true } : { ok: true, name: "gre-test" } })
}
return route.fulfill({ json: path === "/api/auth/config" ? { required: false }
: path === "/api/gre/tunnels" ? { tunnels: [tunnel] }
: path === "/api/servers" ? [{ id: 1, name: "Роутер", host: "192.0.2.1", enabled: true, type: "exit-node", country: "RU", status: "online" }] : {} })
})
await page.goto("/gre")
if (mode === "edit") {
await page.getByRole("button", { name: "Действия: gre-test" }).click()
await page.getByRole("menuitem", { name: "Редактировать", exact: true }).click()
} else {
await page.getByRole("button", { name: "Добавить туннель", exact: true }).click()
}
const sheet = page.getByRole("dialog")
if (mode !== "edit") {
await sheet.getByLabel("Имя интерфейса").fill("gre-test")
await sheet.getByLabel("Сервер (MikroTik)").selectOption("1")
}
await sheet.getByLabel("Удалённый адрес").fill("192.0.2.9")
await sheet.getByLabel("Комментарий").fill("сохранённый ввод")
await sheet.getByLabel("Удалённый IP", { exact: true }).fill(remoteInnerIp)
await sheet.getByLabel("Локальный IP", { exact: true }).fill(mode === "edit" ? "" : "fd00:151::1/64")
const submit = sheet.getByRole("button", { name: mode === "edit" ? "Сохранить" : "Создать туннель", 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("Удалённый адрес")).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("Комментарий")).toHaveValue("сохранённый ввод")
for (let attempt = 2; attempt <= 3; attempt++) {
await submit.click()
await expect.poll(() => attempts).toBe(attempt)
await expect(sheet.getByRole("alert")).toContainText("Сервер не подтвердил")
}
await submit.click()
await expect(sheet).toHaveCount(0)
expect(attempts).toBe(4)
})
}