token.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * @license Copyright (c) 2003-2020, 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. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  8. describe( 'Token', () => {
  9. let requests;
  10. beforeEach( () => {
  11. requests = [];
  12. const xhr = sinon.useFakeXMLHttpRequest();
  13. xhr.onCreate = request => {
  14. requests.push( request );
  15. };
  16. } );
  17. afterEach( () => {
  18. sinon.restore();
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'should throw error when no tokenUrl provided', () => {
  22. expect( () => new Token() ).to.throw(
  23. CKEditorError,
  24. 'token-missing-token-url'
  25. );
  26. } );
  27. it( 'should set a init token value', () => {
  28. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  29. expect( token.value ).to.equal( 'initValue' );
  30. } );
  31. it( 'should fire `change:value` event if the value of the token has changed', done => {
  32. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  33. token.on( 'change:value', ( event, name, newValue ) => {
  34. expect( newValue ).to.equal( 'token-value' );
  35. done();
  36. } );
  37. token.init();
  38. requests[ 0 ].respond( 200, '', 'token-value' );
  39. } );
  40. it( 'should accept the callback in the constructor', () => {
  41. expect( () => {
  42. // eslint-disable-next-line
  43. const token = new Token( () => Promise.resolve( 'token' ) );
  44. } ).to.not.throw();
  45. } );
  46. } );
  47. describe( 'init()', () => {
  48. it( 'should get a token value from endpoint', done => {
  49. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  50. token.init()
  51. .then( () => {
  52. expect( token.value ).to.equal( 'token-value' );
  53. done();
  54. } );
  55. requests[ 0 ].respond( 200, '', 'token-value' );
  56. } );
  57. it( 'should get a token from the refreshToken function when is provided', () => {
  58. const token = new Token( () => Promise.resolve( 'token-value' ), { autoRefresh: false } );
  59. return token.init()
  60. .then( () => {
  61. expect( token.value ).to.equal( 'token-value' );
  62. } );
  63. } );
  64. it( 'should refresh token with time specified in token `exp` payload property', async () => {
  65. const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout' ] } );
  66. const tokenInitValue = `header.${ btoa( JSON.stringify( { exp: Date.now() + 3600000 } ) ) }.signature`;
  67. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue } );
  68. await token.init();
  69. await clock.tickAsync( 1800000 );
  70. requests[ 0 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 150000 } ) ) }.signature` );
  71. await clock.tickAsync( 75000 );
  72. requests[ 1 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 10000 } ) ) }.signature` );
  73. await clock.tickAsync( 5000 );
  74. requests[ 2 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 2000 } ) ) }.signature` );
  75. await clock.tickAsync( 1000 );
  76. requests[ 3 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 300 } ) ) }.signature` );
  77. await clock.tickAsync( 150 );
  78. requests[ 4 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 300 } ) ) }.signature` );
  79. expect( requests.length ).to.equal( 5 );
  80. clock.restore();
  81. } );
  82. } );
  83. describe( 'destroy', () => {
  84. it( 'should stop refreshing the token', async () => {
  85. const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout', 'clearTimeout' ] } );
  86. const tokenInitValue = `header.${ btoa( JSON.stringify( { exp: Date.now() + 3600000 } ) ) }.signature`;
  87. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue } );
  88. await token.init();
  89. await clock.tickAsync( 1800000 );
  90. requests[ 0 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 150000 } ) ) }.signature` );
  91. await clock.tickAsync( 100 );
  92. await clock.tickAsync( 75000 );
  93. requests[ 1 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 10000 } ) ) }.signature` );
  94. await clock.tickAsync( 100 );
  95. token.destroy();
  96. await clock.tickAsync( 3600000 );
  97. await clock.tickAsync( 3600000 );
  98. await clock.tickAsync( 3600000 );
  99. expect( requests.length ).to.equal( 2 );
  100. clock.restore();
  101. } );
  102. } );
  103. describe( 'refreshToken()', () => {
  104. it( 'should get a token from the specified address', done => {
  105. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  106. token.refreshToken()
  107. .then( newToken => {
  108. expect( newToken.value ).to.equal( 'token-value' );
  109. done();
  110. } );
  111. requests[ 0 ].respond( 200, '', 'token-value' );
  112. } );
  113. it( 'should get a token from the specified callback function', () => {
  114. const token = new Token( () => Promise.resolve( 'token-value' ), { initValue: 'initValue', autoRefresh: false } );
  115. return token.refreshToken()
  116. .then( newToken => {
  117. expect( newToken.value ).to.equal( 'token-value' );
  118. } );
  119. } );
  120. it( 'should throw an error when cannot download new token', () => {
  121. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  122. const promise = token._refresh();
  123. requests[ 0 ].respond( 401 );
  124. return promise.then( () => {
  125. throw new Error( 'Promise should be rejected' );
  126. }, error => {
  127. expect( error.constructor ).to.equal( CKEditorError );
  128. expect( error ).to.match( /token-cannot-download-new-token/ );
  129. } );
  130. } );
  131. it( 'should throw an error when the response is aborted', () => {
  132. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  133. const promise = token._refresh();
  134. requests[ 0 ].abort();
  135. return promise.then( () => {
  136. throw new Error( 'Promise should be rejected' );
  137. }, error => {
  138. expect( error ).to.match( /Abort/ );
  139. } );
  140. } );
  141. it( 'should throw an error when network error occurs', () => {
  142. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  143. const promise = token._refresh();
  144. requests[ 0 ].error();
  145. return promise.then( () => {
  146. throw new Error( 'Promise should be rejected' );
  147. }, error => {
  148. expect( error ).to.match( /Network Error/ );
  149. } );
  150. } );
  151. it( 'should throw an error when the callback throws error', () => {
  152. const token = new Token( () => Promise.reject( 'Custom error occurred' ), { initValue: 'initValue', autoRefresh: false } );
  153. token.refreshToken()
  154. .catch( error => {
  155. expect( error ).to.equal( 'Custom error occurred' );
  156. } );
  157. } );
  158. } );
  159. describe( '_registerRefreshTokenTimeout()', () => {
  160. it( 'should register refresh token timeout and run refresh after that time', async () => {
  161. const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout' ] } );
  162. const tokenInitValue = `header.${ btoa( JSON.stringify( { exp: Date.now() + 3600000 } ) ) }.signature`;
  163. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue, autoRefresh: false } );
  164. token._registerRefreshTokenTimeout();
  165. await clock.tickAsync( 1800000 );
  166. expect( requests.length ).to.equal( 1 );
  167. clock.restore();
  168. } );
  169. } );
  170. describe( '_getTokenRefreshTimeoutTime', () => {
  171. it( 'should return timeout time based on expiration time in token for valid token', () => {
  172. const tokenInitValue = `header.${ btoa( JSON.stringify( { exp: Date.now() + 3600000 } ) ) }.signature`;
  173. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue, autoRefresh: false } );
  174. const timeoutTime = token._getTokenRefreshTimeoutTime();
  175. expect( timeoutTime ).to.eq( 1800000 );
  176. } );
  177. it( 'should return default refresh timeout time if token parse fails', () => {
  178. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  179. const timeoutTime = token._getTokenRefreshTimeoutTime();
  180. expect( timeoutTime ).to.eq( 3600000 );
  181. } );
  182. } );
  183. describe( 'static create()', () => {
  184. it( 'should return a initialized token', done => {
  185. Token.create( 'http://token-endpoint', { autoRefresh: false } )
  186. .then( token => {
  187. expect( token.value ).to.equal( 'token-value' );
  188. done();
  189. } );
  190. requests[ 0 ].respond( 200, '', 'token-value' );
  191. } );
  192. } );
  193. } );