Нема описа

oauth2.spec.ts 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import OAuth2 from '../src/oauth2';
  2. import Config from '../src/config';
  3. import Shared from '../src/shared';
  4. import Storage from '../src/storage';
  5. import Popup from '../src/popup';
  6. let q;
  7. let window;
  8. let http;
  9. let timeout;
  10. let httpBackend;
  11. let config;
  12. let storage;
  13. let shared;
  14. let popup;
  15. let oauth2;
  16. describe('OAuth2', () => {
  17. beforeEach(angular.mock.inject(($q, $http, $window, $interval, $log, $timeout, $httpBackend) => {
  18. q = $q;
  19. http = $http;
  20. window = $window;
  21. timeout = $timeout;
  22. httpBackend = $httpBackend;
  23. config = new Config();
  24. storage = new Storage($window, config);
  25. shared = new Shared($q, $window, $log, config, storage);
  26. popup = new Popup($interval, $window, $q);
  27. oauth2 = new OAuth2($http, $window, $timeout, $q, config, popup, storage);
  28. }));
  29. afterEach(() => {
  30. httpBackend.verifyNoOutstandingExpectation();
  31. httpBackend.verifyNoOutstandingRequest();
  32. });
  33. describe('init()', () => {
  34. it('should be defined', () => {
  35. expect(oauth2.init).toBeDefined();
  36. });
  37. it('should open a popup window', () => {
  38. spyOn(window, 'open');
  39. oauth2.init(config.providers.github);
  40. timeout.flush();
  41. timeout.verifyNoPendingTasks();
  42. expect(window.open).toHaveBeenCalled();
  43. });
  44. it('should propagate promise rejections', () => {
  45. let result = null;
  46. const p = Promise.reject(new Error('The popup window was closed'));
  47. spyOn(popup, 'polling').and.returnValue(p);
  48. oauth2.init(config.providers.github).catch((err) => {
  49. result = err.message;
  50. expect(result).toEqual('The popup window was closed');
  51. });
  52. timeout.flush();
  53. timeout.verifyNoPendingTasks();
  54. expect(popup.polling).toHaveBeenCalled();
  55. });
  56. });
  57. describe('exchangeForToken()', () => {
  58. it('should be defined', () => {
  59. expect(oauth2.exchangeForToken).toBeDefined();
  60. });
  61. it('should exchange code for token', () => {
  62. let result = null;
  63. httpBackend.expectPOST('/').respond(200, { token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9' });
  64. oauth2.exchangeForToken({ code: '1234567890' }).then((response) => {
  65. result = response.data.token;
  66. });
  67. httpBackend.flush();
  68. expect('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9');
  69. });
  70. it('should exchange code for token with custom responseParams', () => {
  71. let result = null;
  72. const deferred = q.defer();
  73. spyOn(http, 'post').and.returnValue(deferred.promise);
  74. oauth2.defaults.clientId = 'asdf1234';
  75. oauth2.defaults.redirectUri = 'http://localhost/callback';
  76. oauth2.defaults.responseParams = {
  77. code: 'code',
  78. clientId: 'client_id',
  79. redirectUri: 'redirect_uri'
  80. };
  81. oauth2.exchangeForToken({ code: '1234567890' }).then((response) => {
  82. result = response.data.token;
  83. });
  84. expect(http.post).toHaveBeenCalledWith(
  85. '/',
  86. { code: '1234567890', client_id: 'asdf1234', redirect_uri: 'http://localhost/callback' },
  87. { withCredentials: false }
  88. );
  89. });
  90. });
  91. xdescribe('buildQueryString()', () => {
  92. it('should be defined', () => {
  93. expect(oauth2.buildQueryString).toBeDefined();
  94. });
  95. it('should URI-encode state value', () => {
  96. oauth2.open({ defaultUrlParams: ['state'], state: 'foo+bar' }).then(() => {
  97. expect(oauth2.buildQueryString()).toContain('state=foo%2Bbar');
  98. });
  99. timeout.flush();
  100. timeout.verifyNoPendingTasks();
  101. });
  102. it('should use scopePrefix if provided', () => {
  103. oauth2.open(config.providers.google)
  104. .then(() => {
  105. expect(oauth2.buildQueryString()).toContain('scope=openid profile email');
  106. });
  107. timeout.flush();
  108. timeout.verifyNoPendingTasks();
  109. });
  110. it('should remove redirect_uri if param redirectUrl is null', () => {
  111. const tempProvider = angular.copy(config.providers.google);
  112. tempProvider.redirectUri = null;
  113. oauth2.open(tempProvider)
  114. .then(() => {
  115. expect(oauth2.buildQueryString()).not.toContain('redirect_uri=');
  116. });
  117. this.$timeout.flush();
  118. this.$timeout.verifyNoPendingTasks();
  119. });
  120. });
  121. describe('camelCase()', () => {
  122. it('should be defined', () => {
  123. expect(OAuth2.camelCase).toBeDefined();
  124. });
  125. it('should return camelized string', () => {
  126. expect(OAuth2.camelCase('redirect_uri')).toEqual('redirectUri');
  127. });
  128. });
  129. });