token.js 5.9 KB

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