gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit6d4f996e6d4f996esessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre6d4f996e/tests/shortid.mjs

7.6 KB

  1. // tests/shortid.mjs — THE SHORT ID GATE (ident#23): one public id per identity, the same in every app.
  2. // Own ident :8706 (fresh storage .scratch/shortid-gate), no browser: every step is HTTP.
  3. // * every identity (default and added ones) has 5 characters from the alphabet without look-alikes
  4. // * the identity page shows it; the ids are unique; the id never changes (edit, sign out of an app)
  5. // * two apps, one identity → the exchange gives the SAME id; another identity → another id
  6. // * notifications name the identity by it (case does not matter); a wrong id / another app's user → 404
  7. // * migrate-ids: strict body, wrong secret 401, nothing to migrate on a new store
  8. import { spawn } from 'node:child_process';
  9. import { readFileSync, writeFileSync, rmSync, mkdirSync } from 'node:fs';
  10. import { dirname, join } from 'node:path';
  11. import { fileURLToPath } from 'node:url';
  12. import { sleep } from './cdp.mjs';
  13. const APP = join(dirname(fileURLToPath(import.meta.url)), '..');
  14. const G = join(APP, '.scratch/shortid-gate');
  15. const P0 = Number(process.env.SHORTID_GATE_PORT || 8706); // ident = P0, the fixture site = P0+1
  16. const ID = 'http://127.0.0.1:' + P0;
  17. const SITE = 'http://127.0.0.1:' + (P0 + 1);
  18. const J = JSON.stringify;
  19. let passed = 0, failed = 0;
  20. const check = (name, ok, detail = '') => {
  21. if (ok) { passed++; console.log(' ok ' + name); } else { failed++; console.log(' FAIL ' + name + (detail ? ' — ' + detail : '')); }
  22. };
  23. rmSync(G, { recursive: true, force: true });
  24. mkdirSync(G, { recursive: true });
  25. const p = spawn(join(APP, 'bin/hybriel'), ['project.hl'], {
  26. cwd: APP, stdio: ['ignore', 'pipe', 'pipe'],
  27. env: { ...process.env, SMTP_HOST: '', SMTP_USER: '', SMTP_PASSWORD: '', IDENT_PORT: String(P0), IDENT_STORAGE: join(G, 'ident'), IDENT_SESSIONS: join(G, 'sess') + '/', IDENT_MAIL_SINK: join(G, 'mail.txt'), IDENT_IP_LIMIT: '1000', IDENT_IP_DAY_LIMIT: '1000' },
  28. });
  29. let log = ''; p.stdout.on('data', d => { log += d; }); p.stderr.on('data', d => { log += d; });
  30. const SHORT = /^[2-9a-hj-km-np-z]{5}$/;
  31. let emitI = 0;
  32. async function emit(event, payload, cookie) {
  33. const r = await fetch(ID + '/__hl/emit', { method: 'POST', headers: { 'content-type': 'application/json', ...(cookie ? { cookie } : {}) }, body: J({ t: 'emit', i: ++emitI, event, payload }) });
  34. const j = JSON.parse(await r.text());
  35. return { cookie: (r.headers.get('set-cookie') || '').split(';')[0], value: j.value };
  36. }
  37. async function api(path, body) {
  38. const r = await fetch(ID + path, { method: 'POST', headers: { 'content-type': 'application/json' }, body: typeof body === 'string' ? body : J(body) });
  39. const t = await r.text(); let j = null; try { j = JSON.parse(t); } catch {}
  40. return { status: r.status, j, t };
  41. }
  42. const lastCode = (email) => readFileSync(join(G, 'mail.txt'), 'utf8').trim().split('\n').filter(l => l.startsWith(email + ' ')).pop().split(' ')[1];
  43. async function login(email) {
  44. await api('/api/code', { email });
  45. const v = await emit('verifyCode', [email, lastCode(email), 'UTC']);
  46. return { cookie: v.cookie, ids: v.value.identities };
  47. }
  48. async function appLogin(cookie, app, identity) {
  49. const l = await fetch(ID + '/login?key=' + app.key + '&return=' + encodeURIComponent(SITE + '/cb'), { redirect: 'manual' });
  50. const rid = (l.headers.get('location') || '').split('/').pop();
  51. const c = await emit('chooseIdentity', [rid, identity], cookie);
  52. const x = await api('/api/exchange', { key: app.key, secret: app.secret, code: new URL(c.value.url).searchParams.get('ident_code') });
  53. if (x.status !== 200) throw new Error('exchange failed: ' + x.t);
  54. return x.j.identity;
  55. }
  56. try {
  57. for (let i = 0; i < 80; i++) { try { await fetch(ID + '/', { redirect: 'manual' }); break; } catch {} await sleep(250); }
  58. const a = await login('[email protected]');
  59. const def = a.ids[0];
  60. check('the default identity has a short id: 5 characters, no look-alikes', SHORT.test(def.shortId), J(def));
  61. const added = (await emit('addIdentity', [{ identityName: 'Work' }], a.cookie)).value;
  62. const work = added.identities.find(i => i.identityName === 'Work');
  63. check('a new identity gets one too, another one', SHORT.test(work.shortId) && work.shortId !== def.shortId, J(added.identities));
  64. const b = await login('[email protected]');
  65. check('another account: another id', SHORT.test(b.ids[0].shortId) && ![def.shortId, work.shortId].includes(b.ids[0].shortId));
  66. const home = await (await fetch(ID + '/', { headers: { cookie: a.cookie } })).text();
  67. check('the identity page shows the id of each identity', home.includes('>' + def.shortId + '<') && home.includes('>' + work.shortId + '<'));
  68. // many ids: all valid, all different
  69. const many = [];
  70. for (let i = 0; i < 40; i++) many.push((await emit('addIdentity', [{ identityName: 'x' + i }], a.cookie)).value.identities.at(-1).shortId);
  71. check('40 more identities: all valid and different from each other', many.every(v => SHORT.test(v)) && new Set([...many, def.shortId, work.shortId]).size === 42);
  72. const noLookalike = many.join('') + def.shortId + work.shortId;
  73. check('no 0, 1, o, i or l anywhere', !/[01oil]/.test(noLookalike), noLookalike);
  74. const mk = async (name) => { const v = (await emit('appCreate', [{ name, origins: [SITE] }], a.cookie)).value; return { key: v.app.apiKey, secret: v.secret }; };
  75. const A = await mk('Chat'), B = await mk('Shop');
  76. const inA = await appLogin(a.cookie, A, def.id), inB = await appLogin(a.cookie, B, def.id);
  77. check('the same identity gets the SAME id in two apps, and it is its short id', inA === inB && inA === def.shortId, J([inA, inB, def.shortId]));
  78. const inAwork = await appLogin(a.cookie, A, work.id);
  79. check('another identity → another id', inAwork === work.shortId && inAwork !== inA, inAwork);
  80. const edit = (await emit('editIdentity', [def.id, { nickname: 'Al' }], a.cookie)).value;
  81. check('editing the identity does not change its id', edit.identities.find(i => i.id === def.id).shortId === def.shortId);
  82. const bIn = await appLogin(b.cookie, A, b.ids[0].id);
  83. check('bob in app A: his own short id, no email or names in the answer', bIn === b.ids[0].shortId);
  84. // notifications by the short id
  85. const note = (app, identity) => api('/api/notify', { key: app.key, secret: app.secret, identity, name: 'New comment', text: 'hello' });
  86. let n = await note(A, def.shortId);
  87. check('notify by the short id → 200', n.status === 200 && n.j.id, n.t);
  88. n = await note(A, def.shortId.toUpperCase());
  89. check('capital letters do not matter', n.status === 200, n.t);
  90. n = await note(B, work.shortId);
  91. check('an identity that never logged in to this app → 404', n.status === 404, n.t);
  92. n = await note(A, 'zzzzz');
  93. check('an unknown id → 404', n.status === 404, n.t);
  94. n = await note(A, 'a0l1o');
  95. check('look-alike characters are no id → 404', n.status === 404, n.t);
  96. // the mapping endpoint
  97. let m = await api('/api/migrate-ids', { key: A.key, secret: A.secret });
  98. check('migrate-ids on a new store: nothing to map', m.status === 200 && J(m.j.ids) === '{}' && m.j.finished === false, m.t);
  99. m = await api('/api/migrate-ids', { key: A.key, secret: 'sk_' + '0'.repeat(48) });
  100. check('migrate-ids with a wrong secret → 401', m.status === 401, m.t);
  101. m = await api('/api/migrate-ids', { key: A.key, secret: A.secret, extra: 1 });
  102. check('migrate-ids with an unknown field → 400 naming it', m.status === 400 && /extra/.test(m.t), m.t);
  103. const g = await fetch(ID + '/api/migrate-ids');
  104. check('migrate-ids is POST only → 405', g.status === 405);
  105. } catch (err) {
  106. failed++; console.log(' FAIL (aborted) ' + err.stack);
  107. } finally {
  108. p.kill(); await sleep(300);
  109. writeFileSync(join(G, 'ident.log'), log);
  110. }
  111. console.log(`\n${passed} passed, ${failed} failed`);
  112. process.exit(failed ? 1 : 0);

Branches

Latest commits

  • 6d4f996esessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
  • 029eb5e5deploy: security gate (tests/security.mjs)mre
  • d3c065a8security audit (SI): uploadMax 0, Secure __Host- cookie behind https, headers on function routes/files, page CSP+referrer meta, app/identity caps, tests/security.mjs, reportmre
  • 5561a0cbreport I4-mobilemre
  • 4ca2ed20404 page in the shell (viewport, status 404, way home); phone: account actions live in the user menu so the open menu covers nothing; mobile gate; signoutall waits for a cold startmre
  • 9baad745tests: browser gate waits for the reloaded document (offline reload race under CPU throttle); HL_CPU_THROTTLE knobmre
  • b96796bdtests: wait for hydration before real clicks after a navigation (H2)mre
  • 9722964fre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • c9b196c9tests/iplimit: wait for the welcome form itself after B's first login (identities list renders before it); mission 017 reportmre
  • 72e0f587ident#26: identity page with "Used in" (app, since, last use, Notif. chip, empty state); lastLogin recorded at exchange; gates + lisbeth E2E; mission 016 reportmre
  • 94576fbcident#26 WIP: identity page /identity/:iid with "Used in" (app, since, last use, Notif. chip), lastLogin recorded at exchange, "Used in…" link on the identities list; apps gate 4b, e2e-usedin.mjsmre
  • 349cb502tests/invites: wait for hydration on the code page before entering the code (native GET submit raced the welcome form); mission 015 reportmre
  • d1bf4720tests: no hard-coded ports — every gate's ports and fixture origins come from <GATE>_PORT / _CHROME env (defaults unchanged; Bob/fixture origins follow, selector cookie name too)mre
  • 6280c92fre-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 147744bftests/migration: the fixture app origin follows MIGRATION_GATE_TESTAPP_PORT (oldstore-009 OLD_TESTAPP)mre
  • 48cd36bctests/migration: ports from env (MIGRATION_GATE_PORT, MIGRATION_GATE_TESTAPP_PORT); 8395/8396 were taken by a foreign servermre
  • 3d9b2fd8docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 19b7e3c3deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • c8a596f4re-vendor Hybriel 66c3db71 (web batch 4); e2e: main menu marked after in-place sign-in is a normal check; report 014mre
  • 8f3dc1ectests: delivery allow-list for the stopped server's network errors uses the gate port, not 8700mre