getCredentials returns credentials for any saved connection (#11029)

* getCredentials returns credentials for any saved connection

* Update doc comment

* Add tests

* cleanup

* cleanup
This commit is contained in:
Charles Gagnon
2020-06-23 11:04:13 -07:00
committed by GitHub
parent 29bf016fb8
commit a303144226
6 changed files with 59 additions and 14 deletions

View File

@@ -121,7 +121,7 @@ export class MainThreadConnectionManagement extends Disposable implements MainTh
}
public $getCredentials(connectionId: string): Thenable<{ [name: string]: string }> {
return Promise.resolve(this._connectionManagementService.getActiveConnectionCredentials(connectionId));
return Promise.resolve(this._connectionManagementService.getConnectionCredentials(connectionId));
}
public $getServerInfo(connectionId: string): Thenable<azdata.ServerInfo> {

View File

@@ -1357,10 +1357,15 @@ export class ConnectionManagementService extends Disposable implements IConnecti
return this._connectionStore.getProfileWithoutPassword(originalProfile);
}
public getActiveConnectionCredentials(profileId: string): { [name: string]: string } {
public async getConnectionCredentials(profileId: string): Promise<{ [name: string]: string }> {
let profile = find(this.getActiveConnections(), connectionProfile => connectionProfile.id === profileId);
if (!profile) {
return undefined;
// Couldn't find an active profile so try all profiles now - fetching the password if we found one
profile = find(this.getConnections(), connectionProfile => connectionProfile.id === profileId);
if (!profile) {
return undefined;
}
await this.addSavedPassword(profile);
}
// Find the password option for the connection provider

View File

@@ -1199,16 +1199,57 @@ suite('SQL ConnectionManagementService tests', () => {
});
});
test('getActiveConnectionCredentials returns the credentials dictionary for a connection profile', () => {
test('getConnectionCredentials returns the credentials dictionary for an active connection profile', async () => {
let profile = assign({}, connectionProfile);
profile.options = { password: profile.password };
profile.id = 'test_id';
connectionStatusManager.addConnection(profile, 'test_uri');
(connectionManagementService as any)._connectionStatusManager = connectionStatusManager;
let credentials = connectionManagementService.getActiveConnectionCredentials(profile.id);
let credentials = await connectionManagementService.getConnectionCredentials(profile.id);
assert.equal(credentials['password'], profile.options['password']);
});
test('getConnectionCredentials returns the credentials dictionary for a recently used connection profile', async () => {
const test_password = 'test_password';
const profile = createConnectionProfile('test_id', '');
const connectionStoreMock = TypeMoq.Mock.ofType(ConnectionStore, TypeMoq.MockBehavior.Loose, new TestStorageService());
connectionStoreMock.setup(x => x.getRecentlyUsedConnections(undefined)).returns(() => {
return [profile];
});
connectionStoreMock.setup(x => x.addSavedPassword(TypeMoq.It.isAny())).returns(async () => {
profile.password = test_password;
return { profile: profile, savedCred: true };
});
const connectionManagementService = new ConnectionManagementService(connectionStoreMock.object, undefined, undefined, undefined, undefined, undefined, undefined, new TestCapabilitiesService(), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, getBasicExtensionService());
assert.equal(profile.password, '', 'Profile should not have password initially');
assert.equal(profile.options['password'], '', 'Profile options should not have password initially');
let credentials = await connectionManagementService.getConnectionCredentials(profile.id);
assert.equal(credentials['password'], test_password);
});
test('getConnectionCredentials returns the credentials dictionary for a saved connection profile', async () => {
const test_password = 'test_password';
const profile = createConnectionProfile('test_id', '');
const connectionStoreMock = TypeMoq.Mock.ofType(ConnectionStore, TypeMoq.MockBehavior.Loose, new TestStorageService());
const group1 = createConnectionGroup('group1');
group1.connections = [profile];
connectionStoreMock.setup(x => x.getRecentlyUsedConnections(undefined)).returns(() => {
return [];
});
connectionStoreMock.setup(x => x.getConnectionProfileGroups(TypeMoq.It.isAny(), undefined)).returns(() => {
return [group1];
});
connectionStoreMock.setup(x => x.addSavedPassword(TypeMoq.It.isAny())).returns(async () => {
profile.password = test_password;
return { profile: profile, savedCred: true };
});
const connectionManagementService = new ConnectionManagementService(connectionStoreMock.object, undefined, undefined, undefined, undefined, undefined, undefined, new TestCapabilitiesService(), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, getBasicExtensionService());
assert.equal(profile.password, '', 'Profile should not have password initially');
assert.equal(profile.options['password'], '', 'Profile options should not have password initially');
let credentials = await connectionManagementService.getConnectionCredentials(profile.id);
assert.equal(credentials['password'], test_password);
});
test('getConnectionUriFromId returns a URI of an active connection with the given id', () => {
let profile = assign({}, connectionProfile);
profile.options = { password: profile.password };
@@ -1428,8 +1469,7 @@ test('clearRecentConnection and ConnectionsList should call connectionStore func
assert(called);
});
function createConnectionProfile(id: string): ConnectionProfile {
function createConnectionProfile(id: string, password?: string): ConnectionProfile {
const capabilitiesService = new TestCapabilitiesService();
return new ConnectionProfile(capabilitiesService, {
connectionName: 'newName',
@@ -1438,7 +1478,7 @@ function createConnectionProfile(id: string): ConnectionProfile {
serverName: 'testServerName',
databaseName: 'testDatabaseName',
authenticationType: Constants.integrated,
password: 'test',
password: password ?? 'test',
userName: 'testUsername',
groupId: undefined,
providerName: Constants.mssqlProviderName,