104 lines
3.9 KiB
JavaScript
104 lines
3.9 KiB
JavaScript
// Shared setup for the audit-* checks: feed request tracking, URL waits,
|
|
// and the export download-or-popup accept. One behaviour per check module;
|
|
// this file holds only what several of them need.
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
|
|
const DEFAULT_WINDOW_MS = 30 * 24 * 60 * 60 * 1000;
|
|
|
|
function isAudit(u) {
|
|
return u.pathname === '/api/audit';
|
|
}
|
|
|
|
// Installs request tracking on a fresh check page. Returns the helpers the
|
|
// audit checks share; a check destructures only what it uses.
|
|
function trackAudit(page) {
|
|
page.setDefaultTimeout(10000);
|
|
const auditRequests = [];
|
|
page.on('request', (request) => {
|
|
try {
|
|
const url = new URL(request.url());
|
|
if (isAudit(url) || url.pathname === '/api/audit/export') auditRequests.push(url);
|
|
} catch {
|
|
// A non-URL request cannot be an audit call.
|
|
}
|
|
});
|
|
let loads = 0;
|
|
page.on('load', () => (loads += 1));
|
|
|
|
const requestMatching = async (action, predicate, timeout = 6000) => {
|
|
const waiter = page
|
|
.waitForRequest(
|
|
(request) => {
|
|
try {
|
|
const url = new URL(request.url());
|
|
return (isAudit(url) || url.pathname === '/api/audit/export') && predicate(url);
|
|
} catch {
|
|
return false;
|
|
}
|
|
},
|
|
{ timeout },
|
|
)
|
|
.catch(() => null);
|
|
await action();
|
|
return waiter;
|
|
};
|
|
|
|
const waitForUrl = (predicate, timeout = 8000) =>
|
|
page.waitForURL((url) => predicate(new URL(url)), { timeout });
|
|
|
|
const lastRequest = () => auditRequests[auditRequests.length - 1] || null;
|
|
const loadCount = () => loads;
|
|
|
|
return { requestMatching, waitForUrl, lastRequest, loadCount };
|
|
}
|
|
|
|
// Export resolves its window at click time and opens a tab. A successful
|
|
// export arrives as an attachment download; a failed one stays visible in
|
|
// that tab, so the check accepts either and reads the response from it.
|
|
async function exportOnce(page, fail, label, extension) {
|
|
await page.getByRole('button', { name: 'Export', exact: true }).click();
|
|
const item = page.getByRole('menuitem', { name: label, exact: true });
|
|
await item.waitFor();
|
|
const downloadP = page.waitForEvent('download', { timeout: 15000 }).then((download) => ({ download })).catch(() => null);
|
|
const popupP = page.waitForEvent('popup', { timeout: 15000 }).then((popup) => ({ popup })).catch(() => null);
|
|
await item.click();
|
|
const settled = await Promise.race([downloadP, popupP]);
|
|
if (!settled) {
|
|
fail(`${label} export neither downloaded nor opened a tab`);
|
|
return null;
|
|
}
|
|
if (settled.popup) {
|
|
await settled.popup.waitForLoadState('load').catch(() => {});
|
|
const url = new URL(settled.popup.url());
|
|
assert.equal(url.pathname, '/api/audit/export', `${label} opened ${url.href}`);
|
|
assert.equal(url.searchParams.get('format'), extension);
|
|
assert.ok(url.searchParams.get('from'), `${label} export has no explicit start`);
|
|
assert.ok(url.searchParams.get('to'), `${label} export has no explicit end`);
|
|
const response = await page.request.get(url.href);
|
|
const body = await response.text();
|
|
if (response.status() !== 200) {
|
|
fail(`${label} export failed (${response.status()}): ${body.slice(0, 200)}`);
|
|
return null;
|
|
}
|
|
if (!new RegExp(`attachment;.*\\.${extension}`).test(response.headers()['content-disposition'] || '')) {
|
|
fail(`${label} export opened a tab without an attachment filename`);
|
|
return null;
|
|
}
|
|
return body;
|
|
}
|
|
if (!new RegExp(`\\.${extension}$`).test(settled.download.suggestedFilename())) {
|
|
fail(`${label} export did not download a .${extension} file`);
|
|
return null;
|
|
}
|
|
const failure = await settled.download.failure();
|
|
if (failure) {
|
|
fail(`${label} download failed: ${failure}`);
|
|
return null;
|
|
}
|
|
const saved = await settled.download.path();
|
|
return saved ? fs.readFileSync(saved, 'utf8') : '';
|
|
}
|
|
|
|
module.exports = { DEFAULT_WINDOW_MS, isAudit, trackAudit, exportOnce };
|