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(error): Check if session is valid before calling proxy setup fn #3428

Merged
merged 8 commits into from
Feb 3, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,43 @@ async function expectApiWithSession<T>({ name, spy, args, transform }: ITestApi<
expect(spy).toHaveBeenCalledWith(fakeSession, ...params);
}

describe("CommonApi", () => {
describe("getSession", () => {
it("returns undefined and does not throw error if the session was not created", () => {
const api = new ZoweExplorerZosmf.CommonApi();
let session: imperative.Session;
expect(() => (session = api.getSession())).not.toThrow();
expect(session).toBe(undefined);
});
it("returns a built session from _getSession and does not throw error", () => {
const api = new ZoweExplorerZosmf.CommonApi(loadedProfile);
let session: imperative.Session;
const _getSessionSpy = jest.spyOn(api as any, "_getSession");
expect(() => (session = api.getSession())).not.toThrow();
expect(_getSessionSpy).toHaveBeenCalledTimes(1);
expect(session).not.toBeUndefined();
});
});
});

describe("ZosmfUssApi", () => {
afterEach(() => {
jest.clearAllMocks();
});

describe("_getSession", () => {
const exampleProfile = {
message: "",
type: "zosmf",
failNotFound: false,
name: "test.zosmf",
profile: {
host: "localhost",
password: "password",
protocol: "http",
user: "aZosmfUser",
...fakeProperties,
},
} as imperative.IProfileLoaded;
const exampleSession = imperative.Session.createFromUrl(new URL("http://localhost"));
exampleSession.ISession.password = exampleProfile.profile?.password;
exampleSession.ISession.user = exampleProfile.profile?.user;
jest.spyOn(ProfilesCache, "getProfileSessionWithVscProxy").mockReturnValueOnce(exampleSession as any);
const exampleSession = Object.assign(Object.create(Object.getPrototypeOf(fakeSession)), fakeSession);
exampleSession.ISession.password = loadedProfile.profile?.password;
exampleSession.ISession.user = loadedProfile.profile?.user;
jest.spyOn(ProfilesCache, "getProfileSessionWithVscProxy").mockReturnValueOnce(exampleSession);

it("should include profile properties in the built session object", () => {
const api = new ZoweExplorerZosmf.UssApi(loadedProfile);

const transformedProps: Record<string, any> = { ...exampleProfile.profile, hostname: exampleProfile.profile?.host, ...fakeProperties };
const transformedProps: Record<string, any> = { ...loadedProfile.profile, hostname: loadedProfile.profile?.host, ...fakeProperties };
delete transformedProps["host"];
delete transformedProps["responseTimeout"];
expect((api as any)._getSession(exampleProfile).mISession).toMatchObject(transformedProps);
expect((api as any)._getSession(loadedProfile).mISession).toMatchObject(transformedProps);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export namespace ZoweExplorerZosmf {
/**
* An implementation of the Zowe Explorer API Common interface for zOSMF.
*/
class CommonApi implements MainframeInteraction.ICommon {
export class CommonApi implements MainframeInteraction.ICommon {
public static getProfileTypeName(): string {
return zosmf.ZosmfProfile.type;
}
Expand All @@ -55,7 +55,7 @@ export namespace ZoweExplorerZosmf {
// todo: initialize and use logging
imperative.Logger.getAppLogger().error(error as string);
}
return ProfilesCache.getProfileSessionWithVscProxy(this.session);
return this.session ? ProfilesCache.getProfileSessionWithVscProxy(this.session) : undefined;
}

private _getSession(serviceProfile: imperative.IProfileLoaded): imperative.Session {
Expand Down
1 change: 1 addition & 0 deletions packages/zowe-explorer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to the "vscode-extension-for-zowe" extension will be documen

### Bug fixes

- Fixed an issue where a TypeError occurred when applying VS Code proxy settings to an invalid session. [#3425](https://github.com/zowe/zowe-explorer-vscode/issues/3425)
- Fixed issue where the 'Delete' key binding for the USS tree returns a 'contextValue' error. [#2796](https://github.com/zowe/zowe-explorer-vscode/issues/2796)

## `3.1.0`
Expand Down