Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ProfileUtils.ts): remove redundant isTheia() function in favor of global variable #2636

Merged
merged 4 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions packages/zowe-explorer/__tests__/__unit__/utils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ import { ZoweLogger } from "../../src/utils/LoggerUtils";

function createGlobalMocks() {
const globalMocks = {
isTheia: jest.fn(),
isTheia: jest.fn().mockReturnValue(false),
testProfileLoaded: createValidIProfile(),
mockProfileInstance: null,
mockProfileInfo: createInstanceOfProfileInfo(),
mockProfilesCache: new ProfilesCache(imperative.Logger.getAppLogger()),
};

globalMocks.mockProfileInstance = createInstanceOfProfile(globalMocks.testProfileLoaded);
const isTheia = jest.fn();

Object.defineProperty(vscode.window, "showQuickPick", { value: jest.fn(), configurable: true });
Object.defineProperty(vscode.window, "createQuickPick", { value: jest.fn(), configurable: true });
Expand All @@ -42,10 +41,9 @@ function createGlobalMocks() {
.mockReturnValue(globalMocks.mockProfileInstance),
configurable: true,
});
Object.defineProperty(globals, "ISTHEIA", { get: isTheia, configurable: true });
Object.defineProperty(globals, "ISTHEIA", { get: globalMocks.isTheia, configurable: true });
Object.defineProperty(globals, "LOG", { value: jest.fn(), configurable: true });
Object.defineProperty(globals.LOG, "error", { value: jest.fn(), configurable: true });
Object.defineProperty(utils, "isTheia", { value: jest.fn(), configurable: true });

Object.defineProperty(globalMocks.mockProfilesCache, "getProfileInfo", {
value: jest.fn(() => {
Expand All @@ -55,9 +53,7 @@ function createGlobalMocks() {
Object.defineProperty(ZoweLogger, "error", { value: jest.fn(), configurable: true });
Object.defineProperty(ZoweLogger, "trace", { value: jest.fn(), configurable: true });

return {
isTheia,
};
return globalMocks;
}

// Idea is borrowed from: https://github.com/kulshekhar/ts-jest/blob/master/src/util/testing.ts
Expand Down Expand Up @@ -112,10 +108,11 @@ describe("Utils Unit Tests - Function errorHandling", () => {
});
it("Checking common error handling - Theia", async () => {
const blockMocks = createBlockMocks();
const globalMocks = createGlobalMocks();
globalMocks.isTheia.mockReturnValue(true);

mocked(Profiles.getInstance).mockReturnValue(blockMocks.profile);
mocked(vscode.window.showErrorMessage).mockResolvedValueOnce({ title: "Update Credentials" });
jest.spyOn(utils, "isTheia").mockReturnValue(true);
const errorDetails = new imperative.ImperativeError({
msg: "Invalid credentials",
errorCode: 401 as unknown as string,
Expand All @@ -127,8 +124,7 @@ describe("Utils Unit Tests - Function errorHandling", () => {
// TODO: check why this return two messages?
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith(
`Invalid Credentials for profile '${label}'. Please ensure the username and password are valid or this may lead to a lock-out.`,
{ modal: true },
"Update Credentials"
undefined // covers undefined label in function
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ describe("ProfilesUtils unit tests", () => {
});
const label = "test";
const moreInfo = "Task failed successfully";
jest.spyOn(profUtils, "isTheia").mockReturnValue(false);
const showMessageSpy = jest.spyOn(Gui, "errorMessage").mockImplementation(() => Promise.resolve("Update Credentials"));
const promptCredsSpy = jest.fn();
jest.spyOn(Profiles, "getInstance").mockReturnValue({
Expand All @@ -162,15 +161,14 @@ describe("ProfilesUtils unit tests", () => {
showMessageSpy.mockClear();
promptCredsSpy.mockClear();
});
it("should handle token error and procede to login", async () => {
it("should handle token error and proceed to login", async () => {
const errorDetails = new zowe.imperative.ImperativeError({
msg: "Invalid credentials",
errorCode: 401 as unknown as string,
additionalDetails: "Token is not valid or expired.",
});
const label = "test";
const moreInfo = "Task failed successfully";
jest.spyOn(profUtils, "isTheia").mockReturnValue(false);
const showErrorSpy = jest.spyOn(Gui, "errorMessage");
const showMessageSpy = jest.spyOn(Gui, "showMessage").mockImplementation(() => Promise.resolve("selection"));
const ssoLoginSpy = jest.fn();
Expand All @@ -189,19 +187,16 @@ describe("ProfilesUtils unit tests", () => {
showMessageSpy.mockClear();
ssoLoginSpy.mockClear();
});
it("should handle token error and procede to login - Theia", async () => {
it("should handle token error and proceed to login - Theia", async () => {
const errorDetails = new zowe.imperative.ImperativeError({
msg: "Invalid credentials",
errorCode: String(401),
additionalDetails: "Token is not valid or expired.",
});
const label = "test";
const moreInfo = "Task failed successfully";
Object.defineProperty(vscode, "env", {
value: {
appName: "Theia",
},
configurable: true,
Object.defineProperty(globals, "ISTHEIA", {
value: true,
});
const showErrorSpy = jest.spyOn(Gui, "errorMessage").mockImplementation(() => Promise.resolve(undefined));
const showMessageSpy = jest.spyOn(Gui, "showMessage");
Expand All @@ -220,6 +215,9 @@ describe("ProfilesUtils unit tests", () => {
showErrorSpy.mockClear();
showMessageSpy.mockClear();
ssoLoginSpy.mockClear();
Object.defineProperty(globals, "ISTHEIA", {
value: false,
});
});
it("should handle credential error and no selection made for update", async () => {
const errorDetails = new zowe.imperative.ImperativeError({
Expand Down Expand Up @@ -261,7 +259,6 @@ describe("ProfilesUtils unit tests", () => {
});
const label = "test";
const moreInfo = "Task failed successfully";
jest.spyOn(profUtils, "isTheia").mockReturnValue(true);
const showErrorSpy = jest.spyOn(Gui, "errorMessage");
const promptCredentialsSpy = jest.fn();
jest.spyOn(Profiles, "getInstance").mockReturnValue({
Expand Down
15 changes: 2 additions & 13 deletions packages/zowe-explorer/src/utils/ProfilesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function errorHandling(errorDetails: Error | string, label?: string
const isTokenAuth = await ProfilesUtils.isUsingTokenAuth(label);

if (tokenError.includes("Token is not valid or expired.") || isTokenAuth) {
if (isTheia()) {
if (globals.ISTHEIA) {
Gui.errorMessage(errToken);
await Profiles.getInstance().ssoLogin(null, label);
return;
Expand All @@ -93,7 +93,7 @@ export async function errorHandling(errorDetails: Error | string, label?: string
}
}

if (isTheia()) {
if (globals.ISTHEIA) {
Gui.errorMessage(errMsg);
return;
}
Expand Down Expand Up @@ -121,17 +121,6 @@ export async function errorHandling(errorDetails: Error | string, label?: string
Gui.errorMessage(moreInfo + errorDetails.toString().replace(/\n/g, " | "));
}

// TODO: remove this second occurence
export function isTheia(): boolean {
ZoweLogger.trace("ProfileUtils.isTheia called.");
const VSCODE_APPNAME: string[] = ["Visual Studio Code", "VSCodium"];
const appName = vscode.env.appName;
if (appName && !VSCODE_APPNAME.includes(appName)) {
return true;
}
return false;
}

/**
* Function to update session and profile information in provided node
* @param profiles is data source to find profiles
Expand Down
Loading