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

Convert the LoginForm to react-router. #6478

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
159 changes: 103 additions & 56 deletions dashboard/src/components/LoginForm/LoginForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { IStoreState } from "shared/types";
import LoginForm from "./LoginForm";
import OAuthLogin from "./OauthLogin";
import TokenLogin from "./TokenLogin";
import actions from "actions";
import * as ReactRedux from "react-redux";

const emptyLocation: Location = {
hash: "",
Expand All @@ -20,14 +22,9 @@ const emptyLocation: Location = {
key: "",
};

const defaultCluster = "default";
const defaultCluster = "default-cluster";

const defaultProps = {
cluster: defaultCluster,
authenticate: jest.fn(),
authenticated: false,
authenticating: false,
authenticationError: undefined,
location: emptyLocation,
checkCookieAuthentication: jest.fn().mockReturnValue({
then: jest.fn(f => f()),
Expand All @@ -38,15 +35,27 @@ const defaultProps = {
authProxySkipLoginPage: false,
};

let spyOnUseDispatch: jest.SpyInstance;
beforeEach(() => {
const mockDispatch = jest.fn(res => res);
spyOnUseDispatch = jest.spyOn(ReactRedux, "useDispatch").mockReturnValue(mockDispatch);
});
afterEach(() => {
spyOnUseDispatch.mockRestore();
jest.restoreAllMocks();
});

const authenticationError = "it's a trap";

describe("while authenticating", () => {
it("behaves like a loading component", () => {
const props = {
...defaultProps,
authenticating: true,
const state = {
...defaultStore,
auth: {
authenticating: true,
},
};
const wrapper = mountWrapper(defaultStore, <LoginForm {...props} />);
const wrapper = mountWrapper(getStore(state), <LoginForm {...defaultProps} />);
expect(wrapper.find(LoadingWrapper)).toExist();
expect(wrapper.find(TokenLogin)).not.toExist();
expect(wrapper.find(OAuthLogin)).not.toExist();
Expand All @@ -61,7 +70,13 @@ describe("token login form", () => {
});

it("renders a link to the access control documentation", () => {
const wrapper = mountWrapper(defaultStore, <LoginForm {...defaultProps} />);
const state = {
...defaultStore,
config: {
appVersion: "devel",
},
};
const wrapper = mountWrapper(getStore(state), <LoginForm {...defaultProps} />);
expect(wrapper.find("a").props()).toMatchObject({
href: "/~https://github.com/vmware-tanzu/kubeapps/blob/devel/site/content/docs/latest/howto/access-control.md",
target: "_blank",
Expand All @@ -84,65 +99,79 @@ describe("token login form", () => {

describe("redirect if authenticated", () => {
it("redirects to / if no current location", () => {
const wrapper = mountWrapper(
defaultStore,
<LoginForm {...defaultProps} authenticated={true} />,
);
const state = {
...defaultStore,
auth: {
authenticated: true,
},
};
const wrapper = mountWrapper(getStore(state), <LoginForm {...defaultProps} />);
const redirect = wrapper.find(Redirect);
expect(redirect.props()).toEqual({ to: { pathname: "/" } });
});

it("redirects to previous location", () => {
const location = Object.assign({}, emptyLocation);
location.state = { from: "/test" };
const state = {
...defaultStore,
auth: {
authenticated: true,
},
};
const wrapper = mountWrapper(
defaultStore,
<LoginForm {...defaultProps} authenticated={true} location={location} />,
getStore(state),
<LoginForm {...defaultProps} location={location} />,
);
const redirect = wrapper.find(Redirect);
expect(redirect.props()).toEqual({ to: "/test" });
});
});

it("calls the authenticate handler when the form is submitted", () => {
const authenticate = jest.fn();
const wrapper = mountWrapper(
defaultStore,
<LoginForm {...defaultProps} authenticate={authenticate} />,
);
const authenticate = jest.fn().mockReturnValue({
then: jest.fn(f => f()),
catch: jest.fn(f => f()),
});
actions.auth.authenticate = authenticate;
const wrapper = mountWrapper(defaultStore, <LoginForm {...defaultProps} />);
act(() => {
wrapper.find("input#token").simulate("change", { target: { value: "f00b4r" } });
});
act(() => {
wrapper.find("form").simulate("submit", { preventDefault: jest.fn() });
});
expect(authenticate).toBeCalledWith(defaultCluster, "f00b4r");
expect(authenticate).toBeCalledWith(defaultCluster, "f00b4r", false);
});

it("calls the authenticate handler if a token is passed as query param", () => {
const authenticate = jest.fn();
const authenticate = jest.fn().mockReturnValue({
then: jest.fn(f => f()),
catch: jest.fn(f => f()),
});
actions.auth.authenticate = authenticate;
mountWrapper(
defaultStore,
<MemoryRouter initialEntries={["/login?token=f00b4r"]}>
<LoginForm {...defaultProps} authenticate={authenticate} />
<LoginForm {...defaultProps} />
</MemoryRouter>,
);
expect(authenticate).toBeCalledWith(defaultCluster, "f00b4r");
expect(authenticate).toBeCalledWith(defaultCluster, "f00b4r", false);
});

it("calls the authenticate handler just once if a failed token is passed as query param", () => {
const authenticate = jest.fn();
const authenticate = jest.fn().mockReturnValue({
then: jest.fn(f => f()),
catch: jest.fn(f => f()),
});
actions.auth.authenticate = authenticate;
mountWrapper(
defaultStore,
<MemoryRouter initialEntries={["/login?token=bad-token"]}>
<LoginForm
{...defaultProps}
authenticate={authenticate}
authenticationError={authenticationError}
/>
<LoginForm {...defaultProps} />
</MemoryRouter>,
);
expect(authenticate).toBeCalledWith(defaultCluster, "bad-token");
expect(authenticate).toBeCalledWith(defaultCluster, "bad-token", false);
expect(authenticate).toBeCalledTimes(1);
});

Expand All @@ -151,17 +180,20 @@ describe("token login form", () => {
mountWrapper(
defaultStore,
<MemoryRouter initialEntries={["/login?token=f00b4r"]}>
<LoginForm {...defaultProps} authenticate={authenticate} oauthLoginURI={"/sign/in"} />
<LoginForm {...defaultProps} />
</MemoryRouter>,
);
expect(authenticate).not.toBeCalled();
});

it("displays an error if the authentication error is passed", () => {
const wrapper = mountWrapper(
defaultStore,
<LoginForm {...defaultProps} authenticationError={authenticationError} />,
);
const state = {
...defaultStore,
auth: {
authenticationError,
},
};
const wrapper = mountWrapper(getStore(state), <LoginForm {...defaultProps} />);

expect(wrapper.find(".error").exists()).toBe(true);
});
Expand All @@ -179,30 +211,39 @@ describe("token login form", () => {
});

describe("oauth login form", () => {
const props = {
...defaultProps,
oauthLoginURI: "/sign/in",
};
it("does not display the token login if oauthLoginURI provided", () => {
const wrapper = mountWrapper(defaultStore, <LoginForm {...props} />);
const state = {
...defaultStore,
config: {
oauthLoginURI: "/sign/in",
} as IConfigState,
};
const wrapper = mountWrapper(getStore(state), <LoginForm {...defaultProps} />);

expect(wrapper.find("input#token").exists()).toBe(false);
});

it("displays the oauth login if authProxyEnabled", () => {
const checkCookieAuthentication = jest.fn().mockReturnValue({
then: jest.fn(f => f()),
catch: jest.fn(f => f()),
});
actions.auth.checkCookieAuthentication = checkCookieAuthentication;

const state = {
...defaultStore,
config: {
authProxyEnabled: true,
oauthLoginURI: "/sign/in",
} as IConfigState,
};
const wrapper = mountWrapper(
getStore({ ...state } as Partial<IStoreState>),
<LoginForm {...props} />,
<LoginForm {...defaultProps} />,
);
expect(props.checkCookieAuthentication).toHaveBeenCalled();
expect(checkCookieAuthentication).toHaveBeenCalled();
expect(wrapper.find(OAuthLogin)).toExist();
expect(wrapper.find("a").findWhere(a => a.prop("href") === props.oauthLoginURI)).toExist();
expect(wrapper.find("a").findWhere(a => a.prop("href") === "/sign/in")).toExist();
});

it("doesn't render the login form if the cookie has not been checked yet", () => {
Expand All @@ -212,16 +253,15 @@ describe("oauth login form", () => {
authProxyEnabled: true,
} as IConfigState,
};
const props2 = {
...props,
checkCookieAuthentication: jest.fn().mockReturnValue({
then: jest.fn(() => false),
catch: jest.fn(() => false),
}),
};
const checkCookieAuthentication = jest.fn().mockReturnValue({
then: jest.fn(() => false),
catch: jest.fn(() => false),
});
actions.auth.checkCookieAuthentication = checkCookieAuthentication;

const wrapper = mountWrapper(
getStore({ ...state } as Partial<IStoreState>),
<LoginForm {...props2} />,
<LoginForm {...defaultProps} />,
);
expect(wrapper.find(LoadingWrapper)).toExist();
expect(wrapper.find(OAuthLogin)).not.toExist();
Expand All @@ -235,7 +275,14 @@ describe("oauth login form", () => {
writable: true,
value: { replace: jest.fn() },
});
mountWrapper(defaultStore, <LoginForm {...props} authProxySkipLoginPage={true} />);
expect(window.location.replace).toHaveBeenCalledWith(props.oauthLoginURI);
const state = {
...defaultStore,
config: {
authProxySkipLoginPage: true,
oauthLoginURI: "/sign/in",
},
};
mountWrapper(getStore(state), <LoginForm {...defaultProps} />);
expect(window.location.replace).toHaveBeenCalledWith("/sign/in");
});
});
Loading