|
@@ -26,7 +26,9 @@ describe( 'Token', () => {
|
|
|
|
|
|
|
|
describe( 'constructor()', () => {
|
|
describe( 'constructor()', () => {
|
|
|
it( 'should throw error when no tokenUrl provided', () => {
|
|
it( 'should throw error when no tokenUrl provided', () => {
|
|
|
- expect( () => new Token() ).to.throw( '`tokenUrl` must be provided' );
|
|
|
|
|
|
|
+ expect( () => new Token() ).to.throw(
|
|
|
|
|
+ 'A `tokenUrl` must be provided as the first constructor argument.'
|
|
|
|
|
+ );
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
it( 'should set a init token value', () => {
|
|
it( 'should set a init token value', () => {
|
|
@@ -48,6 +50,13 @@ describe( 'Token', () => {
|
|
|
|
|
|
|
|
requests[ 0 ].respond( 200, '', 'token-value' );
|
|
requests[ 0 ].respond( 200, '', 'token-value' );
|
|
|
} );
|
|
} );
|
|
|
|
|
+
|
|
|
|
|
+ it( 'should accept the callback in the constructor', () => {
|
|
|
|
|
+ expect( () => {
|
|
|
|
|
+ // eslint-disable-next-line
|
|
|
|
|
+ const token = new Token( () => Promise.resolve( 'token' ) );
|
|
|
|
|
+ } ).to.not.throw();
|
|
|
|
|
+ } );
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
describe( 'init()', () => {
|
|
describe( 'init()', () => {
|
|
@@ -64,6 +73,15 @@ describe( 'Token', () => {
|
|
|
requests[ 0 ].respond( 200, '', 'token-value' );
|
|
requests[ 0 ].respond( 200, '', 'token-value' );
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
|
|
+ it( 'should get a token from the refreshToken function when is provided', () => {
|
|
|
|
|
+ const token = new Token( () => Promise.resolve( 'token-value' ), { autoRefresh: false } );
|
|
|
|
|
+
|
|
|
|
|
+ return token.init()
|
|
|
|
|
+ .then( () => {
|
|
|
|
|
+ expect( token.value ).to.equal( 'token-value' );
|
|
|
|
|
+ } );
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
it( 'should start token refresh every 1 hour', done => {
|
|
it( 'should start token refresh every 1 hour', done => {
|
|
|
const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
|
|
const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
|
|
|
|
|
|
|
@@ -86,6 +104,29 @@ describe( 'Token', () => {
|
|
|
} );
|
|
} );
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
|
|
+ describe( 'destroy', () => {
|
|
|
|
|
+ it( 'should stop refreshing the token', () => {
|
|
|
|
|
+ const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
|
|
|
|
|
+ const token = new Token( 'http://token-endpoint', { initValue: 'initValue' } );
|
|
|
|
|
+
|
|
|
|
|
+ return token.init()
|
|
|
|
|
+ .then( () => {
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+
|
|
|
|
|
+ expect( requests.length ).to.equal( 2 );
|
|
|
|
|
+
|
|
|
|
|
+ token.destroy();
|
|
|
|
|
+
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+
|
|
|
|
|
+ expect( requests.length ).to.equal( 2 );
|
|
|
|
|
+ } );
|
|
|
|
|
+ } );
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
describe( '_refreshToken()', () => {
|
|
describe( '_refreshToken()', () => {
|
|
|
it( 'should get a token from the specified address', done => {
|
|
it( 'should get a token from the specified address', done => {
|
|
|
const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
|
|
const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
|
|
@@ -100,43 +141,61 @@ describe( 'Token', () => {
|
|
|
requests[ 0 ].respond( 200, '', 'token-value' );
|
|
requests[ 0 ].respond( 200, '', 'token-value' );
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
- it( 'should throw error when cannot download new token ', done => {
|
|
|
|
|
- const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
|
|
|
|
|
-
|
|
|
|
|
- token._refreshToken()
|
|
|
|
|
- .catch( error => {
|
|
|
|
|
- expect( error ).to.equal( 'Cannot download new token!' );
|
|
|
|
|
|
|
+ it( 'should get a token from the specified callback function', () => {
|
|
|
|
|
+ const token = new Token( () => Promise.resolve( 'token-value' ), { initValue: 'initValue', autoRefresh: false } );
|
|
|
|
|
|
|
|
- done();
|
|
|
|
|
|
|
+ return token._refreshToken()
|
|
|
|
|
+ .then( newToken => {
|
|
|
|
|
+ expect( newToken.value ).to.equal( 'token-value' );
|
|
|
} );
|
|
} );
|
|
|
-
|
|
|
|
|
- requests[ 0 ].respond( 401 );
|
|
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
- it( 'should throw error when response is aborted', done => {
|
|
|
|
|
|
|
+ it( 'should throw an error when cannot download new token', () => {
|
|
|
const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
|
|
const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
|
|
|
|
|
+ const promise = token._refresh();
|
|
|
|
|
|
|
|
- token._refreshToken()
|
|
|
|
|
- .catch( error => {
|
|
|
|
|
- expect( error ).to.equal( 'Abort' );
|
|
|
|
|
|
|
+ requests[ 0 ].respond( 401 );
|
|
|
|
|
|
|
|
- done();
|
|
|
|
|
- } );
|
|
|
|
|
|
|
+ return promise.then( () => {
|
|
|
|
|
+ throw new Error( 'Promise should be rejected' );
|
|
|
|
|
+ }, error => {
|
|
|
|
|
+ expect( error ).to.match( /Cannot download new token!/ );
|
|
|
|
|
+ } )
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ it( 'should throw an error when the response is aborted', () => {
|
|
|
|
|
+ const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
|
|
|
|
|
+ const promise = token._refresh();
|
|
|
|
|
|
|
|
requests[ 0 ].abort();
|
|
requests[ 0 ].abort();
|
|
|
|
|
+
|
|
|
|
|
+ return promise.then( () => {
|
|
|
|
|
+ throw new Error( 'Promise should be rejected' );
|
|
|
|
|
+ }, error => {
|
|
|
|
|
+ expect( error ).to.match( /Abort/ );
|
|
|
|
|
+ } )
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
- it( 'should throw error event when network error occurs', done => {
|
|
|
|
|
|
|
+ it( 'should throw an error when network error occurs', () => {
|
|
|
const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
|
|
const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
|
|
|
|
|
+ const promise = token._refresh();
|
|
|
|
|
+
|
|
|
|
|
+ requests[ 0 ].error();
|
|
|
|
|
+
|
|
|
|
|
+ return promise.then( () => {
|
|
|
|
|
+ throw new Error( 'Promise should be rejected' );
|
|
|
|
|
+ }, error => {
|
|
|
|
|
+ expect( error ).to.match( /Network Error/ );
|
|
|
|
|
+ } )
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ it( 'should throw an error when the callback throws error', () => {
|
|
|
|
|
+ const token = new Token( () => Promise.reject( 'Custom error occurred' ), { initValue: 'initValue', autoRefresh: false } );
|
|
|
|
|
|
|
|
token._refreshToken()
|
|
token._refreshToken()
|
|
|
.catch( error => {
|
|
.catch( error => {
|
|
|
- expect( error ).to.equal( 'Network Error' );
|
|
|
|
|
-
|
|
|
|
|
- done();
|
|
|
|
|
|
|
+ expect( error ).to.equal( 'Custom error occurred' );
|
|
|
} );
|
|
} );
|
|
|
-
|
|
|
|
|
- requests[ 0 ].error();
|
|
|
|
|
} );
|
|
} );
|
|
|
} );
|
|
} );
|
|
|
|
|
|