Skip to content

Commit

Permalink
Fix unit test failures (microsoft#1767)
Browse files Browse the repository at this point in the history
* fixed SQL Login connection issue

* fixed unit tests
  • Loading branch information
cssuh authored Oct 13, 2020
1 parent 7941161 commit 3ca3663
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 17 deletions.
3 changes: 3 additions & 0 deletions localization/xliff/enu/constants/localizedConstants.enu.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
<trans-unit id="azureAddAccount">
<source xml:lang="en">Add an Account...</source>
</trans-unit>
<trans-unit id="aad">
<source xml:lang="en">AAD</source>
</trans-unit>
<trans-unit id="usernamePrompt">
<source xml:lang="en">User name</source>
</trans-unit>
Expand Down
2 changes: 1 addition & 1 deletion src/models/connectionProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class ConnectionProfile extends ConnectionCredentials implements IConnect
},
{
type: QuestionTypes.expand,
name: 'AAD',
name: LocalizedConstants.aad,
message: LocalizedConstants.azureChooseAccount,
choices: azureAccountChoices,
shouldPrompt: (answers) => profile.isAzureActiveDirectory()
Expand Down
5 changes: 2 additions & 3 deletions src/models/connectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,10 @@ export class ConnectionStore {
// Add the profile to the saved list, taking care to clear out the password field if necessary
let savedProfile: IConnectionProfile;
if (forceWritePlaintextPassword) {
savedProfile = Object.assign({}, profile);
savedProfile = Object.assign({}, profile, { azureAccountToken: ''});
} else {
savedProfile = Object.assign({}, profile, { password: '' });
savedProfile = Object.assign({}, profile, { password: '', azureAccountToken: '' });
}
savedProfile = Object.assign({}, profile, { azureAccountToken: ''});

self._connectionConfig.addConnection(savedProfile)
.then(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/models/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ export interface IAzureSession {
}

export interface IAzureResourceFilter {
readonly session: IAzureSession;
readonly sessions: IAzureSession[];
readonly subscription: ISubscription;
}

Expand Down
26 changes: 18 additions & 8 deletions test/connectionProfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* ------------------------------------------------------------------------------------------ */

'use strict';
import vscode = require('vscode');
import * as TypeMoq from 'typemoq';
import { IConnectionCredentials, IConnectionProfile, AuthenticationTypes } from '../src/models/interfaces';
import { ConnectionCredentials } from '../src/models/connectionCredentials';
Expand All @@ -14,9 +15,10 @@ import { ConnectionUI } from '../src/views/connectionUI';
import { ConnectionStore } from '../src/models/connectionStore';
import ConnectionManager from '../src/controllers/connectionManager';
import VscodeWrapper from '../src/controllers/vscodeWrapper';

import Constants = require('../src/constants/constants');
import LocalizedConstants = require('../src/constants/localizedConstants');
import assert = require('assert');
import { AccountStore } from '../src/azure/accountStore';

function createTestCredentials(): IConnectionCredentials {
const creds: IConnectionCredentials = {
Expand Down Expand Up @@ -57,9 +59,16 @@ function createTestCredentials(): IConnectionCredentials {

suite('Connection Profile tests', () => {
let authTypeQuestionIndex = 2;
let mockAccountStore: AccountStore;
let mockContext: TypeMoq.IMock<vscode.ExtensionContext>;
let globalstate: TypeMoq.IMock<vscode.Memento>;

setup(() => {
// No setup currently needed

globalstate = TypeMoq.Mock.ofType<vscode.Memento>();
mockContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
mockContext.setup(c => c.globalState).returns(() => globalstate.object);
mockAccountStore = new AccountStore(mockContext.object);
});

test('CreateProfile should ask questions in correct order', async () => {
Expand All @@ -80,7 +89,7 @@ suite('Connection Profile tests', () => {
return Promise.resolve(answers);
});

await ConnectionProfile.createProfile(prompter.object, undefined, undefined)
await ConnectionProfile.createProfile(prompter.object, undefined, undefined, mockAccountStore)
.then(profile => profileReturned = profile);

// Then expect the following flow:
Expand All @@ -91,6 +100,7 @@ suite('Connection Profile tests', () => {
LocalizedConstants.usernamePrompt, // UserName
LocalizedConstants.passwordPrompt, // Password
LocalizedConstants.msgSavePassword, // Save Password
LocalizedConstants.aad, // Choose AAD Account
LocalizedConstants.profileNamePrompt // Profile Name
];

Expand Down Expand Up @@ -120,7 +130,7 @@ suite('Connection Profile tests', () => {
return answers;
});

await ConnectionProfile.createProfile(prompter.object, undefined, undefined);
await ConnectionProfile.createProfile(prompter.object, undefined, undefined, mockAccountStore);

// Then expect SqlAuth to be the only default type
let authChoices = <INameValueChoice[]>profileQuestions[authTypeQuestionIndex].choices;
Expand All @@ -143,7 +153,7 @@ suite('Connection Profile tests', () => {
});

// When createProfile is called on an OS
await ConnectionProfile.createProfile(prompter.object, undefined, undefined);
await ConnectionProfile.createProfile(prompter.object, undefined, undefined, mockAccountStore);

// Then integrated auth should/should not be supported
// TODO if possible the test should mock out the OS dependency but it's not clear
Expand Down Expand Up @@ -232,7 +242,7 @@ suite('Connection Profile tests', () => {
vscodeWrapperMock.setup(x => x.activeTextEditorUri).returns(() => 'test.sql');

let connectionUI = new ConnectionUI(connectionManagerMock.object, undefined,
connectionStoreMock.object, undefined, prompter.object, vscodeWrapperMock.object);
connectionStoreMock.object, mockAccountStore, prompter.object, vscodeWrapperMock.object);

// create a new connection profile
connectionUI.createAndSaveProfile().then(profile => {
Expand Down Expand Up @@ -278,7 +288,7 @@ suite('Connection Profile tests', () => {
vscodeWrapperMock.setup(x => x.showErrorMessage(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve(undefined));

let connectionUI = new ConnectionUI(connectionManagerMock.object, undefined,
connectionStoreMock.object, undefined, prompter.object, vscodeWrapperMock.object);
connectionStoreMock.object, mockAccountStore, prompter.object, vscodeWrapperMock.object);

// create a new connection profile
connectionUI.createAndSaveProfile().then(profile => {
Expand Down Expand Up @@ -311,7 +321,7 @@ suite('Connection Profile tests', () => {
});

// Verify that a profile was created
ConnectionProfile.createProfile(prompter.object, undefined, undefined).then( profile => {
ConnectionProfile.createProfile(prompter.object, undefined, undefined, mockAccountStore).then( profile => {
assert.equal(Boolean(profile), true);
done();
});
Expand Down
2 changes: 1 addition & 1 deletion test/connectionStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ suite('ConnectionStore tests', () => {
// When SaveProfile is called with savePassword true
let connectionProfile: interfaces.IConnectionProfile = Object.assign(new ConnectionProfile(), defaultNamedProfile, { savePassword: true });

connectionStore.saveProfile(connectionProfile)
connectionStore.saveProfile(connectionProfile, false)
.then(savedProfile => {
// Then expect password saved in the credential store
assert.ok(credsToSave !== undefined && credsToSave.length === 1);
Expand Down
10 changes: 9 additions & 1 deletion test/connectionUI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { IConnectionProfile, IConnectionCredentials, IConnectionCredentialsQuick
import { ConnectionProfile } from '../src/models/connectionProfile';
import { ConnectionCredentials } from '../src/models/connectionCredentials';
import LocalizedConstants = require('../src/constants/localizedConstants');
import { AccountStore } from '../src/azure/accountStore';

suite('Connection UI tests', () => {

Expand All @@ -26,6 +27,9 @@ suite('Connection UI tests', () => {
let prompter: TypeMoq.IMock<IPrompter>;
let connectionStore: TypeMoq.IMock<ConnectionStore>;
let connectionManager: TypeMoq.IMock<ConnectionManager>;
let mockAccountStore: AccountStore;
let mockContext: TypeMoq.IMock<vscode.ExtensionContext>;
let globalstate: TypeMoq.IMock<vscode.Memento>;

setup(() => {
vscodeWrapper = TypeMoq.Mock.ofType(VscodeWrapper, TypeMoq.MockBehavior.Loose);
Expand All @@ -40,8 +44,12 @@ suite('Connection UI tests', () => {
connectionStore = TypeMoq.Mock.ofType(ConnectionStore, TypeMoq.MockBehavior.Loose);
connectionStore.setup(c => c.getPickListItems()).returns(() => TypeMoq.It.isAny());
connectionManager = TypeMoq.Mock.ofType(ConnectionManager, TypeMoq.MockBehavior.Loose);
globalstate = TypeMoq.Mock.ofType<vscode.Memento>();
mockContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
mockContext.setup(c => c.globalState).returns(() => globalstate.object);
mockAccountStore = new AccountStore(mockContext.object);
connectionUI = new ConnectionUI(connectionManager.object, undefined,
connectionStore.object, undefined, prompter.object, vscodeWrapper.object);
connectionStore.object, mockAccountStore, prompter.object, vscodeWrapper.object);
});

test('showConnectionErrors should show errors in the output channel', () => {
Expand Down
6 changes: 4 additions & 2 deletions test/firewallService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ suite('Firewall Service Tests', () => {
tenantId: 'test',
credentials: undefined
};
let mockSessions: IAzureSession[] = [mockSession];
let mockFilter: IAzureResourceFilter = {
session: mockSession,
sessions: mockSessions,
subscription: undefined
};
let mockExtension: vscode.Extension<any> = {
Expand All @@ -53,7 +54,8 @@ suite('Firewall Service Tests', () => {
activate: undefined,
extensionUri: undefined,
exports: {
filters: [mockFilter]
sessions: mockSessions,
filters: mockFilter
}
};
vscodeWrapper.setup(v => v.azureAccountExtension).returns(() => mockExtension);
Expand Down

0 comments on commit 3ca3663

Please sign in to comment.