Aucune description

authProvider.spec.ts 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import Config from '../src/config';
  2. import Storage from '../src/storage';
  3. import Shared from '../src/shared';
  4. import Popup from '../src/popup';
  5. import OAuth1 from '../src/oauth1';
  6. import OAuth2 from '../src/oauth2';
  7. import OAuth from '../src/oauth';
  8. import Local from '../src/local';
  9. import AuthProvider from '../src/authProvider';
  10. let window;
  11. let http;
  12. let httpBackend;
  13. let config;
  14. let authProvider;
  15. let storage;
  16. let shared;
  17. let popup;
  18. let oauth1;
  19. let oauth2;
  20. let oauth;
  21. let local;
  22. let auth;
  23. const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7Il9pZCI6IjUzZTU3ZDZiY2MzNmMxNTgwNzU4NDJkZCIsImVtYWlsIjoiZm9vQGJhci5jb20iLCJfX3YiOjB9LCJpYXQiOjE0MDc1NDg3ODI5NzMsImV4cCI6MTQwODE1MzU4Mjk3M30.1Ak6mij5kfkSi6d_wtPOx4yK7pS7ZFSiwbkL7AJbnYs';
  24. describe('AuthProvider', () => {
  25. beforeEach(() => {
  26. config = new Config();
  27. authProvider = new AuthProvider(config);
  28. });
  29. beforeEach(angular.mock.inject(($q, $http, $window, $interval, $log, $timeout, $httpBackend) => {
  30. window = $window;
  31. http = $http;
  32. httpBackend = $httpBackend;
  33. storage = new Storage($window, config);
  34. shared = new Shared($q, $window, $log, config, storage);
  35. popup = new Popup($interval, $window, $q);
  36. oauth1 = new OAuth1($http, $window, config, popup);
  37. oauth2 = new OAuth2($http, $window, $timeout, $q, config, popup, storage);
  38. oauth = new OAuth($http, $window, $timeout, $q, config, popup, storage, shared, oauth1, oauth2);
  39. local = new Local($http, config, shared);
  40. auth = authProvider.$get(shared, local, oauth);
  41. }));
  42. it('should set baseUrl', () => {
  43. authProvider.baseUrl = '/api/v2/';
  44. expect(authProvider.baseUrl).toEqual('/api/v2/');
  45. });
  46. it('should set loginUrl', () => {
  47. authProvider.loginUrl = '/api/sign_in';
  48. expect(authProvider.loginUrl).toEqual('/api/sign_in');
  49. });
  50. it('should set signupUrl', () => {
  51. authProvider.signupUrl = '/api/register';
  52. expect(authProvider.signupUrl).toEqual('/api/register');
  53. });
  54. it('should set unlinkUrl', () => {
  55. authProvider.unlinkUrl = '/disconnect';
  56. expect(authProvider.unlinkUrl).toEqual('/disconnect');
  57. });
  58. it('should set tokenRoot', () => {
  59. authProvider.tokenRoot = 'deep.nested.object';
  60. expect(authProvider.tokenRoot).toEqual('deep.nested.object');
  61. });
  62. it('should set tokenName', () => {
  63. authProvider.tokenName = 'access_token';
  64. expect(authProvider.tokenName).toEqual('access_token');
  65. });
  66. it('should set tokenPrefix', () => {
  67. authProvider.tokenPrefix = 'myApp';
  68. expect(authProvider.tokenPrefix).toEqual('myApp');
  69. });
  70. it('should set tokenHeader', () => {
  71. authProvider.tokenHeader = 'x-auth-token';
  72. expect(authProvider.tokenHeader).toEqual('x-auth-token');
  73. });
  74. it('should set tokenType', () => {
  75. authProvider.tokenType = 'TOKEN';
  76. expect(authProvider.tokenType).toEqual('TOKEN');
  77. });
  78. it('should set withCredentials', () => {
  79. authProvider.withCredentials = false;
  80. expect(authProvider.withCredentials).toEqual(false);
  81. });
  82. it('should set storageType', () => {
  83. authProvider.storageType = 'sessionStorage';
  84. expect(authProvider.storageType).toEqual('sessionStorage');
  85. });
  86. it('should set httpInterceptor as a boolean', () => {
  87. authProvider.httpInterceptor = false;
  88. expect(authProvider.httpInterceptor()).toEqual(false);
  89. });
  90. it('should set httpInterceptor as a function', () => {
  91. authProvider.httpInterceptor = (request) => {
  92. return request.uri.indexOf('/api/') === 0;
  93. };
  94. expect(authProvider.httpInterceptor({ uri: '/somewhere/else' })).toEqual(false);
  95. });
  96. it('should set facebook with new params', () => {
  97. authProvider.facebook({ clientId: '1234' });
  98. expect(config.providers.facebook.clientId).toBe('1234');
  99. });
  100. it('should set google with new params', () => {
  101. authProvider.google({ state: 'secret' });
  102. expect(config.providers.google.state).toBe('secret');
  103. });
  104. it('should set github with new params', () => {
  105. authProvider.github({ clientId: '1234' });
  106. expect(config.providers.github.clientId).toBe('1234');
  107. });
  108. it('should set linkedin with new params', () => {
  109. authProvider.linkedin({ state: 'secret' });
  110. expect(config.providers.linkedin.state).toBe('secret');
  111. });
  112. it('should set twitter with new params', () => {
  113. authProvider.twitter({ url: '/api/twitter' });
  114. expect(config.providers.twitter.url).toBe('/api/twitter');
  115. });
  116. it('should create new OAuth 2.0 provider', () => {
  117. authProvider.oauth2({ name: 'instagram', url: '/auth/instagram' });
  118. expect(config.providers.instagram.name).toBe('instagram');
  119. expect(config.providers.instagram.url).toBe('/auth/instagram');
  120. });
  121. it('should create new OAuth 1.0 provider', () => {
  122. authProvider.oauth1({ name: 'goodreads', url: '/auth/goodreads' });
  123. expect(config.providers.goodreads.url).toBe('/auth/goodreads');
  124. });
  125. describe('$auth service', () => {
  126. it('should be defined', () => {
  127. expect(auth).toBeDefined();
  128. });
  129. describe('authenticate()', () => {
  130. it('should be defined', () => {
  131. expect(auth.authenticate).toBeDefined();
  132. });
  133. it('should authenticate', () => {
  134. spyOn(oauth, 'authenticate');
  135. auth.authenticate('facebook');
  136. expect(oauth.authenticate).toHaveBeenCalled();
  137. });
  138. });
  139. describe('isAuthenticated()', () => {
  140. it('should be defined', () => {
  141. expect(auth.isAuthenticated).toBeDefined();
  142. });
  143. it('should check if user is authenticated', () => {
  144. const storageType = config.storageType;
  145. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  146. window[storageType][tokenName] = token;
  147. expect(auth.isAuthenticated()).toBe(true);
  148. });
  149. });
  150. describe('getToken()', () => {
  151. it('should be defined', () => {
  152. expect(auth.getToken).toBeDefined();
  153. });
  154. it('should get token', () => {
  155. const storageType = config.storageType;
  156. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  157. window[storageType][tokenName] = token;
  158. expect(auth.getToken()).toEqual(window[storageType][tokenName]);
  159. });
  160. });
  161. describe('setToken()', () => {
  162. it('should be defined', () => {
  163. expect(auth.setToken).toBeDefined();
  164. });
  165. it('should set token', () => {
  166. const response = {
  167. data: {
  168. token: token
  169. }
  170. };
  171. auth.setToken(response);
  172. expect(token).toEqual(auth.getToken());
  173. });
  174. });
  175. describe('removeToken()', () => {
  176. it('should be defined', () => {
  177. expect(auth.removeToken).toBeDefined();
  178. });
  179. it('should remove token', () => {
  180. const storageType = config.storageType;
  181. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  182. window[storageType][tokenName] = token;
  183. auth.removeToken();
  184. expect(window.localStorage[tokenName]).toBeUndefined();
  185. });
  186. });
  187. describe('getPayload()', () => {
  188. it('should be defined', () => {
  189. expect(auth.getPayload).toBeDefined();
  190. });
  191. it('should get a JWT payload', () => {
  192. const storageType = config.storageType;
  193. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  194. window[storageType][tokenName] = token;
  195. const payload = auth.getPayload();
  196. expect(payload).toBeDefined();
  197. expect(angular.isObject(payload)).toBe(true);
  198. });
  199. });
  200. describe('link()', () => {
  201. it('should be defined', () => {
  202. expect(auth.link).toBeDefined();
  203. });
  204. it('should link third-party provider', () => {
  205. spyOn(oauth, 'authenticate');
  206. auth.link('facebook');
  207. expect(oauth.authenticate).toHaveBeenCalled();
  208. });
  209. });
  210. describe('unlink()', () => {
  211. it('should be defined', () => {
  212. expect(auth.unlink).toBeDefined();
  213. });
  214. it('should unlink third-party provider', () => {
  215. let result = null;
  216. httpBackend.expectPOST('/auth/unlink/').respond(200);
  217. auth.unlink('facebook').then((response) => {
  218. result = response.status;
  219. });
  220. httpBackend.flush();
  221. expect(result).toBe(200);
  222. });
  223. });
  224. describe('logout()', () => {
  225. it('should be defined', () => {
  226. expect(auth.logout).toBeDefined();
  227. });
  228. it('should log out a user', () => {
  229. const storageType = config.storageType;
  230. const tokenName = [config.tokenPrefix, config.tokenName].join('_');
  231. auth.logout();
  232. expect([storageType][tokenName]).toBeUndefined();
  233. });
  234. });
  235. describe('login()', () => {
  236. it('should be defined', () => {
  237. expect(auth.login).toBeDefined();
  238. });
  239. it('should be able to call login', function () {
  240. spyOn(local, 'login');
  241. const user = { email: 'foo@bar.com', password: '1234' };
  242. auth.login(user);
  243. expect(local.login).toHaveBeenCalled();
  244. });
  245. describe('signup()', () => {
  246. it('should be able to call signup', () => {
  247. spyOn(local, 'signup');
  248. const user = { email: 'foo@bar.com', password: '1234' };
  249. auth.signup(user);
  250. expect(local.signup).toHaveBeenCalled();
  251. });
  252. });
  253. describe('setStorageType()', () => {
  254. it('should be defined', () => {
  255. expect(auth.setStorageType).toBeDefined();
  256. });
  257. it('should set storage type', () => {
  258. auth.setStorageType('sessionStorage');
  259. expect(config.storageType).toBe('sessionStorage');
  260. });
  261. });
  262. });
  263. });
  264. });