8
0
Szymon Kupś 8 лет назад
Родитель
Сommit
b832db585e

+ 13 - 3
packages/ckeditor5-adapter-ckfinder/src/uploadadapter.js

@@ -11,7 +11,7 @@
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
-import { getCSRFToken } from './utils';
+import { getCsrfToken } from './utils';
 
 /**
  * Plugin that enables CKFinder uploads in CKEditor 5.
@@ -60,8 +60,18 @@ class Adapter {
 	 * @param {module:utils/locale~Locale#t} t
 	 */
 	constructor( loader, url, t ) {
-		// Save Loader instance to update upload progress.
+		/**
+		 * FileLoader instance to use during upload.
+		 *
+		 * @member {module:upload/filerepository~FileLoader} #loader
+		 */
 		this.loader = loader;
+
+		/**
+		 * Upload URL.
+		 *
+		 * @member {String} #url
+		 */
 		this.url = url;
 
 		this.t = t;
@@ -152,7 +162,7 @@ class Adapter {
 		// Prepare form data.
 		const data = new FormData();
 		data.append( 'upload', this.loader.file );
-		data.append( 'ckCsrfToken', getCSRFToken() );
+		data.append( 'ckCsrfToken', getCsrfToken() );
 
 		// Send request.
 		this.xhr.send( data );

+ 13 - 15
packages/ckeditor5-adapter-ckfinder/src/utils.js

@@ -20,7 +20,7 @@ const tokenCharset = 'abcdefghijklmnopqrstuvwxyz0123456789';
  *
  * @returns {String}
  */
-export function getCSRFToken() {
+export function getCsrfToken() {
 	let token = getCookie( TOKEN_COOKIE_NAME );
 
 	if ( !token || token.length != TOKEN_LENGTH ) {
@@ -31,17 +31,22 @@ export function getCSRFToken() {
 	return token;
 }
 
+/**
+ * Returns the value of the cookie with a given name or `null` if the cookie is not found.
+ *
+ * @param {String} name
+ * @returns {String|null}
+ */
 export function getCookie( name ) {
 	name = name.toLowerCase();
 	const parts = document.cookie.split( ';' );
-	let pair, key;
 
-	for ( let i = 0; i < parts.length; i++ ) {
-		pair = parts[ i ].split( '=' );
-		key = decodeURIComponent( pair[ 0 ].trim().toLowerCase() );
+	for ( let part of parts ) {
+		const pair = part.split( '=' );
+		const key = decodeURIComponent( pair[ 0 ].trim().toLowerCase() );
 
 		if ( key === name ) {
-			return decodeURIComponent( pair.length > 1 ? pair[ 1 ] : '' );
+			return decodeURIComponent( pair[ 1 ] );
 		}
 	}
 
@@ -64,17 +69,10 @@ export function setCookie( name, value ) {
 // @param {Number} length
 // @returns {string}
 function generateToken( length ) {
-	let randValues = [];
 	let result = '';
+	const randValues = new Uint8Array( length );
 
-	if ( window.crypto && window.crypto.getRandomValues ) {
-		randValues = new Uint8Array( length );
-		window.crypto.getRandomValues( randValues );
-	} else {
-		for ( let i = 0; i < length; i++ ) {
-			randValues.push( Math.floor( Math.random() * 256 ) );
-		}
-	}
+	window.crypto.getRandomValues( randValues );
 
 	for ( let j = 0; j < randValues.length; j++ ) {
 		let character = tokenCharset.charAt( randValues[ j ] % tokenCharset.length );

+ 72 - 0
packages/ckeditor5-adapter-ckfinder/tests/utils.js

@@ -0,0 +1,72 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals document */
+
+import { getCsrfToken, getCookie, setCookie } from '../src/utils';
+
+describe( 'utils', () => {
+	beforeEach( () => {
+		clearCookie( 'ckCsrfToken' );
+	} );
+
+	describe( 'getCsrfToken', () => {
+		let token;
+
+		beforeEach( () => {
+			token = getCsrfToken();
+		} );
+
+		it( 'should be saved in cookie', () => {
+			expect( document.cookie.indexOf( `ckCsrfToken=${ token }` ) > -1 ).to.be.true;
+		} );
+
+		it( 'should have proper length', () => {
+			expect( token.length ).to.equal( 40 );
+		} );
+
+		it( 'should produce same token for all cals', () => {
+			expect( token ).to.equal( getCsrfToken() );
+		} );
+	} );
+
+	describe( 'get/set cookie', () => {
+		let cookieName, cookieValue;
+
+		beforeEach( () => {
+			cookieName = 'test-cookie-name';
+			cookieValue = 'test-value' + Math.random();
+
+			clearCookie( cookieName );
+		} );
+
+		describe( 'setCookie', () => {
+			it( 'should set cookie', () => {
+				setCookie( cookieName, cookieValue );
+				expect( document.cookie.indexOf( `${ cookieName }=${ cookieValue }` ) > -1 ).to.be.true;
+			} );
+		} );
+
+		describe( 'getCookie', () => {
+			it( 'should get cookie', () => {
+				document.cookie = encodeURIComponent( cookieName ) + '=' + encodeURIComponent( cookieValue ) + ';path=/';
+				expect( getCookie( cookieName ) ).to.equal( cookieValue );
+			} );
+
+			it( 'should return null if cookie is not present', () => {
+				expect( getCookie( cookieName ) ).to.be.null;
+			} );
+
+			it( 'should return empty cookie', () => {
+				document.cookie = encodeURIComponent( cookieName ) + '=;path=/';
+				expect( getCookie( cookieName ) ).to.equal( '' );
+			} );
+		} );
+	} );
+} );
+
+function clearCookie( name ) {
+	document.cookie = `${ name }=;expires=Thu, 01 Jan 1970 00:00:00 GMT`;
+}