Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js-sdk): list relations should throw when an underlying check errors #184

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/clients/js/template/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,11 @@ export class {{appShortName}}Client 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 config/clients/js/template/tests/client.test.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as nock from "nock";
import {
ClientWriteStatus,
CredentialsMethod,
FgaApiError,
FgaApiAuthenticationError,
FgaValidationError,
{{appShortName}}Client
Expand Down Expand Up @@ -501,11 +502,8 @@ describe("{{appTitleCaseName}} 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 @@ -535,6 +533,67 @@ describe("{{appTitleCaseName}} 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
Loading