token.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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( '`tokenUrl` must be provided' );
  21. } );
  22. it( 'should set a init token value', () => {
  23. const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue', autoRefresh: false } );
  24. expect( token.value ).to.equal( 'initValue' );
  25. } );
  26. it( 'should fire `change:value` event if the value of the token has changed', done => {
  27. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  28. token.on( 'change:value', ( event, name, newValue ) => {
  29. expect( newValue ).to.equal( 'token-value' );
  30. done();
  31. } );
  32. token.init();
  33. requests[ 0 ].respond( 200, '', 'token-value' );
  34. } );
  35. } );
  36. describe( 'init()', () => {
  37. it( 'should get a token value from endpoint', done => {
  38. const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
  39. token.init()
  40. .then( () => {
  41. expect( token.value ).to.equal( 'token-value' );
  42. done();
  43. } );
  44. requests[ 0 ].respond( 200, '', 'token-value' );
  45. } );
  46. it( 'should start token refresh every 1 hour', done => {
  47. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
  48. const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue' } );
  49. token.init()
  50. .then( () => {
  51. clock.tick( 3600000 );
  52. clock.tick( 3600000 );
  53. clock.tick( 3600000 );
  54. clock.tick( 3600000 );
  55. clock.tick( 3600000 );
  56. expect( requests.length ).to.equal( 5 );
  57. clock.restore();
  58. done();
  59. } );
  60. } );
  61. } );
  62. describe( '_refreshToken()', () => {
  63. it( 'should get a token from the specified address', done => {
  64. const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue', autoRefresh: false } );
  65. token._refreshToken()
  66. .then( newToken => {
  67. expect( newToken.value ).to.equal( 'token-value' );
  68. done();
  69. } );
  70. requests[ 0 ].respond( 200, '', 'token-value' );
  71. } );
  72. it( 'should throw error when cannot download new token ', done => {
  73. const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue', autoRefresh: false } );
  74. token._refreshToken()
  75. .catch( error => {
  76. expect( error ).to.equal( 'Cannot download new token!' );
  77. done();
  78. } );
  79. requests[ 0 ].respond( 401 );
  80. } );
  81. it( 'should throw error when response is aborted', done => {
  82. const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue', autoRefresh: false } );
  83. token._refreshToken()
  84. .catch( error => {
  85. expect( error ).to.equal( 'Abort' );
  86. done();
  87. } );
  88. requests[ 0 ].abort();
  89. } );
  90. it( 'should throw error event when network error occurs', done => {
  91. const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue', autoRefresh: false } );
  92. token._refreshToken()
  93. .catch( error => {
  94. expect( error ).to.equal( 'Network Error' );
  95. done();
  96. } );
  97. requests[ 0 ].error();
  98. } );
  99. } );
  100. describe( '_startRefreshing()', () => {
  101. it( 'should start refreshing', () => {
  102. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
  103. const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue', autoRefresh: false } );
  104. token._startRefreshing();
  105. clock.tick( 3600000 );
  106. clock.tick( 3600000 );
  107. clock.tick( 3600000 );
  108. clock.tick( 3600000 );
  109. clock.tick( 3600000 );
  110. expect( requests.length ).to.equal( 5 );
  111. clock.restore();
  112. } );
  113. } );
  114. describe( '_stopRefreshing()', () => {
  115. it( 'should stop refreshing', done => {
  116. const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
  117. const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue' } );
  118. token.init()
  119. .then( () => {
  120. clock.tick( 3600000 );
  121. clock.tick( 3600000 );
  122. clock.tick( 3600000 );
  123. token._stopRefreshing();
  124. clock.tick( 3600000 );
  125. clock.tick( 3600000 );
  126. expect( requests.length ).to.equal( 3 );
  127. clock.restore();
  128. done();
  129. } );
  130. } );
  131. } );
  132. describe( 'static create()', () => {
  133. it( 'should return a initialized token', done => {
  134. Token.create( 'http://token-endpoint', { autoRefresh: false } )
  135. .then( token => {
  136. expect( token.value ).to.equal( 'token-value' );
  137. done();
  138. } );
  139. requests[ 0 ].respond( 200, '', 'token-value' );
  140. } );
  141. } );
  142. } );