2018-07-09 11:38:13 +02:00
|
|
|
// @flow
|
|
|
|
|
import reducer, {
|
2018-07-11 22:01:36 +02:00
|
|
|
login,
|
2018-07-12 11:33:41 +02:00
|
|
|
logout,
|
2018-07-09 11:38:13 +02:00
|
|
|
LOGIN_REQUEST,
|
|
|
|
|
LOGIN_FAILED,
|
2018-07-12 11:33:41 +02:00
|
|
|
LOGIN_SUCCESSFUL,
|
|
|
|
|
LOGOUT_REQUEST,
|
|
|
|
|
LOGOUT_SUCCESSFUL,
|
|
|
|
|
LOGOUT_FAILED
|
|
|
|
|
} from "./auth";
|
2018-07-09 11:38:13 +02:00
|
|
|
|
2018-07-11 22:01:36 +02:00
|
|
|
import { ME_AUTHENTICATED_REQUEST, ME_AUTHENTICATED_SUCCESS } from "./me";
|
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-09 11:38:13 +02:00
|
|
|
|
2018-07-11 22:01:36 +02:00
|
|
|
describe("action tests", () => {
|
|
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("login success", () => {
|
|
|
|
|
fetchMock.postOnce("/scm/api/rest/v2/auth/access_token", {
|
|
|
|
|
body: {
|
|
|
|
|
cookie: true,
|
|
|
|
|
grant_type: "password",
|
|
|
|
|
username: "tricia",
|
|
|
|
|
password: "secret123"
|
|
|
|
|
},
|
|
|
|
|
headers: { "content-type": "application/json" }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fetchMock.getOnce("/scm/api/rest/v2/me", {
|
|
|
|
|
body: {
|
|
|
|
|
username: "tricia"
|
|
|
|
|
},
|
|
|
|
|
headers: { "content-type": "application/json" }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{ type: LOGIN_REQUEST },
|
|
|
|
|
{ type: ME_AUTHENTICATED_REQUEST },
|
|
|
|
|
{ type: LOGIN_SUCCESSFUL }
|
|
|
|
|
];
|
2018-07-09 11:38:13 +02:00
|
|
|
|
2018-07-11 22:01:36 +02:00
|
|
|
const store = mockStore({});
|
|
|
|
|
|
|
|
|
|
return store.dispatch(login("tricia", "secret123")).then(() => {
|
|
|
|
|
expect(store.getActions()).toEqual(expectedActions);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("login failed", () => {
|
|
|
|
|
fetchMock.postOnce("/scm/api/rest/v2/auth/access_token", {
|
|
|
|
|
status: 400
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const expectedActions = [{ type: LOGIN_REQUEST }, { type: LOGIN_FAILED }];
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
return store.dispatch(login("tricia", "secret123")).then(() => {
|
|
|
|
|
const actions = store.getActions();
|
|
|
|
|
expect(actions[0].type).toEqual(LOGIN_REQUEST);
|
|
|
|
|
expect(actions[1].type).toEqual(LOGIN_FAILED);
|
|
|
|
|
expect(actions[1].payload).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-07-12 11:33:41 +02:00
|
|
|
|
|
|
|
|
test("logout success", () => {
|
|
|
|
|
fetchMock.deleteOnce("/scm/api/rest/v2/auth/access_token", {
|
|
|
|
|
status: 204
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fetchMock.getOnce("/scm/api/rest/v2/me", {
|
|
|
|
|
status: 401
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{ type: LOGOUT_REQUEST },
|
|
|
|
|
{ type: LOGOUT_SUCCESSFUL },
|
|
|
|
|
{ type: ME_AUTHENTICATED_REQUEST }
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
|
|
|
|
|
return store.dispatch(logout()).then(() => {
|
|
|
|
|
expect(store.getActions()).toEqual(expectedActions);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("logout failed", () => {
|
|
|
|
|
fetchMock.deleteOnce("/scm/api/rest/v2/auth/access_token", {
|
|
|
|
|
status: 500
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const expectedActions = [{ type: LOGOUT_REQUEST }, { type: LOGOUT_FAILED }];
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
return store.dispatch(logout()).then(() => {
|
|
|
|
|
const actions = store.getActions();
|
|
|
|
|
expect(actions[0].type).toEqual(LOGOUT_REQUEST);
|
|
|
|
|
expect(actions[1].type).toEqual(LOGOUT_FAILED);
|
|
|
|
|
expect(actions[1].payload).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-07-09 11:38:13 +02:00
|
|
|
});
|
|
|
|
|
|
2018-07-11 22:01:36 +02:00
|
|
|
describe("reducer tests", () => {
|
|
|
|
|
test("login request", () => {
|
|
|
|
|
var newState = reducer({}, { type: LOGIN_REQUEST });
|
|
|
|
|
expect(newState.loading).toBeTruthy();
|
|
|
|
|
expect(newState.login).toBeFalsy();
|
|
|
|
|
expect(newState.error).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("login successful", () => {
|
|
|
|
|
var newState = reducer({ login: false }, { type: LOGIN_SUCCESSFUL });
|
|
|
|
|
expect(newState.loading).toBeFalsy();
|
|
|
|
|
expect(newState.login).toBeTruthy();
|
|
|
|
|
expect(newState.error).toBe(null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("login failed", () => {
|
|
|
|
|
const err = new Error("error!");
|
|
|
|
|
var newState = reducer({}, { type: LOGIN_FAILED, payload: err });
|
|
|
|
|
expect(newState.loading).toBeFalsy();
|
|
|
|
|
expect(newState.login).toBeFalsy();
|
|
|
|
|
expect(newState.error).toBe(err);
|
|
|
|
|
});
|
2018-07-12 11:33:41 +02:00
|
|
|
|
|
|
|
|
test("logout request", () => {
|
|
|
|
|
var newState = reducer({ login: true }, { type: LOGOUT_REQUEST });
|
|
|
|
|
expect(newState.loading).toBeTruthy();
|
|
|
|
|
expect(newState.login).toBeTruthy();
|
|
|
|
|
expect(newState.error).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("logout successful", () => {
|
|
|
|
|
var newState = reducer({ login: true }, { type: LOGOUT_SUCCESSFUL });
|
|
|
|
|
expect(newState.loading).toBeFalsy();
|
|
|
|
|
expect(newState.login).toBeFalsy();
|
|
|
|
|
expect(newState.error).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("logout failed", () => {
|
|
|
|
|
const err = new Error("error!");
|
|
|
|
|
var newState = reducer(
|
|
|
|
|
{ login: true },
|
|
|
|
|
{ type: LOGOUT_FAILED, payload: err }
|
|
|
|
|
);
|
|
|
|
|
expect(newState.loading).toBeFalsy();
|
|
|
|
|
expect(newState.login).toBeTruthy();
|
|
|
|
|
expect(newState.error).toBe(err);
|
|
|
|
|
});
|
2018-07-09 11:38:13 +02:00
|
|
|
});
|