選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

auth.js 4.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import test from 'ava';
  2. import {LocalStorageMock} from './_helpers';
  3. import Auth from '../src/api/auth';
  4. import Storage, {LOCAL_STORAGE_KEY} from '../src/api/storage';
  5. import nock from 'nock';
  6. function AuthFactory(token, localStorage = new LocalStorageMock()) {
  7. const storage = new Storage(localStorage);
  8. storage.saveToken(token);
  9. return new Auth(storage);
  10. }
  11. test('request token', t => {
  12. const token = '5e0651';
  13. const user = {email: 'test@example.org', password: 'password'};
  14. nock('https://lesspass.com').post('/api/tokens/auth/', user).reply(201, {token});
  15. return Auth._requestToken(user, {baseURL: 'https://lesspass.com'}).then(requestedToken => {
  16. t.is(requestedToken, token);
  17. });
  18. });
  19. test('request new token', t => {
  20. const token = '3e3231';
  21. const newToken = 'wibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9eyJzdWIiOiIxMjM0NTY3ODkwIi';
  22. nock('https://lesspass.com').post('/api/tokens/refresh/', {token}).reply(200, {token: newToken});
  23. return Auth._requestNewToken({token}, {baseURL: 'https://lesspass.com'}).then(refreshedToken => {
  24. t.is(refreshedToken, newToken);
  25. });
  26. });
  27. test('user first connection is guest', t => {
  28. const storage = new Storage(new LocalStorageMock());
  29. const auth = new Auth(storage);
  30. t.true(auth.isGuest());
  31. });
  32. test('user return on site before token expire', t => {
  33. const auth = AuthFactory('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTc1NzkyODQzNH0.KzEBhVgm3xa51jsBklB0Ib9DDwAkvynOnkwLLJoD5AU');
  34. t.true(auth.isAuthenticated());
  35. t.false(auth.isGuest());
  36. });
  37. test('user return on site after token expiration', t => {
  38. const auth = AuthFactory('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTQzNzAxODU4M30.NmMv7sXjM1dW0eALNXud8LoXknZ0mH14GtnFclwJv0s');
  39. t.false(auth.isAuthenticated());
  40. t.true(auth.isGuest());
  41. t.false(auth.user.authenticated);
  42. });
  43. test('login save token', t => {
  44. const token = '3e3231';
  45. const storage = new LocalStorageMock();
  46. const auth = AuthFactory(token, storage);
  47. const user = {
  48. email: 'test@lesspass.com',
  49. password: 'password'
  50. };
  51. nock('https://lesspass.com').post('/api/tokens/auth/', user).reply(201, {token});
  52. return auth.login(user).then(() => {
  53. t.is(JSON.parse(storage.getItem(LOCAL_STORAGE_KEY)).jwt, token);
  54. });
  55. });
  56. test('logout user remove token and unauthenticate user', t => {
  57. const token = '3e3231';
  58. const storage = new LocalStorageMock();
  59. const auth = AuthFactory(token, storage);
  60. return auth.logout().then(() => {
  61. t.falsy(storage.getItem(LOCAL_STORAGE_KEY));
  62. });
  63. });
  64. test('login custom endpoint', t => {
  65. const token = '3e3231';
  66. const storage = new LocalStorageMock();
  67. const auth = AuthFactory(token, storage);
  68. const user = {
  69. email: 'test@lesspass.com',
  70. password: 'password'
  71. };
  72. nock('https://test.example.org').post('/api/tokens/auth/', user).reply(201, {token});
  73. return auth.login(user, 'https://test.example.org').then(() => {
  74. t.is(JSON.parse(storage.getItem(LOCAL_STORAGE_KEY)).jwt, token);
  75. });
  76. });
  77. test('refresh token', t => {
  78. const token = '3e3231';
  79. const storage = new LocalStorageMock();
  80. const auth = AuthFactory(token, storage);
  81. const newToken = 'wibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9eyJzdWIiOiIxMjM0NTY3ODkwIi';
  82. nock('https://lesspass.com').post('/api/tokens/refresh/', {token}).reply(200, {token: newToken});
  83. return auth.refreshToken().then(() => {
  84. t.is(JSON.parse(storage.getItem(LOCAL_STORAGE_KEY)).jwt, newToken);
  85. });
  86. });
  87. test('should register a user', t => {
  88. const user = {
  89. email: 'test@lesspass.com',
  90. password: 'password'
  91. };
  92. const localStorage = new LocalStorageMock();
  93. const storage = new Storage(localStorage);
  94. const auth = new Auth(storage);
  95. nock('https://lesspass.com').post('/api/auth/register/', user).reply(201, {email: user.email, pk: 1});
  96. return auth.register(user).then(newUser => {
  97. t.is(newUser.email, user.email);
  98. });
  99. });
  100. test('should reset a password', t => {
  101. var email = 'test@lesspass.com';
  102. const localStorage = new LocalStorageMock();
  103. const storage = new Storage(localStorage);
  104. const auth = new Auth(storage);
  105. nock('https://lesspass.com').post('/api/auth/password/reset/', {email}).reply(204);
  106. t.notThrows(auth.resetPassword({email}));
  107. });