Nav apraksta

shared.spec.ts 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import Shared from '../src/shared';
  2. import Storage from '../src/storage';
  3. import Config from '../src/config';
  4. let window;
  5. let config;
  6. let storage;
  7. let shared;
  8. describe('Shared', () => {
  9. beforeEach(angular.mock.inject(($q, $window, $log) => {
  10. window = $window;
  11. config = new Config();
  12. storage = new Storage($window, config);
  13. shared = new Shared($q, $window, $log, config, storage);
  14. }));
  15. afterEach(() => {
  16. window.localStorage.clear();
  17. window.sessionStorage.clear();
  18. });
  19. describe('logout()', () => {
  20. it('should be defined', () => {
  21. expect(shared.logout).toBeDefined();
  22. });
  23. it('should log out a user', () => {
  24. const storageType = config.storageType;
  25. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  26. window[storageType][tokenName] = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9';
  27. shared.logout();
  28. expect(window[storageType][tokenName]).toBeFalsy();
  29. });
  30. });
  31. describe('getToken()', () => {
  32. it('should be defined', () => {
  33. expect(shared.getToken).toBeDefined();
  34. });
  35. it('should get a token from Local Storage', () => {
  36. const storageType = config.storageType;
  37. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  38. window[storageType][tokenName] = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9';
  39. expect(shared.getToken()).toEqual(window[storageType][tokenName]);
  40. });
  41. });
  42. describe('getPayload()', () => {
  43. it('should be defined', () => {
  44. expect(shared.getPayload).toBeDefined();
  45. });
  46. it('should get a JWT payload', () => {
  47. const storageType = config.storageType;
  48. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  49. window[storageType][tokenName] = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEyMzQ1Njc4OTAsIm5hbWUiOiJKb2huIERvZSJ9.kRkUHzvZMWXjgB4zkO3d6P1imkdp0ogebLuxnTCiYUU';
  50. const payload = shared.getPayload();
  51. expect(angular.isObject(payload)).toBe(true);
  52. expect(payload.name).toEqual('John Doe');
  53. });
  54. it('should get a JWT payload with non-english characters', () => {
  55. const storageType = config.storageType;
  56. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  57. window[storageType][tokenName] = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEyMzQ1Njc4OTAsIm5hbWUiOiJSb2LDqXJ0w6AgVGjDqcO0In0.sgREDWs78UYxIMDfa9cwYqvDgco3i_Ap4MUwmprZWN0';
  58. const payload = shared.getPayload();
  59. expect(angular.isObject(payload)).toBe(true);
  60. expect(payload.name).toEqual('Robértà Théô');
  61. });
  62. it('should return undefined if not a valid JWT', () => {
  63. const storageType = config.storageType;
  64. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  65. window[storageType][tokenName] = 'f0af717251950dbd4d73154fdf0a474a5c5119adad999683f5b450c460726aa';
  66. const payload = shared.getPayload();
  67. expect(payload).toBeUndefined();
  68. });
  69. it('should return undefined if token looks like JWT but is not valid', function () {
  70. const storageType = config.storageType;
  71. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  72. window[storageType][tokenName] = 'f0af717251950dbd4.d73154fdf0a474a5c5119ada.d999683f5b450c460726aa';
  73. const payload = shared.getPayload();
  74. expect(payload).toBeUndefined();
  75. });
  76. });
  77. describe('isAuthenticated()', () => {
  78. it('should be defined', () => {
  79. expect(shared.isAuthenticated).toBeDefined();
  80. expect(angular.isFunction(shared.isAuthenticated)).toBe(true);
  81. });
  82. it('should handle expired token', () => {
  83. const storageType = config.storageType;
  84. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  85. window[storageType][tokenName] = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNDQ1NDEzODU5LCJleHAiOjE0NDU0MTM4NjR9.FWflFgVTYu4riXLDzOAx9xy1x-qYyzwi09oN_pmoLcg';
  86. expect(shared.isAuthenticated()).toBe(false);
  87. });
  88. it('should work with non-JWT token', () => {
  89. const storageType = config.storageType;
  90. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  91. window[storageType][tokenName] = 'foo.bar.baz';
  92. expect(shared.isAuthenticated()).toBe(true);
  93. });
  94. });
  95. describe('setToken()', () => {
  96. it('should gracefully return without a response param', () => {
  97. const storageType = config.storageType;
  98. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  99. shared.setToken();
  100. expect(window[storageType][tokenName]).toBeUndefined();
  101. });
  102. it('should set the token when tokenRoot is provided', () => {
  103. const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsb…YzMn0.YATZN37JENCQWeNAoN4M7KxJl7OAIJL4ka_fSM_gYkE';
  104. const response = {
  105. data: {
  106. foo: {
  107. bar: {
  108. token: token
  109. }
  110. }
  111. }
  112. };
  113. config.tokenRoot = 'foo.bar';
  114. shared.setToken(response);
  115. expect(token).toEqual(shared.getToken());
  116. });
  117. it('should set string access_token', () => {
  118. const storageType = config.storageType;
  119. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  120. const response = { access_token: 'test' };
  121. shared.setToken(response);
  122. expect(window[storageType][tokenName]).toBe('test');
  123. });
  124. it('should set object access_token', () => {
  125. const storageType = config.storageType;
  126. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  127. const response = {
  128. access_token: {
  129. data: {
  130. token: 'access_token_object_test'
  131. }
  132. }
  133. };
  134. shared.setToken(response);
  135. expect(window[storageType][tokenName]).toBe('access_token_object_test');
  136. });
  137. it('should gracefully return if no token is passed in', () => {
  138. const storageType = config.storageType;
  139. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  140. delete window[storageType][tokenName];
  141. const response = {
  142. access_token: {
  143. data: {}
  144. }
  145. };
  146. shared.setToken(response);
  147. expect(window[storageType][tokenName]).toBeUndefined();
  148. });
  149. });
  150. describe('removeToken()', () => {
  151. it('should be defined', () => {
  152. expect(shared.removeToken).toBeDefined();
  153. });
  154. it('should remove a token', () => {
  155. const storageType = config.storageType;
  156. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  157. window[storageType][tokenName] = 'f0af717251950dbd4d73154fdf0a474a5c5119adad999683f5b450c460726aa';
  158. shared.removeToken();
  159. expect(window[storageType][tokenName]).toBeUndefined();
  160. });
  161. });
  162. describe('setStorageType()', () => {
  163. it('should be defined', () => {
  164. expect(shared.setStorageType).toBeDefined();
  165. });
  166. it('should be able to set storage type to session storage', () => {
  167. shared.setStorageType('sessionStorage');
  168. expect(config.storageType).toBe('sessionStorage');
  169. });
  170. });
  171. });