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

feat: add support for deleting profiles #689

Merged
merged 21 commits into from
Apr 27, 2020
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
3 changes: 3 additions & 0 deletions __mocks__/@zowe/imperative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export class CliProfileManager {
public getAllProfileNames(){
return ["name1", "name2"];
}
public delete(){
return { name: "profile1", profile: {}, type: "zosmf" };
}
public get configurations() {
return [{
type: "zosmf",
Expand Down
257 changes: 252 additions & 5 deletions __tests__/__unit__/Profiles.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ import * as path from "path";
import * as os from "os";
import * as vscode from "vscode";
import * as child_process from "child_process";
import { Logger, IProfileLoaded, ICommandProfileTypeConfiguration, CliProfileManager } from "@zowe/imperative";
import { ZoweExplorerApiRegister } from "../../src/api/ZoweExplorerApiRegister";
import * as globals from "../../src/globals";
import { Logger, IProfileLoaded, Session } from "@zowe/imperative";
import { Profiles, ValidProfileEnum } from "../../src/Profiles";
import { ZosmfSession } from "@zowe/cli";
import { ZosmfSession, IJob } from "@zowe/cli";
import { ZoweUSSNode } from "../../src/uss/ZoweUSSNode";
import { ZoweDatasetNode } from "../../src/dataset/ZoweDatasetNode";
import { Job } from "../../src/job/ZoweJobNode";
import { IZoweDatasetTreeNode, IZoweUSSTreeNode, IZoweJobTreeNode, IZoweNodeType } from "../../src/api/IZoweTreeNode";
import { IZoweTree } from "../../src/api/IZoweTree";
import { DatasetTree } from "../../src/dataset/DatasetTree";
import { USSTree } from "../../src/uss/USSTree";
import { ZosJobsProvider } from "../../src/job/ZosJobsProvider";

describe("Profile class unit tests", () => {
// Mocking log.debug
Expand All @@ -28,6 +36,53 @@ describe("Profile class unit tests", () => {

const profileOne = { name: "profile1", profile: {}, type: "zosmf" };
const profileTwo = { name: "profile2", profile: {}, type: "zosmf" };
const mockLoadNamedProfile = jest.fn();
const profileThree: IProfileLoaded = {
name: "profile3",
profile: {
user: undefined,
password: undefined
},
type: "zosmf",
message: "",
failNotFound: false
};
mockLoadNamedProfile.mockReturnValue(profileThree);

const session = new Session({
user: "fake",
password: "fake",
hostname: "fake",
protocol: "https",
type: "basic",
});

const iJob: IJob = {
"jobid": "JOB1234",
"jobname": "TESTJOB",
"files-url": "fake/files",
"job-correlator": "correlator",
"phase-name": "PHASE",
"reason-not-running": "",
"step-data": [{
"proc-step-name": "",
"program-name": "",
"step-name": "",
"step-number": 1,
"active": "",
"smfid": ""

}],
"class": "A",
"owner": "USER",
"phase": 0,
"retcode": "",
"status": "ACTIVE",
"subsystem": "SYS",
"type": "JOB",
"url": "fake/url"
};

const inputBox: vscode.InputBox = {
value: "input",
title: null,
Expand Down Expand Up @@ -57,7 +112,7 @@ describe("Profile class unit tests", () => {
const createInputBox = jest.fn();
const showQuickPick = jest.fn();
const showErrorMessage = jest.fn();
const getConfiguration = jest.fn();
const getConfigurationMock = jest.fn();
const createTreeView = jest.fn();
const createBasicZosmfSession = jest.fn();

Expand All @@ -67,9 +122,14 @@ describe("Profile class unit tests", () => {
Object.defineProperty(vscode.window, "createInputBox", { value: createInputBox });
Object.defineProperty(vscode.window, "showQuickPick", { value: showQuickPick });
Object.defineProperty(vscode.window, "createTreeView", {value: createTreeView});
Object.defineProperty(vscode.workspace, "getConfiguration", { value: getConfiguration });
Object.defineProperty(vscode.workspace, "getConfiguration", { value: getConfigurationMock });
Object.defineProperty(vscode, "ConfigurationTarget", { value: getConfigurationMock });
Object.defineProperty(ZosmfSession, "createBasicZosmfSession", { value: createBasicZosmfSession });

const sessTree: IZoweTree<IZoweDatasetTreeNode> = new DatasetTree();
const ussTree: IZoweTree<IZoweUSSTreeNode> = new USSTree();
const jobsTree: IZoweTree<IZoweJobTreeNode> = new ZosJobsProvider();

beforeEach(() => {
mockJSONParse.mockReturnValue({
overrides: {
Expand Down Expand Up @@ -399,6 +459,193 @@ describe("Profile class unit tests", () => {

});

describe("Deleting Profiles", () => {
let profiles: Profiles;
crawr marked this conversation as resolved.
Show resolved Hide resolved
const getRecallMockValue = jest.fn();
const getRecallUSSMockValue = jest.fn();
beforeEach(async () => {
profiles = await Profiles.createInstance(log);
Object.defineProperty(DatasetTree, "getRecall", { value: getRecallMockValue });
Object.defineProperty(USSTree, "getRecall", { value: getRecallUSSMockValue });
Object.defineProperty(Profiles, "getInstance", {
value: jest.fn(() => {
return {
allProfiles: [{name: "profile1"}, {name: "profile2"}, {name: "profile3"}],
defaultProfile: {name: "profile1"},
loadNamedProfile: mockLoadNamedProfile,
promptCredentials: jest.fn(()=> {
return {};
}),
createNewConnection: jest.fn(()=>{
return {};
}),
listProfile: jest.fn(()=>{
return {};
}),
saveProfile: jest.fn(()=>{
return {profile: {}};
}),
validateAndParseUrl: jest.fn(()=>{
return {};
}),
updateProfile: jest.fn(()=>{
return {};
}),
getDeleteProfile: jest.fn(()=>{
return {};
}),
deletePrompt: jest.fn(()=>{
return {};
}),
deleteProf: jest.fn(()=>{
return {};
})
};
})
});
getConfigurationMock.mockReturnValue({
persistence: true,
get: () => {
return {
sessions: ["profile1"],
favorites: ["[profile1]: /u/myFile.txt{textFile"]
};
},
update: jest.fn(()=>{
return {};
})
});
});

afterEach(() => {
showInputBox.mockReset();
showQuickPick.mockReset();
createInputBox.mockReset();
showInformationMessage.mockReset();
showErrorMessage.mockReset();
getConfigurationMock.mockClear();
});

it("should delete profile from command palette", async () => {
showQuickPick.mockResolvedValueOnce("profile1");
showQuickPick.mockResolvedValueOnce("Yes");
await profiles.deleteProfile(sessTree, ussTree, jobsTree);
expect(showInformationMessage.mock.calls.length).toBe(1);
expect(showInformationMessage.mock.calls[0][0]).toBe("Profile profile1 was deleted.");
});

it("should handle missing selection: profile name", async () => {
showQuickPick.mockResolvedValueOnce(undefined);
await profiles.deleteProfile(sessTree, ussTree, jobsTree);
expect(showInformationMessage.mock.calls.length).toBe(1);
expect(showInformationMessage.mock.calls[0][0]).toBe("Operation Cancelled");
});

it("should handle case where user selects No", async () => {
showQuickPick.mockResolvedValueOnce("profile1");
showQuickPick.mockResolvedValueOnce("No");
await profiles.deleteProfile(sessTree, ussTree, jobsTree);
expect(showInformationMessage.mock.calls.length).toBe(1);
expect(showInformationMessage.mock.calls[0][0]).toBe("Operation Cancelled");
});

it("should handle case where there are no profiles to delete", async () => {
Object.defineProperty(Profiles, "getInstance", {
value: jest.fn(() => {
return {
allProfiles: []
};
})
});
profiles.refresh();
await profiles.deleteProfile(sessTree, ussTree, jobsTree);
expect(showInformationMessage.mock.calls.length).toBe(1);
expect(showInformationMessage.mock.calls[0][0]).toBe("No profiles available");
});

it("should delete profile from context menu", async () => {
const dsNode = new ZoweDatasetNode(
"profile3", vscode.TreeItemCollapsibleState.Expanded, null, session, undefined, undefined, profileThree);
dsNode.contextValue = globals.DS_SESSION_CONTEXT;
showQuickPick.mockResolvedValueOnce("Yes");
await profiles.deleteProfile(sessTree, ussTree, jobsTree, dsNode);
expect(showInformationMessage.mock.calls.length).toBe(1);
expect(showInformationMessage.mock.calls[0][0]).toBe("Profile profile3 was deleted.");
});

it("should delete session from Data Set tree", async () => {
const startLength = sessTree.mSessionNodes.length;
const favoriteLength = sessTree.mFavorites.length;
const dsNode = new ZoweDatasetNode(
"profile3", vscode.TreeItemCollapsibleState.Expanded, null, session, undefined, undefined, profileThree);
dsNode.contextValue = globals.DS_SESSION_CONTEXT;
sessTree.mSessionNodes.push(dsNode);
sessTree.addFavorite(dsNode);
showQuickPick.mockResolvedValueOnce("Yes");
await profiles.deleteProfile(sessTree, ussTree, jobsTree, dsNode);
expect(showInformationMessage.mock.calls.length).toBe(1);
expect(showInformationMessage.mock.calls[0][0]).toBe("Profile profile3 was deleted.");
expect(sessTree.mSessionNodes.length).toEqual(startLength);
expect(sessTree.mFavorites.length).toEqual(favoriteLength);
});

it("should delete session from USS tree", async () => {
const startLength = ussTree.mSessionNodes.length;
const favoriteLength = ussTree.mFavorites.length;
const ussNode = new ZoweUSSNode(
"[profile3]: profile3", vscode.TreeItemCollapsibleState.Expanded,
null, session, null, false, profileThree.name, null, profileThree);
ussNode.contextValue = globals.USS_SESSION_CONTEXT;
ussNode.profile = profileThree;
ussTree.addSession("profile3");
ussTree.mSessionNodes.push(ussNode);
ussTree.mFavorites.push(ussNode);
showQuickPick.mockResolvedValueOnce("Yes");
await profiles.deleteProfile(sessTree, ussTree, jobsTree, ussNode);
expect(showInformationMessage.mock.calls.length).toBe(1);
expect(showInformationMessage.mock.calls[0][0]).toBe("Profile profile3 was deleted.");
expect(ussTree.mSessionNodes.length).toEqual(startLength);
expect(ussTree.mFavorites.length).toEqual(favoriteLength);
});

it("should delete session from Jobs tree", async () => {
const startLength = jobsTree.mSessionNodes.length;
const favoriteLength = jobsTree.mFavorites.length;
const jobNode = new Job(
"profile3", vscode.TreeItemCollapsibleState.Expanded, null, session, iJob, profileThree);
jobNode.contextValue = globals.JOBS_SESSION_CONTEXT;
jobsTree.mSessionNodes.push(jobNode);
jobsTree.addFavorite(jobNode);
showQuickPick.mockResolvedValueOnce("Yes");
await profiles.deleteProfile(sessTree, ussTree, jobsTree, jobNode);
expect(showInformationMessage.mock.calls.length).toBe(1);
expect(showInformationMessage.mock.calls[0][0]).toBe("Profile profile3 was deleted.");
expect(jobsTree.mSessionNodes.length).toEqual(startLength);
expect(jobsTree.mFavorites.length).toEqual(favoriteLength);
});

it("should test deletion of recall for DS", async () => {
sessTree.addRecall("[profile1]: TEST.DATA");
showQuickPick.mockResolvedValueOnce("profile1");
showQuickPick.mockResolvedValueOnce("Yes");
await profiles.deleteProfile(sessTree, ussTree, jobsTree);
expect(showInformationMessage.mock.calls.length).toBe(1);
expect(showInformationMessage.mock.calls[0][0]).toBe("Profile profile1 was deleted.");
expect(sessTree.getRecall()[0]).toBeUndefined();
});

it("should test deletion of recall for USS", async () => {
ussTree.addRecall("[profile1]: /node1/node2/node3.txt");
showQuickPick.mockResolvedValueOnce("profile1");
showQuickPick.mockResolvedValueOnce("Yes");
await profiles.deleteProfile(sessTree, ussTree, jobsTree);
expect(showInformationMessage.mock.calls.length).toBe(1);
expect(showInformationMessage.mock.calls[0][0]).toBe("Profile profile1 was deleted.");
expect(ussTree.getRecall()[0]).toBeUndefined();
});

});

it("should route through to spawn. Covers conditional test", async () => {
Object.defineProperty(Profiles, "getInstance", {
value: jest.fn(() => {
Expand Down
18 changes: 18 additions & 0 deletions __tests__/__unit__/__snapshots__/extension.unit.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,14 @@ Array [
"group": "inline",
"when": "view == zowe.explorer && viewItem =~ /^(?!.*_fav.*)session.*/",
},
Object {
"command": "zowe.deleteProfile",
"group": "6_modification@4",
"when": "view == zowe.explorer && viewItem =~ /^(?!.*_fav.*)session.*/",
},
Object {
"command": "zowe.removeSession",
"group": "6_modification@3",
"when": "view == zowe.explorer && viewItem =~ /^(?!.*_fav.*)session.*/",
},
Object {
Expand Down Expand Up @@ -225,8 +231,14 @@ Array [
"group": "inline",
"when": "viewItem =~ /^ussSession.*/",
},
Object {
"command": "zowe.uss.deleteProfile",
"group": "6_modification@4",
"when": "viewItem =~ /^(?!.*_fav.*)ussSession.*/",
},
Object {
"command": "zowe.uss.removeSession",
"group": "6_modification@3",
"when": "viewItem =~ /^(?!.*_fav.*)ussSession.*/",
},
Object {
Expand Down Expand Up @@ -254,6 +266,11 @@ Array [
"group": "inline",
"when": "view == zowe.jobs && viewItem =~ /^(?!.*_fav.*)server.*/",
},
Object {
"command": "zowe.jobs.deleteProfile",
"group": "6_modification@4",
"when": "view == zowe.jobs && viewItem =~ /^(?!.*_fav.*)server.*/",
},
Object {
"command": "zowe.setOwner",
"when": "view == zowe.jobs && viewItem =~ /^(?!.*_fav.*)server.*/",
Expand All @@ -264,6 +281,7 @@ Array [
},
Object {
"command": "zowe.removeJobsSession",
"group": "6_modification@3",
"when": "view == zowe.jobs && viewItem =~ /^(?!.*_fav.*)server.*/",
},
Object {
Expand Down
8 changes: 6 additions & 2 deletions __tests__/__unit__/extension.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ describe("Extension Unit Tests", () => {
expect(createTreeView.mock.calls[0][0]).toBe("zowe.explorer");
expect(createTreeView.mock.calls[1][0]).toBe("zowe.uss.explorer");
// tslint:disable-next-line: no-magic-numbers
expect(registerCommand.mock.calls.length).toBe(69);
expect(registerCommand.mock.calls.length).toBe(73);
registerCommand.mock.calls.forEach((call, i ) => {
expect(registerCommand.mock.calls[i][1]).toBeInstanceOf(Function);
});
Expand Down Expand Up @@ -762,7 +762,11 @@ describe("Extension Unit Tests", () => {
"zowe.jobs.saveSearch",
"zowe.jobs.removeSearchFavorite",
"zowe.openRecentMember",
"zowe.searchInAllLoadedItems"
"zowe.searchInAllLoadedItems",
"zowe.deleteProfile",
"zowe.cmd.deleteProfile",
"zowe.uss.deleteProfile",
"zowe.jobs.deleteProfile",
];
expect(actualCommands).toEqual(expectedCommands);
expect(onDidSaveTextDocument.mock.calls.length).toBe(1);
Expand Down
Loading