Skip to content

Commit

Permalink
add test for basic creds, move logic for updating trees
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Dec 23, 2024
1 parent b1a6984 commit f443bc6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
});
});

Expand Down
24 changes: 15 additions & 9 deletions packages/zowe-explorer-api/src/profiles/AuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ export class AuthHandler {
}
}

private static async updateTreeProvidersWithProfile(profile: imperative.IProfileLoaded) {

Check warning on line 79 in packages/zowe-explorer-api/src/profiles/AuthHandler.ts

View check run for this annotation

Codecov / codecov/patch

packages/zowe-explorer-api/src/profiles/AuthHandler.ts#L79

Added line #L79 was not covered by tests
// 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]) {

Check warning on line 83 in packages/zowe-explorer-api/src/profiles/AuthHandler.ts

View check run for this annotation

Codecov / codecov/patch

packages/zowe-explorer-api/src/profiles/AuthHandler.ts#L82-L83

Added lines #L82 - L83 were not covered by tests
const node = (await (provider as IZoweTree<IZoweTreeNode>).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
Expand All @@ -100,6 +110,9 @@ export class AuthHandler {
});
if (userResp === message) {
if (await opts.ssoLogin(null, profileName)) {
if (typeof profile !== "string") {
AuthHandler.updateTreeProvidersWithProfile(profile);

Check warning on line 114 in packages/zowe-explorer-api/src/profiles/AuthHandler.ts

View check run for this annotation

Codecov / codecov/patch

packages/zowe-explorer-api/src/profiles/AuthHandler.ts#L114

Added line #L114 was not covered by tests
}
// SSO login was successful, unlock profile so it can be used again
AuthHandler.unlockProfile(profileName, true);
return true;
Expand All @@ -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<IZoweTreeNode>).getChildren()).find((n) => n.label === profileName);
if (node && typeof profile !== "string") {
node.setProfileToChoice(profile);
}
if (typeof profile !== "string") {
AuthHandler.updateTreeProvidersWithProfile(profile);

Check warning on line 140 in packages/zowe-explorer-api/src/profiles/AuthHandler.ts

View check run for this annotation

Codecov / codecov/patch

packages/zowe-explorer-api/src/profiles/AuthHandler.ts#L140

Added line #L140 was not covered by tests
}
// Unlock profile so it can be used again
AuthHandler.unlockProfile(profileName, true);
Expand Down

0 comments on commit f443bc6

Please sign in to comment.