auth.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 jwt = {
  23. domain: '',
  24. exp: 1529137738725,
  25. iat: 1529137738725,
  26. iss: 'ApiAuth',
  27. sub: 'test@mail.com',
  28. };
  29. const email = 'test@mail.com';
  30. const password = 'password';
  31. const token =
  32. 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBcGlBdXRoIiwic3ViIjoidGVzdEBtYWlsLmNvbSIsImRvbWFpbiI6IiIsImlhdCI6MTUyOTEzNzczODcyNSwiZXhwIjoxNTI5MTM3NzM4NzI1fQ.tdI7r11bmSCUmbcJBBKIDt7Hkb7POLMRl8VNJv_8O_s';
  33. describe('#signupUser()', () => {
  34. it('should dispatch SENT_VERIFICATION when signing up user has been done', done => {
  35. nock('http://localhost')
  36. .post('/api/auth/signup')
  37. .reply(200, {
  38. email,
  39. message: 'Verification email has been sent.'
  40. });
  41. const store = mockStore({});
  42. const expectedActions = [
  43. { type: SIGNUP_LOADING },
  44. {
  45. type: SENT_VERIFICATION,
  46. payload: email
  47. }
  48. ];
  49. store
  50. .dispatch(signupUser(email, password))
  51. .then(() => {
  52. expect(store.getActions()).to.deep.equal(expectedActions);
  53. done();
  54. })
  55. .catch(error => done(error));
  56. });
  57. });
  58. describe('#loginUser()', () => {
  59. it('should dispatch AUTH_USER when logining user has been done', done => {
  60. const pushStub = sinon.stub(Router, 'push');
  61. pushStub.withArgs('/').returns('/');
  62. const expectedRoute = '/';
  63. nock('http://localhost')
  64. .post('/api/auth/login')
  65. .reply(200, {
  66. token
  67. });
  68. const store = mockStore({});
  69. const expectedActions = [
  70. { type: LOGIN_LOADING },
  71. { type: AUTH_RENEW },
  72. {
  73. type: AUTH_USER,
  74. payload: jwt
  75. },
  76. {
  77. type: SET_DOMAIN,
  78. payload: ''
  79. },
  80. { type: SHOW_PAGE_LOADING }
  81. ];
  82. store
  83. .dispatch(loginUser(email, password))
  84. .then(() => {
  85. expect(store.getActions()).to.deep.equal(expectedActions);
  86. pushStub.restore();
  87. sinon.assert.calledWith(pushStub, expectedRoute);
  88. done();
  89. })
  90. .catch(error => done(error));
  91. });
  92. });
  93. describe('#logoutUser()', () => {
  94. it('should dispatch UNAUTH_USER when loging out user has been done', () => {
  95. const pushStub = sinon.stub(Router, 'push');
  96. pushStub.withArgs('/login').returns('/login');
  97. const expectedRoute = '/login';
  98. const store = mockStore({});
  99. const expectedActions = [
  100. { type: SHOW_PAGE_LOADING },
  101. { type: UNAUTH_USER }
  102. ];
  103. store.dispatch(logoutUser());
  104. expect(store.getActions()).to.deep.equal(expectedActions);
  105. pushStub.restore();
  106. sinon.assert.calledWith(pushStub, expectedRoute);
  107. });
  108. });
  109. describe('#renewAuthUser()', () => {
  110. it('should dispatch AUTH_RENEW when renewing auth user has been done', done => {
  111. const cookieStub = sinon.stub(cookie, 'get');
  112. cookieStub.withArgs('token').returns(token);
  113. nock('http://localhost', {
  114. reqheaders: {
  115. Authorization: token
  116. }
  117. })
  118. .post('/api/auth/renew')
  119. .reply(200, {
  120. token
  121. });
  122. const store = mockStore({ auth: { renew: false } });
  123. const expectedActions = [
  124. { type: AUTH_RENEW },
  125. {
  126. type: AUTH_USER,
  127. payload: jwt
  128. },
  129. {
  130. type: SET_DOMAIN,
  131. payload: ''
  132. }
  133. ];
  134. store
  135. .dispatch(renewAuthUser())
  136. .then(() => {
  137. expect(store.getActions()).to.deep.equal(expectedActions);
  138. cookieStub.restore();
  139. done();
  140. })
  141. .catch(error => done(error));
  142. });
  143. });
  144. });