Bläddra i källkod

Feature: Added possibility to set initial value in token and adjusted others tests

Bartosz Czerwonka 8 år sedan
förälder
incheckning
91f4343288

+ 14 - 8
packages/ckeditor-cloud-services-core/src/token/token.js

@@ -23,11 +23,16 @@ class Token {
 	 * Creates `Token` instance.
 	 * Creates `Token` instance.
 	 *
 	 *
 	 * @param {String} tokenUrl Endpoint address to download the token.
 	 * @param {String} tokenUrl Endpoint address to download the token.
+	 * @param {String} [initTokenValue] Initial value of the token.
 	 * @param {Object} options
 	 * @param {Object} options
 	 * @param {Number} [options.refreshIntervalTime=3600000] Delay between refreshes. Default 1 hour.
 	 * @param {Number} [options.refreshIntervalTime=3600000] Delay between refreshes. Default 1 hour.
 	 * @param {Boolean} [options.startAutoRefresh=true] Specifies whether to start the refresh automatically.
 	 * @param {Boolean} [options.startAutoRefresh=true] Specifies whether to start the refresh automatically.
 	 */
 	 */
-	constructor( tokenUrl, options = DEFAULT_OPTIONS ) {
+	constructor( tokenUrl, initTokenValue, options = DEFAULT_OPTIONS ) {
+		if ( !tokenUrl ) {
+			throw new Error( '`tokenUrl` must be provided' );
+		}
+
 		/**
 		/**
 		 * Value of the token.
 		 * Value of the token.
 		 *
 		 *
@@ -35,7 +40,7 @@ class Token {
 		 * @observable
 		 * @observable
 		 * @member {String} #value
 		 * @member {String} #value
 		 */
 		 */
-		this.set( 'value', '' );
+		this.set( 'value', initTokenValue );
 
 
 		/**
 		/**
 		 * @type {String}
 		 * @type {String}
@@ -103,12 +108,13 @@ class Token {
 	 * @private
 	 * @private
 	 */
 	 */
 	_init() {
 	_init() {
-		this.refreshToken()
-			.then( () => {
-				if ( this._options.startAutoRefresh ) {
-					this.startRefreshing();
-				}
-			} );
+		if ( !this.value ) {
+			this.refreshToken();
+		}
+
+		if ( this._options.startAutoRefresh ) {
+			this.startRefreshing();
+		}
 	}
 	}
 }
 }
 
 

+ 3 - 3
packages/ckeditor-cloud-services-core/src/uploadgateway/fileuploader.js

@@ -20,7 +20,7 @@ class FileUploader {
 	 * Creates `FileUploader` instance.
 	 * Creates `FileUploader` instance.
 	 *
 	 *
 	 * @param {Blob|String} fileOrData A blob object or a data string encoded with Base64.
 	 * @param {Blob|String} fileOrData A blob object or a data string encoded with Base64.
-	 * @param {String} token Token used for authentication.
+	 * @param {Token} token Token used for authentication.
 	 * @param {String} apiAddress API address.
 	 * @param {String} apiAddress API address.
 	 */
 	 */
 	constructor( fileOrData, token, apiAddress ) {
 	constructor( fileOrData, token, apiAddress ) {
@@ -46,7 +46,7 @@ class FileUploader {
 		/**
 		/**
 		 * CKEditor Cloud Services access token.
 		 * CKEditor Cloud Services access token.
 		 *
 		 *
-		 * @type {String}
+		 * @type {Token}
 		 * @private
 		 * @private
 		 */
 		 */
 		this._token = token;
 		this._token = token;
@@ -115,7 +115,7 @@ class FileUploader {
 		const xhr = new XMLHttpRequest();
 		const xhr = new XMLHttpRequest();
 
 
 		xhr.open( 'POST', this._apiAddress );
 		xhr.open( 'POST', this._apiAddress );
-		xhr.setRequestHeader( 'Authorization', this._token );
+		xhr.setRequestHeader( 'Authorization', this._token.value );
 		xhr.responseType = 'json';
 		xhr.responseType = 'json';
 
 
 		this.xhr = xhr;
 		this.xhr = xhr;

+ 2 - 2
packages/ckeditor-cloud-services-core/src/uploadgateway/uploadgateway.js

@@ -14,7 +14,7 @@ export default class UploadGateway {
 	/**
 	/**
 	 * Creates `UploadGateway` instance.
 	 * Creates `UploadGateway` instance.
 	 *
 	 *
-	 * @param {String} token Token used for authentication.
+	 * @param {Token} token Token used for authentication.
 	 * @param {String} apiAddress API address.
 	 * @param {String} apiAddress API address.
 	 */
 	 */
 	constructor( token, apiAddress ) {
 	constructor( token, apiAddress ) {
@@ -29,7 +29,7 @@ export default class UploadGateway {
 		/**
 		/**
 		 * CKEditor Cloud Services access token.
 		 * CKEditor Cloud Services access token.
 		 *
 		 *
-		 * @type {String}
+		 * @type {Token}
 		 * @private
 		 * @private
 		 */
 		 */
 		this._token = token;
 		this._token = token;

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

@@ -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();
 		} );
 		} );
 	} );
 	} );
 } );
 } );

+ 7 - 5
packages/ckeditor-cloud-services-core/tests/uploadgateway/fileuploader.js

@@ -8,17 +8,19 @@
 'use strict';
 'use strict';
 
 
 import FileUploader from '../../src/uploadgateway/fileuploader';
 import FileUploader from '../../src/uploadgateway/fileuploader';
+import Token from '../../src/token/token';
 
 
 const API_ADDRESS = 'https://example.dev';
 const API_ADDRESS = 'https://example.dev';
-const TOKEN = 'token';
 const BASE_64_FILE = 'data:image/gif;base64,R0lGODlhCQAJAPIAAGFhYZXK/1FRUf///' +
 const BASE_64_FILE = 'data:image/gif;base64,R0lGODlhCQAJAPIAAGFhYZXK/1FRUf///' +
 	'9ra2gD/AAAAAAAAACH5BAEAAAUALAAAAAAJAAkAAAMYWFqwru2xERcYJLSNNWNBVimC5wjfaTkJADs=';
 	'9ra2gD/AAAAAAAAACH5BAEAAAUALAAAAAAJAAkAAAMYWFqwru2xERcYJLSNNWNBVimC5wjfaTkJADs=';
 
 
 describe( 'FileUploader', () => {
 describe( 'FileUploader', () => {
+	const token = new Token( 'url', 'token', { startAutoRefresh: false } );
+
 	let fileUploader;
 	let fileUploader;
 
 
 	beforeEach( () => {
 	beforeEach( () => {
-		fileUploader = new FileUploader( BASE_64_FILE, TOKEN, API_ADDRESS );
+		fileUploader = new FileUploader( BASE_64_FILE, token, API_ADDRESS );
 	} );
 	} );
 
 
 	describe( 'constructor()', () => {
 	describe( 'constructor()', () => {
@@ -31,11 +33,11 @@ describe( 'FileUploader', () => {
 		} );
 		} );
 
 
 		it( 'should throw error when no api address provided', () => {
 		it( 'should throw error when no api address provided', () => {
-			expect( () => new FileUploader( 'file', TOKEN ) ).to.throw( 'Api address must be provided' );
+			expect( () => new FileUploader( 'file', token ) ).to.throw( 'Api address must be provided' );
 		} );
 		} );
 
 
 		it( 'should throw error when wrong Base64 file is provided', () => {
 		it( 'should throw error when wrong Base64 file is provided', () => {
-			expect( () => new FileUploader( 'data:image/gif;base64,R', TOKEN, API_ADDRESS ) )
+			expect( () => new FileUploader( 'data:image/gif;base64,R', token, API_ADDRESS ) )
 				.to.throw( 'Problem with decoding Base64 image data.' );
 				.to.throw( 'Problem with decoding Base64 image data.' );
 		} );
 		} );
 
 
@@ -53,7 +55,7 @@ describe( 'FileUploader', () => {
 		it( 'should set `file` field', () => {
 		it( 'should set `file` field', () => {
 			const file = new File( [], 'test.jpg' );
 			const file = new File( [], 'test.jpg' );
 
 
-			const fileUploader = new FileUploader( file, TOKEN, API_ADDRESS );
+			const fileUploader = new FileUploader( file, token, API_ADDRESS );
 
 
 			expect( fileUploader.file.name ).to.be.equal( 'test.jpg' );
 			expect( fileUploader.file.name ).to.be.equal( 'test.jpg' );
 		} );
 		} );

+ 5 - 2
packages/ckeditor-cloud-services-core/tests/uploadgateway/uploadgateway.js

@@ -7,21 +7,24 @@
 
 
 import FileUploader from '../../src/uploadgateway/fileuploader';
 import FileUploader from '../../src/uploadgateway/fileuploader';
 import UploadGateway from '../../src/uploadgateway/uploadgateway';
 import UploadGateway from '../../src/uploadgateway/uploadgateway';
+import Token from '../../src/token/token';
 
 
 describe( 'UploadGateway', () => {
 describe( 'UploadGateway', () => {
+	const token = new Token( 'url', 'token', { startAutoRefresh: false } );
+
 	describe( 'constructor()', () => {
 	describe( 'constructor()', () => {
 		it( 'should throw error when no token provided', () => {
 		it( 'should throw error when no token provided', () => {
 			expect( () => new UploadGateway( undefined, 'test' ) ).to.throw( 'Token must be provided' );
 			expect( () => new UploadGateway( undefined, 'test' ) ).to.throw( 'Token must be provided' );
 		} );
 		} );
 
 
 		it( 'should throw error when no apiAddress provided', () => {
 		it( 'should throw error when no apiAddress provided', () => {
-			expect( () => new UploadGateway( 'token' ) ).to.throw( 'Api address must be provided' );
+			expect( () => new UploadGateway( token ) ).to.throw( 'Api address must be provided' );
 		} );
 		} );
 	} );
 	} );
 
 
 	describe( 'upload()', () => {
 	describe( 'upload()', () => {
 		it( 'should return `FileUploader` instance', () => {
 		it( 'should return `FileUploader` instance', () => {
-			const uploader = new UploadGateway( 'token', 'test' );
+			const uploader = new UploadGateway( token, 'test' );
 
 
 			expect( uploader.upload( 'file' ) ).to.be.instanceOf( FileUploader );
 			expect( uploader.upload( 'file' ) ).to.be.instanceOf( FileUploader );
 		} );
 		} );