|
7 | 7 | import { suite, test } from "mocha-typescript";
|
8 | 8 | import * as chai from 'chai';
|
9 | 9 | const expect = chai.expect;
|
10 |
| -import { TokenResourceGuard, ScopedResourceGuard, GuardedResource } from "./resource-access"; |
| 10 | +import { TokenResourceGuard, ScopedResourceGuard, GuardedResource, ResourceAccessOp } from "./resource-access"; |
11 | 11 |
|
12 | 12 | @suite class TestResourceAccess {
|
13 | 13 |
|
@@ -62,6 +62,8 @@ import { TokenResourceGuard, ScopedResourceGuard, GuardedResource } from "./reso
|
62 | 62 | const tests: {
|
63 | 63 | name: string
|
64 | 64 | guard: TokenResourceGuard
|
| 65 | + resource?: GuardedResource, |
| 66 | + operation?: ResourceAccessOp, |
65 | 67 | expectation: boolean
|
66 | 68 | }[] = [
|
67 | 69 | {
|
@@ -100,11 +102,38 @@ import { TokenResourceGuard, ScopedResourceGuard, GuardedResource } from "./reso
|
100 | 102 | "resource:"+ScopedResourceGuard.marshalResourceScope({kind: "workspace", subjectID: "*", operations: ["get"]}),
|
101 | 103 | ]),
|
102 | 104 | expectation: true,
|
103 |
| - } |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "snaphshot create", |
| 108 | + guard: new TokenResourceGuard(workspaceResource.subject.ownerId, [ |
| 109 | + "resource:"+ScopedResourceGuard.marshalResourceScope({kind: "snapshot", subjectID: ScopedResourceGuard.SNAPSHOT_WORKSPACE_SUBJECT_ID_PREFIX + workspaceResource.subject.id, operations: ["create"]}), |
| 110 | + ]), |
| 111 | + resource: { kind: "snapshot", subject: undefined, workspaceID: workspaceResource.subject.id, workspaceOwnerID: workspaceResource.subject.ownerId}, |
| 112 | + operation: "create", |
| 113 | + expectation: true, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "snaphshot create missing prefix fails", |
| 117 | + guard: new TokenResourceGuard(workspaceResource.subject.ownerId, [ |
| 118 | + "resource:"+ScopedResourceGuard.marshalResourceScope({kind: "snapshot", subjectID: workspaceResource.subject.id, operations: ["create"]}), |
| 119 | + ]), |
| 120 | + resource: { kind: "snapshot", subject: undefined, workspaceID: workspaceResource.subject.id, workspaceOwnerID: workspaceResource.subject.ownerId}, |
| 121 | + operation: "create", |
| 122 | + expectation: false, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "snaphshot create other user fails", |
| 126 | + guard: new TokenResourceGuard(workspaceResource.subject.ownerId, [ |
| 127 | + "resource:"+ScopedResourceGuard.marshalResourceScope({kind: "snapshot", subjectID: workspaceResource.subject.id, operations: ["create"]}), |
| 128 | + ]), |
| 129 | + resource: { kind: "snapshot", subject: undefined, workspaceID: workspaceResource.subject.id, workspaceOwnerID: "other_owner"}, |
| 130 | + operation: "create", |
| 131 | + expectation: false, |
| 132 | + }, |
104 | 133 | ]
|
105 | 134 |
|
106 | 135 | await Promise.all(tests.map(async t => {
|
107 |
| - const res = await t.guard.canAccess(workspaceResource, "get") |
| 136 | + const res = await t.guard.canAccess(t.resource || workspaceResource, t.operation || "get") |
108 | 137 | expect(res).to.be.eq(t.expectation, `"${t.name}" expected canAccess(...) === ${t.expectation}, but was ${res}`);
|
109 | 138 | }))
|
110 | 139 | }
|
|
0 commit comments