token.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* eslint-env commonjs, browser */
  6. 'use strict';
  7. import Token from '../../src/token/token';
  8. describe( 'Token', () => {
  9. let requests;
  10. beforeEach( () => {
  11. requests = [];
  12. global.xhr = sinon.useFakeXMLHttpRequest();
  13. global.xhr.onCreate = xhr => {
  14. requests.push( xhr );
  15. };
  16. } );
  17. afterEach( () => global.xhr.restore() );
  18. describe( 'constructor()', () => {
  19. it( 'should throw error when no tokenUrl provided', () => {
  20. expect( () => new Token() ).to.throw(
  21. 'A `tokenUrl` must be provided as the first constructor argument.'
  22. );
  23. } );
  24. it( 'should set a init token value', () => {
  25. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  26. expect( token.value ).to.equal( 'initValue' );
  27. } );
  28. it( 'should fire `change:value` event if the value of the token has changed', done => {
  29. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  30. token.on( 'change:value', ( event, name, newValue ) => {
  31. expect( newValue ).to.equal( 'token-value' );
  32. done();
  33. } );
  34. token.init();
  35. requests[ 0 ].respond( 200, '', 'token-value' );
  36. } );
  37. it( 'should accept the callback in the constructor', () => {
  38. expect( () => {
  39. // eslint-disable-next-line
  40. const token = new Token( () => Promise.resolve( 'token' ) );
  41. } ).to.not.throw();
  42. } );
  43. } );
  44. describe( 'init()', () => {
  45. it( 'should get a token value from endpoint', done => {
  46. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  47. token.init()
  48. .then( () => {
  49. expect( token.value ).to.equal( 'token-value' );
  50. done();
  51. } );
  52. requests[ 0 ].respond( 200, '', 'token-value' );
  53. } );
  54. it( 'should get a token from the refreshToken function when is provided', () => {
  55. const token = new Token( () => Promise.resolve( 'token-value' ), { autoRefresh: false } );
  56. return token.init()
  57. .then( () => {
  58. expect( token.value ).to.equal( 'token-value' );
  59. } );
  60. } );
  61. it( 'should start token refresh every 1 hour', done => {
  62. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
  63. const token = new Token( 'http://token-endpoint', { initValue: 'initValue' } );
  64. token.init()
  65. .then( () => {
  66. clock.tick( 3600000 );
  67. clock.tick( 3600000 );
  68. clock.tick( 3600000 );
  69. clock.tick( 3600000 );
  70. clock.tick( 3600000 );
  71. expect( requests.length ).to.equal( 5 );
  72. clock.restore();
  73. done();
  74. } );
  75. } );
  76. } );
  77. describe( 'destroy', () => {
  78. it( 'should stop refreshing the token', () => {
  79. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
  80. const token = new Token( 'http://token-endpoint', { initValue: 'initValue' } );
  81. return token.init()
  82. .then( () => {
  83. clock.tick( 3600000 );
  84. clock.tick( 3600000 );
  85. expect( requests.length ).to.equal( 2 );
  86. token.destroy();
  87. clock.tick( 3600000 );
  88. clock.tick( 3600000 );
  89. clock.tick( 3600000 );
  90. expect( requests.length ).to.equal( 2 );
  91. } );
  92. } );
  93. } );
  94. describe( '_refreshToken()', () => {
  95. it( 'should get a token from the specified address', done => {
  96. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  97. token._refreshToken()
  98. .then( newToken => {
  99. expect( newToken.value ).to.equal( 'token-value' );
  100. done();
  101. } );
  102. requests[ 0 ].respond( 200, '', 'token-value' );
  103. } );
  104. it( 'should get a token from the specified callback function', () => {
  105. const token = new Token( () => Promise.resolve( 'token-value' ), { initValue: 'initValue', autoRefresh: false } );
  106. return token._refreshToken()
  107. .then( newToken => {
  108. expect( newToken.value ).to.equal( 'token-value' );
  109. } );
  110. } );
  111. it( 'should throw an error when cannot download new token', () => {
  112. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  113. const promise = token._refresh();
  114. requests[ 0 ].respond( 401 );
  115. return promise.then( () => {
  116. throw new Error( 'Promise should be rejected' );
  117. }, error => {
  118. expect( error ).to.match( /Cannot download new token!/ );
  119. } );
  120. } );
  121. it( 'should throw an error when the response is aborted', () => {
  122. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  123. const promise = token._refresh();
  124. requests[ 0 ].abort();
  125. return promise.then( () => {
  126. throw new Error( 'Promise should be rejected' );
  127. }, error => {
  128. expect( error ).to.match( /Abort/ );
  129. } );
  130. } );
  131. it( 'should throw an error when network error occurs', () => {
  132. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  133. const promise = token._refresh();
  134. requests[ 0 ].error();
  135. return promise.then( () => {
  136. throw new Error( 'Promise should be rejected' );
  137. }, error => {
  138. expect( error ).to.match( /Network Error/ );
  139. } );
  140. } );
  141. it( 'should throw an error when the callback throws error', () => {
  142. const token = new Token( () => Promise.reject( 'Custom error occurred' ), { initValue: 'initValue', autoRefresh: false } );
  143. token._refreshToken()
  144. .catch( error => {
  145. expect( error ).to.equal( 'Custom error occurred' );
  146. } );
  147. } );
  148. } );
  149. describe( '_startRefreshing()', () => {
  150. it( 'should start refreshing', () => {
  151. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
  152. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  153. token._startRefreshing();
  154. clock.tick( 3600000 );
  155. clock.tick( 3600000 );
  156. clock.tick( 3600000 );
  157. clock.tick( 3600000 );
  158. clock.tick( 3600000 );
  159. expect( requests.length ).to.equal( 5 );
  160. clock.restore();
  161. } );
  162. } );
  163. describe( '_stopRefreshing()', () => {
  164. it( 'should stop refreshing', done => {
  165. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
  166. const token = new Token( 'http://token-endpoint', { initValue: 'initValue' } );
  167. token.init()
  168. .then( () => {
  169. clock.tick( 3600000 );
  170. clock.tick( 3600000 );
  171. clock.tick( 3600000 );
  172. token._stopRefreshing();
  173. clock.tick( 3600000 );
  174. clock.tick( 3600000 );
  175. expect( requests.length ).to.equal( 3 );
  176. clock.restore();
  177. done();
  178. } );
  179. } );
  180. } );
  181. describe( 'static create()', () => {
  182. it( 'should return a initialized token', done => {
  183. Token.create( 'http://token-endpoint', { autoRefresh: false } )
  184. .then( token => {
  185. expect( token.value ).to.equal( 'token-value' );
  186. done();
  187. } );
  188. requests[ 0 ].respond( 200, '', 'token-value' );
  189. } );
  190. } );
  191. } );