token.js 4.3 KB

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