8
0

token.js 6.8 KB

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