Skip to content

Commit

Permalink
wip: patch coverage for AuthHandler, DeferredPromise
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 5ca4607 commit 410755c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { Mutex } from "async-mutex";
import { AuthHandler } from "../../../src";
import { FileManagement } from "../../../src/utils/FileManagement";
import { ImperativeError } from "@zowe/imperative";

const TEST_PROFILE_NAME = "lpar.zosmf";

Expand All @@ -25,6 +26,10 @@ describe("AuthHandler.isProfileLocked", () => {
it("returns false if the profile is not locked", async () => {
expect(AuthHandler.isProfileLocked(TEST_PROFILE_NAME)).toBe(false);
});

it("returns false if no mutex is present for the given profile", async () => {
expect(AuthHandler.isProfileLocked("unused_lpar.zosmf")).toBe(false);
});
});

describe("AuthHandler.lockProfile", () => {
Expand All @@ -35,6 +40,22 @@ describe("AuthHandler.lockProfile", () => {
AuthHandler.unlockProfile(TEST_PROFILE_NAME);
});

it("handle promptForAuthentication call if error and options are given", async () => {
const promptForAuthenticationMock = jest.spyOn(AuthHandler, "promptForAuthentication").mockResolvedValueOnce(true);
const impError = new ImperativeError({ msg: "Example auth error" });
const promptOpts = {
promptCredentials: jest.fn(),
ssoLogin: jest.fn(),
};
const releaseSpy = jest.spyOn(Mutex.prototype, "release");
const result = await AuthHandler.lockProfile(TEST_PROFILE_NAME, impError, promptOpts);
expect(result).toBe(true);
expect(promptForAuthenticationMock).toHaveBeenCalledTimes(1);
expect(promptForAuthenticationMock).toHaveBeenCalledWith(impError, TEST_PROFILE_NAME, promptOpts);
expect(releaseSpy).toHaveBeenCalledTimes(1);
AuthHandler.unlockProfile(TEST_PROFILE_NAME);
});

it("reuses the same Mutex for the profile if it already exists", async () => {
await AuthHandler.lockProfile(TEST_PROFILE_NAME);
expect((AuthHandler as any).lockedProfiles.has(TEST_PROFILE_NAME)).toBe(true);
Expand All @@ -57,6 +78,21 @@ describe("AuthHandler.unlockProfile", () => {
expect((AuthHandler as any).lockedProfiles.get(TEST_PROFILE_NAME)!.isLocked()).toBe(false);
});

it("does nothing if there is no mutex in the profile map", async () => {
const releaseSpy = jest.spyOn(Mutex.prototype, "release").mockClear();
AuthHandler.unlockProfile("unused_lpar.zosmf");
expect(releaseSpy).not.toHaveBeenCalled();
});

it("does nothing if the mutex in the map is not locked", async () => {
await AuthHandler.lockProfile(TEST_PROFILE_NAME);
AuthHandler.unlockProfile(TEST_PROFILE_NAME);

const releaseSpy = jest.spyOn(Mutex.prototype, "release").mockClear();
AuthHandler.unlockProfile(TEST_PROFILE_NAME);
expect(releaseSpy).not.toHaveBeenCalled();
});

it("reuses the same Mutex for the profile if it already exists", async () => {
await AuthHandler.lockProfile(TEST_PROFILE_NAME);
AuthHandler.unlockProfile(TEST_PROFILE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,17 @@ describe("DeferredPromise.status", () => {
deferred.resolve(null);
expect(deferred.status).toBe("fulfilled");
});

it("returns rejected when rejected", async () => {
const deferred = new DeferredPromise();
let errorCaught = false;
setImmediate(() => deferred.reject());
try {
await deferred.promise;
} catch (err) {
errorCaught = true;
}
expect(deferred.status).toBe("rejected");
expect(errorCaught).toBe(true);
});
});
4 changes: 1 addition & 3 deletions packages/zowe-explorer-api/src/profiles/AuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ export class AuthHandler {
// Prompt the user to re-authenticate if an error and options were provided
if (imperativeError && opts) {
const credsEntered = await AuthHandler.promptForAuthentication(imperativeError, profile, opts);

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable credsEntered.
if (!credsEntered) {
mutex.release();
}
mutex.release();
// Return `true` as the mutex was still locked successfully.
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/zowe-explorer-api/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
*
*/

import { IFileSystemEntry, ZoweScheme } from "../fs";
import { IZoweTreeNode } from "../tree";
import { window, workspace } from "vscode";
import { workspace } from "vscode";

export * from "./DeferredPromise";
export * from "./ErrorCorrelator";
Expand Down

0 comments on commit 410755c

Please sign in to comment.