Skip to content

Commit 439daab

Browse files
committed
decouple and add unit test
1 parent 632fc54 commit 439daab

File tree

2 files changed

+58
-33
lines changed

2 files changed

+58
-33
lines changed

src/tools/atlas/connect/connectCluster.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { LogId } from "../../../common/logger.js";
77
import { inspectCluster } from "../../../common/atlas/cluster.js";
88
import { ensureCurrentIpInAccessList } from "../../../common/atlas/accessListUtils.js";
99
import { AtlasClusterConnectionInfo } from "../../../common/connectionManager.js";
10-
import { DatabaseUserRole } from "../../../common/atlas/openapi.js";
10+
import { getDefaultRoleFromConfig } from "../../../common/atlas/roles.js";
1111

1212
const EXPIRY_MS = 1000 * 60 * 60 * 12; // 12 hours
1313

@@ -73,7 +73,7 @@ export class ConnectClusterTool extends AtlasToolBase {
7373
const password = await generateSecurePassword();
7474

7575
const expiryDate = new Date(Date.now() + EXPIRY_MS);
76-
const role = this.getRoleFromConfig();
76+
const role = getDefaultRoleFromConfig(this.config);
7777

7878
await this.session.apiClient.createDatabaseUser({
7979
params: {
@@ -245,35 +245,4 @@ export class ConnectClusterTool extends AtlasToolBase {
245245
],
246246
};
247247
}
248-
249-
/**
250-
* @description Get the role name for the database user based on the Atlas Admin API https://www.mongodb.com/docs/atlas/mongodb-users-roles-and-privileges/
251-
* @returns The role name for the database user
252-
*/
253-
private getRoleFromConfig(): DatabaseUserRole {
254-
if (this.config.readOnly) {
255-
return {
256-
roleName: "readAnyDatabase",
257-
databaseName: "admin",
258-
};
259-
}
260-
261-
// If all write tools are enabled, use readWriteAnyDatabase
262-
if (
263-
!this.config.disabledTools?.includes("create") &&
264-
!this.config.disabledTools?.includes("update") &&
265-
!this.config.disabledTools?.includes("delete") &&
266-
!this.config.disabledTools?.includes("metadata")
267-
) {
268-
return {
269-
roleName: "readWriteAnyDatabase",
270-
databaseName: "admin",
271-
};
272-
}
273-
274-
return {
275-
roleName: "readAnyDatabase",
276-
databaseName: "admin",
277-
};
278-
}
279248
}

tests/unit/common/roles.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, it, expect } from "vitest";
2+
import { getDefaultRoleFromConfig } from "../../../src/common/atlas/roles.js";
3+
import { defaultUserConfig, UserConfig } from "../../../src/common/config.js";
4+
5+
describe("getDefaultRoleFromConfig", () => {
6+
const defaultConfig: UserConfig = {
7+
...defaultUserConfig,
8+
};
9+
10+
const readOnlyConfig: UserConfig = {
11+
...defaultConfig,
12+
readOnly: true,
13+
};
14+
15+
const readWriteConfig: UserConfig = {
16+
...defaultConfig,
17+
readOnly: false,
18+
disabledTools: [],
19+
};
20+
21+
it("should return the correct role for a read-only config", () => {
22+
const role = getDefaultRoleFromConfig(readOnlyConfig);
23+
expect(role).toEqual({
24+
roleName: "readAnyDatabase",
25+
databaseName: "admin",
26+
});
27+
});
28+
29+
it("should return the correct role for a read-write config", () => {
30+
const role = getDefaultRoleFromConfig(readWriteConfig);
31+
expect(role).toEqual({
32+
roleName: "readWriteAnyDatabase",
33+
databaseName: "admin",
34+
});
35+
});
36+
37+
it("should return the correct role for a read-write config with all tools enabled", () => {
38+
const role = getDefaultRoleFromConfig(readWriteConfig);
39+
expect(role).toEqual({
40+
roleName: "readWriteAnyDatabase",
41+
databaseName: "admin",
42+
});
43+
});
44+
45+
// loop with each disabled tool
46+
for (const tool of ["create", "update", "delete", "metadata"]) {
47+
it(`should return the correct role for a read-write config with ${tool} disabled`, () => {
48+
const config = { ...readWriteConfig, disabledTools: [tool] };
49+
const role = getDefaultRoleFromConfig(config);
50+
expect(role).toEqual({
51+
roleName: "readAnyDatabase",
52+
databaseName: "admin",
53+
});
54+
});
55+
}
56+
});

0 commit comments

Comments
 (0)