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
- 35980936inbox: preserve desktop columns after list styles were scopedmre
- c077a5cbstyles: format Style with nested language rulesmre
- ccdde2beticket: format Style with nested language rulesmre
- d083535bticket_list: nest Style rules and bare media queriesmre
- c24608ebmarkdown: format Style with nested language rulesmre
- de964b3bsettings: format Style with nested language rulesmre
- 03c1de05new_project: format Style with nested language rulesmre
- bd76cfa6connect: format Style with nested language rulesmre
- 01fad593inbox: format Style with nested language rulesmre
- 79dc9392you: format Style with nested language rulesmre
- 78073baamain: format Style with nested language rulesmre
- 5499c290Sync theme unchanged from layouts 17e0f03mre
- 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