Skip to content

Commit

Permalink
update tests for status component
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivekrajput20 committed Sep 8, 2020
1 parent 6340710 commit be17c96
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 60 deletions.
67 changes: 67 additions & 0 deletions client/components/status/__snapshots__/status.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,72 @@ exports[`<Status /> rendering should render correctly 1`] = `
</div>
</div>
</div>
<React.Fragment>
<form
action="http://localhost/"
className="owisp-auth-hidden"
id="cp-login-form"
method="post"
target="owisp-auth-iframe"
>
<input
name="auth_user"
readOnly={true}
type="text"
value=""
/>
<input
name="auth_pass"
readOnly={true}
type="text"
value=""
/>
<input
name="zone"
readOnly={true}
type="text"
value="zone_name"
/>
<input
name="redirurl"
readOnly={true}
type="text"
value="http://localhost:8080/default/status"
/>
<input
name="accept"
readOnly={true}
type="text"
value="accept"
/>
</form>
<iframe
className="owisp-auth-hidden"
name="owisp-auth-iframe"
onLoad={[Function]}
title="owisp-auth-iframe"
/>
</React.Fragment>
<React.Fragment>
<form
action="http://localhost:8081"
className="owisp-auth-hidden"
id="cp-logout-form"
method="post"
target="owisp-auth-logout-iframe"
>
<input
name="logout_id"
readOnly={true}
type="hidden"
value=""
/>
</form>
<iframe
className="owisp-auth-hidden"
name="owisp-auth-logout-iframe"
title="owisp-auth-iframe"
/>
</React.Fragment>
</React.Fragment>
`;
7 changes: 6 additions & 1 deletion client/components/status/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ export default class Status extends React.Component {
} catch {
//
}
const {sessions} = this.state;
const isValid = await this.validateToken();
if (isValid) {
const macaddr = cookies.get(`${orgSlug}_macaddr`);
if (macaddr) {
await this.getUserRadiusSessions();
/* request to captive portal is made only if there is
no active session from macaddr stored in the cookie */
const {sessions} = this.state;
if (sessions && sessions.length === 0) {
if (this.loginFormRef && this.loginFormRef.current)
this.loginFormRef.current.submit();
Expand Down Expand Up @@ -330,6 +330,11 @@ export default class Status extends React.Component {
}
Status.contextType = LoadingContext;

Status.contextTypes = {
setLoading: PropTypes.func,
getLoading: PropTypes.func,
};

Status.propTypes = {
statusPage: PropTypes.shape({
content: PropTypes.object,
Expand Down
169 changes: 110 additions & 59 deletions client/components/status/status.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import {shallow} from "enzyme";
import React from "react";
import {Cookies} from "react-cookie";
import ShallowRenderer from "react-test-renderer/shallow";

import getConfig from "../../utils/get-config";
import Status from "./status";

jest.mock("axios");

function tick() {
return new Promise(resolve => {
setTimeout(resolve, 0);
});
}

const defaultConfig = getConfig("default");
const createTestProps = props => {
return {
Expand All @@ -19,6 +24,12 @@ const createTestProps = props => {
statusPage: defaultConfig.components.status_page,
cookies: new Cookies(),
logout: jest.fn(),
captivePortalLoginForm: defaultConfig.components.captive_portal_login_form,
captivePortalLogoutForm:
defaultConfig.components.captive_portal_logout_form,
location: {
search: "?macaddr=0.0.0.0",
},
...props,
};
};
Expand All @@ -37,97 +48,137 @@ describe("<Status /> interactions", () => {
let props;
let wrapper;
let originalError;
// eslint-disable-next-line no-unused-vars
let lastConsoleOutuput;
beforeEach(() => {
originalError = console.error;
lastConsoleOutuput = null;
console.error = (data) => {
console.error = data => {
lastConsoleOutuput = data;
};
});
afterEach(() => {
console.error = originalError;
});
it("should call logout function when logout button is clicked", () => {
axios.mockImplementationOnce(() => {
return Promise.resolve({
status: 200,
statusText: 'OK',
data: {
"control:Auth-Type": "Accept",
}
it("should call logout function when logout button is clicked", async () => {
axios
.mockImplementationOnce(() => {
return Promise.resolve({
response: {
status: 200,
statusText: "OK",
},
data: [],
});
})
.mockImplementationOnce(() => {
return Promise.resolve({
status: 200,
statusText: "OK",
data: [{session_id: 1}],
});
});
});
props = createTestProps();
wrapper = shallow(<Status {...props} />);
wrapper = shallow(<Status {...props} />, {
context: {setLoading: jest.fn()},
disableLifecycleMethods: true,
});
jest.spyOn(wrapper.instance(), "handleLogout");
wrapper.find("#owisp-status-logout-btn").simulate("click", {});
expect(wrapper.instance().props.logout.mock.calls.length).toBe(1);
await tick();
expect(wrapper.instance().props.logout).toHaveBeenCalled();
wrapper.find("#owisp-status-logout-btn").simulate("click", {});
await tick();
expect(wrapper.instance().state.sessions.length).toBe(1);
expect(wrapper.instance().props.logout).toHaveBeenCalled();
});
it("test componentDidMount lifecycle method", () => {

it("test componentDidMount lifecycle method", async () => {
axios
.mockImplementationOnce(() => {
return Promise.reject({
status: 500,
statusText: 'Internal Server Error',
response: {
data: {}
}
return Promise.resolve({
status: 200,
data: {
response_code: "AUTH_TOKEN_VALIDATION_SUCCESSFUL",
},
});
})
.mockImplementationOnce(() => {
return Promise.reject({
status: 504,
statusText: "Gateway Timeout",
response: {
data: {}
},
return Promise.resolve({
status: 200,
statusText: "OK",
data: [{session_id: 1}],
});
})
.mockImplementationOnce(() => {
return Promise.resolve({
status: 200,
data: {
"control:Auth-Type": "Accept",
response_code: "AUTH_TOKEN_VALIDATION_SUCCESSFUL",
},
});
});
jest.spyOn(Status.prototype, "getUserRadiusSessions");

props = createTestProps();
wrapper = shallow(<Status {...props} />, {
context: {setLoading: jest.fn()},
});
await tick();
expect(wrapper.instance().state.sessions.length).toBe(1);
expect(wrapper.instance().props.cookies.get("default_macaddr")).toBe(
"0.0.0.0",
);
expect(Status.prototype.getUserRadiusSessions).toHaveBeenCalled();
wrapper.setProps({
location: {
search: "",
},
cookies: new Cookies(),
});
wrapper.instance().componentDidMount();
expect(wrapper.instance().props.cookies.get("default_macaddr")).toBe(
undefined,
);
Status.prototype.getUserRadiusSessions.mockRestore();
});
it("test getUserRadiusSessions method", async () => {
axios
.mockImplementationOnce(() => {
return Promise.resolve({
status: 200,
statusText: "OK",
data: [],
});
})
.mockImplementationOnce(() => {
return Promise.resolve({
status: 200,
statusText: "OK",
data: [{session_id: 1}],
});
})
.mockImplementationOnce(() => {
return Promise.reject({
response: {
status: 200,
statusText: 'OK',
},
data: {
"control:Auth-Type": "Reject",
status: 401,
},
});
});
props = createTestProps();
wrapper = shallow(<Status {...props} />);
return wrapper
.instance()
.componentDidMount()
.then(() => {
expect(wrapper.instance().props.logout.mock.calls.length).toBe(2);
expect(lastConsoleOutuput).not.toBe(null);
lastConsoleOutuput = null;
return wrapper
.instance()
.componentDidMount()
.then(() => {
expect(wrapper.instance().props.logout.mock.calls.length).toBe(2);
expect(lastConsoleOutuput).toBe(null);
lastConsoleOutuput = null;
return wrapper
.instance()
.componentDidMount()
.then(() => {
expect(wrapper.instance().props.logout.mock.calls.length).toBe(
3,
);
expect(lastConsoleOutuput).not.toBe(null);
lastConsoleOutuput = null;
});
});
});
wrapper = shallow(<Status {...props} />, {
context: {setLoading: jest.fn()},
disableLifecycleMethods: true,
});
jest.spyOn(wrapper.instance(), "getUserRadiusSessions");
wrapper.instance().getUserRadiusSessions();
await tick();
expect(wrapper.instance().state.sessions.length).toBe(0);
wrapper.instance().getUserRadiusSessions();
await tick();
expect(wrapper.instance().state.sessions.length).toBe(1);
wrapper.instance().getUserRadiusSessions();
await tick();
expect(wrapper.instance().props.logout.mock.calls.length).toBe(1);
});
});

0 comments on commit be17c96

Please sign in to comment.