gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain5d144817Merge branch 'records-t40' into mainTicketsmain/tests/error-pages.mjs

6.7 KB

  1. // 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
  2. // 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
  3. // 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
  4. // 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).
  5. // Real headless Chrome at 390 and 1280 (this is the automated gate; the proof of the change is done in the user's Chrome).
  6. // Ports: TICKETS_ERRPAGES_PORT (8744), +1 the ident stub; Chrome TICKETS_ERRPAGES_CHROME (8746-8750). TICKETS_COLD_MS: a cold start (120 s).
  7. import { spawn } from 'node:child_process';
  8. import { rmSync, mkdirSync, existsSync } from 'node:fs';
  9. import { dirname, join, resolve } from 'node:path';
  10. import { fileURLToPath } from 'node:url';
  11. import { launchBrowser } from './cdp.mjs';
  12. import http from 'node:http';
  13. if (!process.env.HL_CHROME && existsSync('/opt/google/chrome/chrome')) process.env.HL_CHROME = '/opt/google/chrome/chrome';
  14. const APP = resolve(dirname(fileURLToPath(import.meta.url)), '..');
  15. const BIN = join(APP, 'bin/hybriel');
  16. const PORT = Number(process.env.TICKETS_ERRPAGES_PORT || 8744), BASE = `http://127.0.0.1:${PORT}`;
  17. const IDENT_PORT = PORT + 1;
  18. const WORK = join(APP, '.scratch/error-pages-gate'), STORE = join(WORK, 'store');
  19. const sleep = (ms) => new Promise(r => setTimeout(r, ms));
  20. rmSync(WORK, { recursive: true, force: true }); mkdirSync(join(STORE, 'mpackdb'), { recursive: true });
  21. let passes = 0, failures = 0, log = '', server = null, ident = null, browser = null;
  22. const check = (label, ok, detail = '') => { console.log(`${ok ? 'ok ' : 'FAIL'} ${label}${ok ? '' : ' — ' + detail}`); if (ok) passes++; else failures++; };
  23. // the cases: [what, path, expected redirect target (new code), the sentence the page must show]
  24. const GOOD_RETURN = encodeURIComponent('https://gitoria.worldapi.org/x');
  25. const CASES = [
  26. ['a /connect link without an app name', '/connect?return=' + GOOD_RETURN, '/connect/failed/connect-refused', /not valid/],
  27. ['a /connect link with a bad return address', '/connect?app=a&return=' + encodeURIComponent('https://evil.example/x'), '/connect/failed/connect-refused', /not valid/],
  28. ['/login/callback without a code', '/login/callback', '/login/failed/login-no-code', /no login code/],
  29. ['/login/callback with a code ident does not know', '/login/callback?ident_code=' + 'ab'.repeat(12), '/login/failed/login-refused', /did not accept/],
  30. ];
  31. async function main() {
  32. 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"}'); }); });
  33. await new Promise(ok => ident.listen(IDENT_PORT, '127.0.0.1', ok));
  34. const base = `http://127.0.0.1:${IDENT_PORT}`;
  35. 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'),
  36. 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' } });
  37. server.stdout.on('data', d => log += d); server.stderr.on('data', d => log += d);
  38. 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); }
  39. // over HTTP: each case redirects (302, no body page), the page it names is a normal app page
  40. for (const [what, path, target, say] of CASES) {
  41. const r = await fetch(BASE + path, { redirect: 'manual' });
  42. check(`${what}: 302 to ${target}`, r.status === 302 && r.headers.get('location') === target, r.status + ' ' + r.headers.get('location'));
  43. const t = await fetch(BASE + target); const html = await t.text();
  44. 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));
  45. }
  46. // the HTML never repeats text of the URL: an unknown code gets the general sentence
  47. const evil = await (await fetch(BASE + '/login/failed/' + encodeURIComponent('<img src=x onerror=alert(1)>'))).text();
  48. check('an unknown code shows the general sentence and no text of the URL', !/<img src=x/.test(evil) && /Something went wrong/.test(evil));
  49. const post = await fetch(BASE + '/login/callback', { method: 'POST', redirect: 'manual' });
  50. 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);
  51. browser = await launchBrowser({ debugPortRange: (process.env.TICKETS_ERRPAGES_CHROME || '8746-8750').split('-').map(Number) });
  52. for (const W of [390, 1280]) {
  53. const p = await browser.newPage();
  54. try {
  55. await p.send('Emulation.setDeviceMetricsOverride', { width: W, height: W === 390 ? 844 : 800, deviceScaleFactor: 1, mobile: W === 390 });
  56. for (const [what, path, target, say] of CASES) {
  57. await p.goto(BASE + path);
  58. await p.waitForSelector('#error');
  59. 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 } })()`);
  60. check(`${W}px ${what}: lands on ${target} showing the sentence`, st.path === target && say.test(st.text), st.path + ' ' + st.text);
  61. 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));
  62. }
  63. } finally { await p.close(); }
  64. }
  65. }
  66. main().catch(e => { check('gate ran to the end', false, e.stack || String(e)); }).finally(async () => {
  67. try { if (browser) await browser.close(); } catch {}
  68. if (server) server.kill(); if (ident) ident.close();
  69. console.log(`\n${passes} passed, ${failures} failed`);
  70. if (failures) console.log(log.slice(-1500));
  71. process.exit(failures ? 1 : 0);
  72. });

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