Skip to content

Commit

Permalink
expose Mutex, fix failing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Dec 20, 2024
1 parent 18d485a commit 665c4a4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/zowe-explorer-api/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { IZoweTreeNode } from "../tree";
import { window, workspace } from "vscode";

export * from "./DeferredPromise";
export * from "./Mutex";
export * from "./ErrorCorrelator";
export * from "./Poller";
export * from "./FileManagement";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ describe("fetchSpoolAtUri", () => {
throw new Error("Failed to download spool");
}),
};
const promptForAuthErrorMock = jest.spyOn(AuthUtils, "lockProfileOnAuthError").mockImplementation();
const promptForAuthErrorMock = jest.spyOn(AuthUtils, "handleProfileAuthOnError").mockImplementation();
const jesApiMock = jest.spyOn(ZoweExplorerApiRegister, "getJesApi").mockReturnValueOnce(mockJesApi as any);
await expect(JobFSProvider.instance.fetchSpoolAtUri(testUris.spool)).rejects.toThrow();
expect(promptForAuthErrorMock).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Constants } from "../../../src/configuration/Constants";
import { MockedProperty } from "../../__mocks__/mockUtils";

describe("AuthUtils", () => {
describe("lockProfileOnAuthError", () => {
describe("handleProfileAuthOnError", () => {
it("should prompt for authentication", async () => {
const errorDetails = new imperative.ImperativeError({
errorCode: 401 as unknown as string,
Expand All @@ -37,7 +37,7 @@ describe("AuthUtils", () => {
});
const isUsingTokenAuthMock = jest.spyOn(AuthUtils, "isUsingTokenAuth").mockResolvedValueOnce(false);

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused variable isUsingTokenAuthMock.
const promptForAuthenticationMock = jest.spyOn(AuthHandler, "promptForAuthentication").mockResolvedValueOnce(true);
await AuthUtils.lockProfileOnAuthError(errorDetails, profile);
await AuthUtils.handleProfileAuthOnError(errorDetails, profile);
expect(correlateErrorMock).toHaveBeenCalledWith(ZoweExplorerApiType.All, errorDetails, {
templateArgs: {
profileName: profile.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 { Mutex, Gui, imperative, ProfilesCache, ZoweVsCodeExtension } from "@zowe/zowe-explorer-api";
import { createAltTypeIProfile, createInstanceOfProfile, createValidIProfile } from "../../__mocks__/mockCreators/shared";
import { MockedProperty } from "../../__mocks__/mockUtils";
import { Constants } from "../../../src/configuration/Constants";
Expand Down Expand Up @@ -145,7 +145,7 @@ describe("ProfilesUtils unit tests", () => {
expect(openConfigForMissingHostnameMock).toHaveBeenCalled();
});

it("should handle error for invalid credentials and prompt for authentication", async () => {
it("should handle error for invalid credentials and prompt for authentication - credentials entered", async () => {
const errorDetails = new imperative.ImperativeError({
msg: "Invalid credentials",
errorCode: 401 as unknown as string,
Expand All @@ -155,7 +155,11 @@ describe("ProfilesUtils unit tests", () => {
const showMessageSpy = jest.spyOn(Gui, "errorMessage").mockImplementation(() => Promise.resolve("Update Credentials"));
const promptCredsSpy = jest.fn();
const ssoLoginSpy = jest.fn();
const profile = { type: "zosmf" } as any;
const profile = { name: "lpar.zosmf", type: "zosmf" } as any;
// disable locking mechanism for this test, will be tested in separate test cases
const lockMock = jest.spyOn(Mutex.prototype, "lock").mockImplementation();

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused variable lockMock.
const unlockMock = jest.spyOn(Mutex.prototype, "unlock").mockImplementation();

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused variable unlockMock.

Object.defineProperty(Constants, "PROFILES_CACHE", {
value: {
promptCredentials: promptCredsSpy,
Expand All @@ -174,6 +178,7 @@ describe("ProfilesUtils unit tests", () => {
showMessageSpy.mockClear();
promptCredsSpy.mockClear();
});

it("should handle token error and proceed to login", async () => {
const errorDetails = new imperative.ImperativeError({
msg: "Invalid credentials",
Expand All @@ -197,6 +202,8 @@ describe("ProfilesUtils unit tests", () => {
},
configurable: true,
});
const lockMock = jest.spyOn(Mutex.prototype, "lock").mockImplementation();

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused variable lockMock.
const unlockMock = jest.spyOn(Mutex.prototype, "unlock").mockImplementation();

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused variable unlockMock.
await AuthUtils.errorHandling(errorDetails, { profile, scenario });
expect(showMessageSpy).toHaveBeenCalledTimes(1);
expect(ssoLoginSpy).toHaveBeenCalledTimes(1);
Expand Down
2 changes: 1 addition & 1 deletion packages/zowe-explorer/src/utils/AuthUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class AuthUtils {
(httpErrorCode === imperative.RestConstants.HTTP_STATUS_401 ||
imperativeError.message.includes("All configured authentication methods failed"))
) {
await AuthHandler.lockProfile(profile, imperativeError, {
return await AuthHandler.lockProfile(profile, imperativeError, {
ssoLogin: Constants.PROFILES_CACHE.ssoLogin.bind(Constants.PROFILES_CACHE),
promptCredentials: Constants.PROFILES_CACHE.promptCredentials.bind(Constants.PROFILES_CACHE),
isUsingTokenAuth: await AuthUtils.isUsingTokenAuth(profile.name),
Expand Down

0 comments on commit 665c4a4

Please sign in to comment.