11import type { Session } from "../../../../src/common/session.js" ;
22import { expectDefined , getResponseContent } from "../../helpers.js" ;
3- import { describeWithAtlas , withProject , randomId , deleteCluster , waitCluster , sleep } from "./atlasHelpers.js" ;
4- import { afterAll , beforeAll , describe , expect , it } from "vitest" ;
3+ import {
4+ describeWithAtlas ,
5+ withProject ,
6+ withCluster ,
7+ randomId ,
8+ deleteCluster ,
9+ waitCluster ,
10+ sleep ,
11+ } from "./atlasHelpers.js" ;
12+ import { afterAll , beforeAll , describe , expect , it , vitest } from "vitest" ;
513
614describeWithAtlas ( "clusters" , ( integration ) => {
715 withProject ( integration , ( { getProjectId, getIpAddress } ) => {
8- const clusterName = "ClusterTest-" + randomId ;
16+ const clusterName = "ClusterTest-" + randomId ( ) ;
917
1018 afterAll ( async ( ) => {
1119 const projectId = getProjectId ( ) ;
@@ -142,6 +150,11 @@ describeWithAtlas("clusters", (integration) => {
142150 } ) ;
143151
144152 it ( "connects to cluster" , async ( ) => {
153+ const createDatabaseUserSpy = vitest . spyOn (
154+ integration . mcpServer ( ) . session . apiClient ,
155+ "createDatabaseUser"
156+ ) ;
157+
145158 const projectId = getProjectId ( ) ;
146159 const connectionType = "standard" ;
147160 let connected = false ;
@@ -158,6 +171,8 @@ describeWithAtlas("clusters", (integration) => {
158171 if ( content . includes ( `Connected to cluster "${ clusterName } "` ) ) {
159172 connected = true ;
160173
174+ expect ( createDatabaseUserSpy ) . toHaveBeenCalledTimes ( 1 ) ;
175+
161176 // assert that some of the element s have the message
162177 expect ( content ) . toContain (
163178 "Note: A temporary user has been created to enable secure connection to the cluster. For more information, see https://dochub.mongodb.org/core/mongodb-mcp-server-tools-considerations"
@@ -172,6 +187,58 @@ describeWithAtlas("clusters", (integration) => {
172187 expect ( connected ) . toBe ( true ) ;
173188 } ) ;
174189
190+ describe ( "when connected" , ( ) => {
191+ withCluster (
192+ integration ,
193+ ( { getProjectId : getSecondaryProjectId , getClusterName : getSecondaryClusterName } ) => {
194+ beforeAll ( async ( ) => {
195+ let connected = false ;
196+ for ( let i = 0 ; i < 10 ; i ++ ) {
197+ const response = await integration . mcpClient ( ) . callTool ( {
198+ name : "atlas-connect-cluster" ,
199+ arguments : {
200+ projectId : getSecondaryProjectId ( ) ,
201+ clusterName : getSecondaryClusterName ( ) ,
202+ connectionType : "standard" ,
203+ } ,
204+ } ) ;
205+
206+ const content = getResponseContent ( response . content ) ;
207+
208+ if ( content . includes ( `Connected to cluster "${ getSecondaryClusterName ( ) } "` ) ) {
209+ connected = true ;
210+ break ;
211+ }
212+
213+ await sleep ( 500 ) ;
214+ }
215+
216+ if ( ! connected ) {
217+ throw new Error ( "Could not connect to cluster before tests" ) ;
218+ }
219+ } ) ;
220+
221+ it ( "disconnects and deletes the database user before connecting to another cluster" , async ( ) => {
222+ const deleteDatabaseUserSpy = vitest . spyOn (
223+ integration . mcpServer ( ) . session . apiClient ,
224+ "deleteDatabaseUser"
225+ ) ;
226+
227+ await integration . mcpClient ( ) . callTool ( {
228+ name : "atlas-connect-cluster" ,
229+ arguments : {
230+ projectId : getProjectId ( ) ,
231+ clusterName : clusterName ,
232+ connectionType : "standard" ,
233+ } ,
234+ } ) ;
235+
236+ expect ( deleteDatabaseUserSpy ) . toHaveBeenCalledTimes ( 1 ) ;
237+ } ) ;
238+ }
239+ ) ;
240+ } ) ;
241+
175242 describe ( "when not connected" , ( ) => {
176243 it ( "prompts for atlas-connect-cluster when querying mongodb" , async ( ) => {
177244 const response = await integration . mcpClient ( ) . callTool ( {
0 commit comments