Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import configureMockStore from 'redux-mock-store';
- import thunk from 'redux-thunk';
- import nock from 'nock';
- import * as types from '../../../../client/auth/actions/action_types';
- import { GET_NOTEBOOKS_REQUEST } from '../../../../client/notebook/actions/action_types';
- import loginAuth from '../../../../client/auth/actions/login_action';
- require('dotenv').config();
- const expect = require('chai').expect;
- const mockStore = configureMockStore([thunk]);
- const user = {
- email: 'john@loginActionTest.com',
- password: 'secret',
- };
- describe('authAction -> loginUser() ', () => {
- afterEach(() => {
- nock.cleanAll();
- });
- it('should create AUTH_SUCCESS when finishes without error', () => {
- const store = mockStore({});
- const token = 'authToken';
- const expectedActions = [
- { type: types.AUTH_REQUEST },
- { type: types.AUTH_SUCCESS, token },
- { type: GET_NOTEBOOKS_REQUEST },
- { type: types.REMOVE_AUTH_ERROR },
- ];
- nock('http://localhost:3000')
- .post('/api/auth/login', {
- email: user.email,
- password: user.password,
- })
- .reply(200, { success: true, token });
- return store.dispatch(loginAuth(user)).then(() => {
- expect(store.getActions()).to.deep.equal(expectedActions);
- });
- });
- it('should create AUTH_ERROR when finishes with error', () => {
- const store = mockStore({});
- const messages = ['Invalid password'];
- const expectedActions = [
- { type: types.AUTH_REQUEST },
- { type: types.AUTH_ERROR, messages },
- ];
- nock('http://localhost:3000')
- .post('/api/auth/login', {
- email: user.email,
- password: user.password,
- })
- .reply(200, { success: false, messages });
- return store.dispatch(loginAuth(user)).then(() => {
- expect(store.getActions()).to.deep.equal(expectedActions);
- });
- });
- });
- /* @flow */
- import fetch from 'isomorphic-fetch';
- import {
- authRequest,
- authComplete,
- removeAuthError,
- } from './auth_actions';
- function loginAuth(user: { email: string, password: string }) {
- return function (dispatch: any) {
- dispatch(authRequest());
- return fetch('http://localhost:3000/api/auth/login', {
- method: 'POST',
- headers: {
- Accept: 'application/json',
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({
- email: user.email,
- password: user.password,
- }),
- })
- .then((response: any) => { return response.json(); })
- .then((data: any) => {
- if (data.success === true) {
- dispatch(authComplete(null, data.token));
- dispatch(removeAuthError());
- } else if (data.success === false) {
- dispatch(authComplete(data.messages));
- } else {
- const error = new Error('/api/auth/login data.success isn't defined');
- throw error;
- }
- })
- .catch((error: any) => {
- throw error;
- });
- };
- }
- export default loginAuth;
- /* @flow */
- import { MongoClient } from 'mongodb';
- import jwt from 'jsonwebtoken';
- import { findUser } from '../methods';
- const MONGO_URL = process.env.MONGO_URL;
- const JWT_SECRET = String(process.env.JWT_SECRET);
- async function loginController(ctx: any) {
- type Body = { success: boolean, messages?: string[], token?: string };
- let status: number;
- const user = {
- email: ctx.request.body.email,
- password: ctx.request.body.password,
- };
- // connect to database
- const db = await MongoClient.connect(MONGO_URL);
- const body: Body = await findUser(db, user).then((doc) => {
- const token = jwt.sign({
- _id: doc._id,
- email: doc.email,
- }, JWT_SECRET, { expiresIn: '30 days' });
- status = 200;
- return { success: true, token };
- })
- .catch((errors) => {
- // returns login error messages
- status = 400;
- return { success: false, messages: errors };
- });
- await db.close();
- ctx.status = status;
- ctx.body = body;
- }
- export default loginController;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement