Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions extensions/mssql/src/controllers/connectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1141,12 +1141,16 @@ export default class ConnectionManager {
if (ConnectionCredentials.isPasswordBasedCredential(connectionCreds)) {
// show password prompt if SQL Login and password isn't saved
let password = connectionCreds.password;
if (Utils.isEmpty(password)) {
if (
Utils.isEmpty(password) &&
!(connectionCreds as IConnectionProfile).emptyPasswordInput
) {
password = await this.connectionStore.lookupPassword(connectionCreds);
if (!password) {
password = await this.connectionUI.promptForPassword();
if (!password) {
return false;
// If user provided empty password, set the flag to avoid re-prompting
if (password === undefined || password === "") {
(connectionCreds as IConnectionProfile).emptyPasswordInput = true;
}
}

Expand Down Expand Up @@ -1406,6 +1410,7 @@ export default class ConnectionManager {
// Handle password-based credentials
const passwordResult = await this.handlePasswordBasedCredentials(credentials);
if (!passwordResult) {
// User cancelled the password prompt
const passwordError = new Error(LocalizedConstants.cannotConnect);
telemetryActivity?.endFailed(
passwordError,
Expand Down
68 changes: 68 additions & 0 deletions extensions/mssql/test/unit/connectionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,4 +561,72 @@ suite("ConnectionManager Tests", () => {
expect(result.tenantId).to.equal("tenant456");
});
});

suite("handlePasswordBasedCredentials", () => {
let mockConnectionStore: sinon.SinonStubbedInstance<ConnectionStore>;
let mockConnectionUI: sinon.SinonStubbedInstance<ConnectionUI>;
let testConnectionManager: ConnectionManager;

setup(() => {
mockConnectionStore = sandbox.createStubInstance(ConnectionStore);
mockConnectionUI = sandbox.createStubInstance(ConnectionUI);

const mockPrompter = sandbox.createStubInstance(TestPrompter);

testConnectionManager = new ConnectionManager(
mockContext,
mockStatusView,
mockPrompter,
mockLogger,
);

testConnectionManager.connectionStore = mockConnectionStore;
(testConnectionManager as any)._connectionUI = mockConnectionUI;
});

teardown(() => {
sandbox.restore();
});

test("skips lookup and prompt when emptyPasswordInput is already set", async () => {
const creds = {
server: "testServer",
authenticationType: "SqlLogin",
password: "",
emptyPasswordInput: true,
} as unknown as IConnectionProfile;

mockConnectionStore.lookupPassword.resolves("should-not-be-used");
mockConnectionUI.promptForPassword.resolves("should-not-be-used");

const result = await testConnectionManager.handlePasswordBasedCredentials(creds);

expect(result).to.be.true;
expect(mockConnectionStore.lookupPassword).to.not.have.been.called;
expect(mockConnectionUI.promptForPassword).to.not.have.been.called;
expect(creds.password).to.equal("");
});

test("prompts once, allows empty password, and sets flag", async () => {
const creds = {
server: "testServer",
authenticationType: "SqlLogin",
password: "",
azureAccountToken: "token",
emptyPasswordInput: false,
} as unknown as IConnectionProfile;

mockConnectionStore.lookupPassword.resolves(undefined);
mockConnectionUI.promptForPassword.resolves("");

const result = await testConnectionManager.handlePasswordBasedCredentials(creds);

expect(result).to.be.true;
expect(mockConnectionStore.lookupPassword).to.have.been.calledOnce;
expect(mockConnectionUI.promptForPassword).to.have.been.calledOnce;
expect(creds.emptyPasswordInput).to.be.true;
expect(creds.password).to.equal("");
expect(creds.azureAccountToken).to.be.undefined;
});
});
});
Loading