Skip to content

Commit 7c69be7

Browse files
committed
fix: tests
1 parent 67263f6 commit 7c69be7

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

.vscode/launch.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Tests",
11+
"runtimeExecutable": "npm",
12+
"runtimeArgs": ["test", "--", "tests/integration/tools/atlas/clusters.test.ts"],
13+
"cwd": "${workspaceFolder}",
14+
"envFile": "${workspaceFolder}/.env"
15+
},
716
{
817
"type": "node",
918
"request": "launch",

src/common/atlas/apiClient.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ApiClientError } from "./apiClientError.js";
55
import { paths, operations } from "./openapi.js";
66
import { CommonProperties, TelemetryEvent } from "../../telemetry/types.js";
77
import { packageInfo } from "../../helpers/packageInfo.js";
8+
import logger, { LogId } from "../../logger.js";
89

910
const ATLAS_API_VERSION = "2025-03-12";
1011

@@ -34,9 +35,7 @@ export class ApiClient {
3435

3536
private getAccessToken = async () => {
3637
if (this.oauth2Client && (!this.accessToken || this.accessToken.expired())) {
37-
this.accessToken = await this.oauth2Client.getToken({
38-
agent: this.options.userAgent,
39-
});
38+
this.accessToken = await this.oauth2Client.getToken({});
4039
}
4140
return this.accessToken?.token.access_token as string | undefined;
4241
};
@@ -49,7 +48,9 @@ export class ApiClient {
4948

5049
try {
5150
const accessToken = await this.getAccessToken();
52-
request.headers.set("Authorization", `Bearer ${accessToken}`);
51+
if (accessToken) {
52+
request.headers.set("Authorization", `Bearer ${accessToken}`);
53+
}
5354
return request;
5455
} catch {
5556
// ignore not availble tokens, API will return 401
@@ -81,14 +82,20 @@ export class ApiClient {
8182
auth: {
8283
tokenHost: this.options.baseUrl,
8384
tokenPath: "/api/oauth/token",
85+
revokePath: "/api/oauth/revoke",
86+
},
87+
http: {
88+
headers: {
89+
"User-Agent": this.options.userAgent,
90+
},
8491
},
8592
});
8693
this.client.use(this.authMiddleware);
8794
}
8895
}
8996

9097
public hasCredentials(): boolean {
91-
return !!(this.oauth2Client && this.accessToken);
98+
return !!this.oauth2Client;
9299
}
93100

94101
public async validateAccessToken(): Promise<void> {
@@ -97,7 +104,13 @@ export class ApiClient {
97104

98105
public async close(): Promise<void> {
99106
if (this.accessToken) {
100-
await this.accessToken.revokeAll();
107+
try {
108+
await this.accessToken.revoke("access_token");
109+
} catch (error: unknown) {
110+
const err = error instanceof Error ? error : new Error(String(error));
111+
logger.error(LogId.atlasApiRevokeFailure, "apiClient", `Failed to revoke access token: ${err.message}`);
112+
}
113+
this.accessToken = undefined;
101114
}
102115
}
103116

src/logger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const LogId = {
1919
atlasInspectFailure: mongoLogId(1_001_004),
2020
atlasConnectAttempt: mongoLogId(1_001_005),
2121
atlasConnectSucceeded: mongoLogId(1_001_006),
22+
atlasApiRevokeFailure: mongoLogId(1_001_007),
2223

2324
telemetryDisabled: mongoLogId(1_002_001),
2425
telemetryEmitFailure: mongoLogId(1_002_002),

src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ export class Server {
101101
}
102102

103103
async close(): Promise<void> {
104-
await this.mcpServer.close();
105104
await this.telemetry.close();
106105
await this.session.close();
106+
await this.mcpServer.close();
107107
}
108108

109109
/**

tests/integration/helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ export function setupIntegrationTest(getUserConfig: () => UserConfig): Integrati
8484
});
8585

8686
afterEach(async () => {
87-
if (mcpServer) {
88-
await mcpServer.session.close();
87+
if (mcpServer && !mcpServer.session.connectedAtlasCluster) {
88+
await mcpServer.session.disconnect();
89+
mcpServer.session.emit("close");
8990
}
9091
});
9192

0 commit comments

Comments
 (0)