From bbe30e55b29409f3ade06a05b0d65615b168b800 Mon Sep 17 00:00:00 2001 From: Likhitha Nimma Date: Thu, 14 Dec 2023 18:52:29 +0530 Subject: [PATCH 1/9] Handling-multiple-ssh-profiles Signed-off-by: Likhitha Nimma --- .../command/unixCommandHandler.unit.test.ts | 174 +++++++++++++++++- .../unixCommandHandlerTheia.unit.test.ts | 14 ++ .../src/command/UnixCommandHandler.i18n.json | 3 +- .../src/command/UnixCommandHandler.ts | 135 ++++++++++---- 4 files changed, 291 insertions(+), 35 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts index ee429ae92b..62588c6245 100644 --- a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts @@ -25,6 +25,7 @@ import { SshSession } from "@zowe/zos-uss-for-zowe-sdk"; import { ZoweLocalStorage } from "../../../src/utils/ZoweLocalStorage"; import { ProfileManagement } from "../../../src/utils/ProfileManagement"; import { profile } from "console"; +import { unix } from "dayjs"; describe("UnixCommand Actions Unit Testing", () => { const showQuickPick = jest.fn(); @@ -128,6 +129,7 @@ describe("UnixCommand Actions Unit Testing", () => { Object.defineProperty(vscode.window, "createQuickPick", { value: createQuickPick }); Object.defineProperty(vscode.window, "createOutputChannel", { value: createOutputChannel }); Object.defineProperty(vscode.workspace, "getConfiguration", { value: getConfiguration }); + Object.defineProperty(imperative.ProfileInfo, "profAttrsToProfLoaded", { value: () => ({ profile: {} }) }); Object.defineProperty(imperative.ConnectionPropsForSessCfg, "addPropsOrPrompt", { value: jest.fn(() => { return { privateKey: undefined, keyPassphrase: undefined, handshakeTimeout: undefined, type: "basic", port: 22 }; @@ -165,6 +167,25 @@ describe("UnixCommand Actions Unit Testing", () => { return { privateKey: undefined, keyPassphrase: undefined, handshakeTimeout: undefined }; }); + // Object.defineProperty(profileLoader.Profiles, "getInstance", { + // value: jest.fn(() => ({ + // getCliProfileManager: () => null, + // getProfileInfo: jest.fn().mockReturnValue({ + // usingTeamConfig: true, + // getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + // mergeArgsForProfile: jest.fn().mockReturnValue({ + // knownArgs: [ + // { argName: "port", argValue: "TEST", secure: false }, + // { argName: "host", argValue: "TEST", secure: false }, + // { argName: "user", argValue: "TEST", secure: true }, + // { argName: "password", argValue: "TEST", secure: true }, + // ], + // }), + // loadSecureArg: jest.fn().mockReturnValue("fake"), + // } as any), + // })), + // }); + it("test the issueUnixCommand function", async () => { Object.defineProperty(profileLoader.Profiles, "getInstance", { value: jest.fn(() => { @@ -179,6 +200,19 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, + getProfileInfo: jest.fn().mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", argValue: "TEST", secure: false }, + { argName: "password", argValue: "TEST", secure: false }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue("fake"), + } as any), }; }), }); @@ -232,6 +266,19 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, + getProfileInfo: jest.fn().mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", argValue: "TEST", secure: false }, + { argName: "password", argValue: "TEST", secure: false }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue("fake"), + } as any), }; }), }); @@ -284,6 +331,19 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, + getProfileInfo: jest.fn().mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", argValue: "TEST", secure: false }, + { argName: "password", argValue: "TEST", secure: false }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue("fake"), + } as any), }; }), }); @@ -329,6 +389,19 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, + getProfileInfo: jest.fn().mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", argValue: "TEST", secure: false }, + { argName: "password", argValue: "TEST", secure: false }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue("fake"), + } as any), }; }), }); @@ -371,6 +444,19 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, + getProfileInfo: jest.fn().mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", argValue: "TEST", secure: false }, + { argName: "password", argValue: "TEST", secure: false }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue("fake"), + } as any), }; }), }); @@ -397,6 +483,19 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, + getProfileInfo: jest.fn().mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", argValue: "TEST", secure: false }, + { argName: "password", argValue: "TEST", secure: false }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue("fake"), + } as any), }; }), }); @@ -422,6 +521,19 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, + getProfileInfo: jest.fn().mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", argValue: "TEST", secure: false }, + { argName: "password", argValue: "TEST", secure: false }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue("fake"), + } as any), }; }), }); @@ -465,6 +577,19 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), validProfile: ValidProfileEnum.VALID, getDefaultProfile: mockdefaultProfile, + getProfileInfo: jest.fn().mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", argValue: "TEST", secure: false }, + { argName: "password", argValue: "TEST", secure: false }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue("fake"), + } as any), }; }), }); @@ -521,6 +646,19 @@ describe("UnixCommand Actions Unit Testing", () => { }), validProfile: ValidProfileEnum.INVALID, getDefaultProfile: mockdefaultProfile, + getProfileInfo: jest.fn().mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", argValue: "TEST", secure: false }, + { argName: "password", argValue: "TEST", secure: false }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue("fake"), + } as any), }; }), }); @@ -589,6 +727,19 @@ describe("UnixCommand Actions Unit Testing", () => { checkCurrentProfile: jest.fn(), zosmfProfile: mockLoadNamedProfile, getDefaultProfile: mockdefaultProfile, + getProfileInfo: jest.fn().mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", argValue: "TEST", secure: false }, + { argName: "password", argValue: "TEST", secure: false }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue("fake"), + } as any), }; }), }); @@ -609,12 +760,13 @@ describe("UnixCommand Actions Unit Testing", () => { }); it("ssh profile not found", async () => { - mockdefaultProfile.mockReset(); - mockdefaultProfile.mockReturnValueOnce(undefined); Object.defineProperty(profileLoader.Profiles, "getInstance", { value: jest.fn(() => { return { - getDefaultProfile: mockdefaultProfile, + getProfileInfo: jest.fn().mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(undefined), + } as any), }; }), }); @@ -622,4 +774,20 @@ describe("UnixCommand Actions Unit Testing", () => { expect(showErrorMessage.mock.calls.length).toBe(1); expect(showErrorMessage.mock.calls[0][0]).toEqual("No SSH profile found. Please create an SSH profile before issuing Unix commands."); }); + + it("tests the selectSshProfile function", async () => { + showQuickPick.mockReturnValueOnce("test1" as any); + await expect( + (unixActions as any).selectSshProfile([ + { + name: "test1", + }, + { + name: "test2", + }, + ]) + ).resolves.toEqual({ + name: "test1", + }); + }); }); diff --git a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandlerTheia.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandlerTheia.unit.test.ts index 728cebd7a6..65a3ad811e 100644 --- a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandlerTheia.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandlerTheia.unit.test.ts @@ -106,6 +106,7 @@ describe("unixCommandActions unit testing", () => { return { privateKey: undefined, keyPassphrase: undefined, handshakeTimeout: undefined, type: "basic", port: 22 }; }), }); + Object.defineProperty(imperative.ProfileInfo, "profAttrsToProfLoaded", { value: () => ({ profile: {} }) }); Object.defineProperty(ProfileManagement, "getRegisteredProfileNameList", { value: jest.fn().mockReturnValue(["firstName", "secondName"]), configurable: true, @@ -147,6 +148,19 @@ describe("unixCommandActions unit testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, + getProfileInfo: jest.fn().mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", argValue: "TEST", secure: true }, + { argName: "password", argValue: "TEST", secure: true }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue("user"), + } as any), }; }), }); diff --git a/packages/zowe-explorer/i18n/sample/src/command/UnixCommandHandler.i18n.json b/packages/zowe-explorer/i18n/sample/src/command/UnixCommandHandler.i18n.json index 5af7c281ef..1b183b03a5 100644 --- a/packages/zowe-explorer/i18n/sample/src/command/UnixCommandHandler.i18n.json +++ b/packages/zowe-explorer/i18n/sample/src/command/UnixCommandHandler.i18n.json @@ -2,12 +2,13 @@ "issueUnixCommand.outputchannel.title": "Zowe Unix Command", "issueUnixCommand.quickPickOption": "Select the Profile to use to submit the Unix command", "issueUnixCommand.cancelled": "Operation Cancelled", - "issueUnixCommand.apiNonExisting": "Not implemented yet for profile of type: ", "unixCommand.HomeDirectory": "Redirecting to Home Directory", "issueUnixCommand.checkProfile": "Profile is invalid", "issueUnixCommand.noProfilesLoaded": "No profiles available", "path.notselected": "Enter a UNIX file filter search to enable Issue Unix Command from the tree view.", "issueUnixCommand.options.nopathentered": "Operation cancelled.", + "issueUnixCommand.apiNonExisting": "Not implemented yet for profile of type: ", + "issueUnixCommand.sshProfile.quickPickOption": "Select the ssh Profile.", "setsshProfile.couldnotfindprofile": "No SSH profile found. Please create an SSH profile before issuing Unix commands.", "issueUnixCommand.command.hostname": "Select a Unix command to run against ", "issueUnixCommand.command.edit": " (An option to edit will follow)", diff --git a/packages/zowe-explorer/src/command/UnixCommandHandler.ts b/packages/zowe-explorer/src/command/UnixCommandHandler.ts index 9d31b91be1..9852d28593 100644 --- a/packages/zowe-explorer/src/command/UnixCommandHandler.ts +++ b/packages/zowe-explorer/src/command/UnixCommandHandler.ts @@ -89,25 +89,31 @@ export class UnixCommandHandler extends ZoweCommandProvider { } } } + let res: boolean = true; if (!session) { + const allProfiles = profiles.allProfiles; + res = this.checkforsshrequired(allProfiles); const profileNamesList = ProfileManagement.getRegisteredProfileNameList(globals.Trees.USS); if (profileNamesList.length) { - const quickPickOptions: vscode.QuickPickOptions = { - placeHolder: localize("issueUnixCommand.quickPickOption", "Select the Profile to use to submit the Unix command"), - ignoreFocusOut: true, - canPickMany: false, - }; - const sesName = await Gui.showQuickPick(profileNamesList, quickPickOptions); - if (sesName === undefined) { - Gui.showMessage(localize("issueUnixCommand.cancelled", "Operation Cancelled")); - return; + if (!res) { + const quickPickOptions: vscode.QuickPickOptions = { + placeHolder: localize("issueUnixCommand.quickPickOption", "Select the Profile to use to submit the Unix command"), + ignoreFocusOut: true, + canPickMany: false, + }; + const sesName = await Gui.showQuickPick(profileNamesList, quickPickOptions); + if (sesName === undefined) { + Gui.showMessage(localize("issueUnixCommand.cancelled", "Operation Cancelled")); + return; + } + profile = allProfiles.find((temprofile) => temprofile.name === sesName); } - const allProfiles = profiles.allProfiles; - profile = allProfiles.find((temprofile) => temprofile.name === sesName); - if (ZoweExplorerApiRegister.getCommandApi(profile).sshProfileRequired) sshRequiredBoolean = true; - if (!sshRequiredBoolean) { - Gui.showMessage(localize("issueUnixCommand.apiNonExisting", "Not implemented yet for profile of type: ") + profile.type); - return; + if (ZoweExplorerApiRegister.getCommandApi(profile).sshProfileRequired) { + sshRequiredBoolean = true; + this.sshSession = await this.setsshSession(); + if (!this.sshSession) { + return; + } } if (cwd == "") { cwd = await vscode.window.showInputBox({ @@ -135,14 +141,6 @@ export class UnixCommandHandler extends ZoweCommandProvider { } else { profile = node.getProfile(); } - if (ZoweExplorerApiRegister.getCommandApi(profile).sshProfileRequired) sshRequiredBoolean = true; - if (sshRequiredBoolean) { - this.sshSession = await this.setsshSession(); - if (!this.sshSession) return; - } else { - Gui.showMessage(localize("issueUnixCommand.apiNonExisting", "Not implemented yet for profile of type: ") + profile.type); - return; - } if (cwd == "" && this.flag) { Gui.errorMessage(localize("path.notselected", "Enter a UNIX file filter search to enable Issue Unix Command from the tree view.")); return; @@ -151,9 +149,19 @@ export class UnixCommandHandler extends ZoweCommandProvider { Gui.showMessage(localize("issueUnixCommand.options.nopathentered", "Operation cancelled.")); return; } + if (ZoweExplorerApiRegister.getCommandApi(profile).sshProfileRequired && sshRequiredBoolean == undefined) { + this.sshSession = await this.setsshSession(); + if (!this.sshSession) { + return; + } + } try { if (Profiles.getInstance().validProfile !== ValidProfileEnum.INVALID) { const commandApi = ZoweExplorerApiRegister.getInstance().getCommandApi(profile); + if (!ZoweExplorerApiRegister.getCommandApi(profile).issueUnixCommand) { + Gui.errorMessage(localize("issueUnixCommand.apiNonExisting", "Not implemented yet for profile of type: ") + profile.type); + return; + } if (commandApi) { let command1: string = command; if (!command) { @@ -175,21 +183,86 @@ export class UnixCommandHandler extends ZoweCommandProvider { } } + public checkforsshrequired(allProfiles: imperative.IProfileLoaded[]): boolean { + try { + allProfiles.forEach((p) => { + // eslint-disable-next-line @typescript-eslint/unbound-method + if (!ZoweExplorerApiRegister.getCommandApi(p).sshProfileRequired) { + return false; + } + }); + } catch (error) { + return false; + } + } + public async setsshSession(): Promise { ZoweLogger.trace("UnixCommandHandler.setsshSession called."); - const sshprofile: imperative.IProfileLoaded = Profiles.getInstance().getDefaultProfile("ssh"); - if (!sshprofile) { + const sshprofile: imperative.IProfileLoaded = await this.getsshProfile(); + if (sshprofile) { + const cmdArgs: imperative.ICommandArguments = this.getCmdArgs(sshprofile?.profile as imperative.IProfileLoaded); + // create the ssh session + const sshSessCfg = SshSession.createSshSessCfgFromArgs(cmdArgs); + imperative.ConnectionPropsForSessCfg.resolveSessCfgProps(sshSessCfg, cmdArgs); + this.sshSession = new SshSession(sshSessCfg); + } + return this.sshSession; + } + + private async selectSshProfile(sshProfiles: imperative.IProfileLoaded[] = []): Promise { + ZoweLogger.trace("UnixCommandHandler.selectSshProfile called."); + let sshProfile: imperative.IProfileLoaded; + if (sshProfiles.length > 1) { + const sshProfileNamesList = sshProfiles.map((temprofile) => { + return temprofile.name; + }); + if (sshProfileNamesList.length) { + const quickPickOptions: vscode.QuickPickOptions = { + placeHolder: localize("issueUnixCommand.sshProfile.quickPickOption", "Select the ssh Profile."), + ignoreFocusOut: true, + canPickMany: false, + }; + const sesName = await Gui.showQuickPick(sshProfileNamesList, quickPickOptions); + if (sesName === undefined) { + Gui.showMessage(localize("issueUnixCommand.cancelled", "Operation Cancelled")); + return; + } + + sshProfile = sshProfiles.filter((temprofile) => temprofile.name === sesName)[0]; + } + } else if (sshProfiles.length > 0) { + sshProfile = sshProfiles[0]; + } + return sshProfile; + } + + private async getsshProfile(): Promise { + ZoweLogger.trace("UnixCommandHandler.getsshParams called."); + const profileInfo = await Profiles.getInstance().getProfileInfo(); + const params = ["port", "host", "user", "password"]; + const profiles = profileInfo.getAllProfiles("ssh"); + if (!profiles) { Gui.errorMessage( localize("setsshProfile.couldnotfindprofile", "No SSH profile found. Please create an SSH profile before issuing Unix commands.") ); return; } - const cmdArgs: imperative.ICommandArguments = this.getCmdArgs(sshprofile?.profile as imperative.IProfileLoaded); - // create the ssh session - const sshSessCfg = SshSession.createSshSessCfgFromArgs(cmdArgs); - const sshSessCfgWithCreds = await imperative.ConnectionPropsForSessCfg.addPropsOrPrompt(sshSessCfg, cmdArgs); - this.sshSession = new SshSession(sshSessCfgWithCreds); - return this.sshSession; + let sshProfile: imperative.IProfileLoaded; + if (profiles.length > 0) { + sshProfile = await this.selectSshProfile(profiles.map((p) => imperative.ProfileInfo.profAttrsToProfLoaded(p))); + if (sshProfile != null) { + const prof = profileInfo.mergeArgsForProfile(sshProfile.profile as imperative.IProfAttrs); + params.forEach((p) => { + const obj = prof.knownArgs.find((a) => a.argName === p); + if (obj.secure === false) { + sshProfile.profile[p] = obj.argValue; + } else { + sshProfile.profile[p] = profileInfo.loadSecureArg(obj); + } + }); + } + } + return sshProfile; } private async getQuickPick(cwd: string): Promise { From be5c0bc21d977b01c2ae8d623523c2d3d974acc0 Mon Sep 17 00:00:00 2001 From: Likhitha Nimma Date: Thu, 14 Dec 2023 21:09:13 +0530 Subject: [PATCH 2/9] Tests Modified Signed-off-by: Likhitha Nimma --- .../command/unixCommandHandler.unit.test.ts | 37 ++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts index 62588c6245..18844cffa2 100644 --- a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts @@ -24,8 +24,6 @@ import { ZoweLogger } from "../../../src/utils/LoggerUtils"; import { SshSession } from "@zowe/zos-uss-for-zowe-sdk"; import { ZoweLocalStorage } from "../../../src/utils/ZoweLocalStorage"; import { ProfileManagement } from "../../../src/utils/ProfileManagement"; -import { profile } from "console"; -import { unix } from "dayjs"; describe("UnixCommand Actions Unit Testing", () => { const showQuickPick = jest.fn(); @@ -167,25 +165,6 @@ describe("UnixCommand Actions Unit Testing", () => { return { privateKey: undefined, keyPassphrase: undefined, handshakeTimeout: undefined }; }); - // Object.defineProperty(profileLoader.Profiles, "getInstance", { - // value: jest.fn(() => ({ - // getCliProfileManager: () => null, - // getProfileInfo: jest.fn().mockReturnValue({ - // usingTeamConfig: true, - // getAllProfiles: jest.fn().mockReturnValue(["dummy"]), - // mergeArgsForProfile: jest.fn().mockReturnValue({ - // knownArgs: [ - // { argName: "port", argValue: "TEST", secure: false }, - // { argName: "host", argValue: "TEST", secure: false }, - // { argName: "user", argValue: "TEST", secure: true }, - // { argName: "password", argValue: "TEST", secure: true }, - // ], - // }), - // loadSecureArg: jest.fn().mockReturnValue("fake"), - // } as any), - // })), - // }); - it("test the issueUnixCommand function", async () => { Object.defineProperty(profileLoader.Profiles, "getInstance", { value: jest.fn(() => { @@ -790,4 +769,20 @@ describe("UnixCommand Actions Unit Testing", () => { name: "test1", }); }); + + it("tests the selectSshProfile function when user escapes", async () => { + showQuickPick.mockReturnValueOnce(undefined); + await expect( + (unixActions as any).selectSshProfile([ + { + name: "test1", + }, + { + name: "test2", + }, + ]) + ).resolves.toBe(undefined); + expect(showInformationMessage.mock.calls[0][0]).toEqual("Operation Cancelled"); + }); + }); From c303af00c30157982cfce0f836e9f8d3c0a28340 Mon Sep 17 00:00:00 2001 From: Likhitha Nimma Date: Fri, 15 Dec 2023 21:21:07 +0530 Subject: [PATCH 3/9] Extenders.md file modified Signed-off-by: Likhitha Nimma --- docs/early-access/v3/Extenders.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/early-access/v3/Extenders.md b/docs/early-access/v3/Extenders.md index 96e3c9c6f3..4ec2a15ee1 100644 --- a/docs/early-access/v3/Extenders.md +++ b/docs/early-access/v3/Extenders.md @@ -14,3 +14,7 @@ - `ZoweVsCodeExtension.showVsCodeMessage` removed in favor of `Gui.showMessage`. - `ZoweVsCodeExtension.inputBox` removed in favor of `Gui.showInputBox`. - `ZoweVsCodeExtension.promptCredentials` removed in favor of `ZoweVsCodeExtension.updateCredentials`. + +## New APIs Added + +- `ICommand.issueUnixCommand` added for issuing Unix Commands From 94a2339f2cbdb33c3844b3a3207991c04edc3c98 Mon Sep 17 00:00:00 2001 From: Billie Simmons <49491949+JillieBeanSim@users.noreply.github.com> Date: Fri, 15 Dec 2023 11:30:13 -0500 Subject: [PATCH 4/9] update Extender.md file Signed-off-by: Billie Simmons <49491949+JillieBeanSim@users.noreply.github.com> --- docs/early-access/v3/Extenders.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/early-access/v3/Extenders.md b/docs/early-access/v3/Extenders.md index 4ec2a15ee1..cf5769f0d2 100644 --- a/docs/early-access/v3/Extenders.md +++ b/docs/early-access/v3/Extenders.md @@ -18,3 +18,4 @@ ## New APIs Added - `ICommand.issueUnixCommand` added for issuing Unix Commands +- Optional `ICommand.sshProfileRequired` API returning a boolean value for extenders that would like to use the ssh profile for issuing UNIX commands via Zowe Explorer. From 925fc8bfeaf4ba8ad8471c871326ffec7c4413c5 Mon Sep 17 00:00:00 2001 From: Likhitha Nimma Date: Mon, 18 Dec 2023 11:57:28 +0530 Subject: [PATCH 5/9] Unit-Tests Modified Signed-off-by: Likhitha Nimma --- .../command/unixCommandHandler.unit.test.ts | 155 +++--------------- .../src/command/UnixCommandHandler.ts | 8 +- 2 files changed, 29 insertions(+), 134 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts index 18844cffa2..2fcde3d43d 100644 --- a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts @@ -36,6 +36,7 @@ describe("UnixCommand Actions Unit Testing", () => { const mockLoadNamedProfile = jest.fn(); const mockdefaultProfile = jest.fn(); const getConfiguration = jest.fn(); + const getProfileInfomock = jest.fn(); const session = new imperative.Session({ user: "fake", @@ -165,6 +166,20 @@ describe("UnixCommand Actions Unit Testing", () => { return { privateKey: undefined, keyPassphrase: undefined, handshakeTimeout: undefined }; }); + getProfileInfomock.mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", argValue: "TEST", secure: false }, + { argName: "password", argValue: "TEST", secure: false }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue("fake"), + } as any); + it("test the issueUnixCommand function", async () => { Object.defineProperty(profileLoader.Profiles, "getInstance", { value: jest.fn(() => { @@ -179,19 +194,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: jest.fn().mockReturnValue({ - usingTeamConfig: true, - getAllProfiles: jest.fn().mockReturnValue(["dummy"]), - mergeArgsForProfile: jest.fn().mockReturnValue({ - knownArgs: [ - { argName: "port", argValue: "TEST", secure: false }, - { argName: "host", argValue: "TEST", secure: false }, - { argName: "user", argValue: "TEST", secure: false }, - { argName: "password", argValue: "TEST", secure: false }, - ], - }), - loadSecureArg: jest.fn().mockReturnValue("fake"), - } as any), + getProfileInfo: getProfileInfomock }; }), }); @@ -245,19 +248,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: jest.fn().mockReturnValue({ - usingTeamConfig: true, - getAllProfiles: jest.fn().mockReturnValue(["dummy"]), - mergeArgsForProfile: jest.fn().mockReturnValue({ - knownArgs: [ - { argName: "port", argValue: "TEST", secure: false }, - { argName: "host", argValue: "TEST", secure: false }, - { argName: "user", argValue: "TEST", secure: false }, - { argName: "password", argValue: "TEST", secure: false }, - ], - }), - loadSecureArg: jest.fn().mockReturnValue("fake"), - } as any), + getProfileInfo: getProfileInfomock }; }), }); @@ -310,19 +301,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: jest.fn().mockReturnValue({ - usingTeamConfig: true, - getAllProfiles: jest.fn().mockReturnValue(["dummy"]), - mergeArgsForProfile: jest.fn().mockReturnValue({ - knownArgs: [ - { argName: "port", argValue: "TEST", secure: false }, - { argName: "host", argValue: "TEST", secure: false }, - { argName: "user", argValue: "TEST", secure: false }, - { argName: "password", argValue: "TEST", secure: false }, - ], - }), - loadSecureArg: jest.fn().mockReturnValue("fake"), - } as any), + getProfileInfo: getProfileInfomock }; }), }); @@ -368,19 +347,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: jest.fn().mockReturnValue({ - usingTeamConfig: true, - getAllProfiles: jest.fn().mockReturnValue(["dummy"]), - mergeArgsForProfile: jest.fn().mockReturnValue({ - knownArgs: [ - { argName: "port", argValue: "TEST", secure: false }, - { argName: "host", argValue: "TEST", secure: false }, - { argName: "user", argValue: "TEST", secure: false }, - { argName: "password", argValue: "TEST", secure: false }, - ], - }), - loadSecureArg: jest.fn().mockReturnValue("fake"), - } as any), + getProfileInfo: getProfileInfomock }; }), }); @@ -423,19 +390,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: jest.fn().mockReturnValue({ - usingTeamConfig: true, - getAllProfiles: jest.fn().mockReturnValue(["dummy"]), - mergeArgsForProfile: jest.fn().mockReturnValue({ - knownArgs: [ - { argName: "port", argValue: "TEST", secure: false }, - { argName: "host", argValue: "TEST", secure: false }, - { argName: "user", argValue: "TEST", secure: false }, - { argName: "password", argValue: "TEST", secure: false }, - ], - }), - loadSecureArg: jest.fn().mockReturnValue("fake"), - } as any), + getProfileInfo: getProfileInfomock }; }), }); @@ -462,19 +417,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: jest.fn().mockReturnValue({ - usingTeamConfig: true, - getAllProfiles: jest.fn().mockReturnValue(["dummy"]), - mergeArgsForProfile: jest.fn().mockReturnValue({ - knownArgs: [ - { argName: "port", argValue: "TEST", secure: false }, - { argName: "host", argValue: "TEST", secure: false }, - { argName: "user", argValue: "TEST", secure: false }, - { argName: "password", argValue: "TEST", secure: false }, - ], - }), - loadSecureArg: jest.fn().mockReturnValue("fake"), - } as any), + getProfileInfo: getProfileInfomock }; }), }); @@ -500,19 +443,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: jest.fn().mockReturnValue({ - usingTeamConfig: true, - getAllProfiles: jest.fn().mockReturnValue(["dummy"]), - mergeArgsForProfile: jest.fn().mockReturnValue({ - knownArgs: [ - { argName: "port", argValue: "TEST", secure: false }, - { argName: "host", argValue: "TEST", secure: false }, - { argName: "user", argValue: "TEST", secure: false }, - { argName: "password", argValue: "TEST", secure: false }, - ], - }), - loadSecureArg: jest.fn().mockReturnValue("fake"), - } as any), + getProfileInfo: getProfileInfomock }; }), }); @@ -556,19 +487,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), validProfile: ValidProfileEnum.VALID, getDefaultProfile: mockdefaultProfile, - getProfileInfo: jest.fn().mockReturnValue({ - usingTeamConfig: true, - getAllProfiles: jest.fn().mockReturnValue(["dummy"]), - mergeArgsForProfile: jest.fn().mockReturnValue({ - knownArgs: [ - { argName: "port", argValue: "TEST", secure: false }, - { argName: "host", argValue: "TEST", secure: false }, - { argName: "user", argValue: "TEST", secure: false }, - { argName: "password", argValue: "TEST", secure: false }, - ], - }), - loadSecureArg: jest.fn().mockReturnValue("fake"), - } as any), + getProfileInfo: getProfileInfomock }; }), }); @@ -625,19 +544,7 @@ describe("UnixCommand Actions Unit Testing", () => { }), validProfile: ValidProfileEnum.INVALID, getDefaultProfile: mockdefaultProfile, - getProfileInfo: jest.fn().mockReturnValue({ - usingTeamConfig: true, - getAllProfiles: jest.fn().mockReturnValue(["dummy"]), - mergeArgsForProfile: jest.fn().mockReturnValue({ - knownArgs: [ - { argName: "port", argValue: "TEST", secure: false }, - { argName: "host", argValue: "TEST", secure: false }, - { argName: "user", argValue: "TEST", secure: false }, - { argName: "password", argValue: "TEST", secure: false }, - ], - }), - loadSecureArg: jest.fn().mockReturnValue("fake"), - } as any), + getProfileInfo: getProfileInfomock }; }), }); @@ -706,19 +613,7 @@ describe("UnixCommand Actions Unit Testing", () => { checkCurrentProfile: jest.fn(), zosmfProfile: mockLoadNamedProfile, getDefaultProfile: mockdefaultProfile, - getProfileInfo: jest.fn().mockReturnValue({ - usingTeamConfig: true, - getAllProfiles: jest.fn().mockReturnValue(["dummy"]), - mergeArgsForProfile: jest.fn().mockReturnValue({ - knownArgs: [ - { argName: "port", argValue: "TEST", secure: false }, - { argName: "host", argValue: "TEST", secure: false }, - { argName: "user", argValue: "TEST", secure: false }, - { argName: "password", argValue: "TEST", secure: false }, - ], - }), - loadSecureArg: jest.fn().mockReturnValue("fake"), - } as any), + getProfileInfo: getProfileInfomock }; }), }); diff --git a/packages/zowe-explorer/src/command/UnixCommandHandler.ts b/packages/zowe-explorer/src/command/UnixCommandHandler.ts index 9852d28593..b638f84c91 100644 --- a/packages/zowe-explorer/src/command/UnixCommandHandler.ts +++ b/packages/zowe-explorer/src/command/UnixCommandHandler.ts @@ -92,7 +92,7 @@ export class UnixCommandHandler extends ZoweCommandProvider { let res: boolean = true; if (!session) { const allProfiles = profiles.allProfiles; - res = this.checkforsshrequired(allProfiles); + res = this.checkForSshRequired(allProfiles); const profileNamesList = ProfileManagement.getRegisteredProfileNameList(globals.Trees.USS); if (profileNamesList.length) { if (!res) { @@ -183,7 +183,7 @@ export class UnixCommandHandler extends ZoweCommandProvider { } } - public checkforsshrequired(allProfiles: imperative.IProfileLoaded[]): boolean { + public checkForSshRequired(allProfiles: imperative.IProfileLoaded[]): boolean { try { allProfiles.forEach((p) => { // eslint-disable-next-line @typescript-eslint/unbound-method @@ -198,7 +198,7 @@ export class UnixCommandHandler extends ZoweCommandProvider { public async setsshSession(): Promise { ZoweLogger.trace("UnixCommandHandler.setsshSession called."); - const sshprofile: imperative.IProfileLoaded = await this.getsshProfile(); + const sshprofile: imperative.IProfileLoaded = await this.getSshProfile(); if (sshprofile) { const cmdArgs: imperative.ICommandArguments = this.getCmdArgs(sshprofile?.profile as imperative.IProfileLoaded); // create the ssh session @@ -236,7 +236,7 @@ export class UnixCommandHandler extends ZoweCommandProvider { return sshProfile; } - private async getsshProfile(): Promise { + private async getSshProfile(): Promise { ZoweLogger.trace("UnixCommandHandler.getsshParams called."); const profileInfo = await Profiles.getInstance().getProfileInfo(); const params = ["port", "host", "user", "password"]; From 8bb365cf1b50ce6f1806daa5126c74c08a64d9bc Mon Sep 17 00:00:00 2001 From: Likhitha Nimma Date: Tue, 19 Dec 2023 22:31:20 +0530 Subject: [PATCH 6/9] Unix-File Changed Signed-off-by: Likhitha Nimma --- packages/zowe-explorer/src/command/UnixCommandHandler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/zowe-explorer/src/command/UnixCommandHandler.ts b/packages/zowe-explorer/src/command/UnixCommandHandler.ts index b638f84c91..206c83e532 100644 --- a/packages/zowe-explorer/src/command/UnixCommandHandler.ts +++ b/packages/zowe-explorer/src/command/UnixCommandHandler.ts @@ -254,7 +254,7 @@ export class UnixCommandHandler extends ZoweCommandProvider { const prof = profileInfo.mergeArgsForProfile(sshProfile.profile as imperative.IProfAttrs); params.forEach((p) => { const obj = prof.knownArgs.find((a) => a.argName === p); - if (obj.secure === false) { + if (obj.argValue) { sshProfile.profile[p] = obj.argValue; } else { sshProfile.profile[p] = profileInfo.loadSecureArg(obj); From 5ca0e77b732f078d6e1d5dc57b2d8a1acf8874da Mon Sep 17 00:00:00 2001 From: Likhitha Nimma Date: Tue, 19 Dec 2023 22:40:53 +0530 Subject: [PATCH 7/9] Improved code coverage Signed-off-by: Likhitha Nimma --- .../__unit__/command/unixCommandHandler.unit.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts index 2fcde3d43d..83a1827cf8 100644 --- a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts @@ -173,8 +173,8 @@ describe("UnixCommand Actions Unit Testing", () => { knownArgs: [ { argName: "port", argValue: "TEST", secure: false }, { argName: "host", argValue: "TEST", secure: false }, - { argName: "user", argValue: "TEST", secure: false }, - { argName: "password", argValue: "TEST", secure: false }, + { argName: "user", secure: true }, + { argName: "password", secure: true }, ], }), loadSecureArg: jest.fn().mockReturnValue("fake"), From cc8b9be542432904efbf6fe8a0a2aab754b7e42c Mon Sep 17 00:00:00 2001 From: Likhitha Nimma Date: Tue, 19 Dec 2023 23:36:27 +0530 Subject: [PATCH 8/9] Unix file corrected Signed-off-by: Likhitha Nimma --- .../command/unixCommandHandler.unit.test.ts | 21 +++++++++---------- .../src/command/UnixCommandHandler.ts | 12 ++++++----- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts index 83a1827cf8..2ba510e49f 100644 --- a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts @@ -194,7 +194,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: getProfileInfomock + getProfileInfo: getProfileInfomock, }; }), }); @@ -248,7 +248,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: getProfileInfomock + getProfileInfo: getProfileInfomock, }; }), }); @@ -301,7 +301,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: getProfileInfomock + getProfileInfo: getProfileInfomock, }; }), }); @@ -347,7 +347,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: getProfileInfomock + getProfileInfo: getProfileInfomock, }; }), }); @@ -390,7 +390,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: getProfileInfomock + getProfileInfo: getProfileInfomock, }; }), }); @@ -417,7 +417,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: getProfileInfomock + getProfileInfo: getProfileInfomock, }; }), }); @@ -443,7 +443,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), getDefaultProfile: mockdefaultProfile, validProfile: ValidProfileEnum.VALID, - getProfileInfo: getProfileInfomock + getProfileInfo: getProfileInfomock, }; }), }); @@ -487,7 +487,7 @@ describe("UnixCommand Actions Unit Testing", () => { getBaseProfile: jest.fn(), validProfile: ValidProfileEnum.VALID, getDefaultProfile: mockdefaultProfile, - getProfileInfo: getProfileInfomock + getProfileInfo: getProfileInfomock, }; }), }); @@ -544,7 +544,7 @@ describe("UnixCommand Actions Unit Testing", () => { }), validProfile: ValidProfileEnum.INVALID, getDefaultProfile: mockdefaultProfile, - getProfileInfo: getProfileInfomock + getProfileInfo: getProfileInfomock, }; }), }); @@ -613,7 +613,7 @@ describe("UnixCommand Actions Unit Testing", () => { checkCurrentProfile: jest.fn(), zosmfProfile: mockLoadNamedProfile, getDefaultProfile: mockdefaultProfile, - getProfileInfo: getProfileInfomock + getProfileInfo: getProfileInfomock, }; }), }); @@ -679,5 +679,4 @@ describe("UnixCommand Actions Unit Testing", () => { ).resolves.toBe(undefined); expect(showInformationMessage.mock.calls[0][0]).toEqual("Operation Cancelled"); }); - }); diff --git a/packages/zowe-explorer/src/command/UnixCommandHandler.ts b/packages/zowe-explorer/src/command/UnixCommandHandler.ts index 206c83e532..3083543e21 100644 --- a/packages/zowe-explorer/src/command/UnixCommandHandler.ts +++ b/packages/zowe-explorer/src/command/UnixCommandHandler.ts @@ -239,7 +239,7 @@ export class UnixCommandHandler extends ZoweCommandProvider { private async getSshProfile(): Promise { ZoweLogger.trace("UnixCommandHandler.getsshParams called."); const profileInfo = await Profiles.getInstance().getProfileInfo(); - const params = ["port", "host", "user", "password"]; + const params = ["port", "host", "user", "password","xyzh"]; const profiles = profileInfo.getAllProfiles("ssh"); if (!profiles) { Gui.errorMessage( @@ -254,10 +254,12 @@ export class UnixCommandHandler extends ZoweCommandProvider { const prof = profileInfo.mergeArgsForProfile(sshProfile.profile as imperative.IProfAttrs); params.forEach((p) => { const obj = prof.knownArgs.find((a) => a.argName === p); - if (obj.argValue) { - sshProfile.profile[p] = obj.argValue; - } else { - sshProfile.profile[p] = profileInfo.loadSecureArg(obj); + if (obj) { + if (obj.argValue) { + sshProfile.profile[p] = obj.argValue; + } else { + sshProfile.profile[p] = profileInfo.loadSecureArg(obj); + } } }); } From 933a3e3fb1cf1214cb1afc36eb26d0ca75cbfc78 Mon Sep 17 00:00:00 2001 From: Likhitha Nimma Date: Wed, 20 Dec 2023 01:01:54 +0530 Subject: [PATCH 9/9] Check for SSH credentials Signed-off-by: Likhitha Nimma --- .../command/unixCommandHandler.unit.test.ts | 23 +++++++++++++++++++ .../src/command/UnixCommandHandler.i18n.json | 1 + .../src/command/UnixCommandHandler.ts | 8 ++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts index 2ba510e49f..ffef6ed8ac 100644 --- a/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/command/unixCommandHandler.unit.test.ts @@ -679,4 +679,27 @@ describe("UnixCommand Actions Unit Testing", () => { ).resolves.toBe(undefined); expect(showInformationMessage.mock.calls[0][0]).toEqual("Operation Cancelled"); }); + it("ssh profile doesn't contain credentials", async () => { + Object.defineProperty(profileLoader.Profiles, "getInstance", { + value: jest.fn(() => { + return { + getProfileInfo: getProfileInfomock.mockReturnValue({ + usingTeamConfig: true, + getAllProfiles: jest.fn().mockReturnValue(["dummy"]), + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { argName: "port", argValue: "TEST", secure: false }, + { argName: "host", argValue: "TEST", secure: false }, + { argName: "user", secure: true }, + { argName: "password", secure: true }, + ], + }), + loadSecureArg: jest.fn().mockReturnValue(undefined), + } as any) + } + }) + }); + await (unixActions as any).getSshProfile(); + expect(showErrorMessage.mock.calls[0][0]).toEqual("Credentials are missing for SSH profile"); + }) }); diff --git a/packages/zowe-explorer/i18n/sample/src/command/UnixCommandHandler.i18n.json b/packages/zowe-explorer/i18n/sample/src/command/UnixCommandHandler.i18n.json index 1b183b03a5..794c60ab49 100644 --- a/packages/zowe-explorer/i18n/sample/src/command/UnixCommandHandler.i18n.json +++ b/packages/zowe-explorer/i18n/sample/src/command/UnixCommandHandler.i18n.json @@ -10,6 +10,7 @@ "issueUnixCommand.apiNonExisting": "Not implemented yet for profile of type: ", "issueUnixCommand.sshProfile.quickPickOption": "Select the ssh Profile.", "setsshProfile.couldnotfindprofile": "No SSH profile found. Please create an SSH profile before issuing Unix commands.", + "sshcredentialsMissing": "Credentials are missing for SSH profile", "issueUnixCommand.command.hostname": "Select a Unix command to run against ", "issueUnixCommand.command.edit": " (An option to edit will follow)", "issueUnixCommand.options.noselection": "No selection made. Operation cancelled.", diff --git a/packages/zowe-explorer/src/command/UnixCommandHandler.ts b/packages/zowe-explorer/src/command/UnixCommandHandler.ts index 3083543e21..a1d176817e 100644 --- a/packages/zowe-explorer/src/command/UnixCommandHandler.ts +++ b/packages/zowe-explorer/src/command/UnixCommandHandler.ts @@ -239,7 +239,7 @@ export class UnixCommandHandler extends ZoweCommandProvider { private async getSshProfile(): Promise { ZoweLogger.trace("UnixCommandHandler.getsshParams called."); const profileInfo = await Profiles.getInstance().getProfileInfo(); - const params = ["port", "host", "user", "password","xyzh"]; + const params = ["port", "host","user","password"]; const profiles = profileInfo.getAllProfiles("ssh"); if (!profiles) { Gui.errorMessage( @@ -247,6 +247,7 @@ export class UnixCommandHandler extends ZoweCommandProvider { ); return; } + let exitflag: boolean; let sshProfile: imperative.IProfileLoaded; if (profiles.length > 0) { sshProfile = await this.selectSshProfile(profiles.map((p) => imperative.ProfileInfo.profAttrsToProfLoaded(p))); @@ -259,11 +260,16 @@ export class UnixCommandHandler extends ZoweCommandProvider { sshProfile.profile[p] = obj.argValue; } else { sshProfile.profile[p] = profileInfo.loadSecureArg(obj); + if(!sshProfile.profile[p]){ + exitflag = true; + Gui.errorMessage(localize("sshcredentialsMissing", "Credentials are missing for SSH profile")); + } } } }); } } + if(exitflag) return; return sshProfile; }