auth.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. customDomain: '',
  80. }
  81. },
  82. { type: SHOW_PAGE_LOADING }
  83. ];
  84. store
  85. .dispatch(loginUser(email, password))
  86. .then(() => {
  87. expect(store.getActions()).to.deep.equal(expectedActions);
  88. pushStub.restore();
  89. sinon.assert.calledWith(pushStub, expectedRoute);
  90. done();
  91. })
  92. .catch(error => done(error));
  93. });
  94. });
  95. describe('#logoutUser()', () => {
  96. it('should dispatch UNAUTH_USER when loging out user has been done', () => {
  97. const pushStub = sinon.stub(Router, 'push');
  98. pushStub.withArgs('/login').returns('/login');
  99. const expectedRoute = '/login';
  100. const store = mockStore({});
  101. const expectedActions = [
  102. { type: SHOW_PAGE_LOADING },
  103. { type: UNAUTH_USER }
  104. ];
  105. store.dispatch(logoutUser());
  106. expect(store.getActions()).to.deep.equal(expectedActions);
  107. pushStub.restore();
  108. sinon.assert.calledWith(pushStub, expectedRoute);
  109. });
  110. });
  111. describe('#renewAuthUser()', () => {
  112. it('should dispatch AUTH_RENEW when renewing auth user has been done', done => {
  113. const cookieStub = sinon.stub(cookie, 'get');
  114. cookieStub.withArgs('token').returns(token);
  115. nock('http://localhost', {
  116. reqheaders: {
  117. Authorization: token
  118. }
  119. })
  120. .post('/api/auth/renew')
  121. .reply(200, {
  122. token
  123. });
  124. const store = mockStore({ auth: { renew: false } });
  125. const expectedActions = [
  126. { type: AUTH_RENEW },
  127. {
  128. type: AUTH_USER,
  129. payload: jwt
  130. },
  131. {
  132. type: SET_DOMAIN,
  133. payload: {
  134. customDomain: '',
  135. }
  136. }
  137. ];
  138. store
  139. .dispatch(renewAuthUser())
  140. .then(() => {
  141. expect(store.getActions()).to.deep.equal(expectedActions);
  142. cookieStub.restore();
  143. done();
  144. })
  145. .catch(error => done(error));
  146. });
  147. });
  148. });