Advertisement
Guest User

Untitled

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