token.js 6.1 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', () => {
  95. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  96. const promise = token._refresh();
  97. requests[ 0 ].respond( 401 );
  98. return promise.then( () => {
  99. throw new Error( 'Promise should be rejected' );
  100. }, error => {
  101. expect( error ).to.match( /Cannot download new token!/ );
  102. } )
  103. } );
  104. it( 'should throw an error when the response is aborted', () => {
  105. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  106. const promise = token._refresh();
  107. requests[ 0 ].abort();
  108. return promise.then( () => {
  109. throw new Error( 'Promise should be rejected' );
  110. }, error => {
  111. expect( error ).to.match( /Abort/ );
  112. } )
  113. } );
  114. it( 'should throw an error when network error occurs', () => {
  115. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  116. const promise = token._refresh();
  117. requests[ 0 ].error();
  118. return promise.then( () => {
  119. throw new Error( 'Promise should be rejected' );
  120. }, error => {
  121. expect( error ).to.match( /Network Error/ );
  122. } )
  123. } );
  124. it( 'should throw an error when the callback throws error', () => {
  125. const token = new Token( () => Promise.reject( 'Custom error occurred' ), { initValue: 'initValue', autoRefresh: false } );
  126. token._refreshToken()
  127. .catch( error => {
  128. expect( error ).to.equal( 'Custom error occurred' );
  129. } );
  130. } );
  131. } );
  132. describe( '_startRefreshing()', () => {
  133. it( 'should start refreshing', () => {
  134. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
  135. const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
  136. token._startRefreshing();
  137. clock.tick( 3600000 );
  138. clock.tick( 3600000 );
  139. clock.tick( 3600000 );
  140. clock.tick( 3600000 );
  141. clock.tick( 3600000 );
  142. expect( requests.length ).to.equal( 5 );
  143. clock.restore();
  144. } );
  145. } );
  146. describe( '_stopRefreshing()', () => {
  147. it( 'should stop refreshing', done => {
  148. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
  149. const token = new Token( 'http://token-endpoint', { initValue: 'initValue' } );
  150. token.init()
  151. .then( () => {
  152. clock.tick( 3600000 );
  153. clock.tick( 3600000 );
  154. clock.tick( 3600000 );
  155. token._stopRefreshing();
  156. clock.tick( 3600000 );
  157. clock.tick( 3600000 );
  158. expect( requests.length ).to.equal( 3 );
  159. clock.restore();
  160. done();
  161. } );
  162. } );
  163. } );
  164. describe( 'static create()', () => {
  165. it( 'should return a initialized token', done => {
  166. Token.create( 'http://token-endpoint', { autoRefresh: false } )
  167. .then( token => {
  168. expect( token.value ).to.equal( 'token-value' );
  169. done();
  170. } );
  171. requests[ 0 ].respond( 200, '', 'token-value' );
  172. } );
  173. } );
  174. } );