Skip to content
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
2 changes: 1 addition & 1 deletion configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class Configuration {
}

this.baseOptions = baseOptions;
this.retryParams = params.retryParams;
this.retryParams = Object.assign(GetDefaultRetryParams(), params.retryParams);
this.telemetry = new TelemetryConfiguration(params?.telemetry?.metrics);
}

Expand Down
11 changes: 5 additions & 6 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import {
ListUsersResponse,
ConsistencyPreference,
ErrorCode,
BatchCheckRequest,
ClientWriteRequestOnDuplicateWrites,
ClientWriteRequestOnMissingDeletes,
} from "../index";
import { baseConfig, defaultConfiguration, getNocks } from "./helpers";

const nocks = getNocks(nock);
nock.disableNetConnect();

describe("OpenFGA Client", () => {

Expand Down Expand Up @@ -1875,7 +1873,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");
// Mock all default retries+1 to exhaust them all and trigger actual failure.
const scope3 = Array.from({ length: 4 }, () => 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"
Expand All @@ -1890,12 +1889,12 @@ describe("OpenFGA Client", () => {
expect(scope0.isDone()).toBe(false);
expect(scope1.isDone()).toBe(false);
expect(scope2.isDone()).toBe(false);
expect(scope3.isDone()).toBe(false);
expect(scope3.every(scope => scope.isDone())).toBe(false);
expect(scope4.isDone()).toBe(false);
expect(scope5.isDone()).toBe(false);

try {
const response = await fgaClient.listRelations({
await fgaClient.listRelations({
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
object: "workspace:1",
relations: ["admin", "guest", "reader", "viewer"],
Expand All @@ -1904,7 +1903,7 @@ describe("OpenFGA Client", () => {
expect(scope0.isDone()).toBe(true);
expect(scope1.isDone()).toBe(true);
expect(scope2.isDone()).toBe(true);
expect(scope3.isDone()).toBe(true);
expect(scope3.every(scope => scope.isDone())).toBe(true);
expect(scope4.isDone()).toBe(false);
expect(scope5.isDone()).toBe(false);
expect(err).toBeInstanceOf(FgaApiError);
Expand Down
52 changes: 50 additions & 2 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,56 @@ describe("OpenFGA SDK", function () {
});
});

describe("429 with default retry config is successful", () => {
const tupleKey = {
user: "user:xyz",
relation: "viewer",
object: "foobar:x",
};
let failedRequestNock: nock.Scope;

beforeEach(async () => {
fgaApi = new OpenFgaApi({ ...baseConfig });

nocks.tokenExchange(OPENFGA_API_TOKEN_ISSUER, "test-token");

failedRequestNock = nock(basePath)
.post(
`/stores/${storeId}/check`,
{
tuple_key: tupleKey,
authorization_model_id: "01GXSA8YR785C4FYS3C0RTG7B1"
},
expect.objectContaining({ Authorization: "Bearer test-token" })
Comment thread
kamilogorek marked this conversation as resolved.
)
.times(1)
.reply(429, {
code: "rate_limit_exceeded",
message: "nock error",
});

nocks.check(baseConfig.storeId!, tupleKey);
});

afterEach(() => {
nock.cleanAll();
});

it("should return allowed", async () => {
const result = await fgaApi.check(baseConfig.storeId!, { tuple_key: tupleKey, authorization_model_id: "01GXSA8YR785C4FYS3C0RTG7B1"}, {});

expect(failedRequestNock.isDone()).toBe(true);
expect(result.allowed).toBe(true);
});
});

describe("429 with retry in config and retry is successful", () => {
const tupleKey = {
user: "user:xyz",
relation: "viewer",
object: "foobar:x",
};
let failedRequestNock: nock.Scope;

beforeEach(async () => {
const updateBaseConfig = {
Expand All @@ -441,11 +485,12 @@ describe("OpenFGA SDK", function () {

nocks.tokenExchange(OPENFGA_API_TOKEN_ISSUER, "test-token");

nock(basePath)
failedRequestNock = nock(basePath)
.post(
`/stores/${storeId}/check`,
{
tuple_key: tupleKey,
authorization_model_id: "01GXSA8YR785C4FYS3C0RTG7B1"
},
Comment thread
kamilogorek marked this conversation as resolved.
expect.objectContaining({ Authorization: "Bearer test-token" })
)
Expand All @@ -465,6 +510,7 @@ describe("OpenFGA SDK", function () {
it("should return allowed", async () => {
const result = await fgaApi.check(baseConfig.storeId!, { tuple_key: tupleKey, authorization_model_id: "01GXSA8YR785C4FYS3C0RTG7B1"}, {});

expect(failedRequestNock.isDone()).toBe(true);
expect(result.allowed).toBe(true);
});
});
Expand All @@ -475,11 +521,12 @@ describe("OpenFGA SDK", function () {
relation: "viewer",
object: "foobar:x",
};
let failedRequestNock: nock.Scope;

beforeEach(async () => {
nocks.tokenExchange(OPENFGA_API_TOKEN_ISSUER, "test-token");

nock(basePath)
failedRequestNock = nock(basePath)
.post(
`/stores/${storeId}/check`,
{
Expand Down Expand Up @@ -507,6 +554,7 @@ describe("OpenFGA SDK", function () {
{ retryParams: GetDefaultRetryParams(2, 10) }
);

expect(failedRequestNock.isDone()).toBe(true);
expect(result.allowed).toBe(true);
});
});
Expand Down
4 changes: 4 additions & 0 deletions tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as nock from "nock";

beforeEach(() => {
nock.disableNetConnect();
});

afterAll(() => {
nock.restore();
});
Loading