Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. import fetch from 'whatwg-fetch';
  2.  
  3. export function authenticateUser(username, password) {
  4.  
  5. return function (dispatch, getState) {
  6. dispatch({type: 'LOGIN'});
  7.  
  8. return fetch(getState().config.components.Authentication.login_endpoint, {
  9. method: 'POST',
  10. headers: {
  11. 'Content-Type': 'application/json'
  12. },
  13. credentials: 'same-origin',
  14. body: JSON.stringify({
  15. username: username,
  16. password: password,
  17. })
  18. }).then(resp => {
  19. if (resp.status === 302) {
  20. window.location = resp.url;
  21. } else if (resp.status >= 300 && resp.status < 500) {
  22. return Promise.resolve(resp);
  23. } else {
  24. return Promise.reject(resp.statusText);
  25. }
  26. }).then(resp => resp.json())
  27. .then(resp => {
  28. const error = data.errors[0];
  29. dispatch({ type: 'LOGIN_FAILED', payload: error.detail });
  30. }).catch(error => {
  31. console.error(error); // I know, still needs to be catched
  32. });
  33. };
  34. }
  35.  
  36. import { expect } from 'chai';
  37. import configureMockStore from 'redux-mock-store';
  38. import thunk from 'redux-thunk';
  39. import sinon from 'sinon';
  40. import { authenticateUser } from '../../src/actions/index';
  41.  
  42. const initialConfig = {
  43. authentication: {
  44. loginform_loading: false,
  45. loginform_errorMessage: null,
  46. forgotpassword_loading: false,
  47. forgotpassword_errorMessage: null,
  48. forgotpassword_passwordSent: false
  49. },
  50. config: {
  51. components: {
  52. Authentication: {
  53. 'login_endpoint': '/api/auth/login',
  54. 'forgotpassword_enabled': true,
  55. 'forgotpassword_path': '/auth/forgot-password',
  56. 'forgotpassword_endpoint': '/auth/forgot-password'
  57. }
  58. }
  59. }
  60. };
  61.  
  62. const middlewares = [ thunk ];
  63. const mockStore = configureMockStore(middlewares);
  64. let store;
  65.  
  66. describe('Action/Index/authenticateUser', () => {
  67.  
  68. const testUsername = 'User';
  69. const testPassword = 'Test123';
  70. let sandbox;
  71.  
  72. beforeEach(function () {
  73. sandbox = sinon.sandbox.create();
  74. store = mockStore(initialConfig);
  75. });
  76.  
  77. afterEach(function () {
  78. sandbox.restore();
  79. });
  80.  
  81. it('LOGIN is dispatched', () => {
  82. var response = mockResponse(400, 'Found', '{"errors": [{ "message": "first" }] }');
  83. sandbox.stub(window, 'fetch')
  84. .resolves(response);
  85.  
  86. return store.dispatch(authenticateUser(testUsername, testPassword))
  87. .then(() => {
  88. var expectedActions = store.getActions();
  89. expect(expectedActions).to.not.be.empty;
  90.  
  91. var action = expectedActions[0];
  92. expect(action.type).to.equal('LOGIN');
  93. });
  94. });
  95. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement