| … | … | @@ -277,6 +277,106 @@ test('checkAccess — denied when no grants', async (t) => { |
| 277 | 277 | t.is(denied, false) |
| 278 | 278 | }) |
| 279 | 279 | |
| 280 | // `read` is the lowest real level. Before it existed, checkAccess with |
| 281 | // requiredPermission:'read' hit `default: 0` and returned false no matter what |
| 282 | // was granted — the model could say who may WRITE but not who may READ, which |
| 283 | // is why repo visibility was unbuildable. |
| 284 | |
| 285 | test('checkAccess — a read grant satisfies a read requirement', async (t) => { |
| 286 | const orgs = await makeOrgs(t, 'test-22r') |
| 287 | await orgs.create('team', 'group') |
| 288 | await orgs.addResource('team', 'repo-a') |
| 289 | |
| 290 | await orgs.createGrant({ |
| 291 | subjectType: 'user', |
| 292 | subjectId: 'reader', |
| 293 | grantType: 'resource', |
| 294 | resourceName: 'repo-a', |
| 295 | permission: 'read' |
| 296 | }) |
| 297 | |
| 298 | t.is(await orgs.checkAccess({ |
| 299 | userId: 'reader', resourceName: 'repo-a', requiredPermission: 'read' |
| 300 | }), true, 'read grant satisfies read') |
| 301 | |
| 302 | t.is(await orgs.checkAccess({ |
| 303 | userId: 'reader', resourceName: 'repo-a', requiredPermission: 'write' |
| 304 | }), false, 'read does NOT imply write') |
| 305 | }) |
| 306 | |
| 307 | test('checkAccess — higher permissions imply read', async (t) => { |
| 308 | const orgs = await makeOrgs(t, 'test-22h') |
| 309 | await orgs.create('team', 'group') |
| 310 | await orgs.addResource('team', 'repo-a') |
| 311 | |
| 312 | for (const [who, perm] of [['w', 'write'], ['m', 'moderator'], ['o', 'owner']]) { |
| 313 | await orgs.createGrant({ |
| 314 | subjectType: 'user', |
| 315 | subjectId: who, |
| 316 | grantType: 'resource', |
| 317 | resourceName: 'repo-a', |
| 318 | permission: perm |
| 319 | }) |
| 320 | t.is(await orgs.checkAccess({ |
| 321 | userId: who, resourceName: 'repo-a', requiredPermission: 'read' |
| 322 | }), true, perm + ' implies read') |
| 323 | } |
| 324 | }) |
| 325 | |
| 326 | test('checkAccess — a read grant does NOT satisfy moderator or owner', async (t) => { |
| 327 | const orgs = await makeOrgs(t, 'test-22n') |
| 328 | await orgs.create('team', 'group') |
| 329 | await orgs.addResource('team', 'repo-a') |
| 330 | |
| 331 | await orgs.createGrant({ |
| 332 | subjectType: 'user', |
| 333 | subjectId: 'reader', |
| 334 | grantType: 'resource', |
| 335 | resourceName: 'repo-a', |
| 336 | permission: 'read' |
| 337 | }) |
| 338 | |
| 339 | t.is(await orgs.checkAccess({ userId: 'reader', resourceName: 'repo-a', requiredPermission: 'moderator' }), false) |
| 340 | t.is(await orgs.checkAccess({ userId: 'reader', resourceName: 'repo-a', requiredPermission: 'owner' }), false) |
| 341 | }) |
| 342 | |
| 343 | test('checkAccess — an UNKNOWN permission still grants nothing', async (t) => { |
| 344 | const orgs = await makeOrgs(t, 'test-22u') |
| 345 | await orgs.create('team', 'group') |
| 346 | await orgs.addResource('team', 'repo-a') |
| 347 | |
| 348 | await orgs.createGrant({ |
| 349 | subjectType: 'user', |
| 350 | subjectId: 'alice', |
| 351 | grantType: 'resource', |
| 352 | resourceName: 'repo-a', |
| 353 | permission: 'owner' |
| 354 | }) |
| 355 | |
| 356 | // A typo'd requirement must not fall through to "allowed". |
| 357 | t.is(await orgs.checkAccess({ |
| 358 | userId: 'alice', resourceName: 'repo-a', requiredPermission: 'reed' |
| 359 | }), false, 'unknown requirement rejected even for an owner') |
| 360 | }) |
| 361 | |
| 362 | test('checkAccess — a read grant works via a GROUP grant too', async (t) => { |
| 363 | const orgs = await makeOrgs(t, 'test-22g') |
| 364 | await orgs.create('team', 'group') |
| 365 | await orgs.addResource('team', 'repo-a') |
| 366 | |
| 367 | await orgs.createGrant({ |
| 368 | subjectType: 'user', |
| 369 | subjectId: 'alice', |
| 370 | grantType: 'group', |
| 371 | groupName: 'team', |
| 372 | permission: 'read' |
| 373 | }) |
| 374 | |
| 375 | t.is(await orgs.checkAccess({ |
| 376 | userId: 'alice', resourceName: 'repo-a', requiredPermission: 'read' |
| 377 | }), true, 'org-wide read reaches the org\'s resources') |
| 378 | }) |
| 379 | |
| 280 | 380 | test('checkAccess — group grant allows resource access', async (t) => { |
| 281 | 381 | const orgs = await makeOrgs(t, 'test-23') |
| 282 | 382 | |