Skip to content

Commit a39df44

Browse files
committed
chore: Add integration tests for proxy support in the Atlas API
Uses a variant devtools-shared/devtools-proxy-support http proxy server for testing.
1 parent 86ec9c2 commit a39df44

File tree

8 files changed

+324
-92
lines changed

8 files changed

+324
-92
lines changed

package-lock.json

Lines changed: 73 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@
5050
"@types/yargs-parser": "^21.0.3",
5151
"@vitest/coverage-v8": "^3.2.4",
5252
"ai": "^4.3.17",
53+
"duplexpair": "^1.0.2",
5354
"eslint": "^9.30.1",
5455
"eslint-config-prettier": "^10.1.5",
5556
"eslint-plugin-prettier": "^5.5.1",
5657
"globals": "^16.3.0",
57-
"http-proxy": "^1.18.1",
5858
"mongodb-runner": "^5.9.2",
5959
"ollama-ai-provider": "^1.2.0",
6060
"openapi-types": "^12.1.3",

src/common/atlas/apiClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface ApiClientOptions {
2121
userAgent?: string;
2222
}
2323

24-
interface AccessToken {
24+
export interface AccessToken {
2525
access_token: string;
2626
expires_at?: number;
2727
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)