From c2fbbcaedf4c0ec3e2166afdb7f6c3abccc2d480 Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Tue, 6 Jun 2023 15:52:40 -0400 Subject: [PATCH 1/8] fix: tokenType/tokenValue assigned to direct-to-service profiles Signed-off-by: Trae Yelovich --- packages/zowe-explorer/src/Profiles.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/zowe-explorer/src/Profiles.ts b/packages/zowe-explorer/src/Profiles.ts index c6d4a0e9bf..4ca2263088 100644 --- a/packages/zowe-explorer/src/Profiles.ts +++ b/packages/zowe-explorer/src/Profiles.ts @@ -883,8 +883,15 @@ export class Profiles extends ProfilesCache { return; // See https://github.com/zowe/vscode-extension-for-zowe/issues/1827 } - const updSession = promptInfo.profile as zowe.imperative.ISession; - const returnValue = [updSession.user, updSession.password, updSession.base64EncodedAuth]; + const isImperativeProfile = (prof: string | zowe.imperative.IProfileLoaded): prof is zowe.imperative.IProfileLoaded => + typeof prof !== "string"; + + if (isImperativeProfile(profile)) { + if (profile.profile?.tokenValue == null || profile.profile?.tokenType == null) { + promptInfo.profile.tokenType = promptInfo.profile.tokenValue = null; + } + } + const returnValue: string[] = [promptInfo.profile.user, promptInfo.profile.password, promptInfo.profile.base64EncodedAuth]; this.updateProfilesArrays(promptInfo); return returnValue; } @@ -1213,7 +1220,7 @@ export class Profiles extends ProfilesCache { }; await this.updateBaseProfileFileLogin(baseProfile, updBaseProfile); const baseIndex = this.allProfiles.findIndex((profile) => profile.name === baseProfile.name); - this.allProfiles[baseIndex] = { ...baseProfile, profile: { ...baseProfile, ...updBaseProfile } }; + this.allProfiles[baseIndex] = { ...baseProfile, profile: { ...baseProfile.profile, ...updBaseProfile } }; node.setProfileToChoice({ ...node.getProfile(), profile: { ...node.getProfile().profile, ...updBaseProfile }, From e9247a5c4d2882879cf95c72262284d86b6d0aec Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Wed, 7 Jun 2023 09:26:42 -0400 Subject: [PATCH 2/8] fix: do not rebuild prof. w/ ProfilesCache if already provided Signed-off-by: Trae Yelovich --- .../src/vscode/ZoweVsCodeExtension.ts | 13 +++++++++---- .../src/vscode/doc/IPromptCredentials.ts | 3 ++- packages/zowe-explorer/src/Profiles.ts | 11 +---------- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts b/packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts index 5436951cfd..20fafecfdd 100644 --- a/packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts +++ b/packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts @@ -80,8 +80,8 @@ export class ZoweVsCodeExtension { * @deprecated */ public static async promptCredentials(options: IPromptCredentialsOptions): Promise { - const loadProfile = await this.profilesCache.getLoadedProfConfig(options.sessionName.trim()); - if (loadProfile == null) { + const loadProfile = options.sessionName ? await this.profilesCache.getLoadedProfConfig(options.sessionName.trim()) : options.profile; + if (typeof loadProfile === "string" || loadProfile == null) { return undefined; } const loadSession = loadProfile.profile as imperative.ISession; @@ -117,10 +117,15 @@ export class ZoweVsCodeExtension { const cache = this.profilesCache; const profInfo = await cache.getProfileInfo(); const setSecure = options.secure ?? profInfo.isSecured(); - const loadProfile = await cache.getLoadedProfConfig(options.sessionName, options.sessionType); - const loadSession = loadProfile.profile as imperative.ISession; + + const loadProfile = typeof options.profile === "string" ? await cache.getLoadedProfConfig(options.profile, undefined) : options.profile; + const loadSession = loadProfile?.profile as imperative.ISession; const creds = await ZoweVsCodeExtension.promptUserPass({ session: loadSession, ...options }); + if (loadProfile == null || loadSession == null) { + return; + } + if (creds && creds.length > 0) { loadProfile.profile.user = loadSession.user = creds[0]; loadProfile.profile.password = loadSession.password = creds[1]; diff --git a/packages/zowe-explorer-api/src/vscode/doc/IPromptCredentials.ts b/packages/zowe-explorer-api/src/vscode/doc/IPromptCredentials.ts index 4551993a66..ff78243a00 100644 --- a/packages/zowe-explorer-api/src/vscode/doc/IPromptCredentials.ts +++ b/packages/zowe-explorer-api/src/vscode/doc/IPromptCredentials.ts @@ -19,7 +19,8 @@ export interface IPromptCredentialsCommonOptions { } export interface IPromptCredentialsOptions extends IPromptCredentialsCommonOptions { - sessionName: string; + profile?: string | imperative.IProfileLoaded; + sessionName?: string; sessionType?: string; secure?: boolean; } diff --git a/packages/zowe-explorer/src/Profiles.ts b/packages/zowe-explorer/src/Profiles.ts index 4ca2263088..35e9701f0a 100644 --- a/packages/zowe-explorer/src/Profiles.ts +++ b/packages/zowe-explorer/src/Profiles.ts @@ -869,8 +869,7 @@ export class Profiles extends ProfilesCache { const promptInfo = await ZoweVsCodeExtension.updateCredentials( { - sessionName: typeof profile !== "string" ? profile.name : profile, - sessionType: typeof profile !== "string" ? profile.type : undefined, + profile, rePrompt, secure: (await this.getProfileInfo()).isSecured(), userInputBoxOptions, @@ -883,14 +882,6 @@ export class Profiles extends ProfilesCache { return; // See https://github.com/zowe/vscode-extension-for-zowe/issues/1827 } - const isImperativeProfile = (prof: string | zowe.imperative.IProfileLoaded): prof is zowe.imperative.IProfileLoaded => - typeof prof !== "string"; - - if (isImperativeProfile(profile)) { - if (profile.profile?.tokenValue == null || profile.profile?.tokenType == null) { - promptInfo.profile.tokenType = promptInfo.profile.tokenValue = null; - } - } const returnValue: string[] = [promptInfo.profile.user, promptInfo.profile.password, promptInfo.profile.base64EncodedAuth]; this.updateProfilesArrays(promptInfo); return returnValue; From e0bf8ed356be220642b78f3a7dc8ee95d622e6ba Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Wed, 7 Jun 2023 09:33:02 -0400 Subject: [PATCH 3/8] docs: add changelogs Signed-off-by: Trae Yelovich --- packages/zowe-explorer-api/CHANGELOG.md | 3 +++ packages/zowe-explorer/CHANGELOG.md | 1 + 2 files changed, 4 insertions(+) diff --git a/packages/zowe-explorer-api/CHANGELOG.md b/packages/zowe-explorer-api/CHANGELOG.md index b8ec7dee7c..f9b6a56283 100644 --- a/packages/zowe-explorer-api/CHANGELOG.md +++ b/packages/zowe-explorer-api/CHANGELOG.md @@ -10,9 +10,12 @@ All notable changes to the "zowe-explorer-api" extension will be documented in t - Added a new type `DataSetAllocTemplate` that is used for data set creation templates. - Added optional `cancelJob` function to `ZoweExplorerApi.IJes` interface. - Added z/OSMF API implementation for `cancelJob` function. +- Added optional `profile` parameter to `IPromptCredentialsOptions` so developers can choose to skip rebuilding the profile with ProfilesCache. ### Bug fixes +- Fixed issue where profiles with authentication tokens were breaking functionality for direct-to-service profiles after user interaction. [#2111](https://github.com/zowe/vscode-extension-for-zowe/issues/2111) + ## `2.8.1` ### Bug fixes diff --git a/packages/zowe-explorer/CHANGELOG.md b/packages/zowe-explorer/CHANGELOG.md index 6ad6da4162..e18be87c5f 100644 --- a/packages/zowe-explorer/CHANGELOG.md +++ b/packages/zowe-explorer/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to the "vscode-extension-for-zowe" extension will be documen - Fixed issue where user was not able to view job spool file with the same DD name in different steps because of duplicated local file name. [#2279](https://github.com/zowe/vscode-extension-for-zowe/issues/2279) - Fixed issue where user was not able to view job spool file from jobs with duplicated step names because of duplicated local file name. [#2315](https://github.com/zowe/vscode-extension-for-zowe/issues/2315) - Fixed issue with Windows path when uploading file to data set. [#2323](https://github.com/zowe/vscode-extension-for-zowe/issues/2323) +- Fixed issue where profiles with authentication tokens were breaking functionality for direct-to-service profiles after user interaction. [#2111](https://github.com/zowe/vscode-extension-for-zowe/issues/2111) ## `2.8.1` From 4348e47990ce88676cd1ef314e098312a17fb8a7 Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Wed, 7 Jun 2023 09:34:58 -0400 Subject: [PATCH 4/8] fix: move sanity check above ZoweVsCodeExtension.promptUserPass call Signed-off-by: Trae Yelovich --- packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts b/packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts index 20fafecfdd..0aef9429ab 100644 --- a/packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts +++ b/packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts @@ -120,11 +120,11 @@ export class ZoweVsCodeExtension { const loadProfile = typeof options.profile === "string" ? await cache.getLoadedProfConfig(options.profile, undefined) : options.profile; const loadSession = loadProfile?.profile as imperative.ISession; - const creds = await ZoweVsCodeExtension.promptUserPass({ session: loadSession, ...options }); if (loadProfile == null || loadSession == null) { - return; + return undefined; } + const creds = await ZoweVsCodeExtension.promptUserPass({ session: loadSession, ...options }); if (creds && creds.length > 0) { loadProfile.profile.user = loadSession.user = creds[0]; From 0e4521f530f10fd7706c1270ea8412c435d8c8fd Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Wed, 7 Jun 2023 09:45:15 -0400 Subject: [PATCH 5/8] fix: change IPromptCredentialsOptions.profile type to IProfileLoaded Signed-off-by: Trae Yelovich --- .../zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts | 8 ++++++-- .../src/vscode/doc/IPromptCredentials.ts | 2 +- packages/zowe-explorer/src/Profiles.ts | 3 ++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts b/packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts index 0aef9429ab..a379383037 100644 --- a/packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts +++ b/packages/zowe-explorer-api/src/vscode/ZoweVsCodeExtension.ts @@ -81,7 +81,7 @@ export class ZoweVsCodeExtension { */ public static async promptCredentials(options: IPromptCredentialsOptions): Promise { const loadProfile = options.sessionName ? await this.profilesCache.getLoadedProfConfig(options.sessionName.trim()) : options.profile; - if (typeof loadProfile === "string" || loadProfile == null) { + if (loadProfile == null) { return undefined; } const loadSession = loadProfile.profile as imperative.ISession; @@ -118,7 +118,11 @@ export class ZoweVsCodeExtension { const profInfo = await cache.getProfileInfo(); const setSecure = options.secure ?? profInfo.isSecured(); - const loadProfile = typeof options.profile === "string" ? await cache.getLoadedProfConfig(options.profile, undefined) : options.profile; + if (options.profile == null && options.sessionName == null) { + return undefined; + } + + const loadProfile = options.sessionName ? await cache.getLoadedProfConfig(options.sessionName) : options.profile; const loadSession = loadProfile?.profile as imperative.ISession; if (loadProfile == null || loadSession == null) { diff --git a/packages/zowe-explorer-api/src/vscode/doc/IPromptCredentials.ts b/packages/zowe-explorer-api/src/vscode/doc/IPromptCredentials.ts index ff78243a00..5c41c72e0f 100644 --- a/packages/zowe-explorer-api/src/vscode/doc/IPromptCredentials.ts +++ b/packages/zowe-explorer-api/src/vscode/doc/IPromptCredentials.ts @@ -19,7 +19,7 @@ export interface IPromptCredentialsCommonOptions { } export interface IPromptCredentialsOptions extends IPromptCredentialsCommonOptions { - profile?: string | imperative.IProfileLoaded; + profile?: imperative.IProfileLoaded; sessionName?: string; sessionType?: string; secure?: boolean; diff --git a/packages/zowe-explorer/src/Profiles.ts b/packages/zowe-explorer/src/Profiles.ts index 35e9701f0a..5c3db45f5d 100644 --- a/packages/zowe-explorer/src/Profiles.ts +++ b/packages/zowe-explorer/src/Profiles.ts @@ -869,7 +869,8 @@ export class Profiles extends ProfilesCache { const promptInfo = await ZoweVsCodeExtension.updateCredentials( { - profile, + profile: typeof profile === "string" ? undefined : profile, + sessionName: typeof profile === "string" ? profile : undefined, rePrompt, secure: (await this.getProfileInfo()).isSecured(), userInputBoxOptions, From 224157fe07fe074ced1217af058fbf0011605150 Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Wed, 7 Jun 2023 15:03:45 -0400 Subject: [PATCH 6/8] test: add 2 unit tests for profile/sessionName args Signed-off-by: Trae Yelovich --- .../vscode/ZoweVsCodeExtension.unit.test.ts | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/zowe-explorer-api/__tests__/__unit__/vscode/ZoweVsCodeExtension.unit.test.ts b/packages/zowe-explorer-api/__tests__/__unit__/vscode/ZoweVsCodeExtension.unit.test.ts index ef83041647..e6ebcad4eb 100644 --- a/packages/zowe-explorer-api/__tests__/__unit__/vscode/ZoweVsCodeExtension.unit.test.ts +++ b/packages/zowe-explorer-api/__tests__/__unit__/vscode/ZoweVsCodeExtension.unit.test.ts @@ -14,7 +14,8 @@ import { Gui } from "../../../src/globals/Gui"; import { MessageSeverity, IZoweLogger } from "../../../src/logger/IZoweLogger"; import { IProfileLoaded } from "@zowe/imperative"; import { IPromptCredentialsOptions, ZoweVsCodeExtension } from "../../../src/vscode"; -import { ZoweExplorerApi } from "../../../src"; +import { ProfilesCache, ZoweExplorerApi } from "../../../src"; +import { imperative } from "@zowe/cli"; describe("ZoweVsCodeExtension", () => { const fakeVsce = { @@ -328,5 +329,50 @@ describe("ZoweVsCodeExtension", () => { expect(showInputBoxSpy).toHaveBeenCalledTimes(2); expect(mockUpdateProperty).toHaveBeenCalledTimes(0); }); + + it("should do nothing if profile and sessionName args are not provided", async () => { + const mockUpdateProperty = jest.fn(); + jest.spyOn(ZoweVsCodeExtension as any, "profilesCache", "get").mockReturnValue({ + getLoadedProfConfig: jest.fn().mockReturnValue({ + profile: {}, + }), + getProfileInfo: jest.fn().mockReturnValue({ + isSecured: jest.fn().mockReturnValue(true), + updateProperty: mockUpdateProperty, + }), + refresh: jest.fn(), + }); + const showInputBoxSpy = jest.spyOn(Gui, "showInputBox").mockResolvedValueOnce("fakeUser").mockResolvedValueOnce(undefined); + const profileLoaded = await ZoweVsCodeExtension.updateCredentials({}, undefined as unknown as ZoweExplorerApi.IApiRegisterClient); + expect(profileLoaded).toBeUndefined(); + expect(showInputBoxSpy).not.toHaveBeenCalled(); + expect(mockUpdateProperty).not.toHaveBeenCalled(); + }); + + it("should not call ProfilesCache.getLoadedProfConfig if profile object is provided", async () => { + const mockUpdateProperty = jest.fn(); + jest.spyOn(ZoweVsCodeExtension as any, "profilesCache", "get").mockReturnValue({ + getLoadedProfConfig: jest.fn().mockReturnValue({ + profile: { + name: "someExampleProfile", + profile: { + user: "testUser", + password: "testPassword", + } as imperative.IProfile, + } as imperative.IProfileLoaded, + }), + getProfileInfo: jest.fn().mockReturnValue({ + isSecured: jest.fn().mockReturnValue(true), + updateProperty: mockUpdateProperty, + }), + refresh: jest.fn(), + }); + const getLoadedProfConfigSpy = jest.spyOn(ProfilesCache.prototype, "getLoadedProfConfig"); + const showInputBoxSpy = jest.spyOn(Gui, "showInputBox").mockResolvedValueOnce("fakeUser").mockResolvedValueOnce(undefined); + const profileLoaded = await ZoweVsCodeExtension.updateCredentials({}, undefined as unknown as ZoweExplorerApi.IApiRegisterClient); + expect(profileLoaded).toBeUndefined(); + expect(getLoadedProfConfigSpy).not.toHaveBeenCalled(); + expect(showInputBoxSpy).not.toHaveBeenCalled(); + }); }); }); From 42cdff109c77552349c92aa72b0964c23d1c4652 Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Thu, 8 Jun 2023 10:29:28 -0400 Subject: [PATCH 7/8] fix: only call `refreshAll` once in `ssoLogin, ssoLogout` Signed-off-by: Trae Yelovich --- .../__tests__/__unit__/globals.unit.test.ts | 11 ++++++++++ .../src/abstract/ZoweTreeProvider.ts | 20 +++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/globals.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/globals.unit.test.ts index 172be8208e..fe2e6372d4 100644 --- a/packages/zowe-explorer/__tests__/__unit__/globals.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/globals.unit.test.ts @@ -1,3 +1,14 @@ +/** + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright Contributors to the Zowe Project. + * + */ + import * as globals from "../../src/globals"; import { ZoweLogger } from "../../src/utils/LoggerUtils"; import * as SettingsConfig from "../../src/utils/SettingsConfig"; diff --git a/packages/zowe-explorer/src/abstract/ZoweTreeProvider.ts b/packages/zowe-explorer/src/abstract/ZoweTreeProvider.ts index 2ab82bb8d1..2843b5766a 100644 --- a/packages/zowe-explorer/src/abstract/ZoweTreeProvider.ts +++ b/packages/zowe-explorer/src/abstract/ZoweTreeProvider.ts @@ -247,17 +247,25 @@ export class ZoweTreeProvider { public async ssoLogin(node: IZoweTreeNode): Promise { ZoweLogger.trace("ZoweTreeProvider.ssoLogin called."); await Profiles.getInstance().ssoLogin(node); - await vscode.commands.executeCommand("zowe.ds.refreshAll"); - await vscode.commands.executeCommand("zowe.uss.refreshAll"); - await vscode.commands.executeCommand("zowe.jobs.refreshAllJobs"); + if (contextually.isDsSession(node)) { + await vscode.commands.executeCommand("zowe.ds.refreshAll"); + } else if (contextually.isUssSession(node)) { + await vscode.commands.executeCommand("zowe.uss.refreshAll"); + } else { + await vscode.commands.executeCommand("zowe.jobs.refreshAllJobs"); + } } public async ssoLogout(node: IZoweTreeNode): Promise { ZoweLogger.trace("ZoweTreeProvider.ssoLogout called."); await Profiles.getInstance().ssoLogout(node); - await vscode.commands.executeCommand("zowe.ds.refreshAll"); - await vscode.commands.executeCommand("zowe.uss.refreshAll"); - await vscode.commands.executeCommand("zowe.jobs.refreshAllJobs"); + if (contextually.isDsSession(node)) { + await vscode.commands.executeCommand("zowe.ds.refreshAll"); + } else if (contextually.isUssSession(node)) { + await vscode.commands.executeCommand("zowe.uss.refreshAll"); + } else { + await vscode.commands.executeCommand("zowe.jobs.refreshAllJobs"); + } } public async createZoweSchema(zoweFileProvider: IZoweTree): Promise { From d35d025d89419c998780c5f8d29bd431eecf75e9 Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Thu, 8 Jun 2023 10:56:50 -0400 Subject: [PATCH 8/8] tests: `ZoweTreeProvider.ssoLogin, ZoweTreeProvider.ssoLogout` Signed-off-by: Trae Yelovich --- .../abstract/TreeProvider.unit.test.ts | 107 +++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/abstract/TreeProvider.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/abstract/TreeProvider.unit.test.ts index 8c172cec19..885b767471 100644 --- a/packages/zowe-explorer/__tests__/__unit__/abstract/TreeProvider.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/abstract/TreeProvider.unit.test.ts @@ -24,12 +24,13 @@ import { Profiles } from "../../../src/Profiles"; import { imperative } from "@zowe/cli"; import * as globals from "../../../src/globals"; import { createUSSTree } from "../../../src/uss/USSTree"; -import { createIJobObject } from "../../../__mocks__/mockCreators/jobs"; +import { createIJobObject, createJobSessionNode } from "../../../__mocks__/mockCreators/jobs"; import { Job } from "../../../src/job/ZoweJobNode"; import { createJobsTree } from "../../../src/job/ZosJobsProvider"; import { SettingsConfig } from "../../../src/utils/SettingsConfig"; import { ZoweTreeProvider } from "../../../src/abstract/ZoweTreeProvider"; import { ZoweLogger } from "../../../src/utils/LoggerUtils"; +import { createDatasetSessionNode } from "../../../__mocks__/mockCreators/datasets"; async function createGlobalMocks() { const globalMocks = { @@ -54,6 +55,8 @@ async function createGlobalMocks() { mockGetProfileSetting: jest.fn(), mockProfilesForValidation: jest.fn(), mockProfilesValidationSetting: jest.fn(), + mockSsoLogin: jest.fn(), + mockSsoLogout: jest.fn(), ProgressLocation: jest.fn().mockImplementation(() => { return { Notification: 15, @@ -115,6 +118,8 @@ async function createGlobalMocks() { name: globalMocks.testProfile.name, setting: true, }), + ssoLogin: globalMocks.mockSsoLogin, + ssoLogout: globalMocks.mockSsoLogout, getProfileInfo: () => globalMocks.mockProfileInfo, fetchAllProfiles: jest.fn(() => { return [{ name: "sestest" }, { name: "profile1" }, { name: "profile2" }]; @@ -405,3 +410,103 @@ describe("Tree Provider Unit Tests - function renameNode", () => { spy.mockClear(); }); }); + +describe("Tree Provider Unit Tests - function ssoLogin", () => { + const createBlockMocks = () => { + const executeCommandSpy = jest.spyOn(vscode.commands, "executeCommand"); + return { + executeCommandSpy, + }; + }; + + const blockMocks = createBlockMocks(); + + afterEach(() => { + blockMocks.executeCommandSpy.mockClear(); + }); + + it("should only call zowe.ds.refreshAll for a Data Set session node", async () => { + const globalMocks = await createGlobalMocks(); + const dsNode = createDatasetSessionNode(globalMocks.testSession, globalMocks.testProfile); + + await globalMocks.testTreeProvider.ssoLogin(dsNode); + expect(globalMocks.mockSsoLogin).toHaveBeenCalled(); + expect(blockMocks.executeCommandSpy).toHaveBeenCalledWith("zowe.ds.refreshAll"); + expect(blockMocks.executeCommandSpy).not.toHaveBeenCalledWith("zowe.uss.refreshAll"); + expect(blockMocks.executeCommandSpy).not.toHaveBeenCalledWith("zowe.jobs.refreshAllJobs"); + }); + + it("should only call zowe.uss.refreshAll for a USS session node", async () => { + const globalMocks = await createGlobalMocks(); + const ussNode = createUSSSessionNode(globalMocks.testSession, globalMocks.testProfile); + + await globalMocks.testTreeProvider.ssoLogin(ussNode); + expect(globalMocks.mockSsoLogin).toHaveBeenCalled(); + expect(blockMocks.executeCommandSpy).toHaveBeenCalledWith("zowe.uss.refreshAll"); + expect(blockMocks.executeCommandSpy).not.toHaveBeenCalledWith("zowe.ds.refreshAll"); + expect(blockMocks.executeCommandSpy).not.toHaveBeenCalledWith("zowe.jobs.refreshAllJobs"); + }); + + it("should only call zowe.jobs.refreshAllJobs for a Job session node", async () => { + const globalMocks = await createGlobalMocks(); + const jobNode = createJobSessionNode(globalMocks.testSession, globalMocks.testProfile); + + await globalMocks.testTreeProvider.ssoLogin(jobNode); + expect(globalMocks.mockSsoLogin).toHaveBeenCalled(); + expect(blockMocks.executeCommandSpy).toHaveBeenCalledWith("zowe.jobs.refreshAllJobs"); + expect(blockMocks.executeCommandSpy).not.toHaveBeenCalledWith("zowe.uss.refreshAll"); + expect(blockMocks.executeCommandSpy).not.toHaveBeenCalledWith("zowe.ds.refreshAll"); + }); +}); + +describe("Tree Provider Unit Tests - function ssoLogout", () => { + const createBlockMocks = () => { + const executeCommandSpy = jest.spyOn(vscode.commands, "executeCommand"); + return { + executeCommandSpy, + }; + }; + + const blockMocks = createBlockMocks(); + + afterEach(() => { + blockMocks.executeCommandSpy.mockClear(); + }); + + afterAll(() => { + blockMocks.executeCommandSpy.mockRestore(); + }); + + it("should only call zowe.ds.refreshAll for a Data Set session node", async () => { + const globalMocks = await createGlobalMocks(); + const dsNode = createDatasetSessionNode(globalMocks.testSession, globalMocks.testProfile); + + await globalMocks.testTreeProvider.ssoLogout(dsNode); + expect(globalMocks.mockSsoLogout).toHaveBeenCalled(); + expect(blockMocks.executeCommandSpy).toHaveBeenCalledWith("zowe.ds.refreshAll"); + expect(blockMocks.executeCommandSpy).not.toHaveBeenCalledWith("zowe.uss.refreshAll"); + expect(blockMocks.executeCommandSpy).not.toHaveBeenCalledWith("zowe.jobs.refreshAllJobs"); + }); + + it("should only call zowe.uss.refreshAll for a USS session node", async () => { + const globalMocks = await createGlobalMocks(); + const ussNode = createUSSSessionNode(globalMocks.testSession, globalMocks.testProfile); + + await globalMocks.testTreeProvider.ssoLogout(ussNode); + expect(globalMocks.mockSsoLogout).toHaveBeenCalled(); + expect(blockMocks.executeCommandSpy).toHaveBeenCalledWith("zowe.uss.refreshAll"); + expect(blockMocks.executeCommandSpy).not.toHaveBeenCalledWith("zowe.ds.refreshAll"); + expect(blockMocks.executeCommandSpy).not.toHaveBeenCalledWith("zowe.jobs.refreshAllJobs"); + }); + + it("should only call zowe.jobs.refreshAllJobs for a Job session node", async () => { + const globalMocks = await createGlobalMocks(); + const jobNode = createJobSessionNode(globalMocks.testSession, globalMocks.testProfile); + + await globalMocks.testTreeProvider.ssoLogout(jobNode); + expect(globalMocks.mockSsoLogout).toHaveBeenCalled(); + expect(blockMocks.executeCommandSpy).toHaveBeenCalledWith("zowe.jobs.refreshAllJobs"); + expect(blockMocks.executeCommandSpy).not.toHaveBeenCalledWith("zowe.uss.refreshAll"); + expect(blockMocks.executeCommandSpy).not.toHaveBeenCalledWith("zowe.ds.refreshAll"); + }); +});