8
0

token.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. global.xhr = sinon.useFakeXMLHttpRequest();
  13. global.xhr.onCreate = xhr => {
  14. requests.push( xhr );
  15. };
  16. } );
  17. afterEach( () => global.xhr.restore() );
  18. describe( 'constructor()', () => {
  19. it( 'should throw error when no tokenUrl provided', () => {
  20. expect( () => new Token() ).to.throw(
  21. CKEditorError,
  22. 'token-missing-token-url'
  23. );
  24. } );
  25. it( 'should set a init token value', () => {
  26. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  27. expect( token.value ).to.equal( 'initValue' );
  28. } );
  29. it( 'should fire `change:value` event if the value of the token has changed', done => {
  30. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  31. token.on( 'change:value', ( event, name, newValue ) => {
  32. expect( newValue ).to.equal( 'token-value' );
  33. done();
  34. } );
  35. token.init();
  36. requests[ 0 ].respond( 200, '', 'token-value' );
  37. } );
  38. it( 'should accept the callback in the constructor', () => {
  39. expect( () => {
  40. // eslint-disable-next-line
  41. const token = new Token( () => Promise.resolve( 'token' ) );
  42. } ).to.not.throw();
  43. } );
  44. } );
  45. describe( 'init()', () => {
  46. it( 'should get a token value from endpoint', done => {
  47. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  48. token.init()
  49. .then( () => {
  50. expect( token.value ).to.equal( 'token-value' );
  51. done();
  52. } );
  53. requests[ 0 ].respond( 200, '', 'token-value' );
  54. } );
  55. it( 'should get a token from the refreshToken function when is provided', () => {
  56. const token = new Token( () => Promise.resolve( 'token-value' ), { autoRefresh: false } );
  57. return token.init()
  58. .then( () => {
  59. expect( token.value ).to.equal( 'token-value' );
  60. } );
  61. } );
  62. it( 'should start token refresh every 1 hour', done => {
  63. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
  64. const token = new Token( 'http://token-endpoint', { initValue: 'initValue' } );
  65. token.init()
  66. .then( () => {
  67. clock.tick( 3600000 );
  68. clock.tick( 3600000 );
  69. clock.tick( 3600000 );
  70. clock.tick( 3600000 );
  71. clock.tick( 3600000 );
  72. expect( requests.length ).to.equal( 5 );
  73. clock.restore();
  74. done();
  75. } );
  76. } );
  77. } );
  78. describe( 'destroy', () => {
  79. it( 'should stop refreshing the token', () => {
  80. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
  81. const token = new Token( 'http://token-endpoint', { initValue: 'initValue' } );
  82. return token.init()
  83. .then( () => {
  84. clock.tick( 3600000 );
  85. clock.tick( 3600000 );
  86. expect( requests.length ).to.equal( 2 );
  87. token.destroy();
  88. clock.tick( 3600000 );
  89. clock.tick( 3600000 );
  90. clock.tick( 3600000 );
  91. expect( requests.length ).to.equal( 2 );
  92. } );
  93. } );
  94. } );
  95. describe( '_refreshToken()', () => {
  96. it( 'should get a token from the specified address', done => {
  97. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  98. token._refreshToken()
  99. .then( newToken => {
  100. expect( newToken.value ).to.equal( 'token-value' );
  101. done();
  102. } );
  103. requests[ 0 ].respond( 200, '', 'token-value' );
  104. } );
  105. it( 'should get a token from the specified callback function', () => {
  106. const token = new Token( () => Promise.resolve( 'token-value' ), { initValue: 'initValue', autoRefresh: false } );
  107. return token._refreshToken()
  108. .then( newToken => {
  109. expect( newToken.value ).to.equal( 'token-value' );
  110. } );
  111. } );
  112. it( 'should throw an error when cannot download new token', () => {
  113. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  114. const promise = token._refresh();
  115. requests[ 0 ].respond( 401 );
  116. return promise.then( () => {
  117. throw new Error( 'Promise should be rejected' );
  118. }, error => {
  119. expect( error.constructor ).to.equal( CKEditorError );
  120. expect( error ).to.match( /token-cannot-download-new-token/ );
  121. } );
  122. } );
  123. it( 'should throw an error when the response is aborted', () => {
  124. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  125. const promise = token._refresh();
  126. requests[ 0 ].abort();
  127. return promise.then( () => {
  128. throw new Error( 'Promise should be rejected' );
  129. }, error => {
  130. expect( error ).to.match( /Abort/ );
  131. } );
  132. } );
  133. it( 'should throw an error when network error occurs', () => {
  134. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  135. const promise = token._refresh();
  136. requests[ 0 ].error();
  137. return promise.then( () => {
  138. throw new Error( 'Promise should be rejected' );
  139. }, error => {
  140. expect( error ).to.match( /Network Error/ );
  141. } );
  142. } );
  143. it( 'should throw an error when the callback throws error', () => {
  144. const token = new Token( () => Promise.reject( 'Custom error occurred' ), { initValue: 'initValue', autoRefresh: false } );
  145. token._refreshToken()
  146. .catch( error => {
  147. expect( error ).to.equal( 'Custom error occurred' );
  148. } );
  149. } );
  150. } );
  151. describe( '_startRefreshing()', () => {
  152. it( 'should start refreshing', () => {
  153. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
  154. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  155. token._startRefreshing();
  156. clock.tick( 3600000 );
  157. clock.tick( 3600000 );
  158. clock.tick( 3600000 );
  159. clock.tick( 3600000 );
  160. clock.tick( 3600000 );
  161. expect( requests.length ).to.equal( 5 );
  162. clock.restore();
  163. } );
  164. } );
  165. describe( '_stopRefreshing()', () => {
  166. it( 'should stop refreshing', done => {
  167. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
  168. const token = new Token( 'http://token-endpoint', { initValue: 'initValue' } );
  169. token.init()
  170. .then( () => {
  171. clock.tick( 3600000 );
  172. clock.tick( 3600000 );
  173. clock.tick( 3600000 );
  174. token._stopRefreshing();
  175. clock.tick( 3600000 );
  176. clock.tick( 3600000 );
  177. expect( requests.length ).to.equal( 3 );
  178. clock.restore();
  179. done();
  180. } );
  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. } );