From a2106eacf27db58295fa3642169395bd79f9b3e4 Mon Sep 17 00:00:00 2001 From: Timothy Johnson Date: Tue, 6 Aug 2024 16:45:35 -0400 Subject: [PATCH 1/2] Fix error using session with auth type "none" Signed-off-by: Timothy Johnson --- .../client/AbstractRestClient.unit.test.ts | 54 +++++++++++++++++-- .../src/rest/src/client/AbstractRestClient.ts | 2 +- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/packages/imperative/src/rest/__tests__/client/AbstractRestClient.unit.test.ts b/packages/imperative/src/rest/__tests__/client/AbstractRestClient.unit.test.ts index 3e480ec45b..b898a3d0ae 100644 --- a/packages/imperative/src/rest/__tests__/client/AbstractRestClient.unit.test.ts +++ b/packages/imperative/src/rest/__tests__/client/AbstractRestClient.unit.test.ts @@ -14,7 +14,7 @@ import * as https from "https"; import * as http from "http"; import { Session } from "../../src/session/Session"; import { - AUTH_TYPE_BASIC, AUTH_TYPE_BEARER, AUTH_TYPE_CERT_PEM, AUTH_TYPE_TOKEN + AUTH_TYPE_BASIC, AUTH_TYPE_BEARER, AUTH_TYPE_CERT_PEM, AUTH_TYPE_NONE, AUTH_TYPE_TOKEN } from "../../src/session/SessConstants"; import { RestClient } from "../../src/client/RestClient"; import { Headers } from "../../src/client/Headers"; @@ -83,15 +83,19 @@ describe("AbstractRestClient tests", () => { expect(error.message).toMatchSnapshot(); }); - it("should throw an error when when no creds are in the session", async () => { + it("should throw an error when session type is basic and no creds are in the session", async () => { // restore setPasswordAuth spy to its original implementation setPasswordAuthSpy.mockRestore(); let caughtError; try { - await RestClient.getExpectString(new Session({ - hostname: "test" - }), "/resource"); + const sessWithoutCreds = new Session({ + hostname: "test", + type: AUTH_TYPE_BASIC, + base64EncodedAuth: "FakeBase64EncodedCred" + }); + delete sessWithoutCreds.ISession.base64EncodedAuth; + await RestClient.getExpectString(sessWithoutCreds, "/resource"); } catch (error) { caughtError = error; } @@ -100,6 +104,46 @@ describe("AbstractRestClient tests", () => { expect(caughtError.message).toContain("No credentials for a BASIC or TOKEN type of session"); }); + it("should not error when session type is none and no creds are in the session", async () => { + const emitter = new MockHttpRequestResponse(); + const requestFnc = jest.fn((options, callback) => { + ProcessUtils.nextTick(async () => { + + const newEmit = new MockHttpRequestResponse(); + callback(newEmit); + + await ProcessUtils.nextTick(() => { + newEmit.emit("data", Buffer.from("{\"newData\":", "utf8")); + }); + + await ProcessUtils.nextTick(() => { + newEmit.emit("data", Buffer.from("\"response data\"}", "utf8")); + }); + + await ProcessUtils.nextTick(() => { + newEmit.emit("end"); + }); + }); + + return emitter; + }); + + (https.request as any) = requestFnc; + + let caughtError; + try { + await RestClient.getExpectString(new Session({ + hostname: "test", + type: AUTH_TYPE_NONE + }), "/resource"); + } catch (error) { + caughtError = error; + } + + expect(caughtError).toBeUndefined(); + expect(requestFnc).toHaveBeenCalledTimes(1); + }); + it("should not error when chunking data and payload data are present in outgoing request", async () => { interface IPayload { diff --git a/packages/imperative/src/rest/src/client/AbstractRestClient.ts b/packages/imperative/src/rest/src/client/AbstractRestClient.ts index b3d37dd2c2..fa1e663d15 100644 --- a/packages/imperative/src/rest/src/client/AbstractRestClient.ts +++ b/packages/imperative/src/rest/src/client/AbstractRestClient.ts @@ -538,7 +538,7 @@ export abstract class AbstractRestClient { /* There is probably a better way report this kind of problem and a better message, * but we do it this way to maintain backward compatibility. */ - if (!credsAreSet) { + if (!credsAreSet && this.session.ISession.type !== SessConstants.AUTH_TYPE_NONE) { throw new ImperativeError({ msg: "No credentials for a BASIC or TOKEN type of session." }); } From 11a1830d52c2fc9516f49cea95dd1aa45e4274eb Mon Sep 17 00:00:00 2001 From: Timothy Johnson Date: Wed, 7 Aug 2024 10:06:32 -0400 Subject: [PATCH 2/2] Update changelog Signed-off-by: Timothy Johnson --- packages/imperative/CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/imperative/CHANGELOG.md b/packages/imperative/CHANGELOG.md index 72e995d43c..a5f31230bd 100644 --- a/packages/imperative/CHANGELOG.md +++ b/packages/imperative/CHANGELOG.md @@ -2,9 +2,13 @@ All notable changes to the Imperative package will be documented in this file. +## Recent Changes + +- BugFix: Fixed error in REST client when making requests with session type of `SessConstants.AUTH_TYPE_NONE`. [#2219](https://github.com/zowe/zowe-cli/issues/2219) + ## `5.26.1` -- Bugfix: Export new Proxy class from Zowe imperative package. [#2205](https://github.com/zowe/zowe-cli/pull/2205) +- BugFix: Fixed missing export for `Proxy` class in Imperative package. [#2205](https://github.com/zowe/zowe-cli/pull/2205) ## `5.26.0`