token.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 not refresh token if autoRefresh is disabled in options', 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, autoRefresh: false } );
  68. await token.init();
  69. await clock.tickAsync( 1800000 );
  70. expect( requests ).to.be.empty;
  71. clock.restore();
  72. } );
  73. it( 'should refresh token with time specified in token `exp` payload property', async () => {
  74. const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout' ] } );
  75. const tokenInitValue = `header.${ btoa( JSON.stringify( { exp: Date.now() + 3600000 } ) ) }.signature`;
  76. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue } );
  77. await token.init();
  78. await clock.tickAsync( 1800000 );
  79. requests[ 0 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 150000 } ) ) }.signature` );
  80. await clock.tickAsync( 75000 );
  81. requests[ 1 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 10000 } ) ) }.signature` );
  82. await clock.tickAsync( 5000 );
  83. requests[ 2 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 2000 } ) ) }.signature` );
  84. await clock.tickAsync( 1000 );
  85. requests[ 3 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 300 } ) ) }.signature` );
  86. await clock.tickAsync( 150 );
  87. requests[ 4 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 300 } ) ) }.signature` );
  88. expect( requests.length ).to.equal( 5 );
  89. clock.restore();
  90. } );
  91. } );
  92. describe( 'destroy', () => {
  93. it( 'should stop refreshing the token', async () => {
  94. const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout', 'clearTimeout' ] } );
  95. const tokenInitValue = `header.${ btoa( JSON.stringify( { exp: Date.now() + 3600000 } ) ) }.signature`;
  96. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue } );
  97. await token.init();
  98. await clock.tickAsync( 1800000 );
  99. requests[ 0 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 150000 } ) ) }.signature` );
  100. await clock.tickAsync( 100 );
  101. await clock.tickAsync( 75000 );
  102. requests[ 1 ].respond( 200, '', `header.${ btoa( JSON.stringify( { exp: Date.now() + 10000 } ) ) }.signature` );
  103. await clock.tickAsync( 100 );
  104. token.destroy();
  105. await clock.tickAsync( 3600000 );
  106. await clock.tickAsync( 3600000 );
  107. await clock.tickAsync( 3600000 );
  108. expect( requests.length ).to.equal( 2 );
  109. clock.restore();
  110. } );
  111. } );
  112. describe( 'refreshToken()', () => {
  113. it( 'should get a token from the specified address', done => {
  114. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  115. token.refreshToken()
  116. .then( newToken => {
  117. expect( newToken.value ).to.equal( 'token-value' );
  118. done();
  119. } );
  120. requests[ 0 ].respond( 200, '', 'token-value' );
  121. } );
  122. it( 'should get a token from the specified callback function', () => {
  123. const token = new Token( () => Promise.resolve( 'token-value' ), { initValue: 'initValue', autoRefresh: false } );
  124. return token.refreshToken()
  125. .then( newToken => {
  126. expect( newToken.value ).to.equal( 'token-value' );
  127. } );
  128. } );
  129. it( 'should throw an error when cannot download new token', () => {
  130. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  131. const promise = token._refresh();
  132. requests[ 0 ].respond( 401 );
  133. return promise.then( () => {
  134. throw new Error( 'Promise should be rejected' );
  135. }, error => {
  136. expect( error.constructor ).to.equal( CKEditorError );
  137. expect( error ).to.match( /token-cannot-download-new-token/ );
  138. } );
  139. } );
  140. it( 'should throw an error when the response is aborted', () => {
  141. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  142. const promise = token._refresh();
  143. requests[ 0 ].abort();
  144. return promise.then( () => {
  145. throw new Error( 'Promise should be rejected' );
  146. }, error => {
  147. expect( error ).to.match( /Abort/ );
  148. } );
  149. } );
  150. it( 'should throw an error when network error occurs', () => {
  151. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  152. const promise = token._refresh();
  153. requests[ 0 ].error();
  154. return promise.then( () => {
  155. throw new Error( 'Promise should be rejected' );
  156. }, error => {
  157. expect( error ).to.match( /Network Error/ );
  158. } );
  159. } );
  160. it( 'should throw an error when the callback throws error', () => {
  161. const token = new Token( () => Promise.reject( 'Custom error occurred' ), { initValue: 'initValue', autoRefresh: false } );
  162. token.refreshToken()
  163. .catch( error => {
  164. expect( error ).to.equal( 'Custom error occurred' );
  165. } );
  166. } );
  167. } );
  168. describe( '_registerRefreshTokenTimeout()', () => {
  169. it( 'should register refresh token timeout and run refresh after that time', async () => {
  170. const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout' ] } );
  171. const tokenInitValue = `header.${ btoa( JSON.stringify( { exp: Date.now() + 3600000 } ) ) }.signature`;
  172. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue, autoRefresh: false } );
  173. token._registerRefreshTokenTimeout();
  174. await clock.tickAsync( 1800000 );
  175. expect( requests.length ).to.equal( 1 );
  176. clock.restore();
  177. } );
  178. } );
  179. describe( '_getTokenRefreshTimeoutTime', () => {
  180. it( 'should return timeout time based on expiration time in token for valid token', () => {
  181. const tokenInitValue = `header.${ btoa( JSON.stringify( { exp: Date.now() + 3600000 } ) ) }.signature`;
  182. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue, autoRefresh: false } );
  183. const timeoutTime = token._getTokenRefreshTimeoutTime();
  184. expect( timeoutTime ).to.eq( 1800000 );
  185. } );
  186. it( 'should return default refresh timeout time if token parse fails', () => {
  187. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  188. const timeoutTime = token._getTokenRefreshTimeoutTime();
  189. expect( timeoutTime ).to.eq( 3600000 );
  190. } );
  191. } );
  192. describe( 'static create()', () => {
  193. it( 'should return a initialized token', done => {
  194. Token.create( 'http://token-endpoint', { autoRefresh: false } )
  195. .then( token => {
  196. expect( token.value ).to.equal( 'token-value' );
  197. done();
  198. } );
  199. requests[ 0 ].respond( 200, '', 'token-value' );
  200. } );
  201. it( 'should use default options when none passed', done => {
  202. Token.create( 'http://token-endpoint' )
  203. .then( token => {
  204. expect( token._options ).to.eql( { autoRefresh: true } );
  205. done();
  206. } );
  207. requests[ 0 ].respond( 200, '', 'token-value' );
  208. } );
  209. } );
  210. } );