|
@@ -7,7 +7,7 @@
|
|
|
|
|
|
|
|
'use strict';
|
|
'use strict';
|
|
|
|
|
|
|
|
-import Token from './../../src/token/token';
|
|
|
|
|
|
|
+import Token from '../../src/token/token';
|
|
|
|
|
|
|
|
describe( 'Token', () => {
|
|
describe( 'Token', () => {
|
|
|
let requests;
|
|
let requests;
|
|
@@ -25,16 +25,26 @@ describe( 'Token', () => {
|
|
|
afterEach( () => global.xhr.restore() );
|
|
afterEach( () => global.xhr.restore() );
|
|
|
|
|
|
|
|
describe( 'constructor()', () => {
|
|
describe( 'constructor()', () => {
|
|
|
- it( 'should set a token value', () => {
|
|
|
|
|
- const token = new Token( 'http://token-endpoint', { startAutoRefresh: false } );
|
|
|
|
|
|
|
+ it( 'should throw error when no tokenUrl provided', () => {
|
|
|
|
|
+ expect( () => new Token() ).to.throw( '`tokenUrl` must be provided' );
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ it( 'should get a token value from endpoint', () => {
|
|
|
|
|
+ const token = new Token( 'http://token-endpoint', '', { startAutoRefresh: false } );
|
|
|
|
|
|
|
|
requests[ 0 ].respond( 200, '', 'token-value' );
|
|
requests[ 0 ].respond( 200, '', 'token-value' );
|
|
|
|
|
|
|
|
expect( token.value ).to.equal( 'token-value' );
|
|
expect( token.value ).to.equal( 'token-value' );
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
|
|
+ it( 'should set a token value', () => {
|
|
|
|
|
+ const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
|
|
|
|
|
+
|
|
|
|
|
+ expect( token.value ).to.equal( 'initValue' );
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
it( 'should fire `change:value` event if the value of the token has changed', done => {
|
|
it( 'should fire `change:value` event if the value of the token has changed', done => {
|
|
|
- const token = new Token( 'http://token-endpoint', { startAutoRefresh: false } );
|
|
|
|
|
|
|
+ const token = new Token( 'http://token-endpoint', '', { startAutoRefresh: false } );
|
|
|
|
|
|
|
|
token.on( 'change:value', ( event, name, newValue ) => {
|
|
token.on( 'change:value', ( event, name, newValue ) => {
|
|
|
expect( newValue ).to.equal( 'token-value' );
|
|
expect( newValue ).to.equal( 'token-value' );
|
|
@@ -45,35 +55,26 @@ describe( 'Token', () => {
|
|
|
requests[ 0 ].respond( 200, '', 'token-value' );
|
|
requests[ 0 ].respond( 200, '', 'token-value' );
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
- it( 'should start token refresh every 1 hour', done => {
|
|
|
|
|
|
|
+ it( 'should start token refresh every 1 hour', () => {
|
|
|
const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
|
|
const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
|
|
|
|
|
|
|
|
- const token = new Token( 'http://token-endpoint' );
|
|
|
|
|
-
|
|
|
|
|
- requests[ 0 ].respond( 200, '', 'token-value' );
|
|
|
|
|
-
|
|
|
|
|
- // waiting for the first request
|
|
|
|
|
- setTimeout( () => {
|
|
|
|
|
- expect( token.value ).to.equal( 'token-value' );
|
|
|
|
|
-
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
|
|
+ const token = new Token( 'http://token-endpoint' ); // eslint-disable-line no-unused-vars
|
|
|
|
|
|
|
|
- expect( requests.length ).to.equal( 6 );
|
|
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
|
|
|
- clock.restore();
|
|
|
|
|
|
|
+ expect( requests.length ).to.equal( 6 );
|
|
|
|
|
|
|
|
- done();
|
|
|
|
|
- }, 10 );
|
|
|
|
|
|
|
+ clock.restore();
|
|
|
} );
|
|
} );
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
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', { startAutoRefresh: false } );
|
|
|
|
|
|
|
+ const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
|
|
|
|
|
|
|
|
token.refreshToken()
|
|
token.refreshToken()
|
|
|
.then( newValue => {
|
|
.then( newValue => {
|
|
@@ -85,11 +86,11 @@ describe( 'Token', () => {
|
|
|
done();
|
|
done();
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
- requests[ 1 ].respond( 200, '', 'token-value' );
|
|
|
|
|
|
|
+ requests[ 0 ].respond( 200, '', 'token-value' );
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
it( 'should throw error when cannot download new token ', done => {
|
|
it( 'should throw error when cannot download new token ', done => {
|
|
|
- const token = new Token( 'http://token-endpoint', { startAutoRefresh: false } );
|
|
|
|
|
|
|
+ const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
|
|
|
|
|
|
|
|
token.refreshToken()
|
|
token.refreshToken()
|
|
|
.catch( error => {
|
|
.catch( error => {
|
|
@@ -98,11 +99,11 @@ describe( 'Token', () => {
|
|
|
done();
|
|
done();
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
- requests[ 1 ].respond( 401 );
|
|
|
|
|
|
|
+ requests[ 0 ].respond( 401 );
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
it( 'should throw error when response is aborted', done => {
|
|
it( 'should throw error when response is aborted', done => {
|
|
|
- const token = new Token( 'http://token-endpoint', { startAutoRefresh: false } );
|
|
|
|
|
|
|
+ const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
|
|
|
|
|
|
|
|
token.refreshToken()
|
|
token.refreshToken()
|
|
|
.catch( error => {
|
|
.catch( error => {
|
|
@@ -111,11 +112,11 @@ describe( 'Token', () => {
|
|
|
done();
|
|
done();
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
- requests[ 1 ].abort();
|
|
|
|
|
|
|
+ requests[ 0 ].abort();
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
it( 'should throw error event when network error occurs', done => {
|
|
it( 'should throw error event when network error occurs', done => {
|
|
|
- const token = new Token( 'http://token-endpoint', { startAutoRefresh: false } );
|
|
|
|
|
|
|
+ const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
|
|
|
|
|
|
|
|
token.refreshToken()
|
|
token.refreshToken()
|
|
|
.catch( error => {
|
|
.catch( error => {
|
|
@@ -124,66 +125,48 @@ describe( 'Token', () => {
|
|
|
done();
|
|
done();
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
- requests[ 1 ].error();
|
|
|
|
|
|
|
+ requests[ 0 ].error();
|
|
|
} );
|
|
} );
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
describe( 'startRefreshing()', () => {
|
|
describe( 'startRefreshing()', () => {
|
|
|
- it( 'should start refreshing', done => {
|
|
|
|
|
|
|
+ it( 'should start refreshing', () => {
|
|
|
const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
|
|
const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
|
|
|
|
|
|
|
|
- const token = new Token( 'http://token-endpoint', { startAutoRefresh: false } );
|
|
|
|
|
|
|
+ const token = new Token( 'http://token-endpoint', '', { startAutoRefresh: false } );
|
|
|
|
|
|
|
|
token.startRefreshing();
|
|
token.startRefreshing();
|
|
|
|
|
|
|
|
- requests[ 0 ].respond( 200, '', 'token-value' );
|
|
|
|
|
-
|
|
|
|
|
- // waiting for the first request
|
|
|
|
|
- setTimeout( () => {
|
|
|
|
|
- expect( token.value ).to.equal( 'token-value' );
|
|
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
|
|
+ expect( requests.length ).to.equal( 6 );
|
|
|
|
|
|
|
|
- expect( requests.length ).to.equal( 6 );
|
|
|
|
|
-
|
|
|
|
|
- clock.restore();
|
|
|
|
|
-
|
|
|
|
|
- done();
|
|
|
|
|
- }, 10 );
|
|
|
|
|
|
|
+ clock.restore();
|
|
|
} );
|
|
} );
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
describe( 'stopRefreshing()', () => {
|
|
describe( 'stopRefreshing()', () => {
|
|
|
- it( 'should stop refreshing', done => {
|
|
|
|
|
|
|
+ it( 'should stop refreshing', () => {
|
|
|
const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
|
|
const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
|
|
|
|
|
|
|
|
- const token = new Token( 'http://token-endpoint' );
|
|
|
|
|
|
|
+ const token = new Token( 'http://token-endpoint', 'initValue' );
|
|
|
|
|
|
|
|
- requests[ 0 ].respond( 200, '', 'token-value' );
|
|
|
|
|
-
|
|
|
|
|
- // waiting for the first request
|
|
|
|
|
- setTimeout( () => {
|
|
|
|
|
- expect( token.value ).to.equal( 'token-value' );
|
|
|
|
|
-
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
|
|
|
- token.stopRefreshing();
|
|
|
|
|
|
|
+ token.stopRefreshing();
|
|
|
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
- clock.tick( 3600000 );
|
|
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
+ clock.tick( 3600000 );
|
|
|
|
|
|
|
|
- expect( requests.length ).to.equal( 4 );
|
|
|
|
|
|
|
+ expect( requests.length ).to.equal( 3 );
|
|
|
|
|
|
|
|
- clock.restore();
|
|
|
|
|
-
|
|
|
|
|
- done();
|
|
|
|
|
- }, 10 );
|
|
|
|
|
|
|
+ clock.restore();
|
|
|
} );
|
|
} );
|
|
|
} );
|
|
} );
|
|
|
} );
|
|
} );
|