token.js 6.7 KB

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