You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

144 lines
4.1 KiB

  1. import test from 'ava';
  2. import nock from 'nock';
  3. import auth from '../src/services/auth';
  4. import {storageMock} from './_helpers';
  5. auth.localStorage = storageMock();
  6. const user = {
  7. email: 'test@lesspass.com',
  8. password: 'password'
  9. };
  10. const token = 'eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9';
  11. test('should make a post request to create a session', t => {
  12. nock('http://localhost/').post('/api/tokens/auth/', user).reply(201, {token});
  13. return auth.login(user).then(r => {
  14. t.is(token, r.token);
  15. });
  16. });
  17. test('should throw error if bad request', t => {
  18. nock('http://localhost/').post('/api/tokens/auth/', user).reply(400, {});
  19. t.plan(1);
  20. return auth.login(user).catch(err => {
  21. t.is(400, err.status);
  22. });
  23. });
  24. test('should get user info', t => {
  25. nock('http://localhost/').get('/api/auth/me/').reply(200, {email: user.email});
  26. return auth.getUser().then(u => {
  27. t.is(u.email, user.email);
  28. });
  29. });
  30. test('should register a user', t => {
  31. nock('http://localhost/').post('/api/auth/register/', user).reply(201, {email: user.email, pk: 1});
  32. return auth.register(user).then(r => {
  33. t.is(r.email, user.email);
  34. });
  35. });
  36. test('should store token in localStorage', t => {
  37. nock('http://localhost/').post('/api/tokens/auth/', user).reply(201, {token});
  38. t.plan(1);
  39. return auth.login(user).then(() => {
  40. t.is(token, auth.localStorage.getItem('token'));
  41. });
  42. });
  43. /* eslint camelcase: 0 */
  44. const credentials = {
  45. current_password: 'current password',
  46. new_password: 'new password'
  47. };
  48. const headers = {headers: {Authorization: `JWT ${token}`}};
  49. test('should send requests with Authorization header', t => {
  50. nock('http://localhost/', headers).post('/api/auth/password/').query(true).reply(200, {});
  51. return auth.changePassword(credentials).then(r => {
  52. t.is(r.status, 200);
  53. });
  54. });
  55. test('should change password', t => {
  56. nock('http://localhost/', headers).post('/api/auth/password/', credentials).reply(200, {});
  57. t.plan(1);
  58. return auth.changePassword(credentials).then(r => {
  59. t.is(r.status, 200);
  60. });
  61. });
  62. test('should authenticate the user', t => {
  63. nock('http://localhost/').post('/api/tokens/auth/', user).reply(201, {token});
  64. t.plan(1);
  65. return auth.login(user).then(() => {
  66. t.true(auth.user.authenticated);
  67. });
  68. });
  69. test('get token', t => {
  70. auth.localStorage.setItem('hwm-token', token);
  71. return auth.getToken('hwm-token').then(expectedToken => {
  72. t.is(token, expectedToken);
  73. });
  74. });
  75. test('get missing token failed', () => {
  76. auth.localStorage.removeItem('token');
  77. return auth.getToken('token').catch(() => {
  78. });
  79. });
  80. test('check refresh token non-expired', t => {
  81. const fakeToken = 'wibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9eyJzdWIiOiIxMjM0NTY3ODkwIi';
  82. nock('http://localhost/').post('/api/tokens/refresh/', {token}).reply(200, {token: fakeToken});
  83. return auth.refreshToken(token).then(newToken => {
  84. t.is(fakeToken, newToken);
  85. });
  86. });
  87. test('check refresh token expired', t => {
  88. auth.localStorage.setItem('token', token);
  89. nock('http://localhost/').post('/api/tokens/refresh/', {token}).reply(400);
  90. return auth.refreshToken(token).catch(err => {
  91. t.is(err.status, 400);
  92. });
  93. });
  94. test('logout', t => {
  95. auth.localStorage.setItem('token', token);
  96. auth.user.authenticated = true;
  97. auth.logout();
  98. t.false(auth.user.authenticated);
  99. t.true(auth.localStorage.getItem('token') === null);
  100. });
  101. test('check token with a valid token', t => {
  102. auth.localStorage.setItem('token', token);
  103. nock('http://localhost/').post('/api/tokens/verify/', {token}).reply(200, {});
  104. return auth.checkAuth().then(() => {
  105. t.true(auth.user.authenticated);
  106. });
  107. });
  108. test('check token with an invalid token', t => {
  109. nock('http://localhost/').post('/api/tokens/verify/', {token}).reply(400);
  110. auth.localStorage.setItem('token', token);
  111. t.plan(1);
  112. return auth.checkAuth().catch(() => {
  113. t.false(auth.user.authenticated);
  114. });
  115. });
  116. test('check auth without any token', t => {
  117. auth.user.authenticated = true;
  118. auth.localStorage.removeItem('token');
  119. return auth.checkAuth().catch(() => {
  120. t.true(!auth.user.authenticated);
  121. });
  122. });