token.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 start token refresh every 1 hour', done => {
  65. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
  66. const token = new Token( 'http://token-endpoint', { initValue: 'initValue' } );
  67. token.init()
  68. .then( () => {
  69. clock.tick( 3600000 );
  70. clock.tick( 3600000 );
  71. clock.tick( 3600000 );
  72. clock.tick( 3600000 );
  73. clock.tick( 3600000 );
  74. expect( requests.length ).to.equal( 5 );
  75. clock.restore();
  76. done();
  77. } );
  78. } );
  79. } );
  80. describe( 'destroy', () => {
  81. it( 'should stop refreshing the token', () => {
  82. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
  83. const token = new Token( 'http://token-endpoint', { initValue: 'initValue' } );
  84. return token.init()
  85. .then( () => {
  86. clock.tick( 3600000 );
  87. clock.tick( 3600000 );
  88. expect( requests.length ).to.equal( 2 );
  89. token.destroy();
  90. clock.tick( 3600000 );
  91. clock.tick( 3600000 );
  92. clock.tick( 3600000 );
  93. expect( requests.length ).to.equal( 2 );
  94. } );
  95. } );
  96. } );
  97. describe( 'refreshToken()', () => {
  98. it( 'should get a token from the specified address', done => {
  99. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  100. token.refreshToken()
  101. .then( newToken => {
  102. expect( newToken.value ).to.equal( 'token-value' );
  103. done();
  104. } );
  105. requests[ 0 ].respond( 200, '', 'token-value' );
  106. } );
  107. it( 'should get a token from the specified callback function', () => {
  108. const token = new Token( () => Promise.resolve( 'token-value' ), { initValue: 'initValue', autoRefresh: false } );
  109. return token.refreshToken()
  110. .then( newToken => {
  111. expect( newToken.value ).to.equal( 'token-value' );
  112. } );
  113. } );
  114. it( 'should throw an error when cannot download new token', () => {
  115. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  116. const promise = token._refresh();
  117. requests[ 0 ].respond( 401 );
  118. return promise.then( () => {
  119. throw new Error( 'Promise should be rejected' );
  120. }, error => {
  121. expect( error.constructor ).to.equal( CKEditorError );
  122. expect( error ).to.match( /token-cannot-download-new-token/ );
  123. } );
  124. } );
  125. it( 'should throw an error when the response is aborted', () => {
  126. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  127. const promise = token._refresh();
  128. requests[ 0 ].abort();
  129. return promise.then( () => {
  130. throw new Error( 'Promise should be rejected' );
  131. }, error => {
  132. expect( error ).to.match( /Abort/ );
  133. } );
  134. } );
  135. it( 'should throw an error when network error occurs', () => {
  136. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  137. const promise = token._refresh();
  138. requests[ 0 ].error();
  139. return promise.then( () => {
  140. throw new Error( 'Promise should be rejected' );
  141. }, error => {
  142. expect( error ).to.match( /Network Error/ );
  143. } );
  144. } );
  145. it( 'should throw an error when the callback throws error', () => {
  146. const token = new Token( () => Promise.reject( 'Custom error occurred' ), { initValue: 'initValue', autoRefresh: false } );
  147. token.refreshToken()
  148. .catch( error => {
  149. expect( error ).to.equal( 'Custom error occurred' );
  150. } );
  151. } );
  152. } );
  153. describe( '_startRefreshing()', () => {
  154. it( 'should start refreshing', () => {
  155. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
  156. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  157. token._startRefreshing();
  158. clock.tick( 3600000 );
  159. clock.tick( 3600000 );
  160. clock.tick( 3600000 );
  161. clock.tick( 3600000 );
  162. clock.tick( 3600000 );
  163. expect( requests.length ).to.equal( 5 );
  164. clock.restore();
  165. } );
  166. } );
  167. describe( '_stopRefreshing()', () => {
  168. it( 'should stop refreshing', done => {
  169. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
  170. const token = new Token( 'http://token-endpoint', { initValue: 'initValue' } );
  171. token.init()
  172. .then( () => {
  173. clock.tick( 3600000 );
  174. clock.tick( 3600000 );
  175. clock.tick( 3600000 );
  176. token._stopRefreshing();
  177. clock.tick( 3600000 );
  178. clock.tick( 3600000 );
  179. expect( requests.length ).to.equal( 3 );
  180. clock.restore();
  181. done();
  182. } );
  183. } );
  184. } );
  185. describe( 'static create()', () => {
  186. it( 'should return a initialized token', done => {
  187. Token.create( 'http://token-endpoint', { autoRefresh: false } )
  188. .then( token => {
  189. expect( token.value ).to.equal( 'token-value' );
  190. done();
  191. } );
  192. requests[ 0 ].respond( 200, '', 'token-value' );
  193. } );
  194. it( 'should use default options when none passed', done => {
  195. const intervalSpy = sinon.spy( window, 'setInterval' );
  196. Token.create( 'http://token-endpoint' )
  197. .then( () => {
  198. expect( intervalSpy.args[ 0 ][ 1 ] ).to.equal( 3600000 );
  199. done();
  200. } );
  201. requests[ 0 ].respond( 200, '', 'token-value' );
  202. } );
  203. } );
  204. } );