Skip to content

Commit f85baec

Browse files
committed
feat: test
1 parent cfd8b6e commit f85baec

File tree

1 file changed

+31
-77
lines changed

1 file changed

+31
-77
lines changed

tests/credentials.test.ts

Lines changed: 31 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,12 @@ import {
1111
} from "./helpers/default-config";
1212
import {FgaValidationError} from "../errors";
1313

14+
nock.disableNetConnect();
15+
1416
describe("Credentials", () => {
1517
const mockTelemetryConfig: TelemetryConfiguration = new TelemetryConfiguration({});
1618

17-
beforeAll(() => {
18-
// Clean up any MSW interceptors that might interfere
19-
const http = require('http');
20-
const https = require('https');
21-
22-
// Store original agents
23-
const originalHttpAgent = http.globalAgent;
24-
const originalHttpsAgent = https.globalAgent;
25-
26-
// Reset to ensure clean state
27-
http.globalAgent = new http.Agent();
28-
https.globalAgent = new https.Agent();
29-
30-
nock.cleanAll();
31-
nock.restore();
32-
nock.activate();
33-
nock.disableNetConnect();
34-
});
35-
36-
afterAll(() => {
37-
nock.cleanAll();
38-
nock.restore();
39-
nock.enableNetConnect();
40-
});
41-
4219
describe("Refreshing access token", () => {
43-
beforeEach(() => {
44-
nock.cleanAll();
45-
});
46-
4720
afterEach(() => {
4821
nock.cleanAll();
4922
});
@@ -52,91 +25,71 @@ describe("Credentials", () => {
5225
{
5326
description: "should use default scheme and token endpoint path when apiTokenIssuer has no scheme and no path",
5427
apiTokenIssuer: "issuer.fga.example",
55-
expectedBaseUrl: "https://issuer.fga.example",
56-
expectedPath: `/${DEFAULT_TOKEN_ENDPOINT_PATH}`,
28+
expectedUrl: `https://issuer.fga.example/${DEFAULT_TOKEN_ENDPOINT_PATH}`,
5729
},
5830
{
5931
description: "should use default token endpoint path when apiTokenIssuer has root path and no scheme",
6032
apiTokenIssuer: "https://issuer.fga.example/",
61-
expectedBaseUrl: "https://issuer.fga.example",
62-
expectedPath: `/${DEFAULT_TOKEN_ENDPOINT_PATH}`,
33+
expectedUrl: `https://issuer.fga.example/${DEFAULT_TOKEN_ENDPOINT_PATH}`,
6334
},
6435
{
6536
description: "should preserve custom token endpoint path when provided",
6637
apiTokenIssuer: "https://issuer.fga.example/some_endpoint",
67-
expectedBaseUrl: "https://issuer.fga.example",
68-
expectedPath: "/some_endpoint",
38+
expectedUrl: "https://issuer.fga.example/some_endpoint",
6939
},
7040
{
7141
description: "should preserve custom token endpoint path with nested path when provided",
7242
apiTokenIssuer: "https://issuer.fga.example/api/v1/oauth/token",
73-
expectedBaseUrl: "https://issuer.fga.example",
74-
expectedPath: "/api/v1/oauth/token",
43+
expectedUrl: "https://issuer.fga.example/api/v1/oauth/token",
7544
},
7645
{
7746
description: "should add https:// prefix when apiTokenIssuer has no scheme",
7847
apiTokenIssuer: "issuer.fga.example/some_endpoint",
79-
expectedBaseUrl: "https://issuer.fga.example",
80-
expectedPath: "/some_endpoint",
48+
expectedUrl: "https://issuer.fga.example/some_endpoint",
8149
},
8250
{
8351
description: "should preserve http:// scheme when provided",
8452
apiTokenIssuer: "http://issuer.fga.example/some_endpoint",
85-
expectedBaseUrl: "http://issuer.fga.example",
86-
expectedPath: "/some_endpoint",
53+
expectedUrl: "http://issuer.fga.example/some_endpoint",
8754
},
8855
{
8956
description: "should use default path when apiTokenIssuer has https:// scheme but no path",
9057
apiTokenIssuer: "https://issuer.fga.example",
91-
expectedBaseUrl: "https://issuer.fga.example",
92-
expectedPath: `/${DEFAULT_TOKEN_ENDPOINT_PATH}`,
58+
expectedUrl: `https://issuer.fga.example/${DEFAULT_TOKEN_ENDPOINT_PATH}`,
9359
},
9460
{
9561
description: "should preserve custom path with query parameters",
9662
apiTokenIssuer: "https://issuer.fga.example/some_endpoint?param=value",
97-
expectedBaseUrl: "https://issuer.fga.example",
98-
expectedPath: "/some_endpoint",
99-
queryParams: { param: "value" },
63+
expectedUrl: "https://issuer.fga.example/some_endpoint?param=value",
10064
},
10165
{
10266
description: "should preserve custom path with port number",
10367
apiTokenIssuer: "https://issuer.fga.example:8080/some_endpoint",
104-
expectedBaseUrl: "https://issuer.fga.example:8080",
105-
expectedPath: "/some_endpoint",
68+
expectedUrl: "https://issuer.fga.example:8080/some_endpoint",
10669
},
10770
{
10871
description: "should use default path when path has multiple trailing slashes",
10972
apiTokenIssuer: "https://issuer.fga.example///",
110-
expectedBaseUrl: "https://issuer.fga.example",
111-
expectedPath: `/${DEFAULT_TOKEN_ENDPOINT_PATH}`,
73+
expectedUrl: `https://issuer.fga.example/${DEFAULT_TOKEN_ENDPOINT_PATH}`,
11274
},
11375
{
11476
description: "should use default path when path only consists of slashes",
11577
apiTokenIssuer: "https://issuer.fga.example//",
116-
expectedBaseUrl: "https://issuer.fga.example",
117-
expectedPath: `/${DEFAULT_TOKEN_ENDPOINT_PATH}`,
78+
expectedUrl: `https://issuer.fga.example/${DEFAULT_TOKEN_ENDPOINT_PATH}`,
11879
},
11980
{
12081
description: "should preserve custom path with consecutive/trailing slashes",
12182
apiTokenIssuer: "https://issuer.fga.example/oauth//token///",
122-
expectedBaseUrl: "https://issuer.fga.example",
123-
expectedPath: "/oauth//token///",
124-
},
125-
])("$description", async ({ apiTokenIssuer, expectedBaseUrl, expectedPath, queryParams }) => {
126-
const scope = queryParams
127-
? nock(expectedBaseUrl)
128-
.post(expectedPath)
129-
.query(queryParams)
130-
.reply(200, {
131-
access_token: "test-token",
132-
expires_in: 300,
133-
})
134-
: nock(expectedBaseUrl)
135-
.post(expectedPath)
136-
.reply(200, {
137-
access_token: "test-token",
138-
expires_in: 300,
139-
});
83+
expectedUrl: "https://issuer.fga.example/oauth//token///",
84+
},
85+
])("$description", async ({ apiTokenIssuer, expectedUrl }) => {
86+
const parsedUrl = new URL(expectedUrl);
87+
const scope = nock(`${parsedUrl.protocol}//${parsedUrl.host}`)
88+
.post(parsedUrl.pathname + parsedUrl.search)
89+
.reply(200, {
90+
access_token: "test-token",
91+
expires_in: 300,
92+
});
14093

14194
const credentials = new Credentials(
14295
{
@@ -190,24 +143,25 @@ describe("Credentials", () => {
190143
{
191144
description: "HTTPS scheme",
192145
apiTokenIssuer: "https://issuer.fga.example/some_endpoint",
193-
expectedBaseUrl: "https://issuer.fga.example",
146+
expectedUrl: "https://issuer.fga.example/some_endpoint",
194147
expectedAudience: "https://issuer.fga.example/some_endpoint/",
195148
},
196149
{
197150
description: "HTTP scheme",
198151
apiTokenIssuer: "http://issuer.fga.example/some_endpoint",
199-
expectedBaseUrl: "http://issuer.fga.example",
152+
expectedUrl: "http://issuer.fga.example/some_endpoint",
200153
expectedAudience: "http://issuer.fga.example/some_endpoint/",
201154
},
202155
{
203156
description: "No scheme",
204157
apiTokenIssuer: "issuer.fga.example/some_endpoint",
205-
expectedBaseUrl: "https://issuer.fga.example",
158+
expectedUrl: "https://issuer.fga.example/some_endpoint",
206159
expectedAudience: "https://issuer.fga.example/some_endpoint/",
207160
}
208-
])("should normalize audience from apiTokenIssuer when using PrivateKeyJWT client credentials ($description)", async ({ apiTokenIssuer, expectedBaseUrl, expectedAudience }) => {
209-
const scope = nock(expectedBaseUrl)
210-
.post("/some_endpoint", (body: string) => {
161+
])("should normalize audience from apiTokenIssuer when using PrivateKeyJWT client credentials ($description)", async ({ apiTokenIssuer, expectedUrl, expectedAudience }) => {
162+
const parsedUrl = new URL(expectedUrl);
163+
const scope = nock(`${parsedUrl.protocol}//${parsedUrl.host}`)
164+
.post(parsedUrl.pathname, (body: string) => {
211165
const params = new URLSearchParams(body);
212166
const clientAssertion = params.get("client_assertion") as string;
213167
const decoded = jose.decodeJwt(clientAssertion);
@@ -238,4 +192,4 @@ describe("Credentials", () => {
238192
expect(scope.isDone()).toBe(true);
239193
});
240194
});
241-
});
195+
});

0 commit comments

Comments
 (0)