Skip to content

Commit 2dbc94a

Browse files
committed
update
1 parent 87fe0f3 commit 2dbc94a

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

src/common/atlas/apiClient.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,35 +119,40 @@ export class ApiClient {
119119
}
120120

121121
public async sendEvents(events: TelemetryEvent<CommonProperties>[]): Promise<void> {
122-
if (!this.hasCredentials()) {
122+
if (!this.options.credentials) {
123123
await this.sendUnauthEvents(events);
124124
return;
125125
}
126126

127127
try {
128-
await this.validateAccessToken();
129128
await this.sendAuthEvents(events);
130129
} catch (error) {
131130
if (error instanceof ApiClientError) {
132131
if (error.response.status !== 401) {
133132
throw error;
134133
}
135134
}
136-
// send unauth events if the token is not valid
135+
136+
// send unauth events if any of the following are true:
137+
// 1: the token is not valid (not ApiClientError)
138+
// 2: if the api responded with 401 (ApiClientError with status 401)
137139
await this.sendUnauthEvents(events);
138-
return;
139140
}
140141
}
141142

142143
private async sendAuthEvents(events: TelemetryEvent<CommonProperties>[]): Promise<void> {
144+
const accessToken = await this.getAccessToken();
145+
if (!accessToken) {
146+
throw new Error("No access token available");
147+
}
143148
const authUrl = new URL("api/private/v1.0/telemetry/events", this.options.baseUrl);
144149
const response = await fetch(authUrl, {
145150
method: "POST",
146151
headers: {
147152
Accept: "application/json",
148153
"Content-Type": "application/json",
149154
"User-Agent": this.options.userAgent,
150-
Authorization: `Bearer ${this.accessToken}`,
155+
Authorization: `Bearer ${accessToken}`,
151156
},
152157
body: JSON.stringify(events),
153158
});

src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export class Server {
188188

189189
if (this.userConfig.apiClientId && this.userConfig.apiClientSecret) {
190190
try {
191-
await this.session.apiClient.hasValidAccessToken();
191+
await this.session.apiClient.validateAccessToken();
192192
} catch (error) {
193193
if (this.userConfig.connectionString === undefined) {
194194
console.error("Failed to validate MongoDB Atlas the credentials from the config: ", error);

tests/integration/helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export function setupIntegrationTest(getUserConfig: () => UserConfig): Integrati
6161
// Mock hasValidAccessToken for tests
6262
if (userConfig.apiClientId && userConfig.apiClientSecret) {
6363
const mockFn = jest.fn<() => Promise<boolean>>().mockResolvedValue(true);
64-
session.apiClient.hasValidAccessToken = mockFn;
64+
// @ts-expect-error accessing private property for testing
65+
session.apiClient.validateAccessToken = mockFn;
6566
}
6667

6768
userConfig.telemetry = "disabled";

tests/unit/apiClient.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe("ApiClient", () => {
9595
});
9696

9797
describe("sendEvents", () => {
98-
it("should send events to authenticated endpoint when token is available", async () => {
98+
it("should send events to authenticated endpoint when token is available and valid", async () => {
9999
const mockFetch = jest.spyOn(global, "fetch");
100100
mockFetch.mockResolvedValueOnce(new Response(null, { status: 200 }));
101101

@@ -114,12 +114,12 @@ describe("ApiClient", () => {
114114
});
115115
});
116116

117-
it("should fall back to unauthenticated endpoint when token is not available", async () => {
117+
it("should fall back to unauthenticated endpoint when token is not available via exception", async () => {
118118
const mockFetch = jest.spyOn(global, "fetch");
119119
mockFetch.mockResolvedValueOnce(new Response(null, { status: 200 }));
120120

121121
// @ts-expect-error accessing private property for testing
122-
apiClient.getAccessToken = jest.fn().mockResolvedValue(undefined);
122+
apiClient.getAccessToken = jest.fn().mockRejectedValue(new Error("No access token available"));
123123

124124
await apiClient.sendEvents(mockEvents);
125125

0 commit comments

Comments
 (0)