Files
SCM-Manager/scm-ui/src/modules/auth.test.js

292 lines
7.7 KiB
JavaScript
Raw Normal View History

2018-07-09 11:38:13 +02:00
import reducer, {
2018-07-13 10:57:11 +02:00
fetchMeSuccess,
logout,
2018-07-13 10:57:11 +02:00
logoutSuccess,
loginSuccess,
fetchMeUnauthenticated,
LOGIN_SUCCESS,
login,
LOGIN_FAILURE,
LOGOUT_FAILURE,
LOGOUT_SUCCESS,
2018-07-13 10:57:11 +02:00
FETCH_ME_SUCCESS,
fetchMe,
FETCH_ME_FAILURE,
FETCH_ME_UNAUTHORIZED,
isAuthenticated,
LOGIN_PENDING,
FETCH_ME_PENDING,
LOGOUT_PENDING,
getMe,
isFetchMePending,
isLoginPending,
isLogoutPending,
getFetchMeFailure,
LOGIN,
FETCH_ME,
LOGOUT,
getLoginFailure,
getLogoutFailure
} from "./auth";
2018-07-09 11:38:13 +02:00
2018-07-11 22:01:36 +02:00
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
import fetchMock from "fetch-mock";
2018-07-13 10:57:11 +02:00
const me = { name: "tricia", displayName: "Tricia McMillian" };
2018-07-13 10:57:11 +02:00
describe("auth reducer", () => {
2018-07-13 10:57:11 +02:00
it("should set me and login on successful fetch of me", () => {
const state = reducer(undefined, fetchMeSuccess(me));
expect(state.me).toBe(me);
expect(state.authenticated).toBe(true);
2018-07-13 10:57:11 +02:00
});
it("should set authenticated to false", () => {
const initialState = {
authenticated: true,
me
2018-07-13 10:57:11 +02:00
};
const state = reducer(initialState, fetchMeUnauthenticated());
expect(state.me.name).toBeUndefined();
expect(state.authenticated).toBe(false);
2018-07-13 10:57:11 +02:00
});
it("should reset the state after logout", () => {
const initialState = {
authenticated: true,
me
2018-07-13 10:57:11 +02:00
};
const state = reducer(initialState, logoutSuccess());
expect(state.me).toBeUndefined();
expect(state.authenticated).toBeUndefined();
2018-07-13 10:57:11 +02:00
});
it("should set state authenticated and me after login", () => {
const state = reducer(undefined, loginSuccess(me));
expect(state.me).toBe(me);
expect(state.authenticated).toBe(true);
2018-07-13 10:57:11 +02:00
});
});
describe("auth actions", () => {
2018-07-11 22:01:36 +02:00
const mockStore = configureMockStore([thunk]);
2018-07-09 11:38:13 +02:00
2018-07-11 22:01:36 +02:00
afterEach(() => {
fetchMock.reset();
fetchMock.restore();
});
2018-07-13 10:57:11 +02:00
it("should dispatch login success and dispatch fetch me", () => {
2018-10-01 17:22:03 +02:00
fetchMock.postOnce("/api/v2/auth/access_token", {
2018-07-11 22:01:36 +02:00
body: {
cookie: true,
grant_type: "password",
username: "tricia",
password: "secret123"
},
headers: { "content-type": "application/json" }
});
2018-10-01 17:22:03 +02:00
fetchMock.getOnce("/api/v2/me", {
body: me,
2018-07-11 22:01:36 +02:00
headers: { "content-type": "application/json" }
});
2018-10-11 10:26:54 +02:00
fetchMock.getOnce("/api/v2/", {
_links: {
me: {
href: "/me"
}
}
});
2018-07-11 22:01:36 +02:00
const expectedActions = [
{ type: LOGIN_PENDING },
{ type: LOGIN_SUCCESS, payload: me }
2018-07-11 22:01:36 +02:00
];
2018-07-09 11:38:13 +02:00
2018-07-11 22:01:36 +02:00
const store = mockStore({});
2018-10-11 08:30:37 +02:00
return store
.dispatch(login("/auth/access_token", "tricia", "secret123"))
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
2018-07-11 22:01:36 +02:00
});
2018-07-13 10:57:11 +02:00
it("should dispatch login failure", () => {
2018-10-01 17:22:03 +02:00
fetchMock.postOnce("/api/v2/auth/access_token", {
2018-07-11 22:01:36 +02:00
status: 400
});
const store = mockStore({});
2018-10-11 08:30:37 +02:00
return store
.dispatch(login("/auth/access_token", "tricia", "secret123"))
.then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(LOGIN_PENDING);
expect(actions[1].type).toEqual(LOGIN_FAILURE);
expect(actions[1].payload).toBeDefined();
});
2018-07-11 22:01:36 +02:00
});
2018-07-13 10:57:11 +02:00
it("should dispatch fetch me success", () => {
2018-10-01 17:22:03 +02:00
fetchMock.getOnce("/api/v2/me", {
body: me,
2018-07-13 10:57:11 +02:00
headers: { "content-type": "application/json" }
});
const expectedActions = [
{ type: FETCH_ME_PENDING },
2018-07-24 17:05:38 +02:00
{
type: FETCH_ME_SUCCESS,
payload: me
2018-07-24 17:05:38 +02:00
}
2018-07-13 10:57:11 +02:00
];
const store = mockStore({});
2018-10-11 09:54:12 +02:00
return store.dispatch(fetchMe("me")).then(() => {
2018-07-13 10:57:11 +02:00
expect(store.getActions()).toEqual(expectedActions);
});
});
it("should dispatch fetch me failure", () => {
2018-10-01 17:22:03 +02:00
fetchMock.getOnce("/api/v2/me", {
2018-07-13 10:57:11 +02:00
status: 500
});
const store = mockStore({});
2018-10-11 09:54:12 +02:00
return store.dispatch(fetchMe("me")).then(() => {
2018-07-13 10:57:11 +02:00
const actions = store.getActions();
expect(actions[0].type).toEqual(FETCH_ME_PENDING);
2018-07-13 10:57:11 +02:00
expect(actions[1].type).toEqual(FETCH_ME_FAILURE);
expect(actions[1].payload).toBeDefined();
});
});
it("should dispatch fetch me unauthorized", () => {
2018-10-01 17:22:03 +02:00
fetchMock.getOnce("/api/v2/me", {
2018-07-13 10:57:11 +02:00
status: 401
});
const expectedActions = [
{ type: FETCH_ME_PENDING },
{ type: FETCH_ME_UNAUTHORIZED, resetPending: true }
2018-07-13 10:57:11 +02:00
];
const store = mockStore({});
2018-10-11 09:54:12 +02:00
return store.dispatch(fetchMe("me")).then(() => {
2018-07-13 10:57:11 +02:00
// return of async actions
expect(store.getActions()).toEqual(expectedActions);
});
});
it("should dispatch logout success", () => {
2018-10-01 17:22:03 +02:00
fetchMock.deleteOnce("/api/v2/auth/access_token", {
status: 204
});
2018-10-01 17:22:03 +02:00
fetchMock.getOnce("/api/v2/me", {
status: 401
});
const expectedActions = [
{ type: LOGOUT_PENDING },
{ type: LOGOUT_SUCCESS }
];
const store = mockStore({});
2018-10-11 08:27:24 +02:00
return store.dispatch(logout("/auth/access_token")).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
2018-07-13 10:57:11 +02:00
it("should dispatch logout failure", () => {
2018-10-01 17:22:03 +02:00
fetchMock.deleteOnce("/api/v2/auth/access_token", {
status: 500
});
const store = mockStore({});
2018-10-11 08:27:24 +02:00
return store.dispatch(logout("/auth/access_token")).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(LOGOUT_PENDING);
2018-07-13 10:57:11 +02:00
expect(actions[1].type).toEqual(LOGOUT_FAILURE);
expect(actions[1].payload).toBeDefined();
});
});
2018-07-09 11:38:13 +02:00
});
2018-07-13 10:57:11 +02:00
describe("auth selectors", () => {
const error = new Error("yo it failed");
it("should be false, if authenticated is undefined or false", () => {
expect(isAuthenticated({})).toBe(false);
expect(isAuthenticated({ auth: {} })).toBe(false);
expect(isAuthenticated({ auth: { authenticated: false } })).toBe(false);
});
it("should be true, if authenticated is true", () => {
expect(isAuthenticated({ auth: { authenticated: true } })).toBe(true);
});
it("should return me", () => {
expect(getMe({ auth: { me } })).toBe(me);
});
it("should return undefined, if me is not set", () => {
expect(getMe({})).toBeUndefined();
});
it("should return true, if FETCH_ME is pending", () => {
expect(isFetchMePending({ pending: { [FETCH_ME]: true } })).toBe(true);
});
it("should return false, if FETCH_ME is not in pending state", () => {
expect(isFetchMePending({ pending: {} })).toBe(false);
});
it("should return true, if LOGIN is pending", () => {
expect(isLoginPending({ pending: { [LOGIN]: true } })).toBe(true);
});
it("should return false, if LOGIN is not in pending state", () => {
expect(isLoginPending({ pending: {} })).toBe(false);
});
it("should return true, if LOGOUT is pending", () => {
expect(isLogoutPending({ pending: { [LOGOUT]: true } })).toBe(true);
});
it("should return false, if LOGOUT is not in pending state", () => {
expect(isLogoutPending({ pending: {} })).toBe(false);
});
it("should return the error, if failure state is set for FETCH_ME", () => {
expect(getFetchMeFailure({ failure: { [FETCH_ME]: error } })).toBe(error);
});
it("should return unknown, if failure state is not set for FETCH_ME", () => {
expect(getFetchMeFailure({})).toBeUndefined();
});
it("should return the error, if failure state is set for LOGIN", () => {
expect(getLoginFailure({ failure: { [LOGIN]: error } })).toBe(error);
});
it("should return unknown, if failure state is not set for LOGIN", () => {
expect(getLoginFailure({})).toBeUndefined();
});
it("should return the error, if failure state is set for LOGOUT", () => {
expect(getLogoutFailure({ failure: { [LOGOUT]: error } })).toBe(error);
});
it("should return unknown, if failure state is not set for LOGOUT", () => {
expect(getLogoutFailure({})).toBeUndefined();
});
2018-07-09 11:38:13 +02:00
});