|
| 1 | +import { |
| 2 | + canAuthor, |
| 3 | + hasAdminAccess, |
| 4 | + hasAuthorAccess, |
| 5 | + hasCreateUserAccess, |
| 6 | + hasManageUserAccess, |
| 7 | + hasEditorAccess, |
| 8 | + hasLoggedInAccess, |
| 9 | + isAuthor, |
| 10 | +} from "./abilities"; |
| 11 | +import { anyone } from "./anyone"; |
| 12 | +import { ROLE_ADMIN, ROLE_AUTHOR, ROLE_EDITOR } from "./roles"; |
| 13 | + |
| 14 | +describe("payload.access", () => { |
| 15 | + describe("abilities.hasAdminAccess", () => { |
| 16 | + it("allows requests from administrators", () => { |
| 17 | + expect(hasAdminAccess({ req: { user: { role: ROLE_ADMIN } } })).toBe( |
| 18 | + true, |
| 19 | + ); |
| 20 | + }); |
| 21 | + |
| 22 | + it("denies requests from authors, editors, and anonymous users", () => { |
| 23 | + expect(hasAdminAccess({ req: { user: { role: ROLE_AUTHOR } } })).toBe( |
| 24 | + false, |
| 25 | + ); |
| 26 | + expect(hasAdminAccess({ req: { user: { role: ROLE_EDITOR } } })).toBe( |
| 27 | + false, |
| 28 | + ); |
| 29 | + expect(hasAdminAccess({ req: {} })).toBe(false); |
| 30 | + expect(hasAdminAccess(undefined)).toBe(false); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe("abilities.isAuthor", () => { |
| 35 | + it("allows users with administrators, authors and editors roles", () => { |
| 36 | + expect(isAuthor({ role: ROLE_ADMIN })).toBe(true); |
| 37 | + expect(isAuthor({ role: ROLE_AUTHOR })).toBe(true); |
| 38 | + expect(isAuthor({ role: ROLE_EDITOR })).toBe(true); |
| 39 | + }); |
| 40 | + |
| 41 | + it("denies users without a valid role", () => { |
| 42 | + expect(isAuthor({ role: "unknown" })).toBe(false); |
| 43 | + expect(isAuthor(1)).toBe(false); |
| 44 | + expect(isAuthor(undefined)).toBe(false); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("abilities.canAuthor", () => { |
| 49 | + it("allows administrators and editors to author any content", () => { |
| 50 | + expect(canAuthor({ role: ROLE_ADMIN })).toBe(true); |
| 51 | + expect(canAuthor({ role: ROLE_EDITOR })).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + it("limits authors to their own content", () => { |
| 55 | + expect(canAuthor({ id: 1, role: ROLE_AUTHOR })).toEqual({ |
| 56 | + createdBy: { equals: 1 }, |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it("denies authors without a user id for ownership checks", () => { |
| 61 | + expect(canAuthor({ role: ROLE_AUTHOR })).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it("denies users without a valid role", () => { |
| 65 | + expect(canAuthor({ role: "unknown" })).toBe(false); |
| 66 | + expect(canAuthor(1)).toBe(false); |
| 67 | + expect(canAuthor(undefined)).toBe(false); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe("abilities.hasAuthorAccess", () => { |
| 72 | + it("allows requests from administrators, authors and editors", () => { |
| 73 | + expect(hasAuthorAccess({ req: { user: { role: ROLE_ADMIN } } })).toBe( |
| 74 | + true, |
| 75 | + ); |
| 76 | + expect( |
| 77 | + hasAuthorAccess({ req: { user: { id: 1, role: ROLE_AUTHOR } } }), |
| 78 | + ).toEqual({ createdBy: { equals: 1 } }); |
| 79 | + expect(hasAuthorAccess({ req: { user: { role: ROLE_EDITOR } } })).toBe( |
| 80 | + true, |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it("denies requests from anonymous users", () => { |
| 85 | + expect(hasAuthorAccess({ req: {} })).toBe(false); |
| 86 | + expect(hasAuthorAccess(undefined)).toBe(false); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe("abilities.hasCreateUserAccess", () => { |
| 91 | + it("allows requests from administrators and editors", () => { |
| 92 | + expect(hasCreateUserAccess({ req: { user: { role: ROLE_ADMIN } } })).toBe( |
| 93 | + true, |
| 94 | + ); |
| 95 | + expect( |
| 96 | + hasCreateUserAccess({ req: { user: { role: ROLE_EDITOR } } }), |
| 97 | + ).toBe(true); |
| 98 | + }); |
| 99 | + |
| 100 | + it("denies requests from authors and anonymous users", () => { |
| 101 | + expect( |
| 102 | + hasCreateUserAccess({ req: { user: { role: ROLE_AUTHOR } } }), |
| 103 | + ).toBe(false); |
| 104 | + expect(hasCreateUserAccess({ req: {} })).toBe(false); |
| 105 | + expect(hasCreateUserAccess(undefined)).toBe(false); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe("abilities.hasManageUserAccess", () => { |
| 110 | + it("allows requests from administrators, authors and editors", () => { |
| 111 | + expect(hasManageUserAccess({ req: { user: { role: ROLE_ADMIN } } })).toBe( |
| 112 | + true, |
| 113 | + ); |
| 114 | + expect( |
| 115 | + hasManageUserAccess({ req: { user: { id: 1, role: ROLE_AUTHOR } } }), |
| 116 | + ).toEqual({ or: [{ id: { equals: 1 } }] }); |
| 117 | + expect( |
| 118 | + hasManageUserAccess({ req: { user: { id: 1, role: ROLE_EDITOR } } }), |
| 119 | + ).toEqual({ |
| 120 | + or: [{ id: { equals: 1 } }, { role: { equals: ROLE_AUTHOR } }], |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it("denies requests from invalid-role and anonymous users", () => { |
| 125 | + expect( |
| 126 | + hasManageUserAccess({ req: { user: { id: 1, role: "unknown" } } }), |
| 127 | + ).toBe(false); |
| 128 | + expect(hasManageUserAccess({ req: {} })).toBe(false); |
| 129 | + expect(hasManageUserAccess(undefined)).toBe(false); |
| 130 | + }); |
| 131 | + |
| 132 | + it("denies requests from non-admin users without a user id for ownership checks", () => { |
| 133 | + expect( |
| 134 | + hasManageUserAccess({ req: { user: { role: ROLE_AUTHOR } } }), |
| 135 | + ).toBe(false); |
| 136 | + expect( |
| 137 | + hasManageUserAccess({ req: { user: { role: ROLE_EDITOR } } }), |
| 138 | + ).toBe(false); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + describe("abilities.hasEditorAccess", () => { |
| 143 | + it("allows requests from administrators and editors", () => { |
| 144 | + expect(hasEditorAccess({ req: { user: { role: ROLE_ADMIN } } })).toBe( |
| 145 | + true, |
| 146 | + ); |
| 147 | + expect(hasEditorAccess({ req: { user: { role: ROLE_EDITOR } } })).toBe( |
| 148 | + true, |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + it("denies requests from authors and anonymous users", () => { |
| 153 | + expect(hasEditorAccess({ req: { user: { role: ROLE_AUTHOR } } })).toBe( |
| 154 | + false, |
| 155 | + ); |
| 156 | + expect(hasEditorAccess({ req: {} })).toBe(false); |
| 157 | + expect(hasEditorAccess(undefined)).toBe(false); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe("abilities.hasLoggedInAccess", () => { |
| 162 | + it("allows requests with users that have valid roles", () => { |
| 163 | + expect(hasLoggedInAccess({ req: { user: { role: ROLE_ADMIN } } })).toBe( |
| 164 | + true, |
| 165 | + ); |
| 166 | + expect(hasLoggedInAccess({ req: { user: { role: ROLE_AUTHOR } } })).toBe( |
| 167 | + true, |
| 168 | + ); |
| 169 | + expect(hasLoggedInAccess({ req: { user: { role: ROLE_EDITOR } } })).toBe( |
| 170 | + true, |
| 171 | + ); |
| 172 | + }); |
| 173 | + |
| 174 | + it("denies requests without a user that has a valid role", () => { |
| 175 | + expect(hasLoggedInAccess({ req: { user: { role: "unknown" } } })).toBe( |
| 176 | + false, |
| 177 | + ); |
| 178 | + expect(hasLoggedInAccess({ req: { user: 1 } })).toBe(false); |
| 179 | + expect(hasLoggedInAccess({ req: {} })).toBe(false); |
| 180 | + expect(hasLoggedInAccess(undefined)).toBe(false); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + describe("anyone.anyone", () => { |
| 185 | + it("allows requests from administrators, authors, editors and anonymous users", () => { |
| 186 | + expect(anyone({ req: { user: { role: ROLE_ADMIN } } })).toBe(true); |
| 187 | + expect(anyone({ req: { user: { role: ROLE_AUTHOR } } })).toBe(true); |
| 188 | + expect(anyone({ req: { user: { role: ROLE_EDITOR } } })).toBe(true); |
| 189 | + expect(anyone({ req: { user: 1 } })).toBe(true); |
| 190 | + expect(anyone({ req: {} })).toBe(true); |
| 191 | + expect(anyone(undefined)).toBe(true); |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
0 commit comments