gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitfb7111fafb7111faident#34: access page: unstyled test-hook classes (idrow, never, who, whoname, appname, neverrevoke) dropped; the tests use element/name selectorsmrefb7111fa/tests/cookie.mjs

6.3 KB

  1. // tests/cookie.mjs — GATE of ident#28 (F28F): a session whose browser cookie was issued before the 99-year change (issuedAt 0,
  2. // the old 14-day life) gets the 99-year cookie again on its next request: on a selector endpoint, on a page, signed in and anonymous,
  3. // with the same id, and stays signed in. A request with no cookie gets no cookie (lazy sessions). HTTP only, own server (P0, default 8781).
  4. import { spawn } from 'node:child_process';
  5. import { readFileSync, writeFileSync, rmSync, mkdirSync, existsSync } from 'node:fs';
  6. import { createHash } from 'node:crypto';
  7. import { dirname, join } from 'node:path';
  8. import { fileURLToPath } from 'node:url';
  9. const APP = join(dirname(fileURLToPath(import.meta.url)), '..');
  10. const G = join(APP, '.scratch/cookie-gate');
  11. const P0 = Number(process.env.COOKIE_GATE_PORT || 8781);
  12. const BASE = 'http://127.0.0.1:' + P0;
  13. const J = JSON.stringify;
  14. const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
  15. let passed = 0, failed = 0;
  16. const check = (name, ok, detail = '') => { if (ok) { passed++; console.log(' ok ' + name); } else { failed++; console.log(' FAIL ' + name + (detail ? ' — ' + String(detail).slice(0, 300) : '')); } };
  17. rmSync(G, { recursive: true, force: true }); mkdirSync(join(G, 'sess'), { recursive: true });
  18. let out = '';
  19. const p = spawn(join(APP, 'bin/hybriel'), ['project.hl'], { cwd: APP, env: { ...process.env, HL_HOST: '127.0.0.1', SMTP_HOST: '', SMTP_USER: '', SMTP_PASSWORD: '', IDENT_WATCH: '0', IDENT_PORT: String(P0), IDENT_STORAGE: join(G, 'store'), IDENT_SESSIONS: join(G, 'sess'), IDENT_MAIL_SINK: join(G, 'mail.txt'), IDENT_IP_LIMIT: '1000', IDENT_IP_DAY_LIMIT: '1000' }, stdio: ['ignore', 'pipe', 'pipe'] });
  20. p.stdout.on('data', (d) => { out += d; }); p.stderr.on('data', (d) => { out += d; });
  21. p.on('exit', () => writeFileSync(join(G, 'ident.log'), out));
  22. process.on('exit', () => { try { p.kill('SIGTERM'); } catch {} });
  23. let up = false; for (let i = 0; i < 480 && !up; i++) { try { await fetch(BASE + '/api/online'); up = true; } catch { await sleep(250); } }
  24. if (!up) { console.log('FAIL server did not start'); process.exit(1); }
  25. async function emit(cookie, event, payload) {
  26. const r = await fetch(BASE + '/__hl/emit', { method: 'POST', headers: { 'content-type': 'application/json', ...(cookie ? { cookie } : {}) }, body: J({ t: 'emit', i: 1, event, payload }) });
  27. const sc = r.headers.get('set-cookie'); let j = null; try { j = await r.json(); } catch {}
  28. return { v: j && j.value !== undefined ? j.value : j, cookie: sc ? sc.split(';')[0] : null };
  29. }
  30. async function signIn(email) {
  31. const sink = join(G, 'mail.txt');
  32. await fetch(BASE + '/api/code', { method: 'POST', headers: { 'content-type': 'application/json' }, body: J({ email }) });
  33. let code = null;
  34. for (let i = 0; i < 50 && !code; i++) { if (existsSync(sink)) { const l = readFileSync(sink, 'utf8').split('\n').filter((x) => x.startsWith(email + ' ')); if (l.length) code = l.at(-1).split(' ')[1]; } await sleep(100); }
  35. return (await emit(null, 'verifyCode', [email, code, 'UTC'])).cookie;
  36. }
  37. const idOf = (c) => /identsid=([0-9a-f]+)/.exec(c)[1];
  38. const file = (id) => join(G, 'sess', createHash('sha256').update(id).digest('hex'));
  39. const edit = (id, f) => { const j = JSON.parse(readFileSync(file(id), 'utf8')); f(j); writeFileSync(file(id), J(j)); return j; };
  40. const KEY = 'ignored';
  41. // an app so the selector endpoint answers: sign in, create an app
  42. const alice = await signIn('[email protected]');
  43. const app = (await emit(alice, 'appCreate', [{ name: 'GateApp', origins: ['http://127.0.0.1:' + (P0 + 5)] }])).v;
  44. const key = app.app.apiKey, origin = 'http://127.0.0.1:' + (P0 + 5);
  45. // an anonymous session with content: a sign-in code asked for makes one
  46. await fetch(BASE + '/api/code', { method: 'POST', headers: { 'content-type': 'application/json' }, body: J({ email: '[email protected]' }) });
  47. const anonRes = await emit(null, 'rememberPending', ['[email protected]']);
  48. const anonCookie = anonRes.cookie || '';
  49. check('setup: an anonymous session with a cookie exists (pending address remembered)', /identsid=/.test(anonCookie), J(anonRes.v));
  50. const signedId = idOf(alice), anonId = anonCookie ? idOf(anonCookie) : null;
  51. const age = (sc) => { const m = /Max-Age=(\d+)/.exec(sc || ''); return m ? Number(m[1]) : null; };
  52. const ask = async (path, cookie, headers = {}) => { const r = await fetch(BASE + path, { redirect: 'manual', headers: { cookie, ...headers } }); await r.text(); return r.headers.getSetCookie(); };
  53. const old = (id) => edit(id, (j) => { j.issuedSig = ''; j.issuedAt = 0; }); // what a session has whose cookie went out before the change
  54. for (const [who, id, cookie] of [['signed in', signedId, alice], ['anonymous', anonId, anonCookie]]) {
  55. old(id);
  56. let c = await ask('/api/selector/identities?key=' + key, cookie, { origin });
  57. check(who + ': an old cookie on the selector endpoint gets Set-Cookie with the same id and Max-Age 99 years', c.length === 1 && c[0].startsWith('identsid=' + id + ';') && age(c[0]) === 3122064000, J(c));
  58. check(who + ': the session file records the issue now', edit(id, () => {}).issuedAt > 0);
  59. c = await ask('/api/selector/identities?key=' + key, cookie, { origin });
  60. check(who + ': the next selector request sets none', c.length === 0, J(c));
  61. old(id);
  62. c = await ask('/start', cookie);
  63. check(who + ': an old cookie on a page gets the 99-year cookie, same id', c.length === 1 && c[0].startsWith('identsid=' + id + ';') && age(c[0]) === 3122064000, J(c));
  64. old(id);
  65. c = await ask('/api/online', cookie);
  66. check(who + ': an old cookie on another function route (/api/online) gets it too', c.length === 1 && c[0].startsWith('identsid=' + id + ';') && age(c[0]) === 3122064000, J(c));
  67. }
  68. const still = await (await fetch(BASE + '/api/selector/identities?key=' + key, { headers: { cookie: alice, origin } })).json();
  69. check('the signed-in session is still signed in after the re-issue', still.signedIn === true, J(still));
  70. const none = await fetch(BASE + '/api/selector/identities?key=' + key, { headers: { origin } }); await none.text();
  71. check('a selector request with no cookie gets no cookie', none.headers.getSetCookie().length === 0, J(none.headers.getSetCookie()));
  72. const nonePage = await fetch(BASE + '/start'); await nonePage.text();
  73. check('a page request with no cookie gets no cookie (lazy sessions)', nonePage.headers.getSetCookie().length === 0, J(nonePage.headers.getSetCookie()));
  74. console.log(`cookie gate: ${passed} passed, ${failed} failed`);
  75. process.exit(failed ? 1 : 0);

Branches

Latest commits

  • fb7111faident#34: access page: unstyled test-hook classes (idrow, never, who, whoname, appname, neverrevoke) dropped; the tests use element/name selectorsmre
  • 9f2ad803ident#34: notification switches: push/email by name=..., locked by aria-disabled, the switch look is the role=switch button (no switch/push/email/locked classes)mre
  • e0f0ddaaident#34: inbox: the Mark read/unread button is styled as the button of its note-foot, the icon is found as the row's img (no toggle/icon classes)mre
  • 16b49756ident#34: unused row classes (choice, usedrow, kind, shortid) droppedmre
  • f9d6fb38ident#34: apps and identity rows: the Edit/New secret/Delete buttons carry name=..., the API key is found by its element; unused row classes (app, identity, shortid) droppedmre
  • b323265aMerge branch 'f5-styles' into mainIdent
  • 9ced53c3|||PR ident#33 ident#35 every component's styles live in its own Style block; styles.hl keeps the page-wide rest; the account bar is phone first (min-width: 60rem block); the avatar preview is hidden with the hidden attribute (ident#34 in part)mre
  • f28333e4ident#33 ident#35: styles.hl keeps only the accent, the page column and the shared patterns; the account bar is written phone first (base rule + one min-width: 60rem block) instead of a max-width overridemre
  • 5b4541fcident#33: the selector sign-in frame's rules move into embed.hl's Stylemre
  • 00235f21ident#33: the per-app notification switches move into appsettings.hl's Stylemre
  • 2249945cident#33: the inbox and connection list rules move into inbox.hl's Stylemre
  • f2d9f79fident#33: the apps list and the secret box move into apps.hl's Stylemre
  • 8370b748ident#33: the app request box moves into home.hl's Stylemre
  • 40bf3b97ident#33: the 404 page's home link rule moves into notfound.hlmre
  • 40627a89ident#33: the time zone section's spacing moves into timezone.hlmre
  • d389db08ident#33: the identity page's rules and the Notif. chip move into identity.hl's Stylemre
  • ddda0b5bident#33: the identity row's parts move into identitylist.hl's Stylemre
  • 47e516afident#33 ident#34: the identity form's avatar rules move into identityform.hl; the preview and Remove button are hidden with the hidden attribute instead of built class strings (avatarPreviewClass, avatarRemoveClass gone)mre
  • 5cb5cf0eident#33: the code field's look moves into codestep.hl's Stylemre
  • f421cd60ident#33: the shell's own rules (user menu address, offline note, network probe) move into main.hl's Stylemre