Skip to content

Commit

Permalink
fix(error): Check if session is valid before calling proxy setup fn (
Browse files Browse the repository at this point in the history
…#3428)

* fix: check for valid session before calling proxy setup fn

Signed-off-by: Trae Yelovich <trae.yelovich@broadcom.com>

* tests: CommonApi.getSession, reuse fake profile in other test

Signed-off-by: Trae Yelovich <trae.yelovich@broadcom.com>

* chore: update ZE changelog

Signed-off-by: Trae Yelovich <trae.yelovich@broadcom.com>

* fix: correct expected value in createZoweSchema test

Signed-off-by: Trae Yelovich <trae.yelovich@broadcom.com>

* lint: resolve lint error in test code

Signed-off-by: Trae Yelovich <trae.yelovich@broadcom.com>

---------

Signed-off-by: Trae Yelovich <trae.yelovich@broadcom.com>
  • Loading branch information
traeok authored Feb 3, 2025
1 parent 5606bfc commit 55bb5fa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
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

0 comments on commit 55bb5fa

Please sign in to comment.