8
0
Просмотр исходного кода

Internal: Added init method to token

Bartosz Czerwonka 8 лет назад
Родитель
Сommit
d625f9f785

+ 25 - 18
packages/ckeditor-cloud-services-core/src/token/token.js

@@ -55,14 +55,36 @@ class Token {
 		 * @private
 		 */
 		this._options = Object.assign( {}, DEFAULT_OPTIONS, options );
+	}
 
-		this._init();
+	/**
+	 * Initializes the token.
+	 * If `initTokenValue` is not provided then the value of the token is download from url.
+	 *
+	 * @returns {Promise.<Token>}
+	 */
+	init() {
+		return new Promise( ( resolve, reject ) => {
+			if ( this._options.startAutoRefresh ) {
+				this.startRefreshing();
+			}
+
+			if ( !this.value ) {
+				this.refreshToken()
+					.then( resolve )
+					.catch( reject );
+
+				return;
+			}
+
+			resolve( this );
+		} );
 	}
 
 	/**
 	 * Gets the new token.
 	 *
-	 * @returns {Promise}
+	 * @returns {Promise.<Token>}
 	 */
 	refreshToken() {
 		return new Promise( ( resolve, reject ) => {
@@ -80,7 +102,7 @@ class Token {
 
 				this.set( 'value', xhrResponse );
 
-				return resolve( xhrResponse );
+				return resolve( this );
 			} );
 
 			xhr.addEventListener( 'error', () => reject( 'Network Error' ) );
@@ -103,21 +125,6 @@ class Token {
 	stopRefreshing() {
 		clearInterval( this._refreshInterval );
 	}
-
-	/**
-	 * Initializes the value of the token.
-	 *
-	 * @private
-	 */
-	_init() {
-		if ( !this.value ) {
-			this.refreshToken();
-		}
-
-		if ( this._options.startAutoRefresh ) {
-			this.startRefreshing();
-		}
-	}
 }
 
 mix( Token, ObservableMixin );

+ 50 - 34
packages/ckeditor-cloud-services-core/tests/token/token.js

@@ -29,15 +29,7 @@ describe( 'Token', () => {
 			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' );
-
-			expect( token.value ).to.equal( 'token-value' );
-		} );
-
-		it( 'should set a token value', () => {
+		it( 'should set a init token value', () => {
 			const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
 
 			expect( token.value ).to.equal( 'initValue' );
@@ -52,23 +44,45 @@ describe( 'Token', () => {
 				done();
 			} );
 
+			token.init();
+
+			requests[ 0 ].respond( 200, '', 'token-value' );
+		} );
+	} );
+
+	describe( 'init()', () => {
+		it( 'should get a token value from endpoint', done => {
+			const token = new Token( 'http://token-endpoint', '', { startAutoRefresh: false } );
+
+			token.init()
+				.then( () => {
+					expect( token.value ).to.equal( 'token-value' );
+
+					done();
+				} );
+
 			requests[ 0 ].respond( 200, '', 'token-value' );
 		} );
 
-		it( 'should start token refresh every 1 hour', () => {
+		it( 'should start token refresh every 1 hour', done => {
 			const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
 
-			const token = new Token( 'http://token-endpoint' ); // eslint-disable-line no-unused-vars
+			const token = new Token( 'http://token-endpoint', 'initValues' );
 
-			clock.tick( 3600000 );
-			clock.tick( 3600000 );
-			clock.tick( 3600000 );
-			clock.tick( 3600000 );
-			clock.tick( 3600000 );
+			token.init()
+				.then( () => {
+					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( 5 );
 
-			clock.restore();
+					clock.restore();
+
+					done();
+				} );
 		} );
 	} );
 
@@ -77,11 +91,8 @@ describe( 'Token', () => {
 			const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
 
 			token.refreshToken()
-				.then( newValue => {
-					expect( newValue ).to.equal( 'token-value' );
-					expect( token.value ).to.equal( newValue );
-
-					token.stopRefreshing();
+				.then( newToken => {
+					expect( newToken.value ).to.equal( 'token-value' );
 
 					done();
 				} );
@@ -133,7 +144,7 @@ describe( 'Token', () => {
 		it( 'should start refreshing', () => {
 			const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
 
-			const token = new Token( 'http://token-endpoint', '', { startAutoRefresh: false } );
+			const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
 
 			token.startRefreshing();
 
@@ -143,30 +154,35 @@ describe( 'Token', () => {
 			clock.tick( 3600000 );
 			clock.tick( 3600000 );
 
-			expect( requests.length ).to.equal( 6 );
+			expect( requests.length ).to.equal( 5 );
 
 			clock.restore();
 		} );
 	} );
 
 	describe( 'stopRefreshing()', () => {
-		it( 'should stop refreshing', () => {
+		it( 'should stop refreshing', done => {
 			const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
 
 			const token = new Token( 'http://token-endpoint', 'initValue' );
 
-			clock.tick( 3600000 );
-			clock.tick( 3600000 );
-			clock.tick( 3600000 );
+			token.init()
+				.then( () => {
+					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( 3 );
+					expect( requests.length ).to.equal( 3 );
 
-			clock.restore();
+					clock.restore();
+
+					done();
+				} );
 		} );
 	} );
 } );