您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

79 行
2.7 KiB

  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('user first connection is guest', t => {
  20. const storage = new Storage(new LocalStorageMock());
  21. const auth = new Auth(storage);
  22. t.true(auth.isGuest());
  23. });
  24. test('user return on site before token expire', t => {
  25. const auth = AuthFactory('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTc1NzkyODQzNH0.KzEBhVgm3xa51jsBklB0Ib9DDwAkvynOnkwLLJoD5AU');
  26. t.true(auth.isAuthenticated());
  27. t.false(auth.isGuest());
  28. });
  29. test('user return on site after token expiration', t => {
  30. const auth = AuthFactory('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTQzNzAxODU4M30.NmMv7sXjM1dW0eALNXud8LoXknZ0mH14GtnFclwJv0s');
  31. t.false(auth.isAuthenticated());
  32. t.true(auth.isGuest());
  33. t.false(auth.user.authenticated);
  34. });
  35. test('login save token', t => {
  36. const token = '3e3231';
  37. const storage = new LocalStorageMock();
  38. const auth = AuthFactory(token, storage);
  39. const user = {
  40. email: 'test@lesspass.com',
  41. password: 'password'
  42. };
  43. nock('https://lesspass.com').post('/api/tokens/auth/', user).reply(201, {token});
  44. return auth.login(user).then(() => {
  45. t.is(JSON.parse(storage.getItem(LOCAL_STORAGE_KEY)).jwt, token);
  46. });
  47. });
  48. test('logout user remove token and unauthenticate user', t => {
  49. const token = '3e3231';
  50. const storage = new LocalStorageMock();
  51. const auth = AuthFactory(token, storage);
  52. return auth.logout().then(() => {
  53. t.falsy(storage.getItem(LOCAL_STORAGE_KEY));
  54. });
  55. });
  56. test('login custom endpoint', t => {
  57. const token = '3e3231';
  58. const storage = new LocalStorageMock();
  59. const auth = AuthFactory(token, storage);
  60. const user = {
  61. email: 'test@lesspass.com',
  62. password: 'password'
  63. };
  64. nock('https://test.example.org').post('/api/tokens/auth/', user).reply(201, {token});
  65. return auth.login(user, 'https://test.example.org').then(() => {
  66. t.is(JSON.parse(storage.getItem(LOCAL_STORAGE_KEY)).jwt, token);
  67. });
  68. });