auth.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import nock from 'nock';
  2. import sinon from 'sinon';
  3. import { expect } from 'chai';
  4. import cookie from 'js-cookie';
  5. import thunk from 'redux-thunk';
  6. import Router from 'next/router';
  7. import configureMockStore from 'redux-mock-store';
  8. import { signupUser, loginUser, logoutUser, renewAuthUser } from '../auth';
  9. import {
  10. SIGNUP_LOADING,
  11. SENT_VERIFICATION,
  12. LOGIN_LOADING,
  13. AUTH_RENEW,
  14. AUTH_USER,
  15. SET_DOMAIN,
  16. SHOW_PAGE_LOADING,
  17. UNAUTH_USER
  18. } from '../actionTypes';
  19. const middlewares = [thunk];
  20. const mockStore = configureMockStore(middlewares);
  21. describe('auth actions', () => {
  22. const email = 'test@mail.com';
  23. const password = 'password';
  24. const token =
  25. 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBcGlBdXRoIiwic3ViIjoidGVzdEBtYWlsLmNvbSIsImRvbWFpbiI6IiIsImlhdCI6MTUyOTEzNzczODcyNSwiZXhwIjoxNTI5MTM3NzM4NzI1fQ.tdI7r11bmSCUmbcJBBKIDt7Hkb7POLMRl8VNJv_8O_s';
  26. describe('#signupUser()', () => {
  27. it('should dispatch SENT_VERIFICATION when signing up user has been done', done => {
  28. nock('http://localhost')
  29. .post('/api/auth/signup')
  30. .reply(200, {
  31. email,
  32. message: 'Verification email has been sent.'
  33. });
  34. const store = mockStore({});
  35. const expectedActions = [
  36. { type: SIGNUP_LOADING },
  37. {
  38. type: SENT_VERIFICATION,
  39. payload: email
  40. }
  41. ];
  42. store
  43. .dispatch(signupUser(email, password))
  44. .then(() => {
  45. expect(store.getActions()).to.deep.equal(expectedActions);
  46. done();
  47. })
  48. .catch(error => done(error));
  49. });
  50. });
  51. describe('#loginUser()', () => {
  52. it('should dispatch AUTH_USER when logining user has been done', done => {
  53. const pushStub = sinon.stub(Router, 'push');
  54. pushStub.withArgs('/').returns('/');
  55. const expectedRoute = '/';
  56. nock('http://localhost')
  57. .post('/api/auth/login')
  58. .reply(200, {
  59. token
  60. });
  61. const store = mockStore({});
  62. const expectedActions = [
  63. { type: LOGIN_LOADING },
  64. { type: AUTH_RENEW },
  65. {
  66. type: AUTH_USER,
  67. payload: email
  68. },
  69. {
  70. type: SET_DOMAIN,
  71. payload: ''
  72. },
  73. { type: SHOW_PAGE_LOADING }
  74. ];
  75. store
  76. .dispatch(loginUser(email, password))
  77. .then(() => {
  78. expect(store.getActions()).to.deep.equal(expectedActions);
  79. pushStub.restore();
  80. sinon.assert.calledWith(pushStub, expectedRoute);
  81. done();
  82. })
  83. .catch(error => done(error));
  84. });
  85. });
  86. describe('#logoutUser()', () => {
  87. it('should dispatch UNAUTH_USER when loging out user has been done', () => {
  88. const pushStub = sinon.stub(Router, 'push');
  89. pushStub.withArgs('/login').returns('/login');
  90. const expectedRoute = '/login';
  91. const store = mockStore({});
  92. const expectedActions = [
  93. { type: SHOW_PAGE_LOADING },
  94. { type: UNAUTH_USER }
  95. ];
  96. store.dispatch(logoutUser());
  97. expect(store.getActions()).to.deep.equal(expectedActions);
  98. pushStub.restore();
  99. sinon.assert.calledWith(pushStub, expectedRoute);
  100. });
  101. });
  102. describe('#renewAuthUser()', () => {
  103. it('should dispatch AUTH_RENEW when renewing auth user has been done', done => {
  104. const cookieStub = sinon.stub(cookie, 'get');
  105. cookieStub.withArgs('token').returns(token);
  106. nock('http://localhost', {
  107. reqheaders: {
  108. Authorization: token
  109. }
  110. })
  111. .post('/api/auth/renew')
  112. .reply(200, {
  113. token
  114. });
  115. const store = mockStore({ auth: { renew: false } });
  116. const expectedActions = [
  117. { type: AUTH_RENEW },
  118. {
  119. type: AUTH_USER,
  120. payload: email
  121. },
  122. {
  123. type: SET_DOMAIN,
  124. payload: ''
  125. }
  126. ];
  127. store
  128. .dispatch(renewAuthUser())
  129. .then(() => {
  130. expect(store.getActions()).to.deep.equal(expectedActions);
  131. cookieStub.restore();
  132. done();
  133. })
  134. .catch(error => done(error));
  135. });
  136. });
  137. });