From 53f77ac9bcda82f39a97e29bce5b7676708fefdd Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Wed, 26 Feb 2025 10:49:37 +0100 Subject: [PATCH 1/8] Improve test assertions --- src/test/suite/connectionController.test.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/test/suite/connectionController.test.ts b/src/test/suite/connectionController.test.ts index 92ad2a1ac..66fe37e88 100644 --- a/src/test/suite/connectionController.test.ts +++ b/src/test/suite/connectionController.test.ts @@ -135,7 +135,7 @@ suite('Connection Controller Test Suite', function () { expect(successfullyRemovedMongoDBConnection).to.be.false; }); - test('when adding a new connection it disconnects from the current connection', async () => { + test.only('when adding a new connection it disconnects from the current connection', async () => { const succesfullyConnected = await testConnectionController.addNewConnectionStringAndConnect( TEST_DATABASE_URI @@ -150,9 +150,9 @@ suite('Connection Controller Test Suite', function () { } catch (error) { const expectedError = 'Failed to connect'; - expect(formatError(error).message.includes(expectedError)).to.be.true; - expect(testConnectionController.getActiveDataService()).to.equal(null); - expect(testConnectionController.getActiveConnectionId()).to.equal(null); + expect(formatError(error).message).includes(expectedError); + expect(testConnectionController.getActiveDataService()).to.be.null; + expect(testConnectionController.getActiveConnectionId()).to.be.null; } }); @@ -261,7 +261,7 @@ suite('Connection Controller Test Suite', function () { await testConnectionController.loadSavedConnections(); - expect(testConnectionController.getSavedConnections().length).to.equal(1); + expect(testConnectionController.getSavedConnections()).to.have.lengthOf(1); expect(testConnectionController._connections['1234']).is.undefined; }); @@ -295,7 +295,7 @@ suite('Connection Controller Test Suite', function () { const connections = testConnectionController._connections; - expect(Object.keys(connections).length).to.equal(4); + expect(Object.keys(connections)).to.have.lengthOf(4); expect(connections[Object.keys(connections)[0]].name).to.equal( 'localhost:27088' ); @@ -331,7 +331,7 @@ suite('Connection Controller Test Suite', function () { StorageVariables.WORKSPACE_SAVED_CONNECTIONS ); - expect(workspaceStoreConnections).to.equal(undefined); + expect(workspaceStoreConnections).to.be.undefined; }); test('when a connection is added it is saved to the workspace store', async () => { @@ -364,7 +364,7 @@ suite('Connection Controller Test Suite', function () { StorageLocation.GLOBAL ); - expect(globalStoreConnections).to.equal(undefined); + expect(globalStoreConnections).to.be.undefined; }); test('a connection can be connected to by id', async () => { @@ -407,12 +407,12 @@ suite('Connection Controller Test Suite', function () { await testConnectionController.disconnect(); testConnectionController.clearAllConnections(); - expect(testConnectionController.getSavedConnections().length).to.equal(0); + expect(testConnectionController.getSavedConnections()).to.have.lengthOf(0); // Activate (which will load the past connection). await testConnectionController.loadSavedConnections(); - expect(testConnectionController.getSavedConnections().length).to.equal(1); + expect(testConnectionController.getSavedConnections()).to.have.lengthOf(1); const id = testConnectionController.getSavedConnections()[0].id; From 56d799ecf7430c8dc5fa851bcc04949bc680baee Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Wed, 26 Feb 2025 11:08:14 +0100 Subject: [PATCH 2/8] Remove .only --- src/test/suite/connectionController.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/suite/connectionController.test.ts b/src/test/suite/connectionController.test.ts index 66fe37e88..cedd44b9c 100644 --- a/src/test/suite/connectionController.test.ts +++ b/src/test/suite/connectionController.test.ts @@ -135,7 +135,7 @@ suite('Connection Controller Test Suite', function () { expect(successfullyRemovedMongoDBConnection).to.be.false; }); - test.only('when adding a new connection it disconnects from the current connection', async () => { + test('when adding a new connection it disconnects from the current connection', async () => { const succesfullyConnected = await testConnectionController.addNewConnectionStringAndConnect( TEST_DATABASE_URI From 065b64b9585de355efd97b35cbacdd6f56888cdf Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Wed, 26 Feb 2025 17:01:42 +0100 Subject: [PATCH 3/8] Add log dir for server logs --- src/test/suite/oidc.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/suite/oidc.test.ts b/src/test/suite/oidc.test.ts index 29c853894..366f42e5f 100644 --- a/src/test/suite/oidc.test.ts +++ b/src/test/suite/oidc.test.ts @@ -128,6 +128,7 @@ suite('OIDC Tests', function () { ...defaultClusterOptions, version: '8.0.x', downloadOptions: { enterprise: true }, + logDir: path.join(tmpdir, 'logs'), args: [ '--setParameter', 'authenticationMechanisms=SCRAM-SHA-256,MONGODB-OIDC', From 78fb4adad1a905a6edaaf795a2327deca2b257f5 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Wed, 26 Feb 2025 17:11:12 +0100 Subject: [PATCH 4/8] Throw error --- src/connectionController.ts | 35 +++++++++++++++++++++++++++++++++++ src/test/suite/oidc.test.ts | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/connectionController.ts b/src/connectionController.ts index 6fd4531dd..59dd182d8 100644 --- a/src/connectionController.ts +++ b/src/connectionController.ts @@ -331,6 +331,41 @@ export default class ConnectionController { } } + async addNewConnectionStringAndConnect2( + connectionString: string + ): Promise { + log.info('Trying to connect to a new connection configuration...'); + + const connectionStringData = new ConnectionString(connectionString); + + // TODO: Allow overriding appname + use driverInfo instead + // (https://jira.mongodb.org/browse/MONGOSH-1015) + connectionStringData.searchParams.set( + 'appname', + `${packageJSON.name} ${packageJSON.version}` + ); + + try { + const connectResult = await this.saveNewConnectionAndConnect({ + connectionId: uuidv4(), + connectionOptions: { + connectionString: connectionStringData.toString(), + }, + connectionType: ConnectionTypes.CONNECTION_STRING, + }); + + return connectResult.successfullyConnected; + } catch (error) { + const printableError = formatError(error); + log.error('Failed to connect with a connection string', error); + void vscode.window.showErrorMessage( + `Unable to connect: ${printableError.message}` + ); + + throw error; + } + } + private sendTelemetry( newDataService: DataService, connectionType: ConnectionTypes diff --git a/src/test/suite/oidc.test.ts b/src/test/suite/oidc.test.ts index 366f42e5f..aa68e53c2 100644 --- a/src/test/suite/oidc.test.ts +++ b/src/test/suite/oidc.test.ts @@ -189,7 +189,7 @@ suite('OIDC Tests', function () { test('can successfully connect with a connection string', async function () { const succesfullyConnected = - await testConnectionController.addNewConnectionStringAndConnect( + await testConnectionController.addNewConnectionStringAndConnect2( connectionString ); expect(succesfullyConnected).to.be.true; From e819024cc8ad15c7eefa69ff6d64999785aa7769 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Wed, 26 Feb 2025 17:41:23 +0100 Subject: [PATCH 5/8] try a new port --- src/connectionController.ts | 5 ++++- src/test/suite/oidc.test.ts | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/connectionController.ts b/src/connectionController.ts index 59dd182d8..bd9303124 100644 --- a/src/connectionController.ts +++ b/src/connectionController.ts @@ -362,7 +362,9 @@ export default class ConnectionController { `Unable to connect: ${printableError.message}` ); - throw error; + throw new Error( + `Failed to connect: connection string: ${connectionString}, error: ${error}` + ); } } @@ -476,6 +478,7 @@ export default class ConnectionController { ...connectionOptions, oidc: { ...cloneDeep(connectionOptions.oidc), + redirectURI: 'http://localhost:37197/redirect', openBrowser: browserAuthCommand ? { command: browserAuthCommand } : async ({ signal, url }): Promise => { diff --git a/src/test/suite/oidc.test.ts b/src/test/suite/oidc.test.ts index aa68e53c2..32d3c55bd 100644 --- a/src/test/suite/oidc.test.ts +++ b/src/test/suite/oidc.test.ts @@ -231,7 +231,7 @@ suite('OIDC Tests', function () { }; expect( - await testConnectionController.addNewConnectionStringAndConnect( + await testConnectionController.addNewConnectionStringAndConnect2( connectionString ) ).to.be.true; From 98f10a5e55424a3d588e98a3cb4924ef31564def Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Wed, 26 Feb 2025 18:03:16 +0100 Subject: [PATCH 6/8] Add console.log --- src/logging.ts | 3 +++ src/test/suite/oidc.test.ts | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/logging.ts b/src/logging.ts index fc38518f1..e513d46d1 100644 --- a/src/logging.ts +++ b/src/logging.ts @@ -60,6 +60,9 @@ class Logger implements ILogger { private append(type: string, message: string): void { // https://code.visualstudio.com/api/references/vscode-api#window.createOutputChannel + console.log( + `aaa - ${new Date().toISOString()} ${this.name} ${type} ${message}` + ); Logger.channel.appendLine( `${new Date().toISOString()} ${this.name} ${type} ${message}\n` ); diff --git a/src/test/suite/oidc.test.ts b/src/test/suite/oidc.test.ts index 32d3c55bd..f0140155e 100644 --- a/src/test/suite/oidc.test.ts +++ b/src/test/suite/oidc.test.ts @@ -59,7 +59,7 @@ const DEFAULT_TOKEN_PAYLOAD = { }, }; -suite('OIDC Tests', function () { +suite.only('OIDC Tests', function () { this.timeout(50000); const extensionContextStub = new ExtensionContextStub(); From 6a398f2319f1cc4d72f1b5e366fa7b348c08665b Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Wed, 26 Feb 2025 18:18:51 +0100 Subject: [PATCH 7/8] Log deeper --- src/logging.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/logging.ts b/src/logging.ts index e513d46d1..a02871f8b 100644 --- a/src/logging.ts +++ b/src/logging.ts @@ -32,7 +32,9 @@ class Logger implements ILogger { public info(message?: any, ...optionalParams: any[]): void { this.append( 'INFO ', - `${message} ${optionalParams ? util.inspect(optionalParams) : ''}` + `${message} ${ + optionalParams ? util.inspect(optionalParams, { depth: 100 }) : '' + }` ); } From 684368ccb3dc8ea7b7d4505848655ade4526b219 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Tue, 4 Mar 2025 12:22:44 +0100 Subject: [PATCH 8/8] Bump oidc-plugin --- package-lock.json | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index b94a7b22d..25ab229ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,7 +48,7 @@ "@babel/preset-typescript": "^7.25.7", "@babel/types": "^7.25.8", "@mongodb-js/oidc-mock-provider": "^0.10.2", - "@mongodb-js/oidc-plugin": "^1.1.5", + "@mongodb-js/oidc-plugin": "^1.1.6", "@mongodb-js/prettier-config-devtools": "^1.0.1", "@mongodb-js/sbom-tools": "^0.7.2", "@mongodb-js/signing-utils": "^0.3.8", @@ -7563,9 +7563,9 @@ } }, "node_modules/@mongodb-js/oidc-plugin": { - "version": "1.1.5", - "resolved": "https://registry.npmjs.org/@mongodb-js/oidc-plugin/-/oidc-plugin-1.1.5.tgz", - "integrity": "sha512-K76ADgrDpL+lg6L/QsEBIGbSjTEUljYDGDX75Tq4+zIkx3JQgeQhS5J3qZNzKwJa4nj+EwhihaADLRgsMpAtrA==", + "version": "1.1.6", + "resolved": "https://registry.npmjs.org/@mongodb-js/oidc-plugin/-/oidc-plugin-1.1.6.tgz", + "integrity": "sha512-fuL4B9x1njcqdJqV+V3pt8s/9PX4uy9ojhcsP12BavDcg61ju6WEqCkDmUZCykDIvsDbb8tIhO97aCKDxcXROw==", "license": "Apache-2.0", "dependencies": { "express": "^4.18.2", diff --git a/package.json b/package.json index 4d27c382d..1ed0dbc8f 100644 --- a/package.json +++ b/package.json @@ -1350,7 +1350,7 @@ "@babel/preset-typescript": "^7.25.7", "@babel/types": "^7.25.8", "@mongodb-js/oidc-mock-provider": "^0.10.2", - "@mongodb-js/oidc-plugin": "^1.1.5", + "@mongodb-js/oidc-plugin": "^1.1.6", "@mongodb-js/prettier-config-devtools": "^1.0.1", "@mongodb-js/sbom-tools": "^0.7.2", "@mongodb-js/signing-utils": "^0.3.8",