Skip to content

Commit

Permalink
add unit tests for ProfilesUtils
Browse files Browse the repository at this point in the history
Signed-off-by: Billie Simmons <[email protected]>
  • Loading branch information
JillieBeanSim committed Jul 12, 2023
1 parent 9579f72 commit b309e23
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
}

it("Checking adding of new filter - Theia", async () => {
const globalMocks = await createGlobalMocks();
const globalMocks = createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);

globalMocks.isTheia.mockReturnValue(true);
Expand All @@ -1454,7 +1454,7 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
expect(testTree.mSessionNodes[1].pattern).toEqual("HLQ.PROD1.STUFF");
});
it("Checking cancelled attempt to add a filter - Theia", async () => {
const globalMocks = await createGlobalMocks();
const globalMocks = createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);

globalMocks.isTheia.mockReturnValue(true);
Expand All @@ -1469,7 +1469,7 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
expect(mocked(Gui.showMessage)).toBeCalledWith("You must enter a pattern.");
});
it("Checking usage of existing filter - Theia", async () => {
const globalMocks = await createGlobalMocks();
const globalMocks = createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);

globalMocks.isTheia.mockReturnValue(true);
Expand All @@ -1485,7 +1485,7 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
expect(testTree.mSessionNodes[1].pattern).toEqual("HLQ.PROD1.STUFF");
});
it("Checking cancelling of filter prompt with available filters - Theia", async () => {
const globalMocks = await createGlobalMocks();
const globalMocks = createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);

globalMocks.isTheia.mockReturnValue(true);
Expand All @@ -1500,7 +1500,7 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
expect(mocked(Gui.showMessage)).toBeCalledWith("No selection made. Operation cancelled.");
});
it("Checking function on favorites", async () => {
const globalMocks = await createGlobalMocks();
const globalMocks = createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);

mocked(vscode.window.createTreeView).mockReturnValueOnce(blockMocks.treeView);
Expand All @@ -1523,7 +1523,7 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
expect(addSessionSpy).toHaveBeenLastCalledWith(blockMocks.datasetSessionNode.label.trim());
});
it("Checking adding of new filter", async () => {
const globalMocks = await createGlobalMocks();
const globalMocks = createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);

mocked(vscode.window.showQuickPick).mockResolvedValueOnce(new utils.FilterDescriptor("\uFF0B " + "Create a new filter"));
Expand All @@ -1538,7 +1538,7 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
expect(testTree.mSessionNodes[1].pattern).toEqual("HLQ.PROD1.STUFF");
});
it("Checking adding of new filter with data set member", async () => {
const globalMocks = await createGlobalMocks();
const globalMocks = createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);

mocked(vscode.window.showQuickPick).mockResolvedValueOnce(new utils.FilterDescriptor("\uFF0B " + "Create a new filter"));
Expand All @@ -1551,7 +1551,7 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
expect(testTree.mSessionNodes[1].pattern).toEqual("HLQ.PROD1");
});
it("Checking adding of new filter with Unverified profile", async () => {
const globalMocks = await createGlobalMocks();
const globalMocks = createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);
Object.defineProperty(Profiles, "getInstance", {
value: jest.fn(() => {
Expand Down Expand Up @@ -1579,7 +1579,7 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
expect(testTree.mSessionNodes[1].pattern).toEqual("HLQ.PROD1.STUFF");
});
it("Checking cancelled attempt to add a filter", async () => {
const globalMocks = await createGlobalMocks();
const globalMocks = createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);

mocked(vscode.window.showQuickPick).mockResolvedValueOnce(new utils.FilterDescriptor("\uFF0B " + "Create a new filter"));
Expand All @@ -1593,7 +1593,7 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
expect(mocked(Gui.showMessage)).toBeCalledWith("You must enter a pattern.");
});
it("Checking usage of existing filter", async () => {
const globalMocks = await createGlobalMocks();
const globalMocks = createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);

const quickPickItem = new utils.FilterDescriptor("HLQ.PROD1.STUFF");
Expand All @@ -1614,7 +1614,7 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
expect(testTree.mSessionNodes[1].pattern).toEqual("HLQ.PROD1.STUFF");
});
it("Checking cancelling of filter prompt with available filters", async () => {
const globalMocks = await createGlobalMocks();
const globalMocks = createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);

const quickPickItem = undefined;
Expand All @@ -1632,7 +1632,7 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
expect(mocked(Gui.showMessage)).toBeCalledWith("No selection made. Operation cancelled.");
});
it("Checking adding of new filter error is caught on getChildren", async () => {
const globalMocks = await createGlobalMocks();
const globalMocks = createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);

mocked(vscode.window.showQuickPick).mockResolvedValueOnce(new utils.FilterDescriptor("\uFF0B " + "Create a new filter"));
Expand Down Expand Up @@ -1718,7 +1718,7 @@ describe("Dataset Tree Unit Tests - Function editSession", () => {
}

it("Checking common run of function", async () => {
const globalMocks = await createGlobalMocks();
createGlobalMocks();
const blockMocks = await createBlockMocks();

mocked(vscode.window.createTreeView).mockReturnValueOnce(blockMocks.treeView);
Expand Down
5 changes: 4 additions & 1 deletion packages/zowe-explorer/__tests__/__unit__/utils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ describe("Utils Unit Tests - Function errorHandling", () => {

mocked(Profiles.getInstance).mockReturnValue(blockMocks.profile);
mocked(vscode.window.showErrorMessage).mockResolvedValueOnce({ title: "Update Credentials" });
mocked(utils.isTheia).mockReturnValue(true);
Object.defineProperty(globals, "ISTHEIA", {
value: true,
configurable: true,
});
const errorDetails = new imperative.ImperativeError({
msg: "Invalid credentials",
errorCode: 401 as unknown as string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,18 @@ describe("ProfilesUtils unit tests", () => {
expect(spyOpenConfigFile).toBeCalledTimes(1);
});

it("should handle error and prompt for authentication", async () => {
it("should handle token error and prompt for authentication", 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);
Object.defineProperty(globals, "ISTHEIA", {
value: false,
configurable: true,
});
const showMessageSpy = jest.spyOn(Gui, "showMessage").mockResolvedValue("selection");
const ssoLoginSpy = jest.fn();
Object.defineProperty(Profiles, "getInstance", {
Expand All @@ -137,6 +140,114 @@ describe("ProfilesUtils unit tests", () => {
await profUtils.errorHandling(errorDetails, label, moreInfo);
expect(showMessageSpy).toBeCalledTimes(1);
expect(ssoLoginSpy).toBeCalledTimes(1);
showMessageSpy.mockClear();
ssoLoginSpy.mockClear();
});
it("should handle token error and procede to login - Theia", 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";
Object.defineProperty(globals, "ISTHEIA", {
value: true,
configurable: true,
});
const showErrorSpy = jest.spyOn(Gui, "errorMessage").mockResolvedValue(undefined);
const showMessageSpy = jest.spyOn(Gui, "showMessage");
const ssoLoginSpy = jest.fn();
Object.defineProperty(Profiles, "getInstance", {
value: () => ({
ssoLogin: ssoLoginSpy,
}),
});
await profUtils.errorHandling(errorDetails, label, moreInfo);
expect(showErrorSpy).toBeCalledTimes(1);
expect(ssoLoginSpy).toBeCalledTimes(1);
expect(showMessageSpy).not.toBeCalled();
showErrorSpy.mockClear();
showMessageSpy.mockClear();
ssoLoginSpy.mockClear();
});
it("should handle credential error and prompt for authentication", async () => {
const errorDetails = new zowe.imperative.ImperativeError({
msg: "Invalid credentials",
errorCode: 401 as unknown as string,
additionalDetails: "Authentication failed.",
});
const label = "test";
const moreInfo = "Task failed successfully";
Object.defineProperty(globals, "ISTHEIA", {
value: false,
configurable: true,
});
const showErrorSpy = jest.spyOn(Gui, "errorMessage").mockResolvedValue("selection");
const promptCredentialsSpy = jest.fn();
Object.defineProperty(Profiles, "getInstance", {
value: () => ({
promptCredentials: promptCredentialsSpy,
}),
});
await profUtils.errorHandling(errorDetails, label, moreInfo);
expect(showErrorSpy).toBeCalledTimes(1);
expect(promptCredentialsSpy).toBeCalledTimes(1);
showErrorSpy.mockClear();
promptCredentialsSpy.mockClear();
});
it("should handle credential error and no selection made for update", async () => {
const errorDetails = new zowe.imperative.ImperativeError({
msg: "Invalid credentials",
errorCode: 401 as unknown as string,
additionalDetails: "Authentication failed.",
});
const label = "test";
const moreInfo = "Task failed successfully";
Object.defineProperty(globals, "ISTHEIA", {
value: false,
configurable: true,
});
const showErrorSpy = jest.spyOn(Gui, "errorMessage").mockResolvedValue(undefined);
const showMsgSpy = jest.spyOn(Gui, "showMessage");
const promptCredentialsSpy = jest.fn();
Object.defineProperty(Profiles, "getInstance", {
value: () => ({
promptCredentials: promptCredentialsSpy,
}),
});
await profUtils.errorHandling(errorDetails, label, moreInfo);
expect(showErrorSpy).toBeCalledTimes(1);
expect(promptCredentialsSpy).not.toBeCalled();
expect(showMsgSpy).toBeCalledWith("Operation Cancelled");
showErrorSpy.mockClear();
showMsgSpy.mockClear();
promptCredentialsSpy.mockClear();
});
it("should handle credential error with error message - Theia", async () => {
const errorDetails = new zowe.imperative.ImperativeError({
msg: "Invalid credentials",
errorCode: 401 as unknown as string,
additionalDetails: "Authentication failed.",
});
const label = "test";
const moreInfo = "Task failed successfully";
Object.defineProperty(globals, "ISTHEIA", {
value: true,
configurable: true,
});
const showErrorSpy = jest.spyOn(Gui, "errorMessage");
const promptCredentialsSpy = jest.fn();
Object.defineProperty(Profiles, "getInstance", {
value: () => ({
promptCredentials: promptCredentialsSpy,
}),
});
await profUtils.errorHandling(errorDetails, label, moreInfo);
expect(showErrorSpy).toBeCalledTimes(1);
expect(promptCredentialsSpy).not.toBeCalled();
showErrorSpy.mockClear();
promptCredentialsSpy.mockClear();
});
});

Expand Down
58 changes: 20 additions & 38 deletions packages/zowe-explorer/src/utils/ProfilesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,18 @@ export async function handleInvalidToken(label: string): Promise<void> {
"errorHandling.invalid.token",
"Your connection is no longer active. Please log in to an authentication service to restore the connection."
);
if (isTheia()) {
if (globals.ISTHEIA) {
Gui.errorMessage(tokenErrMsg).then(async () => {
await Profiles.getInstance().ssoLogin(null, label);
});
} else {
const message = localize("errorHandling.authentication.login", "Log in to Authentication Service");
await Gui.showMessage(tokenErrMsg, { items: [message] }).then(async (selection) => {
if (selection) {
await Profiles.getInstance().ssoLogin(null, label);
}
});
return;
}
const message = localize("errorHandling.authentication.login", "Log in to Authentication Service");
await Gui.showMessage(tokenErrMsg, { items: [message] }).then(async (selection) => {
if (selection) {
await Profiles.getInstance().ssoLogin(null, label);
}
});
}

export async function handleInvalidCredentials(label: string): Promise<void> {
Expand All @@ -95,27 +95,20 @@ export async function handleInvalidCredentials(label: string): Promise<void> {
"Invalid Credentials. Please ensure the username and password for {0} are valid or this may lead to a lock-out.",
label
);
if (isTheia()) {
if (globals.ISTHEIA) {
Gui.errorMessage(genErrMsg);
} else {
const updateCredsButton = localize("errorHandling.updateCredentials.button", "Update Credentials");
const choice = await Gui.errorMessage(genErrMsg, {
items: [updateCredsButton],
vsCodeOpts: { modal: true },
});
if (!choice) {
Gui.showMessage(localize("errorHandling.checkCredentials.cancelled", "Operation Cancelled"));
return;
}
await Profiles.getInstance().promptCredentials(label.trim(), true);
// .then(async (selection) => {
// if (selection === updateCredsButton) {

// } else {

// }
// });
return;
}
const updateCredsButton = localize("errorHandling.updateCredentials.button", "Update Credentials");
const choice = await Gui.errorMessage(genErrMsg, {
items: [updateCredsButton],
vsCodeOpts: { modal: true },
});
if (!choice) {
Gui.showMessage(localize("errorHandling.checkCredentials.cancelled", "Operation Cancelled"));
return;
}
await Profiles.getInstance().promptCredentials(label.trim(), true);
}

export async function handleNoHostname(label: string): Promise<void> {
Expand All @@ -132,17 +125,6 @@ export async function handleNoHostname(label: string): Promise<void> {
}
}

// 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

0 comments on commit b309e23

Please sign in to comment.