gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitfd33ee21fd33ee21Sync reviewed components from 8176eb1mrefd33ee21/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

  • fd33ee21Sync reviewed components from 8176eb1mre
  • 93348453Re-vendor tickets to Hybriel set day35mre
  • a4123831tickets#42: the /connect and /login/callback error pages are app pages: the function routes redirect to /connect/failed/<code> and /login/failed/<code> (components/problem.hl, sentences in lib/problems.hl), htmlPage and its hand-written stylesheet are gone; gate tests/error-pages.mjsmre
  • cefc7b4atickets: a startup error prints its message before exit 1 (hybriel#247)mre
  • 99bd6570tickets: lib/migrate.hl writes no table of another topic any more: projects.hl rewriteSlug / addProjectNamed / hasMembers, tickets.hl moveToProject / ticketRecords / ticketRecordsOf, events.hl renameStates / eventRecords / eventRecordsOf; the migration calls them (tickets#47)mre
  • b57161e3tickets: the markup names its own rules (Style.pMessage, Style.olTickets, ...) instead of hand-written class strings that copy a stylesheet rule; the shared bodies sit in lib/looks.hl (tickets#38)mre
  • f767b24dtickets: 111 needless let became plain declarations (3 in ticket.hl, ticket_list.hl, comments.hl and 108 never-reassigned, not captured loop-body ones); the 212 really reassigned lets stay (tickets#44)mre
  • 16c0d3b5tickets: the modal window is phone first: the full-screen sheet is the base rule, the centred window sits in the min-width 60rem block (tickets#39)mre
  • 090ee809tickets: every component's styles in its own nested Style {} block; styles.hl keeps only the theme inherit, the accent and the site-wide rules (tickets#36)mre
  • d395763ftickets: ident-access gate ports 8769/8768 (8797 is taken on loreana)mre
  • afb9a403tickets: an ident access key (Authorization: Bearer idt_…) works in the API (ident#27 app side): lib/ident-access.hl copied from components, read keys GET only, ident down 503; gate tests/ident-access-tickets.mjsmre
  • 48c1ba79tickets: md-paste-files gate with the file manager casesmre
  • f04afc43tickets: MdEditor from components 59704ce (a file manager copy still attaches, pipe rows, tickets#49)mre
  • 06599fa0tickets: MdEditor from components b0f1ec2 (a clipboard with text always pastes the text, tickets#49); real Ctrl+V gatemre
  • 2e1b116ftickets: re-vendor Hybriel master acaceaf3 (framework ec8d6c1d): plugins/web patch queue while a page is built, anonymous session cookies refresh; gate waits for the comment editors to draw (W174) and clicks the form editor when it stands still; security gate: main's code runs on its own binary, lazy-session cookie checksmre
  • 361bc0fetickets: re-vendor Hybriel 7a684920 (WIP, not deployed); on Error exits 1 at startup; migrate passes its log by reference; gate checks follow lazy sessions and the 413 refusalmre
  • 8a69999cident#28: /login/callback never signs out the session it finds; a valid code puts the user on the same sessionmre
  • 337e9b21tickets#37: a comment's files are open to everyone who can read the commentmre
  • 1800600ftickets: write the standby service out in full so the boot gate can read the main servicemre
  • 8179d287tickets: standby service on its port plus 10 and a deploy that hands over through it without a gapmre