From 72873f7628bba943331f2fa87c6dd231444fe7e6 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra <13396919+cheenamalhotra@users.noreply.github.com> Date: Tue, 1 Aug 2023 23:29:09 -0700 Subject: [PATCH] Fixes reset of group name when not saving profile (#24000) --- src/sql/platform/connection/common/utils.ts | 4 ++ .../browser/connectionDialogService.ts | 2 +- .../connection/browser/connectionWidget.ts | 12 ++---- .../browser/connectionDialogService.test.ts | 39 ++++++++++++++++--- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/sql/platform/connection/common/utils.ts b/src/sql/platform/connection/common/utils.ts index 0434d75851..fd6d3a8550 100644 --- a/src/sql/platform/connection/common/utils.ts +++ b/src/sql/platform/connection/common/utils.ts @@ -179,3 +179,7 @@ export function adjustForMssqlAppName(currentAppName: string, suffix?: string): ? currentAppName + finalSuffix : currentAppName ?? appName; } + +export function delay(time: number): Promise { + return new Promise(resolve => setTimeout(resolve, time)); +} diff --git a/src/sql/workbench/services/connection/browser/connectionDialogService.ts b/src/sql/workbench/services/connection/browser/connectionDialogService.ts index b68eed620b..d13b9000d7 100644 --- a/src/sql/workbench/services/connection/browser/connectionDialogService.ts +++ b/src/sql/workbench/services/connection/browser/connectionDialogService.ts @@ -264,7 +264,7 @@ export class ConnectionDialogService implements IConnectionDialogService { } let options: IConnectionCompletionOptions = this._options || { params: params, - saveTheConnection: !isTemporaryConnection, + saveTheConnection: !isTemporaryConnection && !fromEditor, showDashboard: params?.showDashboard ?? false, showConnectionDialogOnError: false, showFirewallRuleOnError: true diff --git a/src/sql/workbench/services/connection/browser/connectionWidget.ts b/src/sql/workbench/services/connection/browser/connectionWidget.ts index ba3ddf01e2..a47d0d02e0 100644 --- a/src/sql/workbench/services/connection/browser/connectionWidget.ts +++ b/src/sql/workbench/services/connection/browser/connectionWidget.ts @@ -67,7 +67,6 @@ export class ConnectionWidget extends lifecycle.Disposable { private _azureAccountList: azdata.Account[]; private _callbacks: IConnectionComponentCallbacks; private _focusedBeforeHandleOnConnection: HTMLElement; - private _saveProfile: boolean; private _databaseDropdownExpanded: boolean = false; private _defaultDatabaseName: string = localize('defaultDatabaseOption', ""); private _loadingDatabaseName: string = localize('loadingDatabaseOption', "Loading..."); @@ -893,20 +892,15 @@ export class ConnectionWidget extends lifecycle.Disposable { this._connectionNameInputBox.value = this.getModelValue(connectionInfo.connectionName); this._userNameInputBox.value = this.getModelValue(connectionInfo.userName); this._passwordInputBox.value = this.getModelValue(connectionInfo.password); - this._saveProfile = connectionInfo.saveProfile; this._azureTenantId = connectionInfo.azureTenantId; if (this._databaseNameInputBox) { this._databaseNameInputBox.value = this.getModelValue(connectionInfo.databaseName); } let groupName: string; - if (this._saveProfile) { - if (!connectionInfo.groupFullName) { - groupName = this.DefaultServerGroup.name; - } else { - groupName = connectionInfo.groupFullName.replace('root/', ''); - } + if (!connectionInfo.groupFullName) { + groupName = this.DefaultServerGroup.name; } else { - groupName = this.NoneServerGroup.name; + groupName = connectionInfo.groupFullName.replace('root/', ''); } if (this._serverGroupSelectBox) { this._serverGroupSelectBox.selectWithOptionName(groupName); diff --git a/src/sql/workbench/services/connection/test/browser/connectionDialogService.test.ts b/src/sql/workbench/services/connection/test/browser/connectionDialogService.test.ts index 421bf93c88..e0729c805a 100644 --- a/src/sql/workbench/services/connection/test/browser/connectionDialogService.test.ts +++ b/src/sql/workbench/services/connection/test/browser/connectionDialogService.test.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------------*/ import { ConnectionManagementService } from 'sql/workbench/services/connection/browser/connectionManagementService'; -import { ConnectionType, IConnectableInput, IConnectionResult, INewConnectionParams, IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement'; +import { ConnectionType, IConnectableInput, IConnectionResult, INewConnectionParams, IConnectionManagementService, IConnectionCallbacks } from 'sql/platform/connection/common/connectionManagement'; import { TestErrorMessageService } from 'sql/platform/errorMessage/test/common/testErrorMessageService'; import * as TypeMoq from 'typemoq'; @@ -54,6 +54,8 @@ import { ConnectionBrowserView } from 'sql/workbench/services/connection/browser import { ConnectionProviderProperties, ICapabilitiesService } from 'sql/platform/capabilities/common/capabilitiesService'; import { Emitter } from 'vs/base/common/event'; import { NullCommandService } from 'vs/platform/commands/test/common/nullCommandService'; +import { delay } from 'sql/platform/connection/common/utils'; +import { IConnectionCompletionOptions } from 'azdata'; suite('ConnectionDialogService tests', () => { const testTreeViewId = 'testTreeView'; @@ -322,7 +324,8 @@ suite('ConnectionDialogService tests', () => { assert(!!returnedModel); assert.strictEqual(returnedModel._groupName, 'testGroup'); - assert(called); + await delay(200); + assert(called, 'fillInConnectionInputs was not called as expected.'); }); test('handleOnConnect calls connectAndSaveProfile when called with profile', async () => { @@ -339,8 +342,32 @@ suite('ConnectionDialogService tests', () => { (connectionDialogService as any)._connectionDialog.connectButtonState = true; ((connectionDialogService as any)._connectionDialog as any).connect(connectionProfile); }); + await delay(200); + assert(called, 'connectAndSaveProfile was not called as expected.'); + }); - setTimeout(() => { assert(called); }, 200); + test('handleOnConnect does not save connection for editor type connection', async () => { + let called = false; + let saveConnection = true; + mockConnectionManagementService.setup(x => x.connectAndSaveProfile(TypeMoq.It.isAny(), TypeMoq.It.isAnyString(), TypeMoq.It.isAny(), TypeMoq.It.isAny())) + .callback((connection: IConnectionProfile, uri: string, options: IConnectionCompletionOptions, callbacks: IConnectionCallbacks) => { + saveConnection = options.saveConnection; + }) + .returns(() => { + called = true; + return Promise.resolve({ connected: true, errorMessage: undefined, errorCode: undefined }); + }); + testConnectionParams.connectionType = ConnectionType.editor; + (connectionDialogService as any)._connectionDialog = undefined; + (connectionDialogService as any)._dialogDeferredPromise = new Deferred(); + await connectionDialogService.showDialog(mockConnectionManagementService.object, testConnectionParams, connectionProfile); + ((connectionDialogService as any)._connectionControllerMap['MSSQL'] as any)._model = connectionProfile; + (connectionDialogService as any)._connectionDialog.connectButtonState = true; + ((connectionDialogService as any)._connectionDialog as any).connect(connectionProfile); + + await delay(200); + assert(called, 'connectAndSaveProfile was not called as expected.'); + assert(!saveConnection, 'Must not save connection for editor connection type'); }); test('handleOnConnect calls connectAndSaveProfile when called without profile', async () => { @@ -356,7 +383,8 @@ suite('ConnectionDialogService tests', () => { ((connectionDialogService as any)._connectionControllerMap['MSSQL'] as any)._model = connectionProfile; (connectionDialogService as any)._connectionDialog.connectButtonState = true; ((connectionDialogService as any)._connectionDialog as any).connect(); - setTimeout(() => { assert(called); }, 200); + await delay(200); + assert(called, 'connectAndSaveProfile was not called as expected.'); }); test('handleOnCancel calls cancelEditorConnection', async () => { @@ -373,6 +401,7 @@ suite('ConnectionDialogService tests', () => { ((connectionDialogService as any)._connectionDialog as any).cancel(); }); mockWidget.verify(x => x.databaseDropdownExpanded = false, TypeMoq.Times.atLeastOnce()); - assert(called); + await delay(200); + assert(called, 'cancelEditorConnection was not called as expected.'); }); });