| … | … | @@ -0,0 +1,47 @@ |
| 1 | // enrollFromInvite (issue #35): the daemon's enroll-writer path. A joiner |
| 2 | // presents the invite it holds + its writer key; the owner verifies the |
| 3 | // invite refers to an org IT owns, then enrolls with the invite's role. |
| 4 | // Possessing the invite is the authorization (no prior membership). |
| 5 | |
| 6 | import test from 'brittle' |
| 7 | import Corestore from 'corestore' |
| 8 | import { mkdtempSync } from 'fs' |
| 9 | import { join } from 'path' |
| 10 | import { tmpdir } from 'os' |
| 11 | import { replicateAndSync } from 'autobase-test-helpers' |
| 12 | import { HyperOrgs } from '../index.js' |
| 13 | |
| 14 | function tmpStore () { |
| 15 | return new Corestore(mkdtempSync(join(tmpdir(), 'hyper-orgs-efi-'))) |
| 16 | } |
| 17 | |
| 18 | test('#35: enrollFromInvite — invite-authorized owner enroll', async (t) => { |
| 19 | const owner = new HyperOrgs(tmpStore(), { namespace: 'efi-owner' }) |
| 20 | const joiner = new HyperOrgs(tmpStore(), { namespace: 'efi-joiner' }) |
| 21 | await owner.ready() |
| 22 | await joiner.ready() |
| 23 | t.teardown(async () => { try { await owner.close() } catch {}; try { await joiner.close() } catch {} }) |
| 24 | |
| 25 | await owner.create('proj', 'group') |
| 26 | const { invitation } = await owner.createOrgInvite('proj', { role: 'contributor' }) |
| 27 | |
| 28 | // joiner mounts + hands {invitation, writerKey} to the owner |
| 29 | const j = await joiner.joinOrg(invitation) |
| 30 | await replicateAndSync([owner.getOrgBase('proj'), joiner.getOrgBase('proj')]) |
| 31 | |
| 32 | const res = await owner.enrollFromInvite(invitation, j.writerKey) |
| 33 | t.is(res.orgName, 'proj') |
| 34 | t.is(res.role, 'contributor') |
| 35 | |
| 36 | await replicateAndSync([owner.getOrgBase('proj'), joiner.getOrgBase('proj')]) |
| 37 | await replicateAndSync([owner.getOrgBase('proj'), joiner.getOrgBase('proj')]) |
| 38 | t.ok(joiner.getOrgRoom('proj').writable, 'joiner became a writer via invite enroll') |
| 39 | |
| 40 | // a forged invite for an org the owner doesn't own is rejected |
| 41 | const other = new HyperOrgs(tmpStore(), { namespace: 'efi-other' }) |
| 42 | await other.ready() |
| 43 | await other.create('elsewhere', 'group') |
| 44 | const { invitation: foreign } = await other.createOrgInvite('elsewhere', { role: 'admin' }) |
| 45 | await t.exception(() => owner.enrollFromInvite(foreign, j.writerKey), /does not match an org owned/) |
| 46 | await other.close() |
| 47 | }) |