Files
compras/scripts/work-context-http-check.mjs
2026-11-02 08:08:05 +02:00

83 lines
2.8 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
const base = 'http://127.0.0.1:8005';
const cookies = new Map();
async function request(path, body) {
const response = await fetch(base + path, {
method: body ? 'POST' : 'GET',
redirect: 'manual',
headers: {
Accept: 'application/json',
Cookie: [...cookies]
.map(([key, value]) => `${key}=${value}`)
.join('; '),
...(body
? {
'Content-Type': 'application/x-www-form-urlencoded',
'X-XSRF-TOKEN': decodeURIComponent(
cookies.get('XSRF-TOKEN') ?? '',
),
}
: {}),
},
...(body ? { body: new URLSearchParams(body) } : {}),
});
for (const cookie of response.headers.getSetCookie()) {
const pair = cookie.split(';', 1)[0],
index = pair.indexOf('=');
cookies.set(pair.slice(0, index), pair.slice(index + 1));
}
const text = await response.text();
const match = text.match(
/<script data-page="app" type="application\/json">([\s\S]*?)<\/script>/,
);
return {
status: response.status,
page: match ? JSON.parse(match[1]) : null,
};
}
const first = await request('/dashboard');
assert.equal(first.status, 200);
assert.equal(first.page.component, 'work-context');
assert.equal(first.page.props.context.available.length, 4);
const results = [];
for (const [option, allowed, denied, expected] of [
['procurement.internal', '/requisicoes', '/pessoas', 200],
['people.internal', '/pessoas', '/requisicoes', 200],
['procurement.sales', '/requisicoes', '/pessoas', 302],
['people.third_party', '/pessoas', '/requisicoes', 302],
]) {
const selected = await request('/contexto', {
area: option.split('.')[0],
option,
});
assert.equal(selected.status, 302);
const dashboard = await request('/dashboard');
assert.equal(dashboard.page.props.context.active, option);
const access = await request(allowed),
blocked = await request(denied);
assert.equal(access.status, expected);
assert.equal(blocked.status, 403);
results.push({ option, access: access.status, other_area: blocked.status });
}
await request('/contexto', {
area: 'procurement',
option: 'procurement.internal',
});
const proof = {
checked_at: new Date().toISOString(),
environment: base,
evaluation_only: true,
results,
created_accounts: false,
business_commands: false,
visual_verified: false,
};
await fs.writeFile(
new URL('../docs/proofs/work-context-http.json', import.meta.url),
JSON.stringify(proof, null, 2) + '\n',
);
console.log(JSON.stringify(proof));