|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import type { AccessToken } from "../../../src/common/atlas/apiClient.js"; |
| 3 | +import { ApiClient } from "../../../src/common/atlas/apiClient.js"; |
| 4 | +import { HTTPServerProxyTestSetup } from "../fixtures/httpsServerProxyTest.js"; |
| 5 | + |
| 6 | +describe("ApiClient integration test", () => { |
| 7 | + describe("oauth authentication proxy", () => { |
| 8 | + let apiClient: ApiClient; |
| 9 | + let proxyTestSetup: HTTPServerProxyTestSetup; |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; |
| 13 | + proxyTestSetup = new HTTPServerProxyTestSetup(); |
| 14 | + await proxyTestSetup.listen(); |
| 15 | + |
| 16 | + process.env.HTTP_PROXY = `https://localhost:${proxyTestSetup.httpsProxyPort}/`; |
| 17 | + apiClient = new ApiClient({ |
| 18 | + baseUrl: `https://localhost:${proxyTestSetup.httpsServerPort}/`, |
| 19 | + credentials: { |
| 20 | + clientId: "test-client-id", |
| 21 | + clientSecret: "test-client-secret", |
| 22 | + }, |
| 23 | + userAgent: "test-user-agent", |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + function withToken(accessToken: string, expired: boolean) { |
| 28 | + const apiClientMut = apiClient as unknown as { accessToken: AccessToken }; |
| 29 | + const expireAt = expired ? Date.now() - 100000 : Date.now() + 10000; |
| 30 | + |
| 31 | + apiClientMut.accessToken = { |
| 32 | + access_token: accessToken, |
| 33 | + expires_at: expireAt, |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + afterEach(async () => { |
| 38 | + delete process.env.NODE_TLS_REJECT_UNAUTHORIZED; |
| 39 | + delete process.env.HTTP_PROXY; |
| 40 | + |
| 41 | + await apiClient.close(); |
| 42 | + await proxyTestSetup.teardown(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should send the oauth request through a proxy if configured", async () => { |
| 46 | + await apiClient.validateAccessToken(); |
| 47 | + expect(proxyTestSetup.getRequestedUrls()).toEqual([ |
| 48 | + `http://localhost:${proxyTestSetup.httpsServerPort}/api/oauth/token`, |
| 49 | + ]); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should send the oauth revoke request through a proxy if configured", async () => { |
| 53 | + withToken("my non expired token", false); |
| 54 | + await apiClient.close(); |
| 55 | + expect(proxyTestSetup.getRequestedUrls()).toEqual([ |
| 56 | + `http://localhost:${proxyTestSetup.httpsServerPort}/api/oauth/revoke`, |
| 57 | + ]); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should make an atlas call when the token is not expired", async () => { |
| 61 | + withToken("my not expired", false); |
| 62 | + await apiClient.listOrganizations(); |
| 63 | + expect(proxyTestSetup.getRequestedUrls()).toEqual([ |
| 64 | + `http://localhost:${proxyTestSetup.httpsServerPort}/api/atlas/v2/orgs`, |
| 65 | + ]); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should request a new token and an atlas call when the token is expired", async () => { |
| 69 | + withToken("my expired", true); |
| 70 | + await apiClient.listOrganizations(); |
| 71 | + expect(proxyTestSetup.getRequestedUrls()).toEqual([ |
| 72 | + `http://localhost:${proxyTestSetup.httpsServerPort}/api/oauth/token`, |
| 73 | + `http://localhost:${proxyTestSetup.httpsServerPort}/api/atlas/v2/orgs`, |
| 74 | + ]); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
0 commit comments