Skip to content

Commit 7df7eae

Browse files
authored
Merge pull request #226 from authzed/add-test-with-caveat-in-relation
Add test with caveat context in relation
2 parents 47ced02 + a19124d commit 7df7eae

2 files changed

Lines changed: 92 additions & 30 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"only-run-tests": "vitest",
3737
"buf": "buf generate && tsc-esm-fix --src src/authzedapi --ext='.js'",
3838
"lint": "./node_modules/.bin/eslint src",
39+
"format": "prettier -w src",
3940
"build": "tsc",
4041
"postbuild": "rollup dist/src/index.js --file dist/src/index.cjs --format cjs && cp dist/src/index.d.ts dist/src/index.d.cts",
4142
"prepublish": "yarn build",

src/v1-promise.test.ts

Lines changed: 91 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,10 @@ describe("a check with an known namespace", () => {
144144
);
145145
expect(schemaResponse).toBeTruthy();
146146

147-
const response = await client.writeRelationships(
148-
writeRequest,
149-
new grpc.Metadata(),
150-
{} as grpc.CallOptions,
151-
);
147+
const response = await client.writeRelationships(writeRequest);
152148
expect(response).toBeTruthy();
153149

154-
const checkResponse = await client.checkPermission(
155-
checkPermissionRequest,
156-
new grpc.Metadata(),
157-
{} as grpc.CallOptions,
158-
);
150+
const checkResponse = await client.checkPermission(checkPermissionRequest);
159151
expect(checkResponse?.permissionship).toBe(
160152
CheckPermissionResponse_Permissionship.HAS_PERMISSION,
161153
);
@@ -164,7 +156,7 @@ describe("a check with an known namespace", () => {
164156
});
165157

166158
describe("with caveated relations", () => {
167-
it("should succeed", async () => {
159+
it("should succeed when caveat context is provided by request", async () => {
168160
// Write some schema.
169161
const { promises: client } = NewClient(
170162
generateTestToken("v1-promise-caveats"),
@@ -223,11 +215,7 @@ describe("a check with an known namespace", () => {
223215
],
224216
});
225217

226-
const response = await client.writeRelationships(
227-
writeRequest,
228-
new grpc.Metadata(),
229-
{} as grpc.CallOptions,
230-
);
218+
const response = await client.writeRelationships(writeRequest);
231219
expect(response).toBeTruthy();
232220

233221
// Call check when user has special attribute.
@@ -246,11 +234,7 @@ describe("a check with an known namespace", () => {
246234
context: Struct.fromJson({ special: true }),
247235
});
248236

249-
let checkResponse = await client.checkPermission(
250-
checkPermissionRequest,
251-
new grpc.Metadata(),
252-
{} as grpc.CallOptions,
253-
);
237+
let checkResponse = await client.checkPermission(checkPermissionRequest);
254238
expect(checkResponse?.permissionship).toBe(
255239
CheckPermissionResponse_Permissionship.HAS_PERMISSION,
256240
);
@@ -271,11 +255,7 @@ describe("a check with an known namespace", () => {
271255
context: Struct.fromJson({ special: false }),
272256
});
273257

274-
checkResponse = await client.checkPermission(
275-
checkPermissionRequest,
276-
new grpc.Metadata(),
277-
{} as grpc.CallOptions,
278-
);
258+
checkResponse = await client.checkPermission(checkPermissionRequest);
279259
expect(checkResponse?.permissionship).toBe(
280260
CheckPermissionResponse_Permissionship.NO_PERMISSION,
281261
);
@@ -296,13 +276,94 @@ describe("a check with an known namespace", () => {
296276
context: {},
297277
});
298278

299-
checkResponse = await client.checkPermission(
279+
checkResponse = await client.checkPermission(checkPermissionRequest);
280+
expect(checkResponse?.permissionship).toBe(
281+
CheckPermissionResponse_Permissionship.CONDITIONAL_PERMISSION,
282+
);
283+
284+
client.close();
285+
});
286+
it("should succeed when caveat context is provided by relation", async () => {
287+
// Write some schema.
288+
const { promises: client } = NewClient(
289+
generateTestToken("v1-promise-caveats"),
290+
"localhost:50051",
291+
ClientSecurity.INSECURE_LOCALHOST_ALLOWED,
292+
);
293+
294+
const schemaRequest = WriteSchemaRequest.create({
295+
schema: `definition test/user {}
296+
297+
caveat has_special_attribute(special bool) {
298+
special == true
299+
}
300+
301+
definition test/document {
302+
relation viewer: test/user
303+
relation caveated_viewer: test/user with has_special_attribute
304+
permission view = viewer + caveated_viewer
305+
}
306+
`,
307+
});
308+
309+
const schemaResponse = await client.writeSchema(schemaRequest);
310+
expect(schemaResponse).toBeTruthy();
311+
312+
// Write a relationship.
313+
const resource = ObjectReference.create({
314+
objectType: "test/document",
315+
objectId: "somedocument",
316+
});
317+
318+
const testUser = ObjectReference.create({
319+
objectType: "test/user",
320+
objectId: "specialuser",
321+
});
322+
323+
const writeRequest = WriteRelationshipsRequest.create({
324+
updates: [
325+
RelationshipUpdate.create({
326+
relationship: Relationship.create({
327+
resource: resource,
328+
relation: "caveated_viewer",
329+
subject: SubjectReference.create({
330+
object: testUser,
331+
}),
332+
optionalCaveat: ContextualizedCaveat.create({
333+
caveatName: "has_special_attribute",
334+
context: Struct.fromJson({
335+
special: true,
336+
}),
337+
}),
338+
}),
339+
operation: RelationshipUpdate_Operation.CREATE,
340+
}),
341+
],
342+
});
343+
344+
const response = await client.writeRelationships(writeRequest);
345+
expect(response).toBeTruthy();
346+
347+
// Call check when user has special attribute.
348+
const checkPermissionRequest = CheckPermissionRequest.create({
349+
resource,
350+
permission: "view",
351+
subject: SubjectReference.create({
352+
object: testUser,
353+
}),
354+
consistency: Consistency.create({
355+
requirement: {
356+
oneofKind: "fullyConsistent",
357+
fullyConsistent: true,
358+
},
359+
}),
360+
});
361+
362+
const checkResponse = await client.checkPermission(
300363
checkPermissionRequest,
301-
new grpc.Metadata(),
302-
{} as grpc.CallOptions,
303364
);
304365
expect(checkResponse?.permissionship).toBe(
305-
CheckPermissionResponse_Permissionship.CONDITIONAL_PERMISSION,
366+
CheckPermissionResponse_Permissionship.HAS_PERMISSION,
306367
);
307368

308369
client.close();

0 commit comments

Comments
 (0)