Skip to content

Commit

Permalink
Merge pull request #11138 from OfficeDev/nintan/fix-azure-sign-out
Browse files Browse the repository at this point in the history
fix: azure sign out from command palette
  • Loading branch information
eriolchan authored Mar 20, 2024
2 parents 3c48d8d + 2187b21 commit e3e9eda
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
8 changes: 4 additions & 4 deletions packages/vscode-extension/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,7 @@ export async function cmpAccountsHandler(args: any[]) {
quickPick.onDidChangeSelection((selection) => {
if (selection[0]) {
(selection[0] as VscQuickPickItem).function().catch(console.error);
quickPick.hide();
}
});
quickPick.onDidHide(() => quickPick.dispose());
Expand Down Expand Up @@ -2265,10 +2266,9 @@ export async function signOutAzure(isFromTreeView: boolean) {
: TelemetryTriggerFrom.CommandPalette,
[TelemetryProperty.AccountType]: AccountType.Azure,
});
const result = await AzureAccountManager.signout();
if (result) {
accountTreeViewProviderInstance.azureAccountNode.setSignedOut();
}
await vscode.window.showInformationMessage(
localize("teamstoolkit.commands.azureAccount.signOutHelp")
);
}

export async function signOutM365(isFromTreeView: boolean) {
Expand Down
36 changes: 24 additions & 12 deletions packages/vscode-extension/test/extension/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import commandController from "../../src/commandController";
import { AzureAccountManager } from "../../src/commonlib/azureLogin";
import { signedIn, signedOut } from "../../src/commonlib/common/constant";
import { VsCodeLogProvider } from "../../src/commonlib/log";
import M365TokenInstance from "../../src/commonlib/m365Login";
import M365TokenInstance, { M365Login } from "../../src/commonlib/m365Login";
import { DeveloperPortalHomeLink, GlobalKey } from "../../src/constants";
import { PanelType } from "../../src/controls/PanelType";
import { WebviewPanel } from "../../src/controls/webviewPanel";
Expand Down Expand Up @@ -1070,12 +1070,14 @@ describe("handlers", () => {

it("signOutAzure", async () => {
Object.setPrototypeOf(AzureAccountManager, sandbox.stub());
const signOut = sandbox.stub(AzureAccountManager.getInstance(), "signout");
const showMessageStub = sandbox
.stub(vscode.window, "showInformationMessage")
.resolves(undefined);
const sendTelemetryEvent = sandbox.stub(ExtTelemetry, "sendTelemetryEvent");

await handlers.signOutAzure(false);

sandbox.assert.calledOnce(signOut);
sandbox.assert.calledOnce(showMessageStub);
});

describe("decryptSecret", function () {
Expand Down Expand Up @@ -2227,17 +2229,23 @@ describe("handlers", () => {
});

it("cmpAccountsHandler", async () => {
const AzureSignOutStub = sandbox.stub(AzureAccountManager.prototype, "signout");
const showMessageStub = sandbox
.stub(vscode.window, "showInformationMessage")
.resolves(undefined);
const M365SignOutStub = sandbox.stub(M365TokenInstance, "signout");
sandbox
.stub(M365TokenInstance, "getStatus")
.resolves(ok({ status: "SignedIn", accountInfo: { upn: "test.email.com" } }));
sandbox
.stub(AzureAccountManager.prototype, "getStatus")
.resolves({ status: "SignedIn", accountInfo: { upn: "test.email.com" } });
let changeSelectionCallback: (e: readonly vscode.QuickPickItem[]) => any = () => {};
const stubQuickPick = {
items: [],
onDidChangeSelection: () => {
onDidChangeSelection: (
_changeSelectionCallback: (e: readonly vscode.QuickPickItem[]) => any
) => {
changeSelectionCallback = _changeSelectionCallback;
return {
dispose: () => {},
};
Expand All @@ -2248,19 +2256,23 @@ describe("handlers", () => {
};
},
show: () => {},
hide: () => {},
onDidAccept: () => {},
};
const hideStub = sandbox.stub(stubQuickPick, "hide");
sandbox.stub(vscode.window, "createQuickPick").returns(stubQuickPick as any);
sandbox.stub(extension.VS_CODE_UI, "selectOption").resolves(ok({ result: "unknown" } as any));

await handlers.cmpAccountsHandler([]);
changeSelectionCallback([stubQuickPick.items[1]]);

for (const i of stubQuickPick.items) {
await (i as any).function();
}

chai.assert.isTrue(AzureSignOutStub.calledOnce);
chai.assert.isTrue(showMessageStub.calledTwice);
chai.assert.isTrue(M365SignOutStub.calledOnce);
chai.assert.isTrue(hideStub.calledOnce);
});

it("updatePreviewManifest", async () => {
Expand Down Expand Up @@ -2634,29 +2646,29 @@ describe("autoOpenProjectHandler", () => {
};
});
sandbox.stub(vscode.extensions, "getExtension");
const signoutStub = sandbox.stub(AzureAccountManager.prototype, "signout");
const showMessageStub = sandbox
.stub(vscode.window, "showInformationMessage")
.resolves(undefined);

await handlers.registerAccountMenuCommands({
subscriptions: [],
} as unknown as vscode.ExtensionContext);

chai.assert.isTrue(signoutStub.called);
chai.assert.isTrue(showMessageStub.called);
});

it("registerAccountMenuCommands() - error", async () => {
sandbox.stub(ExtTelemetry, "sendTelemetryEvent");
sandbox
.stub(vscode.commands, "registerCommand")
.callsFake((command: string, callback: (...args: any[]) => any) => {
callback({ contextValue: "signedinAzure" }).then(() => {});
callback({ contextValue: "signedinM365" }).then(() => {});
return {
dispose: () => {},
};
});
sandbox.stub(vscode.extensions, "getExtension");
const signoutStub = sandbox
.stub(AzureAccountManager.prototype, "signout")
.throws(new UserCancelError());
const signoutStub = sandbox.stub(M365Login.prototype, "signout").throws(new UserCancelError());

await handlers.registerAccountMenuCommands({
subscriptions: [],
Expand Down

0 comments on commit e3e9eda

Please sign in to comment.