From f443bc69e2fcc43ca86b2354c95817f60622c79d Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Mon, 23 Dec 2024 14:20:16 -0500 Subject: [PATCH] add test for basic creds, move logic for updating trees Signed-off-by: Trae Yelovich --- .../profiles/AuthHandler.unit.test.ts | 22 ++++++++++++++++- .../src/profiles/AuthHandler.ts | 24 ++++++++++++------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/packages/zowe-explorer-api/__tests__/__unit__/profiles/AuthHandler.unit.test.ts b/packages/zowe-explorer-api/__tests__/__unit__/profiles/AuthHandler.unit.test.ts index 51619b730..21c85801a 100644 --- a/packages/zowe-explorer-api/__tests__/__unit__/profiles/AuthHandler.unit.test.ts +++ b/packages/zowe-explorer-api/__tests__/__unit__/profiles/AuthHandler.unit.test.ts @@ -72,11 +72,12 @@ describe("AuthHandler.lockProfile", () => { }); describe("AuthHandler.promptForAuthentication", () => { - it("handles a token-based authentication error - login successful", async () => { + it("handles a token-based authentication error - login successful, profile is string", async () => { const tokenNotValidMsg = "Token is not valid or expired."; const impError = new ImperativeError({ additionalDetails: tokenNotValidMsg, msg: tokenNotValidMsg }); const ssoLogin = jest.fn().mockResolvedValue(true); const promptCredentials = jest.fn(); + const updateTreeProvidersWithProfileMock = jest.spyOn(AuthHandler as any, "updateTreeProvidersWithProfile").mockImplementation(); const showMessageMock = jest.spyOn(Gui, "showMessage").mockResolvedValueOnce("Log in to Authentication Service"); const unlockProfileSpy = jest.spyOn(AuthHandler, "unlockProfile"); await expect(AuthHandler.promptForAuthentication(impError, "lpar.zosmf", { promptCredentials, ssoLogin })).resolves.toBe(true); @@ -86,6 +87,25 @@ describe("AuthHandler.promptForAuthentication", () => { expect(unlockProfileSpy).toHaveBeenCalledTimes(1); expect(unlockProfileSpy).toHaveBeenCalledWith("lpar.zosmf", true); expect(showMessageMock).toHaveBeenCalledTimes(1); + expect(updateTreeProvidersWithProfileMock).not.toHaveBeenCalled(); + }); + + it("handles a standard authentication error - credentials provided, profile is string", async () => { + const tokenNotValidMsg = "Invalid credentials"; + const impError = new ImperativeError({ additionalDetails: tokenNotValidMsg, msg: tokenNotValidMsg }); + const ssoLogin = jest.fn().mockResolvedValue(true); + const updateTreeProvidersWithProfileMock = jest.spyOn(AuthHandler as any, "updateTreeProvidersWithProfile").mockImplementation(); + const promptCredentials = jest.fn().mockResolvedValue(["us3r", "p4ssw0rd"]); + const errorMessageMock = jest.spyOn(Gui, "errorMessage").mockResolvedValueOnce("Update Credentials"); + const unlockProfileSpy = jest.spyOn(AuthHandler, "unlockProfile").mockClear(); + await expect(AuthHandler.promptForAuthentication(impError, "lpar.zosmf", { promptCredentials, ssoLogin })).resolves.toBe(true); + expect(unlockProfileSpy).toHaveBeenCalledTimes(1); + expect(unlockProfileSpy).toHaveBeenCalledWith("lpar.zosmf", true); + expect(ssoLogin).not.toHaveBeenCalled(); + expect(errorMessageMock).toHaveBeenCalledTimes(1); + expect(promptCredentials).toHaveBeenCalledTimes(1); + expect(promptCredentials).toHaveBeenCalledWith("lpar.zosmf", true); + expect(updateTreeProvidersWithProfileMock).not.toHaveBeenCalled(); }); }); diff --git a/packages/zowe-explorer-api/src/profiles/AuthHandler.ts b/packages/zowe-explorer-api/src/profiles/AuthHandler.ts index 598bc2d21..5736b8bd7 100644 --- a/packages/zowe-explorer-api/src/profiles/AuthHandler.ts +++ b/packages/zowe-explorer-api/src/profiles/AuthHandler.ts @@ -76,6 +76,16 @@ export class AuthHandler { } } + private static async updateTreeProvidersWithProfile(profile: imperative.IProfileLoaded) { + // TODO: If we can access extender tree providers (e.g. CICS), it would help to propagate profile updates here. + // For now we will propagate profile changes to core providers (Data Sets, USS, Jobs) + const treeProviders = (await commands.executeCommand("zowe.getTreeProviders")) as any; + for (const provider of [treeProviders.ds, treeProviders.uss, treeProviders.job]) { + const node = (await (provider as IZoweTree).getChildren()).find((n) => n.label === profile?.name); + node?.setProfileToChoice?.(profile); + } + } + /** * Prompts the user to authenticate over SSO or a credential prompt in the event of an error. * @param imperativeError The authentication error that was encountered @@ -100,6 +110,9 @@ export class AuthHandler { }); if (userResp === message) { if (await opts.ssoLogin(null, profileName)) { + if (typeof profile !== "string") { + AuthHandler.updateTreeProvidersWithProfile(profile); + } // SSO login was successful, unlock profile so it can be used again AuthHandler.unlockProfile(profileName, true); return true; @@ -123,15 +136,8 @@ export class AuthHandler { if (creds != null) { // New creds were set, directly propagate new profile to other tree providers. - - // TODO: If we can access extender tree providers (e.g. CICS), it would help to propagate profile updates here. - // For now we will propagate profile changes to core providers (Data Sets, USS, Jobs) - const treeProviders = (await commands.executeCommand("zowe.getTreeProviders")) as any; - for (const provider of [treeProviders.ds, treeProviders.uss, treeProviders.job]) { - const node = (await (provider as IZoweTree).getChildren()).find((n) => n.label === profileName); - if (node && typeof profile !== "string") { - node.setProfileToChoice(profile); - } + if (typeof profile !== "string") { + AuthHandler.updateTreeProvidersWithProfile(profile); } // Unlock profile so it can be used again AuthHandler.unlockProfile(profileName, true);