|
| 1 | +import * as nock from "nock"; |
| 2 | +import { Credentials, CredentialsMethod, DEFAULT_TOKEN_ENDPOINT_PATH } from "../credentials"; |
| 3 | +import { AuthCredentialsConfig } from "../credentials/types"; |
| 4 | +import { TelemetryConfiguration } from "../telemetry/configuration"; |
| 5 | +import { |
| 6 | + OPENFGA_API_AUDIENCE, |
| 7 | + OPENFGA_CLIENT_ID, |
| 8 | + OPENFGA_CLIENT_SECRET, |
| 9 | +} from "./helpers/default-config"; |
| 10 | + |
| 11 | +nock.disableNetConnect(); |
| 12 | + |
| 13 | +describe("Credentials", () => { |
| 14 | + const mockTelemetryConfig: TelemetryConfiguration = new TelemetryConfiguration({}); |
| 15 | + |
| 16 | + describe("Refreshing access token", () => { |
| 17 | + interface TestCase { |
| 18 | + description: string; |
| 19 | + apiTokenIssuer: string; |
| 20 | + expectedBaseUrl: string; |
| 21 | + expectedPath: string; |
| 22 | + queryParams?: Record<string, string>; |
| 23 | + } |
| 24 | + |
| 25 | + const testCases: TestCase[] = [ |
| 26 | + { |
| 27 | + description: "should use default scheme and token endpoint path when apiTokenIssuer has no scheme and no path", |
| 28 | + apiTokenIssuer: "issuer.fga.example", |
| 29 | + expectedBaseUrl: "https://issuer.fga.example", |
| 30 | + expectedPath: `/${DEFAULT_TOKEN_ENDPOINT_PATH}`, |
| 31 | + }, |
| 32 | + { |
| 33 | + description: "should use default token endpoint path when apiTokenIssuer has root path and no scheme", |
| 34 | + apiTokenIssuer: "https://issuer.fga.example/", |
| 35 | + expectedBaseUrl: "https://issuer.fga.example", |
| 36 | + expectedPath: `/${DEFAULT_TOKEN_ENDPOINT_PATH}`, |
| 37 | + }, |
| 38 | + { |
| 39 | + description: "should preserve custom token endpoint path when provided", |
| 40 | + apiTokenIssuer: "https://issuer.fga.example/some_endpoint", |
| 41 | + expectedBaseUrl: "https://issuer.fga.example", |
| 42 | + expectedPath: "/some_endpoint", |
| 43 | + }, |
| 44 | + { |
| 45 | + description: "should preserve custom token endpoint path with nested path when provided", |
| 46 | + apiTokenIssuer: "https://issuer.fga.example/api/v1/oauth/token", |
| 47 | + expectedBaseUrl: "https://issuer.fga.example", |
| 48 | + expectedPath: "/api/v1/oauth/token", |
| 49 | + }, |
| 50 | + { |
| 51 | + description: "should add https:// prefix when apiTokenIssuer has no scheme", |
| 52 | + apiTokenIssuer: "issuer.fga.example/some_endpoint", |
| 53 | + expectedBaseUrl: "https://issuer.fga.example", |
| 54 | + expectedPath: "/some_endpoint", |
| 55 | + }, |
| 56 | + { |
| 57 | + description: "should preserve http:// scheme when provided", |
| 58 | + apiTokenIssuer: "http://issuer.fga.example/some_endpoint", |
| 59 | + expectedBaseUrl: "http://issuer.fga.example", |
| 60 | + expectedPath: "/some_endpoint", |
| 61 | + }, |
| 62 | + { |
| 63 | + description: "should use default path when apiTokenIssuer has https:// scheme but no path", |
| 64 | + apiTokenIssuer: "https://issuer.fga.example", |
| 65 | + expectedBaseUrl: "https://issuer.fga.example", |
| 66 | + expectedPath: `/${DEFAULT_TOKEN_ENDPOINT_PATH}`, |
| 67 | + }, |
| 68 | + { |
| 69 | + description: "should preserve custom path with query parameters", |
| 70 | + apiTokenIssuer: "https://issuer.fga.example/some_endpoint?param=value", |
| 71 | + expectedBaseUrl: "https://issuer.fga.example", |
| 72 | + expectedPath: "/some_endpoint", |
| 73 | + queryParams: { param: "value" }, |
| 74 | + }, |
| 75 | + { |
| 76 | + description: "should preserve custom path with port number", |
| 77 | + apiTokenIssuer: "https://issuer.fga.example:8080/some_endpoint", |
| 78 | + expectedBaseUrl: "https://issuer.fga.example:8080", |
| 79 | + expectedPath: "/some_endpoint", |
| 80 | + }, |
| 81 | + ]; |
| 82 | + |
| 83 | + test.each(testCases)("$description", async ({ apiTokenIssuer, expectedBaseUrl, expectedPath, queryParams }) => { |
| 84 | + const scope = queryParams |
| 85 | + ? nock(expectedBaseUrl) |
| 86 | + .post(expectedPath) |
| 87 | + .query(queryParams) |
| 88 | + .reply(200, { |
| 89 | + access_token: "test-token", |
| 90 | + expires_in: 300, |
| 91 | + }) |
| 92 | + : nock(expectedBaseUrl) |
| 93 | + .post(expectedPath) |
| 94 | + .reply(200, { |
| 95 | + access_token: "test-token", |
| 96 | + expires_in: 300, |
| 97 | + }); |
| 98 | + |
| 99 | + const credentials = new Credentials( |
| 100 | + { |
| 101 | + method: CredentialsMethod.ClientCredentials, |
| 102 | + config: { |
| 103 | + apiTokenIssuer, |
| 104 | + apiAudience: OPENFGA_API_AUDIENCE, |
| 105 | + clientId: OPENFGA_CLIENT_ID, |
| 106 | + clientSecret: OPENFGA_CLIENT_SECRET, |
| 107 | + }, |
| 108 | + } as AuthCredentialsConfig, |
| 109 | + undefined, |
| 110 | + mockTelemetryConfig, |
| 111 | + ); |
| 112 | + |
| 113 | + await credentials.getAccessTokenHeader(); |
| 114 | + |
| 115 | + expect(scope.isDone()).toBe(true); |
| 116 | + nock.cleanAll(); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
0 commit comments