token.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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( 1500 ) );
  103. await clock.tickAsync( 750000 );
  104. requests[ 1 ].respond( 200, '', getTestTokenValue( 900 ) );
  105. await clock.tickAsync( 450000 );
  106. requests[ 2 ].respond( 200, '', getTestTokenValue( 450 ) );
  107. await clock.tickAsync( 225000 );
  108. requests[ 3 ].respond( 200, '', getTestTokenValue( 20 ) );
  109. await clock.tickAsync( 10000 );
  110. requests[ 4 ].respond( 200, '', getTestTokenValue( 20 ) );
  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. it( 'should refresh the token with the default time if the token payload does not contain `exp` property', async () => {
  127. const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout' ] } );
  128. const tokenValue = `header.${ btoa( JSON.stringify( {} ) ) }.signature`;
  129. const token = new Token( 'http://token-endpoint', { initValue: tokenValue } );
  130. await token.init();
  131. await clock.tickAsync( 3600000 );
  132. requests[ 0 ].respond( 200, '', tokenValue );
  133. await clock.tickAsync( 3600000 );
  134. requests[ 1 ].respond( 200, '', tokenValue );
  135. await clock.tickAsync( 3600000 );
  136. requests[ 2 ].respond( 200, '', tokenValue );
  137. expect( requests.length ).to.equal( 3 );
  138. clock.restore();
  139. } );
  140. } );
  141. describe( 'destroy', () => {
  142. it( 'should stop refreshing the token', async () => {
  143. const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout', 'clearTimeout' ] } );
  144. const tokenInitValue = getTestTokenValue();
  145. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue } );
  146. await token.init();
  147. await clock.tickAsync( 1800000 );
  148. requests[ 0 ].respond( 200, '', getTestTokenValue( 1500 ) );
  149. await clock.tickAsync( 100 );
  150. await clock.tickAsync( 750000 );
  151. requests[ 1 ].respond( 200, '', getTestTokenValue( 900 ) );
  152. await clock.tickAsync( 100 );
  153. token.destroy();
  154. await clock.tickAsync( 3600000 );
  155. await clock.tickAsync( 3600000 );
  156. await clock.tickAsync( 3600000 );
  157. expect( requests.length ).to.equal( 2 );
  158. clock.restore();
  159. } );
  160. } );
  161. describe( 'refreshToken()', () => {
  162. it( 'should get a token from the specified address', done => {
  163. const tokenValue = getTestTokenValue();
  164. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  165. token.refreshToken()
  166. .then( newToken => {
  167. expect( newToken.value ).to.equal( tokenValue );
  168. done();
  169. } );
  170. requests[ 0 ].respond( 200, '', tokenValue );
  171. } );
  172. it( 'should throw an error if the returned token is wrapped in additional quotes', done => {
  173. const tokenValue = getTestTokenValue();
  174. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  175. token.refreshToken()
  176. .then( () => {
  177. done( new Error( 'Promise should be rejected' ) );
  178. } )
  179. .catch( error => {
  180. expect( error.constructor ).to.equal( CKEditorError );
  181. expect( error ).to.match( /token-not-in-jwt-format/ );
  182. done();
  183. } );
  184. requests[ 0 ].respond( 200, '', `"${ tokenValue }"` );
  185. } );
  186. it( 'should throw an error if the returned token is not a valid JWT token', done => {
  187. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  188. token.refreshToken()
  189. .then( () => {
  190. done( new Error( 'Promise should be rejected' ) );
  191. } )
  192. .catch( error => {
  193. expect( error.constructor ).to.equal( CKEditorError );
  194. expect( error ).to.match( /token-not-in-jwt-format/ );
  195. done();
  196. } );
  197. requests[ 0 ].respond( 200, '', 'token' );
  198. } );
  199. it( 'should get a token from the specified callback function', () => {
  200. const tokenValue = getTestTokenValue();
  201. const token = new Token( () => Promise.resolve( tokenValue ), { autoRefresh: false } );
  202. return token.refreshToken()
  203. .then( newToken => {
  204. expect( newToken.value ).to.equal( tokenValue );
  205. } );
  206. } );
  207. it( 'should throw an error when cannot download a new token', () => {
  208. const tokenInitValue = getTestTokenValue();
  209. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue, autoRefresh: false } );
  210. const promise = token._refresh();
  211. requests[ 0 ].respond( 401 );
  212. return promise.then( () => {
  213. throw new Error( 'Promise should be rejected' );
  214. }, error => {
  215. expect( error.constructor ).to.equal( CKEditorError );
  216. expect( error ).to.match( /token-cannot-download-new-token/ );
  217. } );
  218. } );
  219. it( 'should throw an error when the response is aborted', () => {
  220. const tokenInitValue = getTestTokenValue();
  221. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue, autoRefresh: false } );
  222. const promise = token._refresh();
  223. requests[ 0 ].abort();
  224. return promise.then( () => {
  225. throw new Error( 'Promise should be rejected' );
  226. }, error => {
  227. expect( error ).to.match( /Abort/ );
  228. } );
  229. } );
  230. it( 'should throw an error when network error occurs', () => {
  231. const tokenInitValue = getTestTokenValue();
  232. const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue, autoRefresh: false } );
  233. const promise = token._refresh();
  234. requests[ 0 ].error();
  235. return promise.then( () => {
  236. throw new Error( 'Promise should be rejected' );
  237. }, error => {
  238. expect( error ).to.match( /Network Error/ );
  239. } );
  240. } );
  241. it( 'should throw an error when the callback throws an error', () => {
  242. const tokenInitValue = getTestTokenValue();
  243. const token = new Token( () => Promise.reject( 'Custom error occurred' ), { initValue: tokenInitValue, autoRefresh: false } );
  244. token.refreshToken()
  245. .catch( error => {
  246. expect( error ).to.equal( 'Custom error occurred' );
  247. } );
  248. } );
  249. } );
  250. describe( 'static create()', () => {
  251. it( 'should return an initialized token', done => {
  252. const tokenValue = getTestTokenValue();
  253. Token.create( 'http://token-endpoint', { autoRefresh: false } )
  254. .then( token => {
  255. expect( token.value ).to.equal( tokenValue );
  256. done();
  257. } );
  258. requests[ 0 ].respond( 200, '', tokenValue );
  259. } );
  260. it( 'should use default options when none passed', done => {
  261. const tokenValue = getTestTokenValue();
  262. Token.create( 'http://token-endpoint' )
  263. .then( token => {
  264. expect( token._options ).to.eql( { autoRefresh: true } );
  265. done();
  266. } );
  267. requests[ 0 ].respond( 200, '', tokenValue );
  268. } );
  269. } );
  270. } );
  271. // Returns valid token for tests with given expiration time offset.
  272. //
  273. // @param {Number} [timeOffset=3600000]
  274. // @returns {String}
  275. function getTestTokenValue( timeOffset = 3600 ) {
  276. return `header.${ btoa( JSON.stringify( { exp: ( Math.floor( Date.now() / 1000 ) ) + timeOffset } ) ) }.signature`;
  277. }