token.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 an error when no tokenUrl provided', () => {
  22. expect( () => new Token() ).to.throw(
  23. CKEditorError,
  24. 'token-missing-token-url'
  25. );
  26. } );
  27. it( 'should throw an error if the token passed in options is not a string', () => {
  28. expect( () => new Token( 'http://token-endpoint', { initValue: 123456 } ) ).to.throw(
  29. CKEditorError,
  30. 'token-not-in-jwt-format'
  31. );
  32. } );
  33. it( 'should throw an error if the token passed in options is wrapped in additional quotes', () => {
  34. const tokenInitValue = getTestTokenValue();
  35. expect( () => new Token( 'http://token-endpoint', { initValue: `"${ tokenInitValue }"` } ) ).to.throw(
  36. CKEditorError,
  37. 'token-not-in-jwt-format'
  38. );
  39. } );
  40. it( 'should throw an error if the token passed in options is not a valid JWT token', () => {
  41. expect( () => new Token( 'http://token-endpoint', { initValue: 'token' } ) ).to.throw(
  42. CKEditorError,
  43. 'token-not-in-jwt-format'
  44. );
  45. } );
  46. it( 'should set token value if the token passed in options is valid', () => {
  47. const tokenInitValue = getTestTokenValue();
  48. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue } );
  49. expect( token.value ).to.equal( tokenInitValue );
  50. } );
  51. it( 'should fire `change:value` event if the value of the token has changed', done => {
  52. const tokenValue = getTestTokenValue();
  53. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  54. token.on( 'change:value', ( event, name, newValue ) => {
  55. expect( newValue ).to.equal( tokenValue );
  56. done();
  57. } );
  58. token.init();
  59. requests[ 0 ].respond( 200, '', tokenValue );
  60. } );
  61. it( 'should accept the callback in the constructor', () => {
  62. expect( () => {
  63. // eslint-disable-next-line
  64. const token = new Token( () => Promise.resolve( 'token' ) );
  65. } ).to.not.throw();
  66. } );
  67. } );
  68. describe( 'init()', () => {
  69. it( 'should get a token value from the endpoint', done => {
  70. const tokenValue = getTestTokenValue();
  71. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  72. token.init()
  73. .then( () => {
  74. expect( token.value ).to.equal( tokenValue );
  75. done();
  76. } );
  77. requests[ 0 ].respond( 200, '', tokenValue );
  78. } );
  79. it( 'should get a token from the refreshToken function when is provided', () => {
  80. const tokenValue = getTestTokenValue();
  81. const token = new Token( () => Promise.resolve( tokenValue ), { autoRefresh: false } );
  82. return token.init()
  83. .then( () => {
  84. expect( token.value ).to.equal( tokenValue );
  85. } );
  86. } );
  87. it( 'should not refresh token if autoRefresh is disabled in options', async () => {
  88. const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout' ] } );
  89. const tokenInitValue = getTestTokenValue();
  90. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue, autoRefresh: false } );
  91. await token.init();
  92. await clock.tickAsync( 3600000 );
  93. expect( requests ).to.be.empty;
  94. clock.restore();
  95. } );
  96. it( 'should refresh token with the time specified in token `exp` payload property', async () => {
  97. const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout' ] } );
  98. const tokenInitValue = getTestTokenValue();
  99. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue } );
  100. await token.init();
  101. await clock.tickAsync( 1800000 );
  102. requests[ 0 ].respond( 200, '', getTestTokenValue( 150000 ) );
  103. await clock.tickAsync( 75000 );
  104. requests[ 1 ].respond( 200, '', getTestTokenValue( 10000 ) );
  105. await clock.tickAsync( 5000 );
  106. requests[ 2 ].respond( 200, '', getTestTokenValue( 2000 ) );
  107. await clock.tickAsync( 1000 );
  108. requests[ 3 ].respond( 200, '', getTestTokenValue( 300 ) );
  109. await clock.tickAsync( 150 );
  110. requests[ 4 ].respond( 200, '', getTestTokenValue( 300 ) );
  111. expect( requests.length ).to.equal( 5 );
  112. clock.restore();
  113. } );
  114. it( 'should refresh the token with the default time if getting token expiration time failed', async () => {
  115. const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout' ] } );
  116. const tokenValue = 'header.test.signature';
  117. const token = new Token( 'http://token-endpoint', { initValue: tokenValue } );
  118. await token.init();
  119. await clock.tickAsync( 3600000 );
  120. requests[ 0 ].respond( 200, '', tokenValue );
  121. await clock.tickAsync( 3600000 );
  122. requests[ 1 ].respond( 200, '', tokenValue );
  123. expect( requests.length ).to.equal( 2 );
  124. clock.restore();
  125. } );
  126. } );
  127. describe( 'destroy', () => {
  128. it( 'should stop refreshing the token', async () => {
  129. const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout', 'clearTimeout' ] } );
  130. const tokenInitValue = getTestTokenValue();
  131. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue } );
  132. await token.init();
  133. await clock.tickAsync( 1800000 );
  134. requests[ 0 ].respond( 200, '', getTestTokenValue( 150000 ) );
  135. await clock.tickAsync( 100 );
  136. await clock.tickAsync( 75000 );
  137. requests[ 1 ].respond( 200, '', getTestTokenValue( 10000 ) );
  138. await clock.tickAsync( 100 );
  139. token.destroy();
  140. await clock.tickAsync( 3600000 );
  141. await clock.tickAsync( 3600000 );
  142. await clock.tickAsync( 3600000 );
  143. expect( requests.length ).to.equal( 2 );
  144. clock.restore();
  145. } );
  146. } );
  147. describe( 'refreshToken()', () => {
  148. it( 'should get a token from the specified address', done => {
  149. const tokenValue = getTestTokenValue();
  150. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  151. token.refreshToken()
  152. .then( newToken => {
  153. expect( newToken.value ).to.equal( tokenValue );
  154. done();
  155. } );
  156. requests[ 0 ].respond( 200, '', tokenValue );
  157. } );
  158. it( 'should throw an error if the returned token is wrapped in additional quotes', done => {
  159. const tokenValue = getTestTokenValue();
  160. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  161. token.refreshToken()
  162. .then( () => {
  163. done( new Error( 'Promise should be rejected' ) );
  164. } )
  165. .catch( error => {
  166. expect( error.constructor ).to.equal( CKEditorError );
  167. expect( error ).to.match( /token-not-in-jwt-format/ );
  168. done();
  169. } );
  170. requests[ 0 ].respond( 200, '', `"${ tokenValue }"` );
  171. } );
  172. it( 'should throw an error if the returned token is not a valid JWT token', done => {
  173. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  174. token.refreshToken()
  175. .then( () => {
  176. done( new Error( 'Promise should be rejected' ) );
  177. } )
  178. .catch( error => {
  179. expect( error.constructor ).to.equal( CKEditorError );
  180. expect( error ).to.match( /token-not-in-jwt-format/ );
  181. done();
  182. } );
  183. requests[ 0 ].respond( 200, '', 'token' );
  184. } );
  185. it( 'should get a token from the specified callback function', () => {
  186. const tokenValue = getTestTokenValue();
  187. const token = new Token( () => Promise.resolve( tokenValue ), { autoRefresh: false } );
  188. return token.refreshToken()
  189. .then( newToken => {
  190. expect( newToken.value ).to.equal( tokenValue );
  191. } );
  192. } );
  193. it( 'should throw an error when cannot download a new token', () => {
  194. const tokenInitValue = getTestTokenValue();
  195. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue, autoRefresh: false } );
  196. const promise = token._refresh();
  197. requests[ 0 ].respond( 401 );
  198. return promise.then( () => {
  199. throw new Error( 'Promise should be rejected' );
  200. }, error => {
  201. expect( error.constructor ).to.equal( CKEditorError );
  202. expect( error ).to.match( /token-cannot-download-new-token/ );
  203. } );
  204. } );
  205. it( 'should throw an error when the response is aborted', () => {
  206. const tokenInitValue = getTestTokenValue();
  207. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue, autoRefresh: false } );
  208. const promise = token._refresh();
  209. requests[ 0 ].abort();
  210. return promise.then( () => {
  211. throw new Error( 'Promise should be rejected' );
  212. }, error => {
  213. expect( error ).to.match( /Abort/ );
  214. } );
  215. } );
  216. it( 'should throw an error when network error occurs', () => {
  217. const tokenInitValue = getTestTokenValue();
  218. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue, autoRefresh: false } );
  219. const promise = token._refresh();
  220. requests[ 0 ].error();
  221. return promise.then( () => {
  222. throw new Error( 'Promise should be rejected' );
  223. }, error => {
  224. expect( error ).to.match( /Network Error/ );
  225. } );
  226. } );
  227. it( 'should throw an error when the callback throws an error', () => {
  228. const tokenInitValue = getTestTokenValue();
  229. const token = new Token( () => Promise.reject( 'Custom error occurred' ), { initValue: tokenInitValue, autoRefresh: false } );
  230. token.refreshToken()
  231. .catch( error => {
  232. expect( error ).to.equal( 'Custom error occurred' );
  233. } );
  234. } );
  235. } );
  236. describe( 'static create()', () => {
  237. it( 'should return an initialized token', done => {
  238. const tokenValue = getTestTokenValue();
  239. Token.create( 'http://token-endpoint', { autoRefresh: false } )
  240. .then( token => {
  241. expect( token.value ).to.equal( tokenValue );
  242. done();
  243. } );
  244. requests[ 0 ].respond( 200, '', tokenValue );
  245. } );
  246. it( 'should use default options when none passed', done => {
  247. const tokenValue = getTestTokenValue();
  248. Token.create( 'http://token-endpoint' )
  249. .then( token => {
  250. expect( token._options ).to.eql( { autoRefresh: true } );
  251. done();
  252. } );
  253. requests[ 0 ].respond( 200, '', tokenValue );
  254. } );
  255. } );
  256. } );
  257. // Returns valid token for tests with given expiration time offset.
  258. //
  259. // @param {Number} [timeOffset=3600000]
  260. // @returns {String}
  261. function getTestTokenValue( timeOffset = 3600000 ) {
  262. return `header.${ btoa( JSON.stringify( { exp: Date.now() + timeOffset } ) ) }.signature`;
  263. }