// Audit filters are server-evaluated through the URL contract: search, // category, time range, back/forward restore, denied, and the actor filter. const assert = require('node:assert/strict'); const { trackAudit } = require('./helpers/audit-helpers.cjs'); module.exports = async ({ page, go, shot, fail }) => { const { requestMatching, waitForUrl } = trackAudit(page); await go('/settings/audit'); const status = await page.evaluate(async () => (await fetch('/api/auth/status')).json()); const actor = status?.user?.id; assert.ok(actor, 'signed-in identity has no id'); const search = page.getByLabel('Search events', { exact: true }); let request = await requestMatching( () => search.fill('repository'), (u) => u.searchParams.get('q') === 'repository', ); assert.ok(request, 'search issued no matching /api/audit request'); await waitForUrl((u) => u.searchParams.get('q') === 'repository'); assert.equal((await search.inputValue()), 'repository'); await go('/settings/audit'); request = await requestMatching( () => page.getByRole('button', { name: 'Runtime', exact: true }).click(), (u) => u.searchParams.get('categories') === 'runtime', ); assert.ok(request, 'category filter issued no matching /api/audit request'); await waitForUrl((u) => u.searchParams.get('categories') === 'runtime'); assert.equal( await page.getByRole('button', { name: 'Runtime', exact: true }).getAttribute('aria-pressed'), 'true', ); // Time: Any time drops the window from the request rather than guessing one. await page.getByRole('combobox', { name: 'Time range', exact: true }).click(); request = await requestMatching( () => page.getByRole('option', { name: 'Any time', exact: true }).click(), (u) => u.searchParams.get('from') === null && u.searchParams.get('categories') === 'runtime', ); assert.ok(request, 'Any time issued no matching /api/audit request'); await waitForUrl((u) => u.searchParams.get('since') === 'any'); assert.equal(await page.getByRole('combobox', { name: 'Time range', exact: true }).innerText(), 'Any time'); // Back/forward restore the controls the URL describes. const backRequest = page.waitForRequest( (r) => { const u = new URL(r.url()); return u.pathname === '/api/audit' && u.searchParams.get('categories') === 'runtime' && !!u.searchParams.get('from'); }, { timeout: 8000 }, ); await page.goBack(); await backRequest; await waitForUrl((u) => u.searchParams.get('categories') === 'runtime' && u.searchParams.get('since') === null); assert.equal(await page.getByRole('combobox', { name: 'Time range', exact: true }).innerText(), 'Last 30 days'); assert.equal( await page.getByRole('button', { name: 'Runtime', exact: true }).getAttribute('aria-pressed'), 'true', 'back did not restore the category toggle', ); const forwardRequest = page.waitForRequest( (r) => { const u = new URL(r.url()); return u.pathname === '/api/audit' && u.searchParams.get('categories') === 'runtime' && u.searchParams.get('from') === null; }, { timeout: 8000 }, ); await page.goForward(); await forwardRequest; await waitForUrl((u) => u.searchParams.get('since') === 'any'); assert.equal(await page.getByRole('combobox', { name: 'Time range', exact: true }).innerText(), 'Any time'); // Denied reaches the server through the same URL contract as the other // filters. It must both update the URL and issue a request. await go('/settings/audit'); const denied = page.getByRole('button', { name: 'Denied', exact: true }); request = await requestMatching( () => denied.click(), (u) => u.searchParams.get('outcome') === 'denied', 3000, ); const deniedUrl = new URL(page.url()); if (!request || deniedUrl.searchParams.get('outcome') !== 'denied') { await shot('denied'); fail( `Denied filter did not reach the server: URL ${page.url()} has outcome=${deniedUrl.searchParams.get('outcome') ?? 'absent'}, ` + `aria-pressed=${await denied.getAttribute('aria-pressed')}, no /api/audit request with outcome=denied`, ); } else { assert.equal(await denied.getAttribute('aria-pressed'), 'true'); } // Advanced actor filter: server-evaluated, in the URL, shown as a chip. await go('/settings/audit'); await page.getByRole('button', { name: 'Filters', exact: true }).click(); const actorInput = page.getByLabel('Actor', { exact: true }); request = await requestMatching( async () => { await actorInput.fill(actor); await actorInput.press('Enter'); }, (u) => u.searchParams.get('actor') === actor, ); assert.ok(request, 'advanced actor filter issued no matching /api/audit request'); await waitForUrl((u) => u.searchParams.get('actor') === actor); assert.match( await page.locator('ul[aria-label="Active filters"] li', { hasText: 'Actor:' }).first().innerText(), new RegExp(actor), ); await shot('filters'); await page.keyboard.press('Escape'); };