From 116ad2df06a0ad82e26cb58c2fffb6f22c25c4c6 Mon Sep 17 00:00:00 2001 From: Santhoshi Boyina Date: Thu, 11 Jul 2024 16:08:00 +0530 Subject: [PATCH] Fix broken unit test cases Signed-off-by: Santhoshi Boyina --- .../__unit__/Profiles.extended.unit.test.ts | 191 +++++++++++++++++- .../i18n/sample/src/Profiles.i18n.json | 9 +- packages/zowe-explorer/src/Profiles.ts | 41 ++-- 3 files changed, 204 insertions(+), 37 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/Profiles.extended.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/Profiles.extended.unit.test.ts index 5d9ba954da..686093b741 100644 --- a/packages/zowe-explorer/__tests__/__unit__/Profiles.extended.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/Profiles.extended.unit.test.ts @@ -1687,7 +1687,7 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { ); }); - it("To switch from Basic to Token-based authentication with Base Profile", async () => { + it("To switch from Basic to Token-based authentication using Base Profile", async () => { jest.spyOn(utils.ProfilesUtils, "isProfileUsingBasicAuth").mockReturnValueOnce(true); jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockResolvedValue({ getTeamConfig: () => ({ @@ -1704,6 +1704,22 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { }, }, ], + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { + argName: "user", + dataType: "string", + argValue: "fake", + argLoc: { jsonLoc: "jsonLoc" }, + }, + { + argName: "password", + dataType: "string", + argValue: "fake", + argLoc: { jsonLoc: "jsonLoc" }, + }, + ], + }), } as any); testNode.profile.profile = { type: "zosmf", @@ -1729,6 +1745,7 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { tokenValue: "12345", secure: ["tokenType"], }; + jest.spyOn(Gui, "resolveQuickPick").mockResolvedValue({ label: "Yes" } as vscode.QuickPickItem); jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValue({ getTokenTypeName: () => "apimlAuthenticationToken", } as never); @@ -1744,7 +1761,7 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { expect(testNode.profile.profile.password).toBeUndefined(); }); - it("To check login fail's when switching from Basic to Token-based authentication with Base Profile", async () => { + it("To check login fail's when trying to switch from Basic to Token-based authentication using Base Profile", async () => { jest.spyOn(utils.ProfilesUtils, "isProfileUsingBasicAuth").mockReturnValueOnce(true); jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockResolvedValue({ getTeamConfig: () => ({ @@ -1786,6 +1803,7 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { tokenValue: undefined, secure: ["user", "password"], }; + jest.spyOn(Gui, "resolveQuickPick").mockResolvedValue({ label: "Yes" } as vscode.QuickPickItem); jest.spyOn(Gui, "errorMessage").mockImplementation(); jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValue({ getTokenTypeName: () => "apimlAuthenticationToken", @@ -1802,7 +1820,7 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { expect(testNode.profile.profile.tokenValue).toBeUndefined(); }); - it("To switch from Basic to Token-based authentication with Regular Profile", async () => { + it("To switch from Basic to Token-based authentication using Regular Profile", async () => { jest.spyOn(utils.ProfilesUtils, "isProfileUsingBasicAuth").mockReturnValueOnce(true); jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockResolvedValue({ getTeamConfig: () => ({ @@ -1819,6 +1837,22 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { }, }, ], + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { + argName: "user", + dataType: "string", + argValue: "fake", + argLoc: { jsonLoc: "jsonLoc" }, + }, + { + argName: "password", + dataType: "string", + argValue: "fake", + argLoc: { jsonLoc: "jsonLoc" }, + }, + ], + }), } as any); testNode.profile.profile = { type: "zosmf", @@ -1844,6 +1878,7 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { tokenValue: "12345", secure: ["tokenType"], }; + jest.spyOn(Gui, "resolveQuickPick").mockResolvedValue({ label: "Yes" } as vscode.QuickPickItem); jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValue({ getTokenTypeName: () => "jwtToken", } as never); @@ -1858,7 +1893,7 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { expect(testNode.profile.profile.password).toBeUndefined(); }); - it("To check login fail's when switching from Basic to Token-based authentication with Regular Profile", async () => { + it("To check login fail's when trying to switch from Basic to Token-based authentication using Regular Profile", async () => { jest.spyOn(utils.ProfilesUtils, "isProfileUsingBasicAuth").mockReturnValueOnce(true); jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockResolvedValue({ getTeamConfig: () => ({ @@ -1900,6 +1935,8 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { tokenValue: undefined, secure: ["user", "password"], }; + jest.spyOn(Gui, "resolveQuickPick").mockResolvedValue({ label: "Yes" } as vscode.QuickPickItem); + jest.spyOn(Gui, "errorMessage").mockImplementation(); jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValue({ getTokenTypeName: () => "jwtToken", } as never); @@ -1914,7 +1951,7 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { expect(testNode.profile.profile.tokenValue).toBeUndefined(); }); - it("To switch from Token-based to Basic authentication when cred values are passed", async () => { + it("To switch from Token-based to Basic authentication when cred values are passed involving base profile", async () => { jest.spyOn(utils.ProfilesUtils, "isUsingTokenAuth").mockResolvedValueOnce(true); jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockResolvedValue({ getTeamConfig: () => ({ @@ -1931,6 +1968,22 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { }, }, ], + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { + argName: "user", + dataType: "string", + argValue: "fake", + argLoc: { jsonLoc: "jsonLoc" }, + }, + { + argName: "password", + dataType: "string", + argValue: "fake", + argLoc: { jsonLoc: "jsonLoc" }, + }, + ], + }), } as any); testNode.profile.profile = { type: "zosmf", @@ -1956,6 +2009,7 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { tokenValue: undefined, secure: ["user", "password"], }; + jest.spyOn(Gui, "resolveQuickPick").mockResolvedValue({ label: "Yes" } as vscode.QuickPickItem); jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValue({ getTokenTypeName: () => "apimlAuthenticationToken", } as never); @@ -1971,7 +2025,7 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { expect(testNode.profile.profile.tokenValue).toBeUndefined(); }); - it("To switch from Token-based to Basic authentication when cred values are not passed", async () => { + it("To switch from Token-based to Basic authentication when cred values are passed involving regular profile", async () => { jest.spyOn(utils.ProfilesUtils, "isUsingTokenAuth").mockResolvedValueOnce(true); jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockResolvedValue({ getTeamConfig: () => ({ @@ -1988,6 +2042,22 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { }, }, ], + mergeArgsForProfile: jest.fn().mockReturnValue({ + knownArgs: [ + { + argName: "user", + dataType: "string", + argValue: "fake", + argLoc: { jsonLoc: "jsonLoc" }, + }, + { + argName: "password", + dataType: "string", + argValue: "fake", + argLoc: { jsonLoc: "jsonLoc" }, + }, + ], + }), } as any); testNode.profile.profile = { type: "zosmf", @@ -2002,6 +2072,52 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { secure: ["tokenType"], }; modifiedTestNode.profile.profile = { + type: "zosmf", + host: "test", + port: 1443, + name: "base", + rejectUnauthorized: false, + user: "testUser", + password: "6789", + tokenType: undefined, + tokenValue: undefined, + secure: ["user", "password"], + }; + jest.spyOn(Gui, "resolveQuickPick").mockResolvedValue({ label: "Yes" } as vscode.QuickPickItem); + jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValue({ + getTokenTypeName: () => "jwtToken", + } as never); + jest.spyOn(Profiles.getInstance(), "promptCredentials").mockResolvedValue(["testUser", "6789"]); + await Profiles.getInstance().handleSwitchAuthentication(testNode); + expect(Gui.showMessage).toBeCalled(); + expect(Gui.showMessage).toBeCalled(); + expect(testNode.profile.profile.tokenType).toBe(modifiedTestNode.profile.profile.tokenType); + expect(testNode.profile.profile.tokenValue).toBe(modifiedTestNode.profile.profile.tokenValue); + expect(testNode.profile.profile.secure.length).toBe(modifiedTestNode.profile.profile.secure.length); + expect(testNode.profile.profile.secure).toEqual(modifiedTestNode.profile.profile.secure); + expect(testNode.profile.profile.tokenType).toBeUndefined(); + expect(testNode.profile.profile.tokenValue).toBeUndefined(); + }); + + it("To not switch from Token-based to Basic authentication when cred values are not passed", async () => { + jest.spyOn(utils.ProfilesUtils, "isUsingTokenAuth").mockResolvedValueOnce(true); + jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockResolvedValue({ + getTeamConfig: () => ({ + properties: jest.fn(), + set: jest.fn(), + delete: jest.fn(), + save: jest.fn(), + }), + getAllProfiles: () => [ + { + profName: "sestest", + profLoc: { + osLoc: ["test"], + }, + }, + ], + } as any); + testNode.profile.profile = { type: "zosmf", host: "test", port: 1443, @@ -2013,6 +2129,19 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { tokenValue: "12345", secure: ["tokenType"], }; + modifiedTestNode.profile.profile = { + type: "zosmf", + host: "test", + port: 1443, + name: "base", + rejectUnauthorized: false, + user: undefined, + password: undefined, + tokenType: "testTokenType", + tokenValue: "12345", + secure: ["tokenType"], + }; + jest.spyOn(Gui, "resolveQuickPick").mockResolvedValue({ label: "Yes" } as vscode.QuickPickItem); jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValue({ getTokenTypeName: () => "apimlAuthenticationToken", } as never); @@ -2028,8 +2157,10 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { expect(testNode.profile.profile.password).toBeUndefined(); }); - it("To check authentication switch for a profile which does not support token-based authentication", async () => { + it("To not perform switching the authentication for a profile which does not support token-based authentication", async () => { jest.spyOn(utils.ProfilesUtils, "isProfileUsingBasicAuth").mockReturnValueOnce(true); + jest.spyOn(Gui, "errorMessage").mockImplementation(); + jest.spyOn(Gui, "resolveQuickPick").mockResolvedValue({ label: "Yes" } as vscode.QuickPickItem); jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValue({ getTokenTypeName: () => { throw new Error("test error."); @@ -2039,7 +2170,9 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { expect(Gui.errorMessage).toBeCalled(); }); - it("When authentication method is unknown", async () => { + it("To not perform switching the authentication when authentication method is unknown", async () => { + jest.spyOn(Gui, "resolveQuickPick").mockResolvedValue({ label: "Yes" } as vscode.QuickPickItem); + jest.spyOn(Gui, "errorMessage").mockImplementation(); jest.spyOn(utils.ProfilesUtils, "isProfileUsingBasicAuth").mockReturnValueOnce(false); jest.spyOn(utils.ProfilesUtils, "isUsingTokenAuth").mockResolvedValueOnce(false); jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValue({ @@ -2048,6 +2181,48 @@ describe("Profiles Unit Tests - function handleSwitchAuthentication", () => { await Profiles.getInstance().handleSwitchAuthentication(testNode); expect(Gui.errorMessage).toBeCalled(); }); + + it("To not perform switching the authentication when user wants to cancel the authentication switch", async () => { + testNode.profile.profile = { + type: "zosmf", + host: "test", + port: 1443, + name: "base", + rejectUnauthorized: false, + user: undefined, + password: undefined, + tokenType: "testTokenType", + tokenValue: "12345", + secure: ["tokenType"], + }; + modifiedTestNode.profile.profile = { + type: "zosmf", + host: "test", + port: 1443, + name: "base", + rejectUnauthorized: false, + user: undefined, + password: undefined, + tokenType: "testTokenType", + tokenValue: "12345", + secure: ["tokenType"], + }; + jest.spyOn(Gui, "resolveQuickPick").mockResolvedValue({ label: "No" } as vscode.QuickPickItem); + await Profiles.getInstance().handleSwitchAuthentication(testNode); + expect(testNode.profile.profile.tokenType).toBe(modifiedTestNode.profile.profile.tokenType); + expect(testNode.profile.profile.tokenValue).toBe(modifiedTestNode.profile.profile.tokenValue); + expect(testNode.profile.profile.secure.length).toBe(modifiedTestNode.profile.profile.secure.length); + expect(testNode.profile.profile.secure).toEqual(modifiedTestNode.profile.profile.secure); + expect(testNode.profile.profile.user).toBeUndefined(); + expect(testNode.profile.profile.password).toBeUndefined(); + }); + + it("To not perform switching the authentication when user wants escapes the quick pick of authentication switch", async () => { + jest.spyOn(Gui, "resolveQuickPick").mockResolvedValue(undefined); + jest.spyOn(Gui, "infoMessage").mockImplementation(); + await Profiles.getInstance().handleSwitchAuthentication(testNode); + expect(Gui.infoMessage).toBeCalled(); + }); }); describe("Profiles Unit Tests - function ssoLogout", () => { diff --git a/packages/zowe-explorer/i18n/sample/src/Profiles.i18n.json b/packages/zowe-explorer/i18n/sample/src/Profiles.i18n.json index 4a9e9a46fa..b48b0f2a1b 100644 --- a/packages/zowe-explorer/i18n/sample/src/Profiles.i18n.json +++ b/packages/zowe-explorer/i18n/sample/src/Profiles.i18n.json @@ -31,11 +31,12 @@ "ssoLogin.tokenType.error": "Error getting supported tokenType value for profile {0}", "ssoLogin.successful": "Login to authentication service was successful.", "ssoLogin.error": "Unable to log in with {0}. {1}", - "Profiles.getSwitchAuthenticationQpItems.yesLbl": "Yes", - "ProfileManagement.getSwitchAuthenticationQpItems.yesDesc": "To change the authentication", - "Profiles.getSwitchAuthenticationQpItems.noLbl": "No", - "Profiles.getSwitchAuthenticationQpItems.noDesc": "To continue in current authentication", + "Profiles.switchAuthenticationQuickPick.yesLbl": "Yes", + "ProfileManagement.switchAuthenticationQuickPick.yesDesc": "To change the authentication", + "Profiles.switchAuthenticationQuickPick.noLbl": "No", + "Profiles.switchAuthenticationQuickPick.noDesc": "To continue in current authentication", "Profiles.switchAuthenticationQuickPick.qpConfirmation": "Do you wish to change the Authentication", + "Profiles.getSwitchAuthenticationQpItems.noLbl": "No", "handleSwitchAuthentication.getTokenTypeName.error": "Cannot switch to Token-based Authentication for profile {0}.", "handleSwitchAuthentication.switchFromBasicToTokenAuth.successful": "Login using token-based authentication service was successful for profile {0}.", "handleSwitchAuthentication.switchFromBasicToTokenAuth.error": "Unable to switch to Token-based authentication for profile {0}.", diff --git a/packages/zowe-explorer/src/Profiles.ts b/packages/zowe-explorer/src/Profiles.ts index 49ce5b825d..e3cd460305 100644 --- a/packages/zowe-explorer/src/Profiles.ts +++ b/packages/zowe-explorer/src/Profiles.ts @@ -1238,30 +1238,6 @@ export class Profiles extends ProfilesCache { } } - public getSwitchAuthenticationQpItems(): vscode.QuickPickItem[] { - const qpItemYes: vscode.QuickPickItem = { - label: localize("Profiles.getSwitchAuthenticationQpItems.yesLbl", "Yes"), - description: localize("ProfileManagement.getSwitchAuthenticationQpItems.yesDesc", "To change the authentication"), - }; - const qpItemNo: vscode.QuickPickItem = { - label: localize("Profiles.getSwitchAuthenticationQpItems.noLbl", "No"), - description: localize("Profiles.getSwitchAuthenticationQpItems.noDesc", "To continue in current authentication"), - }; - return [qpItemYes, qpItemNo]; - } - - public async switchAuthenticationQuickPick(): Promise { - const qp = Gui.createQuickPick(); - const [qpItemYes, qpItemNo] = this.getSwitchAuthenticationQpItems(); - qp.items = [qpItemYes, qpItemNo]; - qp.placeholder = localize("Profiles.switchAuthenticationQuickPick.qpConfirmation", "Do you wish to change the Authentication"); - qp.activeItems = [qpItemYes]; - qp.show(); - const selection = await Gui.resolveQuickPick(qp); - qp.hide(); - return selection; - } - public async basicAuthClearSecureArray(profileName?: string, loginTokenType?: string): Promise { const profInfo = await this.getProfileInfo(); const configApi = profInfo.getTeamConfig(); @@ -1299,7 +1275,22 @@ export class Profiles extends ProfilesCache { } public async handleSwitchAuthentication(node?: IZoweNodeType): Promise { - const qpSelection = await this.switchAuthenticationQuickPick(); + const qp = Gui.createQuickPick(); + const qpItemYes: vscode.QuickPickItem = { + label: localize("Profiles.switchAuthenticationQuickPick.yesLbl", "Yes"), + description: localize("ProfileManagement.switchAuthenticationQuickPick.yesDesc", "To change the authentication"), + }; + const qpItemNo: vscode.QuickPickItem = { + label: localize("Profiles.switchAuthenticationQuickPick.noLbl", "No"), + description: localize("Profiles.switchAuthenticationQuickPick.noDesc", "To continue in current authentication"), + }; + qp.items = [qpItemYes, qpItemNo]; + qp.placeholder = localize("Profiles.switchAuthenticationQuickPick.qpConfirmation", "Do you wish to change the Authentication"); + qp.activeItems = [qpItemYes]; + qp.show(); + const qpSelection = await Gui.resolveQuickPick(qp); + qp.hide(); + if (qpSelection === undefined) { Gui.infoMessage(localize("profiles.operation.cancelled", "Operation Cancelled")); return;