Guest User

Untitled

a guest
Oct 30th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import configureMockStore from 'redux-mock-store';
  2. import thunk from 'redux-thunk';
  3. import nock from 'nock';
  4.  
  5. import * as types from '../../../../client/auth/actions/action_types';
  6. import { GET_NOTEBOOKS_REQUEST } from '../../../../client/notebook/actions/action_types';
  7. import loginAuth from '../../../../client/auth/actions/login_action';
  8.  
  9. require('dotenv').config();
  10. const expect = require('chai').expect;
  11.  
  12. const mockStore = configureMockStore([thunk]);
  13.  
  14. const user = {
  15. password: 'secret',
  16. };
  17.  
  18. describe('authAction -> loginUser() ', () => {
  19. afterEach(() => {
  20. nock.cleanAll();
  21. });
  22.  
  23. it('should create AUTH_SUCCESS when finishes without error', () => {
  24. const store = mockStore({});
  25. const token = 'authToken';
  26. const expectedActions = [
  27. { type: types.AUTH_REQUEST },
  28. { type: types.AUTH_SUCCESS, token },
  29. { type: GET_NOTEBOOKS_REQUEST },
  30. { type: types.REMOVE_AUTH_ERROR },
  31. ];
  32.  
  33. nock('http://localhost:3000')
  34. .post('/api/auth/login', {
  35. email: user.email,
  36. password: user.password,
  37. })
  38. .reply(200, { success: true, token });
  39.  
  40. return store.dispatch(loginAuth(user)).then(() => {
  41. expect(store.getActions()).to.deep.equal(expectedActions);
  42. });
  43. });
  44.  
  45. it('should create AUTH_ERROR when finishes with error', () => {
  46. const store = mockStore({});
  47. const messages = ['Invalid password'];
  48. const expectedActions = [
  49. { type: types.AUTH_REQUEST },
  50. { type: types.AUTH_ERROR, messages },
  51. ];
  52.  
  53. nock('http://localhost:3000')
  54. .post('/api/auth/login', {
  55. email: user.email,
  56. password: user.password,
  57. })
  58. .reply(200, { success: false, messages });
  59.  
  60. return store.dispatch(loginAuth(user)).then(() => {
  61. expect(store.getActions()).to.deep.equal(expectedActions);
  62. });
  63. });
  64. });
  65.  
  66. /* @flow */
  67.  
  68. import fetch from 'isomorphic-fetch';
  69. import {
  70. authRequest,
  71. authComplete,
  72. removeAuthError,
  73. } from './auth_actions';
  74.  
  75. function loginAuth(user: { email: string, password: string }) {
  76. return function (dispatch: any) {
  77. dispatch(authRequest());
  78.  
  79. return fetch('http://localhost:3000/api/auth/login', {
  80. method: 'POST',
  81. headers: {
  82. Accept: 'application/json',
  83. 'Content-Type': 'application/json',
  84. },
  85. body: JSON.stringify({
  86. email: user.email,
  87. password: user.password,
  88. }),
  89. })
  90. .then((response: any) => { return response.json(); })
  91. .then((data: any) => {
  92. if (data.success === true) {
  93. dispatch(authComplete(null, data.token));
  94. dispatch(removeAuthError());
  95. } else if (data.success === false) {
  96. dispatch(authComplete(data.messages));
  97. } else {
  98. const error = new Error('/api/auth/login data.success isn't defined');
  99. throw error;
  100. }
  101. })
  102. .catch((error: any) => {
  103. throw error;
  104. });
  105. };
  106. }
  107.  
  108. export default loginAuth;
  109.  
  110. /* @flow */
  111. import { MongoClient } from 'mongodb';
  112. import jwt from 'jsonwebtoken';
  113. import { findUser } from '../methods';
  114.  
  115. const MONGO_URL = process.env.MONGO_URL;
  116. const JWT_SECRET = String(process.env.JWT_SECRET);
  117.  
  118. async function loginController(ctx: any) {
  119. type Body = { success: boolean, messages?: string[], token?: string };
  120.  
  121. let status: number;
  122. const user = {
  123. email: ctx.request.body.email,
  124. password: ctx.request.body.password,
  125. };
  126.  
  127. // connect to database
  128. const db = await MongoClient.connect(MONGO_URL);
  129. const body: Body = await findUser(db, user).then((doc) => {
  130. const token = jwt.sign({
  131. _id: doc._id,
  132. email: doc.email,
  133. }, JWT_SECRET, { expiresIn: '30 days' });
  134.  
  135. status = 200;
  136. return { success: true, token };
  137. })
  138. .catch((errors) => {
  139. // returns login error messages
  140. status = 400;
  141. return { success: false, messages: errors };
  142. });
  143.  
  144. await db.close();
  145.  
  146. ctx.status = status;
  147. ctx.body = body;
  148. }
  149.  
  150. export default loginController;
Advertisement
Add Comment
Please, Sign In to add comment