Procházet zdrojové kódy

Added tests for the refreshToken function.

Maciej Bukowski před 7 roky
rodič
revize
8df578e3fd

+ 19 - 17
packages/ckeditor-cloud-services-core/src/token/token.js

@@ -31,7 +31,7 @@ class Token {
 	 * @param {Boolean} [options.autoRefresh=true] Specifies whether to start the refresh automatically.
 	 * @param {Boolean} [options.autoRefresh=true] Specifies whether to start the refresh automatically.
 	 */
 	 */
 	constructor( tokenUrlOrTokenRefresh, options = DEFAULT_OPTIONS ) {
 	constructor( tokenUrlOrTokenRefresh, options = DEFAULT_OPTIONS ) {
-		if ( !tokenUrl ) {
+		if ( !tokenUrlOrTokenRefresh ) {
 			throw new Error( 'A `tokenUrl` or a `tokenRefresh` function must be provided as the first constructor argument.' );
 			throw new Error( 'A `tokenUrl` or a `tokenRefresh` function must be provided as the first constructor argument.' );
 		}
 		}
 
 
@@ -47,6 +47,22 @@ class Token {
 		 */
 		 */
 		this.set( 'value', options.initValue );
 		this.set( 'value', options.initValue );
 
 
+		/**
+		 * Refresh token function.
+		 *
+		 * @member {Function} #_refreshToken
+		 * @protected
+		 */
+		if ( typeof tokenUrlOrTokenRefresh === 'function' ) {
+			this._refreshToken = () => {
+				return tokenUrlOrTokenRefresh()
+					.then( value => this.set( 'value', value ) )
+					.then( () => this );
+			};
+		} else {
+			this._refreshToken = this._defaultRefreshToken.bind( this );
+		}
+
 		/**
 		/**
 		 * A token Url, which is requested by the {@link #_defaultRefreshToken} function.
 		 * A token Url, which is requested by the {@link #_defaultRefreshToken} function.
 		 * An empty string when the callback is provided in the constructor.
 		 * An empty string when the callback is provided in the constructor.
@@ -54,27 +70,13 @@ class Token {
 		 * @type {String}
 		 * @type {String}
 		 * @private
 		 * @private
 		 */
 		 */
-		this._tokenUrl = typeof tokenUrlOrTokenRefresh === 'string' ? tokenUrl : '';
+		this._tokenUrl = typeof tokenUrlOrTokenRefresh === 'string' ? tokenUrlOrTokenRefresh : '';
 
 
 		/**
 		/**
 		 * @type {Object}
 		 * @type {Object}
 		 * @private
 		 * @private
 		 */
 		 */
 		this._options = Object.assign( {}, DEFAULT_OPTIONS, options );
 		this._options = Object.assign( {}, DEFAULT_OPTIONS, options );
-
-		/**
-		 * Refresh token function.
-		 *
-		 * @member {Function} #_refreshToken
-		 * @private
-		 */
-		if ( typeof tokenUrlOrTokenRefresh === 'function' ) {
-			this._refreshToken = () => {
-				return tokenUrlOrTokenRefresh().then( value => this.set( 'value', value ) );
-			}
-		} else {
-			this._refreshToken = this._defaultRefreshToken.bind( this );
-		}
 	}
 	}
 
 
 	/**
 	/**
@@ -104,7 +106,7 @@ class Token {
 	 * The default function to get the new token.
 	 * The default function to get the new token.
 	 *
 	 *
 	 * @protected
 	 * @protected
-	 * @returns {Promise.<Token>}
+	 * @returns {Promise}
 	 */
 	 */
 	_defaultRefreshToken() {
 	_defaultRefreshToken() {
 		return new Promise( ( resolve, reject ) => {
 		return new Promise( ( resolve, reject ) => {

+ 40 - 4
packages/ckeditor-cloud-services-core/tests/token/token.js

@@ -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` or a `tokenRefresh` function 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' ] } );
 
 
@@ -100,7 +118,16 @@ describe( 'Token', () => {
 			requests[ 0 ].respond( 200, '', 'token-value' );
 			requests[ 0 ].respond( 200, '', 'token-value' );
 		} );
 		} );
 
 
-		it( 'should throw error when cannot download new token ', done => {
+		it( 'should get a token from the specified callback function', () => {
+			const token = new Token( () => Promise.resolve( 'token-value' ), { initValue: 'initValue', autoRefresh: false } );
+
+			return token._refreshToken()
+				.then( newToken => {
+					expect( newToken.value ).to.equal( 'token-value' );
+				} );
+		} );
+
+		it( 'should throw an error when cannot download new token', done => {
 			const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
 			const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
 
 
 			token._refreshToken()
 			token._refreshToken()
@@ -113,7 +140,7 @@ describe( 'Token', () => {
 			requests[ 0 ].respond( 401 );
 			requests[ 0 ].respond( 401 );
 		} );
 		} );
 
 
-		it( 'should throw error when response is aborted', done => {
+		it( 'should throw an error when the response is aborted', done => {
 			const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
 			const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
 
 
 			token._refreshToken()
 			token._refreshToken()
@@ -126,7 +153,7 @@ describe( 'Token', () => {
 			requests[ 0 ].abort();
 			requests[ 0 ].abort();
 		} );
 		} );
 
 
-		it( 'should throw error event when network error occurs', done => {
+		it( 'should throw an error when network error occurs', done => {
 			const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
 			const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );
 
 
 			token._refreshToken()
 			token._refreshToken()
@@ -138,6 +165,15 @@ describe( 'Token', () => {
 
 
 			requests[ 0 ].error();
 			requests[ 0 ].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()
+				.catch( error => {
+					expect( error ).to.equal( 'Custom error occurred' );
+				} );
+		} );
 	} );
 	} );
 
 
 	describe( '_startRefreshing()', () => {
 	describe( '_startRefreshing()', () => {