Skip to content

Commit

Permalink
Merge pull request #2218 from zowe/fix/error-auth-type-none
Browse files Browse the repository at this point in the history
Fix error using session with auth type "none"
  • Loading branch information
t1m0thyj authored Aug 8, 2024
2 parents de6aacc + 0c05bcc commit d846437
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/imperative/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ All notable changes to the Imperative package will be documented in this file.
## Recent Changes

- BugFix: Refactored code to reduce the use of deprecated functions to prepare for upcoming Node.js 22 support. [#2191](https://github.com/zowe/zowe-cli/issues/2191)
- 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`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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." });
}

Expand Down

0 comments on commit d846437

Please sign in to comment.