Skip to content

Commit

Permalink
refactor: lockedProfiles -> profileLocks, patch cov in ProfilesUtils
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 410755c commit 00fe312
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
13 changes: 7 additions & 6 deletions packages/zowe-explorer-api/src/profiles/AuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface AuthPromptParams extends IAuthMethods {

type ProfileLike = string | imperative.IProfileLoaded;
export class AuthHandler {
private static lockedProfiles: Map<string, Mutex> = new Map();
private static profileLocks: Map<string, Mutex> = new Map();

/**
* Function that checks whether a profile is using token based authentication
Expand All @@ -55,7 +55,8 @@ export class AuthHandler {
*/
public static unlockProfile(profile: ProfileLike, refreshResources?: boolean): void {
const profileName = typeof profile === "string" ? profile : profile.name;
const mutex = this.lockedProfiles.get(profileName);
const mutex = this.profileLocks.get(profileName);
// If a mutex doesn't exist for this profile or the mutex is no longer locked, return
if (mutex == null || !mutex.isLocked()) {
return;
}
Expand Down Expand Up @@ -151,12 +152,12 @@ export class AuthHandler {
const profileName = typeof profile === "string" ? profile : profile.name;

// If the mutex does not exist, make one for the profile and acquire the lock
if (!this.lockedProfiles.has(profileName)) {
this.lockedProfiles.set(profileName, new Mutex());
if (!this.profileLocks.has(profileName)) {
this.profileLocks.set(profileName, new Mutex());
}

// Attempt to acquire the lock
const mutex = this.lockedProfiles.get(profileName);
const mutex = this.profileLocks.get(profileName);
await mutex.acquire();

// Prompt the user to re-authenticate if an error and options were provided
Expand All @@ -176,7 +177,7 @@ export class AuthHandler {
* @returns {boolean} `true` if the given profile is locked, `false` otherwise
*/
public static isProfileLocked(profile: ProfileLike): boolean {
const mutex = this.lockedProfiles.get(typeof profile === "string" ? profile : profile.name);
const mutex = this.profileLocks.get(typeof profile === "string" ? profile : profile.name);
if (mutex == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ import * as fs from "fs";
import * as path from "path";
import * as util from "util";
import * as vscode from "vscode";
import { Gui, imperative, ProfilesCache, ZoweVsCodeExtension } from "@zowe/zowe-explorer-api";
import { createAltTypeIProfile, createInstanceOfProfile, createValidIProfile } from "../../__mocks__/mockCreators/shared";
import { AuthHandler, Gui, imperative, ProfilesCache, ZoweVsCodeExtension } from "@zowe/zowe-explorer-api";
import {
createAltTypeIProfile,
createInstanceOfProfile,
createIProfile,
createISession,
createValidIProfile,
} from "../../__mocks__/mockCreators/shared";
import { MockedProperty } from "../../__mocks__/mockUtils";
import { Constants } from "../../../src/configuration/Constants";
import { ZoweLogger } from "../../../src/tools/ZoweLogger";
Expand All @@ -26,6 +32,7 @@ import { ProfilesConvertStatus, ProfilesUtils } from "../../../src/utils/Profile
import { AuthUtils } from "../../../src/utils/AuthUtils";
import { ZoweLocalStorage } from "../../../src/tools/ZoweLocalStorage";
import { Definitions } from "../../../src/configuration/Definitions";
import { createDatasetSessionNode } from "../../__mocks__/mockCreators/datasets";

jest.mock("../../../src/tools/ZoweLogger");
jest.mock("fs");
Expand Down Expand Up @@ -378,6 +385,25 @@ describe("ProfilesUtils unit tests", () => {
expect(getProfileInfoSpy).toHaveBeenCalled();
});

it("calls unlockProfile once credentials are provided", async () => {
const mockProfileInstance = new Profiles(imperative.Logger.getAppLogger());
const promptCredentialsProfilesMock = jest.spyOn(mockProfileInstance, "promptCredentials").mockResolvedValueOnce(["someusername", "pw"]);
const updateCachedProfileMock = jest.spyOn(mockProfileInstance, "updateCachedProfile").mockResolvedValueOnce(undefined);
const profile = createIProfile();
jest.spyOn(mockProfileInstance, "getLoadedProfConfig").mockResolvedValue(profile);
Object.defineProperty(Constants, "PROFILES_CACHE", { value: mockProfileInstance, configurable: true });
jest.spyOn(ZoweVsCodeExtension as any, "promptUserPass").mockResolvedValue([]);
const unlockProfileSpy = jest.spyOn(AuthHandler, "unlockProfile");
const mockNode = createDatasetSessionNode(createISession(), profile);
await ProfilesUtils.promptCredentials(mockNode);
expect(promptCredentialsProfilesMock).toHaveBeenCalledTimes(1);
expect(promptCredentialsProfilesMock).toHaveBeenCalledWith(profile, true);
expect(unlockProfileSpy).toHaveBeenCalledTimes(1);
expect(unlockProfileSpy).toHaveBeenCalledWith(profile);
expect(updateCachedProfileMock).toHaveBeenCalledTimes(1);
expect(updateCachedProfileMock).toHaveBeenCalledWith(profile, mockNode);
});

it("shows an error message if the profile input is undefined", async () => {
const mockProfileInstance = new Profiles(imperative.Logger.getAppLogger());
jest.spyOn(ProfilesCache.prototype, "getProfileInfo").mockResolvedValue(prof as unknown as imperative.ProfileInfo);
Expand Down
4 changes: 1 addition & 3 deletions packages/zowe-explorer/src/utils/ProfilesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,7 @@ export class ProfilesUtils {
args: [typeof profile === "string" ? profile : profile.name],
comment: ["Profile name"],
});
if (AuthHandler.isProfileLocked(profile)) {
AuthHandler.unlockProfile(profile);
}
AuthHandler.unlockProfile(profile);
if (typeof profile !== "string") {
await Constants.PROFILES_CACHE.updateCachedProfile(profile, node);
}
Expand Down

0 comments on commit 00fe312

Please sign in to comment.