fix(ui): изолировать контекст и операции IPsec
This commit is contained in:
1 parent
f0071a495c
commit
921aba3054
7 files changed
+205
-36
No files matched your search
@@ -0,0 +1,79 @@
|
||||
import { test, expect, type Page } from "@playwright/test"
|
||||
const headers = { "access-control-allow-origin": "*", "access-control-allow-methods": "GET,POST,PUT,PATCH,DELETE,OPTIONS", "access-control-allow-headers": "Content-Type,Authorization" }
|
||||
const payload = (port: string) => ({ servers: [], clients: [{ id: "1-*1", rosId: "*1", serverId: "1", serverName: "Роутер", name: port === "8101" ? "Новый источник" : "Старый источник", authMethod: "certificate", kind: "cert", online: false, disabled: false, managed: true }] })
|
||||
async function setup(page: Page) {
|
||||
let oldReads = 0
|
||||
await page.route("**/health", r => r.fulfill({ headers, json: { ok: true } }))
|
||||
await page.route("**/api/**", r => {
|
||||
const url = new URL(r.request().url()), path = url.pathname
|
||||
if (r.request().method() === "OPTIONS") return r.fulfill({ status: 204, headers })
|
||||
if (path === "/api/ipsec" && url.port === "8000") oldReads++
|
||||
return r.fulfill({ headers, json: path === "/api/ipsec" ? payload(url.port) : path === "/api/servers" ? [{ id: 1, name: "Роутер", host: "192.0.2.1", enabled: true, country: "RU" }] : path === "/api/auth/config" ? { required: false } : {} })
|
||||
})
|
||||
await page.goto("/settings")
|
||||
await page.getByRole("link", { name: /^IPsec/ }).click()
|
||||
await expect(page.locator("tr[data-row-id]")).toContainText("Старый источник")
|
||||
return () => oldReads
|
||||
}
|
||||
async function change(page: Page, kind: string) {
|
||||
await page.goBack()
|
||||
await expect(page).toHaveURL(/settings/)
|
||||
if (kind === "backend") {
|
||||
const input = page.getByPlaceholder("http://localhost:8000")
|
||||
await input.fill("http://localhost:8101")
|
||||
await input.press("Enter")
|
||||
} else await page.getByRole("button", { name: "Моковые", exact: true }).click()
|
||||
await page.getByRole("link", { name: /^IPsec/ }).click()
|
||||
if (kind === "backend") await expect(page.locator("tr[data-row-id]")).toContainText("Новый источник")
|
||||
else await expect(page.locator('tr[data-row-id="1-*1"]')).toHaveCount(0)
|
||||
}
|
||||
async function flush(page: Page) {
|
||||
await page.evaluate(() => new Promise<void>(resolve => requestAnimationFrame(() => requestAnimationFrame(() => resolve()))))
|
||||
}
|
||||
for (const fails of [false, true]) {
|
||||
test(`IPsec: позднее чтение старого backend ${fails}`, async ({ page }) => {
|
||||
await setup(page)
|
||||
let release: (() => void) | undefined, started = false
|
||||
const gate = new Promise<void>(resolve => { release = resolve })
|
||||
await page.route("**/api/ipsec", async r => {
|
||||
if (new URL(r.request().url()).port !== "8000") return r.fallback()
|
||||
started = true
|
||||
await gate
|
||||
return r.fulfill(fails ? { headers, status: 503, json: { error: "Старая ошибка чтения" } } : { headers, json: { servers: [], clients: [] } })
|
||||
})
|
||||
await page.getByRole("button", { name: "Обновить", exact: true }).click()
|
||||
await expect.poll(() => started).toBe(true)
|
||||
await change(page, "backend")
|
||||
const response = page.waitForResponse(r => r.url().includes(":8000/api/ipsec"))
|
||||
release!()
|
||||
await (await response).finished()
|
||||
await flush(page)
|
||||
await expect(page.locator("tr[data-row-id]")).toContainText("Новый источник")
|
||||
await expect(page.getByRole("alert").filter({ hasText: "Не удалось загрузить IPsec" })).toHaveCount(0)
|
||||
})
|
||||
}
|
||||
for (const kind of ["backend", "mode"]) for (const fails of [false, true]) {
|
||||
test(`IPsec: позднее удаление после смены ${kind}/${fails}`, async ({ page }) => {
|
||||
const reads = await setup(page)
|
||||
let release: (() => void) | undefined, started = false
|
||||
const gate = new Promise<void>(resolve => { release = resolve })
|
||||
await page.route("**/api/ipsec/users/1/*", async r => {
|
||||
started = true
|
||||
await gate
|
||||
return r.fulfill(fails ? { headers, status: 403, json: { error: "Старое удаление запрещено" } } : { headers, json: { ok: true } })
|
||||
})
|
||||
await page.getByRole("button", { name: "Удалить клиента", exact: true }).click()
|
||||
await page.getByRole("alertdialog").getByRole("button", { name: "Удалить", exact: true }).click()
|
||||
await expect.poll(() => started).toBe(true)
|
||||
const before = reads()
|
||||
await change(page, kind)
|
||||
const response = page.waitForResponse(r => r.url().includes(":8000/api/ipsec/users/") && r.request().method() === "DELETE")
|
||||
release!()
|
||||
await (await response).finished()
|
||||
await flush(page)
|
||||
expect(reads()).toBe(before)
|
||||
await expect(page.getByText("Удалено", { exact: true })).toHaveCount(0)
|
||||
await expect(page.getByRole("alertdialog")).toHaveCount(0)
|
||||
if (kind === "backend") await expect(page.getByRole("button", { name: "Новый клиент", exact: true })).toBeEnabled()
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { test, expect } from "@playwright/test"
|
||||
|
||||
test("IPsec: удаление держит замок до обновления и отделяет ошибку чтения", async ({ page }) => {
|
||||
const client = { id: "1-*1", rosId: "*1", serverId: "1", serverName: "Роутер", name: "Клиент тест", authMethod: "certificate", online: false, disabled: false, managed: true }
|
||||
let reads = 0, writes = 0, release: (() => void) | undefined, phase = 0
|
||||
const gate = new Promise<void>(resolve => { release = resolve })
|
||||
await page.route("**/health", r => r.fulfill({ json: { ok: true } }))
|
||||
await page.route("**/api/**", async r => {
|
||||
const path = new URL(r.request().url()).pathname
|
||||
if (r.request().method() === "DELETE") { writes++; await gate; phase = 1; return r.fulfill({ json: { ok: true } }) }
|
||||
if (path === "/api/ipsec") {
|
||||
reads++
|
||||
return phase === 1 ? r.fulfill({ status: 503, json: { error: "Опрос после удаления недоступен" } }) : r.fulfill({ json: { servers: [], clients: phase === 2 ? [] : [client] } })
|
||||
}
|
||||
return r.fulfill({ json: path === "/api/servers" ? [{ id: 1, name: "Роутер", host: "192.0.2.1", enabled: true, country: "RU" }] : path === "/api/auth/config" ? { required: false } : {} })
|
||||
})
|
||||
await page.goto("/ipsec")
|
||||
const rows = page.locator("tr[data-row-id]")
|
||||
await expect(rows).toHaveCount(1)
|
||||
await page.getByRole("button", { name: "Удалить клиента", exact: true }).click()
|
||||
const dialog = page.getByRole("alertdialog")
|
||||
const submit = dialog.getByRole("button", { name: "Удалить", exact: true })
|
||||
await submit.evaluate(el => { (el as HTMLButtonElement).click(); (el as HTMLButtonElement).click() })
|
||||
await expect.poll(() => writes).toBe(1)
|
||||
const before = reads
|
||||
const refresh = page.getByRole("button", { name: "Обновить", exact: true, includeHidden: true })
|
||||
await expect(refresh).toBeDisabled()
|
||||
await refresh.evaluate(el => (el as HTMLButtonElement).click())
|
||||
expect(reads).toBe(before)
|
||||
await expect(submit).toBeDisabled()
|
||||
release!()
|
||||
await expect(dialog).toHaveCount(0)
|
||||
await expect(page.getByRole("alert").filter({ hasText: "Не удалось загрузить IPsec" })).toContainText("Опрос после удаления недоступен")
|
||||
await expect(rows).toHaveCount(1)
|
||||
await expect(rows.getByRole("button", { name: "Удалить клиента" })).toBeDisabled()
|
||||
phase = 2
|
||||
await page.getByRole("button", { name: "Повторить загрузку IPsec" }).click()
|
||||
await expect(rows).toHaveCount(0)
|
||||
await expect(page.getByText("Нет клиентов IKEv2", { exact: true })).toBeVisible()
|
||||
expect(writes).toBe(1)
|
||||
})
|
||||
Reference in new issue
Block a user