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 using session with auth type "none" #2218

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion packages/imperative/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

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
Loading