Brak opisu

local.spec.ts 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import Local from '../src/local';
  2. import Shared from '../src/shared';
  3. import Storage from '../src/storage';
  4. import Config from '../src/config';
  5. let httpBackend;
  6. let location;
  7. let window;
  8. let config;
  9. let storage;
  10. let shared;
  11. let local;
  12. describe('Local', () => {
  13. beforeEach(angular.mock.inject(($q, $http, $window, $log, $httpBackend, $location) => {
  14. httpBackend = $httpBackend;
  15. location = $location;
  16. window = $window;
  17. config = new Config();
  18. storage = new Storage($window, config);
  19. shared = new Shared($q, $window, $log, config, storage);
  20. local = new Local($http, config, shared);
  21. }));
  22. afterEach(() => {
  23. window.localStorage.clear();
  24. window.sessionStorage.clear();
  25. httpBackend.verifyNoOutstandingExpectation();
  26. httpBackend.verifyNoOutstandingRequest();
  27. });
  28. describe('login()', () => {
  29. it('should be defined', () => {
  30. expect(local.login).toBeDefined();
  31. });
  32. it('should return a user object on successful login', () => {
  33. let result = null;
  34. const user = { email: 'john@email.com', password: 'password' };
  35. const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7Il9pZCI6IjUzZjYxZTEwNmZjNjFhNmMxM2I1Mjc4ZCIsImVtYWlsIjoic2FoYXQ_QG1lLmNvbSIsIl9fdiI6MH0sImlhdCI6MTQwODgyMTA5MTY3NiwiZXhwIjoxNDA5NDI1ODkxNjc2fQ.0l-ql-ZVjHiILMcMegNb3bNqapt3TZwjHy_ieduioiQ';
  36. httpBackend.expectPOST(config.loginUrl).respond({ token: token });
  37. local.login(user).then((response) => {
  38. result = response.data.token;
  39. });
  40. httpBackend.flush();
  41. expect(shared.isAuthenticated()).toBe(true);
  42. expect(result).toEqual(token);
  43. });
  44. it('should fail login with incorrect credentials', () => {
  45. let result = null;
  46. const user = { email: 'foo@bar.com', password: 'invalid' };
  47. httpBackend.expectPOST(config.loginUrl).respond(401, 'Wrong email or password');
  48. local.login(user).catch((response) => {
  49. result = response.data;
  50. });
  51. httpBackend.flush();
  52. expect(shared.isAuthenticated()).toBe(false);
  53. expect(result).toEqual('Wrong email or password');
  54. });
  55. });
  56. describe('signup()', () => {
  57. it('should have a signup function', () => {
  58. expect(local.signup).toBeDefined();
  59. expect(angular.isFunction(local.signup)).toBe(true);
  60. });
  61. it('should create a new user', () => {
  62. let result = null;
  63. const user = { email: 'john@email.com', password: '1234' };
  64. const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7Il9pZCI6IjUzZjYxZTEwNmZjNjFhNmMxM2I1Mjc4ZCIsImVtYWlsIjoic2FoYXQ_QG1lLmNvbSIsIl9fdiI6MH0sImlhdCI6MTQwODgyMTA5MTY3NiwiZXhwIjoxNDA5NDI1ODkxNjc2fQ.0l-ql-ZVjHiILMcMegNb3bNqapt3TZwjHy_ieduioiQ';
  65. httpBackend.expectPOST(config.signupUrl).respond({ token: token });
  66. local.signup(user).then((response) => {
  67. result = response.data.token;
  68. });
  69. httpBackend.flush();
  70. expect(result).toEqual(token);
  71. });
  72. it('should be able to handle signup errors', () => {
  73. let result = null;
  74. const user = { email: 'foo@bar.com', password: '1234' };
  75. httpBackend.expectPOST(config.signupUrl).respond(400);
  76. local.signup(user).catch(() => {
  77. result = false;
  78. });
  79. httpBackend.flush();
  80. expect(result).toBe(false);
  81. });
  82. });
  83. });