| 1 | import { describe, expect, it } from 'vitest'; |
| 2 | |
| 3 | import { RepositoryNotFoundError } |
| 4 | import { |
| 5 | AuthenticationRequiredError, |
| 6 | RepositoryForbiddenError, |
| 7 | } from './repository-access.errors.js'; |
| 8 | import { roleHierarchy } |
| 9 | import { |
| 10 | type Actor, |
| 11 | decideAccess, |
| 12 | type Grants, |
| 13 | type RepositoryOperation, |
| 14 | roleOf, |
| 15 | } from './repository-access.js'; |
| 16 | |
| 17 | const owner = { |
| 18 | const stranger = { |
| 19 | |
| 20 | const repo = ( |
| 21 | ({ ownerId: owner |
| 22 | |
| 23 | const operations: RepositoryOperation |
| 24 | 'read', |
| 25 | 'triage', |
| 26 | 'write', |
| 27 | 'maintain', |
| 28 | 'admin', |
| 29 | ]; |
| 30 | |
| 31 | const grants = ( |
| 32 | collaboratorRole: Grants |
| 33 | memberRole: string |
| 34 | teamRole: Grants |
| 35 | basePermission: Grants |
| 36 | ): Grants => |
| 37 | |
| 38 | const orgRepo = { |
| 39 | |
| 40 | describe('roleOf', () |
| 41 | it('makes the owner the owner, whatever collaboration says', |
| 42 | expect(roleOf(repo |
| 43 | }); |
| 44 | |
| 45 | it('gives a collaborator their role and everyone else none', |
| 46 | expect(roleOf(repo |
| 47 | expect(roleOf(repo |
| 48 | expect(roleOf(repo |
| 49 | }); |
| 50 | |
| 51 | it("ignores membership on a user's own repository", |
| 52 | expect(roleOf(repo |
| 53 | }); |
| 54 | |
| 55 | it('gives an organization repository no owner but its creator nothing', |
| 56 | expect(roleOf(orgRepo |
| 57 | }); |
| 58 | |
| 59 | it("makes the organization's owners owners and its admins admins of every repository", |
| 60 | expect(roleOf(orgRepo |
| 61 | 'owner', |
| 62 | ); |
| 63 | expect(roleOf(orgRepo |
| 64 | 'admin', |
| 65 | ); |
| 66 | }); |
| 67 | |
| 68 | it('gives a member the base permission, or nothing without one', |
| 69 | expect( |
| 70 | roleOf(orgRepo, grants |
| 71 | ).toBe('write' |
| 72 | expect( |
| 73 | roleOf(orgRepo, grants |
| 74 | ).toBeNull(); |
| 75 | }); |
| 76 | |
| 77 | it('raises a member above the base permission through teams and collaboration', |
| 78 | expect( |
| 79 | roleOf(orgRepo, grants |
| 80 | ).toBe('maintain' |
| 81 | expect( |
| 82 | roleOf(orgRepo, grants |
| 83 | ).toBe('admin' |
| 84 | }); |
| 85 | |
| 86 | it('grants a team role to someone who is not a member', |
| 87 | expect(roleOf(orgRepo |
| 88 | 'triage', |
| 89 | ); |
| 90 | }); |
| 91 | |
| 92 | it('grants nothing for a role outside the organization roles', |
| 93 | expect( |
| 94 | roleOf(orgRepo, grants |
| 95 | ).toBeNull(); |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | describe('decideAccess', () |
| 100 | it('lets anyone read a public repository', |
| 101 | for (const actor |
| 102 | expect(decideAccess(repo |
| 103 | }); |
| 104 | |
| 105 | it('lets a role do exactly the operations at or below it', |
| 106 | for (const role |
| 107 | for (const operation |
| 108 | const allowed = |
| 109 | roleHierarchy.indexOf(role |
| 110 | const decide = |
| 111 | decideAccess(repo('private |
| 112 | if (allowed) expect |
| 113 | else expect(decide) |
| 114 | } |
| 115 | } |
| 116 | }); |
| 117 | |
| 118 | it('hides a private repository from anyone without a role', |
| 119 | expect(() => |
| 120 | RepositoryNotFoundError, |
| 121 | ); |
| 122 | }); |
| 123 | |
| 124 | it('forbids rather than hides a readable repository', |
| 125 | expect(() => |
| 126 | RepositoryForbiddenError, |
| 127 | ); |
| 128 | }); |
| 129 | |
| 130 | it('challenges an anonymous actor instead of revealing anything', |
| 131 | for (const [repository |
| 132 | [repo('private') |
| 133 | [repo('public') |
| 134 | [null, 'read'], |
| 135 | ] as const) |
| 136 | expect(() => |
| 137 | AuthenticationRequiredError, |
| 138 | ); |
| 139 | }); |
| 140 | |
| 141 | it('reports a missing repository to an authenticated actor', |
| 142 | expect(() => |
| 143 | RepositoryNotFoundError, |
| 144 | ); |
| 145 | }); |
| 146 | }); |