tickets
All repositories: gitoria
6.7 KB
// tests/error-pages.mjs — tickets#42: THE ERROR PAGES OF /connect AND /login/callback ARE PAGES OF THE APP. A bad /connect link and a /login/callback// with a bad (or no) code redirect to a component page (/connect/failed/<code>, /login/failed/<code>) inside the normal shell: the header with ident's// selector, the theme; no hand-written HTML string with its own <style> and hard-coded colours (the old lib/api-helpers.hl htmlPage). The reason is a// short code in the path, the sentence comes from lib/problems.hl: nothing of the URL is rendered. Fails on the old code (it answered 400 with the stub page).// Real headless Chrome at 390 and 1280 (this is the automated gate; the proof of the change is done in the user's Chrome).// Ports: TICKETS_ERRPAGES_PORT (8744), +1 the ident stub; Chrome TICKETS_ERRPAGES_CHROME (8746-8750). TICKETS_COLD_MS: a cold start (120 s).import { spawn } from 'node:child_process';import { rmSync, mkdirSync, existsSync } from 'node:fs';import { dirname, join, resolve } from 'node:path';import { fileURLToPath } from 'node:url';import { launchBrowser } from './cdp.mjs';import http from 'node:http';if (!process.env.HL_CHROME && existsSync('/opt/google/chrome/chrome')) process.env.HL_CHROME = '/opt/google/chrome/chrome';const APP = resolve(dirname(fileURLToPath(import.meta.url)), '..');const BIN = join(APP, 'bin/hybriel');const PORT = Number(process.env.TICKETS_ERRPAGES_PORT || 8744), BASE = `http://127.0.0.1:${PORT}`;const IDENT_PORT = PORT + 1;const WORK = join(APP, '.scratch/error-pages-gate'), STORE = join(WORK, 'store');const sleep = (ms) => new Promise(r => setTimeout(r, ms));rmSync(WORK, { recursive: true, force: true }); mkdirSync(join(STORE, 'mpackdb'), { recursive: true });let passes = 0, failures = 0, log = '', server = null, ident = null, browser = null;const check = (label, ok, detail = '') => { console.log(`${ok ? 'ok ' : 'FAIL'} ${label}${ok ? '' : ' — ' + detail}`); if (ok) passes++; else failures++; };// the cases: [what, path, expected redirect target (new code), the sentence the page must show]const GOOD_RETURN = encodeURIComponent('https://gitoria.worldapi.org/x');const CASES = [['a /connect link without an app name', '/connect?return=' + GOOD_RETURN, '/connect/failed/connect-refused', /not valid/],['a /connect link with a bad return address', '/connect?app=a&return=' + encodeURIComponent('https://evil.example/x'), '/connect/failed/connect-refused', /not valid/],['/login/callback without a code', '/login/callback', '/login/failed/login-no-code', /no login code/],['/login/callback with a code ident does not know', '/login/callback?ident_code=' + 'ab'.repeat(12), '/login/failed/login-refused', /did not accept/],];async function main() {ident = http.createServer((req, res) => { let b = ''; req.on('data', d => b += d); req.on('end', () => { res.writeHead(400, { 'content-type': 'application/json' }); res.end('{"error":"unknown code"}'); }); });await new Promise(ok => ident.listen(IDENT_PORT, '127.0.0.1', ok));const base = `http://127.0.0.1:${IDENT_PORT}`;server = spawn(BIN, ['project.hl'], { cwd: APP, stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, TICKETS_PORT: String(PORT), TICKETS_STORAGE: join(STORE, 'mpackdb'), TICKETS_SESSIONS: join(STORE, 'sessions'),IDENT_URL: base, IDENT_EXCHANGE_URL: base, TICKETS_PUBLIC_URL: BASE, IDENT_API_KEY: 'pk_stub', IDENT_API_SECRET: 'sk_stub', TICKETS_WATCH: '0', HL_HOST: '127.0.0.1' } });server.stdout.on('data', d => log += d); server.stderr.on('data', d => log += d);for (const end = Date.now() + Number(process.env.TICKETS_COLD_MS || 120000); Date.now() < end;) { try { if ((await fetch(BASE + '/')).ok) break; } catch {} await sleep(250); }// over HTTP: each case redirects (302, no body page), the page it names is a normal app pagefor (const [what, path, target, say] of CASES) {const r = await fetch(BASE + path, { redirect: 'manual' });check(`${what}: 302 to ${target}`, r.status === 302 && r.headers.get('location') === target, r.status + ' ' + r.headers.get('location'));const t = await fetch(BASE + target); const html = await t.text();check(`${what}: the error page answers 200 with the app's shell (ident selector, no inline stylesheet with colours)`, t.status === 200 && html.includes('id="selector"') && !/rgb\(25, 30, 35\)|#c586c0|#f44747/.test(html) && !/<style>body\{margin:0/.test(html), t.status + ' ' + html.slice(0, 200));}// the HTML never repeats text of the URL: an unknown code gets the general sentenceconst evil = await (await fetch(BASE + '/login/failed/' + encodeURIComponent('<img src=x onerror=alert(1)>'))).text();check('an unknown code shows the general sentence and no text of the URL', !/<img src=x/.test(evil) && /Something went wrong/.test(evil));const post = await fetch(BASE + '/login/callback', { method: 'POST', redirect: 'manual' });check('a POST to /login/callback also redirects to the error page', post.status === 302 && post.headers.get('location') === '/login/failed/login-method', post.status);browser = await launchBrowser({ debugPortRange: (process.env.TICKETS_ERRPAGES_CHROME || '8746-8750').split('-').map(Number) });for (const W of [390, 1280]) {const p = await browser.newPage();try {await p.send('Emulation.setDeviceMetricsOverride', { width: W, height: W === 390 ? 844 : 800, deviceScaleFactor: 1, mobile: W === 390 });for (const [what, path, target, say] of CASES) {await p.goto(BASE + path);await p.waitForSelector('#error');const st = await p.evaluate(`(() => { const h = document.querySelector('application-header'); const hb = h && h.getBoundingClientRect(); return { path: location.pathname, text: document.querySelector('#error').textContent, selector: !!document.querySelector('application-header #selector'), hdr: !!hb && hb.top >= 0 && hb.top < 5 && hb.width >= innerWidth * 0.95 && hb.height > 20, styles: [...document.querySelectorAll('style')].filter(x => /rgb\\(25, 30, 35\\)|#c586c0/.test(x.textContent)).length, overflow: document.documentElement.scrollWidth > innerWidth } })()`);check(`${W}px ${what}: lands on ${target} showing the sentence`, st.path === target && say.test(st.text), st.path + ' ' + st.text);check(`${W}px ${what}: the app's header at the top holds the ident selector, no hard-coded inline style, no sideways scroll`, st.selector && st.hdr && st.styles === 0 && !st.overflow, JSON.stringify(st));}} finally { await p.close(); }}}main().catch(e => { check('gate ran to the end', false, e.stack || String(e)); }).finally(async () => {try { if (browser) await browser.close(); } catch {}if (server) server.kill(); if (ident) ident.close();console.log(`\n${passes} passed, ${failures} failed`);if (failures) console.log(log.slice(-1500));process.exit(failures ? 1 : 0);});
Branches
Latest commits
- 5d144817Merge branch 'records-t40' into mainTickets
- 44ee6f06|||PR records: #43 #45 livemre
- b8fdeee9Merge branch 'p39-routes' into mainTickets
- a0187a0a|||PR Remove redundant route-order loopmre
- 369e4b9cMerge branch 'k40-lists' into mainTickets
- 9af8e780|||PR Use keyed list mutations for live ticket updatesmre
- 610d8e8aMerge branch 'records-t39' into mainTickets
- 32bcdf24|||PR records: GO P39 K40mre
- 4123c6d1Merge branch 'records-t38' into mainTickets
- cd3641a1|||PR records: day72 deploy live, tickets closedmre
- 7d44d852Merge branch 'records-t37' into mainTickets
- 926f7061|||PR records: merges, D36 deploymre
- bf58534eMerge branch 'l33-final' into mainTickets
- 759a9ac3|||PR thin login (tickets#46, #45): merge main, route entry cleanupmre
- 239436d8Merge remote-tracking branch 'origin/main' into l33-finalmre
- 5822cb3aMerge branch 'o34-clean' into mainTickets
- 0870653c|||PR offline note from __hlOnline, no polling (tickets#41)mre
- 14cf96b4Merge branch 'records-t36' into mainTickets
- 07749fa1|||PR records: day72 + #40 mergedmre
- ecd7e226|||PR Consolidate login and move handler logic into topic libraries (partial)mre