Skip to content

Commit

Permalink
fix: list relations should throw when an underlying check errors (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored Aug 18, 2023
2 parents ddf5f76 + 7491a54 commit 9766218
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
5 changes: 5 additions & 0 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,11 @@ export class OpenFgaClient extends BaseAPI {
contextualTuples: listRelationsRequest.contextualTuples,
})), { ...options, headers, maxParallelRequests });

const firstErrorResponse = batchCheckResults.responses.find(response => (response as any).error);
if (firstErrorResponse) {
throw (firstErrorResponse as any).error;
}

return { relations: batchCheckResults.responses.filter(result => result.allowed).map(result => result._request.relation) };
}

Expand Down
69 changes: 64 additions & 5 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as nock from "nock";
import {
ClientWriteStatus,
CredentialsMethod,
FgaApiError,
FgaApiAuthenticationError,
FgaValidationError,
OpenFgaClient
Expand Down Expand Up @@ -512,11 +513,8 @@ describe("OpenFGA Client", () => {
const scope0 = nocks.check(defaultConfiguration.storeId!, tuples[0], defaultConfiguration.getBasePath(), { allowed: true }).matchHeader("X-OpenFGA-Client-Method", "ListRelations");
const scope1 = nocks.check(defaultConfiguration.storeId!, tuples[1], defaultConfiguration.getBasePath(), { allowed: false }).matchHeader("X-OpenFGA-Client-Method", "ListRelations");
const scope2 = nocks.check(defaultConfiguration.storeId!, tuples[2], defaultConfiguration.getBasePath(), { allowed: true }).matchHeader("X-OpenFGA-Client-Method", "ListRelations");
const scope3 = nocks.check(defaultConfiguration.storeId!, tuples[3], defaultConfiguration.getBasePath(), "" as any, 500).matchHeader("X-OpenFGA-Client-Method", "ListRelations");
const scope4 = nocks.check(defaultConfiguration.storeId!, tuples[4], defaultConfiguration.getBasePath(), {
"code": "validation_error",
"message": "relation 'workspace#can_read' not found"
}, 400).matchHeader("X-OpenFGA-Client-Method", "ListRelations");
const scope3 = nocks.check(defaultConfiguration.storeId!, tuples[3], defaultConfiguration.getBasePath(), { allowed: false }).matchHeader("X-OpenFGA-Client-Method", "ListRelations");
const scope4 = nocks.check(defaultConfiguration.storeId!, tuples[4], defaultConfiguration.getBasePath(), { allowed: false }).matchHeader("X-OpenFGA-Client-Method", "ListRelations");
const scope5 = nock(defaultConfiguration.getBasePath())
.get(`/stores/${defaultConfiguration.storeId!}/authorization-models`)
.query({ page_size: 1 })
Expand Down Expand Up @@ -546,6 +544,67 @@ describe("OpenFGA Client", () => {
expect(response.relations.sort()).toEqual(expect.arrayContaining(["admin", "reader"]));
});

it("should throw an error if any check returns an error", async () => {
const tuples = [{
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation: "admin",
object: "workspace:1",
}, {
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation: "guest",
object: "workspace:1",
}, {
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation: "reader",
object: "workspace:1",
}, {
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation: "viewer",
object: "workspace:1",
}, {
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation: "can_read",
object: "workspace:1",
}];
const scope0 = nocks.check(defaultConfiguration.storeId!, tuples[0], defaultConfiguration.getBasePath(), { allowed: true }).matchHeader("X-OpenFGA-Client-Method", "ListRelations");
const scope1 = nocks.check(defaultConfiguration.storeId!, tuples[1], defaultConfiguration.getBasePath(), { allowed: false }).matchHeader("X-OpenFGA-Client-Method", "ListRelations");
const scope2 = nocks.check(defaultConfiguration.storeId!, tuples[2], defaultConfiguration.getBasePath(), { allowed: true }).matchHeader("X-OpenFGA-Client-Method", "ListRelations");
const scope3 = nocks.check(defaultConfiguration.storeId!, tuples[3], defaultConfiguration.getBasePath(), "" as any, 500).matchHeader("X-OpenFGA-Client-Method", "ListRelations");
const scope4 = nocks.check(defaultConfiguration.storeId!, tuples[4], defaultConfiguration.getBasePath(), {
"code": "validation_error",
"message": "relation 'workspace#can_read' not found"
}, 400).matchHeader("X-OpenFGA-Client-Method", "ListRelations");
const scope5 = nock(defaultConfiguration.getBasePath())
.get(`/stores/${defaultConfiguration.storeId!}/authorization-models`)
.query({ page_size: 1 })
.reply(200, {
authorization_models: [],
});

expect(scope0.isDone()).toBe(false);
expect(scope1.isDone()).toBe(false);
expect(scope2.isDone()).toBe(false);
expect(scope3.isDone()).toBe(false);
expect(scope4.isDone()).toBe(false);
expect(scope5.isDone()).toBe(false);

try {
const response = await fgaClient.listRelations({
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
object: "workspace:1",
relations: ["admin", "guest", "reader", "viewer"],
});
} catch (err) {
expect(scope0.isDone()).toBe(true);
expect(scope1.isDone()).toBe(true);
expect(scope2.isDone()).toBe(true);
expect(scope3.isDone()).toBe(true);
expect(scope4.isDone()).toBe(false);
expect(scope5.isDone()).toBe(true);
expect(err).toBeInstanceOf(FgaApiError);
}
});

it("should throw an error if no relations passed", async () => {
try {
await fgaClient.listRelations({
Expand Down

0 comments on commit 9766218

Please sign in to comment.