48 lines
3.2 KiB
TypeScript
48 lines
3.2 KiB
TypeScript
import { test, expect } from '@playwright/test'
|
|
|
|
for (const outcome of ['success', 'error'] as const) {
|
|
test(`Потоки: каталог ${outcome} блокирует неподтверждённые HTTP/SSE запросы`, async ({ page }) => {
|
|
let release!: () => void
|
|
const gate = new Promise<void>(resolve => { release = resolve })
|
|
const scoped: Array<{ path: string; serverId: string | null }> = []
|
|
const stats = { bpsNow: 0, bytes: 200, packets: 2, conversations: 1, uniqueSrc: 1, uniqueDst: 1, topProto: 'TCP', rxSeries: [], txSeries: [], applications: [], protocols: [], sources: [], destinations: [], interfaces: [], conversationsList: [], ifaces: [], live: false }
|
|
const card = { id: '1', name: 'Подтверждённый экспортёр', subtitle: '', site: 'TEST', country: '', status: 'online', rxNow: 0, txNow: 0, sessions: 1, bytes: 200, rxSeries: [], txSeries: [] }
|
|
await page.route('**/health', route => route.fulfill({ json: { ok: true } }))
|
|
await page.route('**/api/**', async route => {
|
|
const url = new URL(route.request().url()), path = url.pathname
|
|
if (['/api/traffic/flow/analytics', '/api/traffic/flow/monthly', '/api/traffic/flow/live'].includes(path)) scoped.push({ path, serverId: url.searchParams.get('serverId') })
|
|
if (path === '/api/traffic/flow/exporters') {
|
|
await gate
|
|
if (outcome === 'error') return route.fulfill({ status: 503, json: { error: 'Каталог экспортёров недоступен' } })
|
|
return route.fulfill({ json: { exporters: [card] } })
|
|
}
|
|
if (path.endsWith('/live')) return route.fulfill({ status: 503, json: {} })
|
|
const json = path === '/api/auth/config' ? { required: false }
|
|
: path === '/api/traffic/flow/analytics' ? stats
|
|
: path === '/api/traffic/flows' ? { exportersOnline: 1, bytesPerMin: 200, uniqueSrc: 1, uniqueDst: 1, topProto: 'TCP', talkers: [], listenerBound: true }
|
|
: path === '/api/traffic/servers' ? { servers: [] }
|
|
: path === '/api/traffic/users' ? { users: [] }
|
|
: path === '/api/traffic/flow/clients' ? { clients: [] }
|
|
: path.endsWith('/interfaces') ? { interfaces: [] }
|
|
: path === '/api/servers' ? [] : {}
|
|
return route.fulfill({ json })
|
|
})
|
|
await page.goto('/traffic')
|
|
const catalog = page.waitForRequest(r => new URL(r.url()).pathname === '/api/traffic/flow/exporters')
|
|
await page.getByRole('group', { name: 'Разделы трафика' }).getByRole('button', { name: 'Потоки', exact: true }).click()
|
|
await catalog
|
|
await expect(page.getByText('Загрузка потоков…', { exact: true })).toBeVisible()
|
|
await page.waitForTimeout(500)
|
|
expect(scoped).toEqual([])
|
|
release()
|
|
if (outcome === 'success') {
|
|
await expect.poll(() => scoped.some(r => r.path.endsWith('/analytics'))).toBe(true)
|
|
await expect.poll(() => scoped.some(r => r.path.endsWith('/live'))).toBe(true)
|
|
expect(scoped.every(r => r.serverId === '1')).toBe(true)
|
|
} else {
|
|
await expect(page.getByRole('alert').filter({ hasText: 'Потоки недоступны' })).toContainText('Каталог экспортёров недоступен')
|
|
expect(scoped).toEqual([])
|
|
}
|
|
})
|
|
}
|