Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Align code to the new ckeditor5-cloudservices package #8

Merged
merged 13 commits into from
Oct 31, 2017
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"ckeditor5-feature"
],
"dependencies": {
"@ckeditor/ckeditor-cloudservices-core": "^0.1.0",
"@ckeditor/ckeditor-cloudservices-core": "^0.1.1",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be changed to "^0.2.0".

"@ckeditor/ckeditor5-cloudservices": "^1.0.0-alpha.1",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that until we will release it on npm, it should be as:

"@ckeditor/ckeditor5-cloudeservices": "ckeditor/ckeditor5-cloudservices"

Otherwise there will be errors.

"@ckeditor/ckeditor5-core": "^1.0.0-alpha.1",
"@ckeditor/ckeditor5-image": "^1.0.0-alpha.1",
"@ckeditor/ckeditor5-upload": "^1.0.0-alpha.1"
Expand Down
14 changes: 6 additions & 8 deletions src/cloudservicesuploadadapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
import UploadGateway from '@ckeditor/ckeditor-cloudservices-core/src/uploadgateway/uploadgateway';
import CloudServices from '@ckeditor/ckeditor5-cloudservices/src/cloudservices';

/**
* A plugin which enables upload to Cloud Services.
Expand All @@ -26,7 +27,7 @@ export default class CloudServicesUploadAdapter extends Plugin {
* @inheritDoc
*/
static get requires() {
return [ FileRepository ];
return [ FileRepository, CloudServicesUploadAdapter._CloudServices ];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use to mock plugins. I would mock CloudServices class in CloudServices plugin instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

}

/**
Expand All @@ -36,14 +37,10 @@ export default class CloudServicesUploadAdapter extends Plugin {
const editor = this.editor;
const config = editor.config;

config.define( 'cloudServices.uploadUrl', 'https://files.cke-cs.com/upload/' );
const cloudServices = editor.plugins.get( CloudServicesUploadAdapter._CloudServices );

const token = config.get( 'cloudServices.token' );
const uploadUrl = config.get( 'cloudServices.uploadUrl' );

if ( !token || !uploadUrl ) {
return;
}
const token = cloudServices.token;
const uploadUrl = cloudServices.uploadUrl || 'https://files.cke-cs.com/upload/';

this._uploadGateway = new CloudServicesUploadAdapter._UploadGateway( token, uploadUrl );

Expand Down Expand Up @@ -82,6 +79,7 @@ class Adapter {
// Store the API in static property to easily overwrite it in tests.
// Too bad dependency injection does not work in Webpack + ES 6 (const) + Babel.
CloudServicesUploadAdapter._UploadGateway = UploadGateway;
CloudServicesUploadAdapter._CloudServices = CloudServices;

/**
* The configuration of the {@link module:easy-image/cloudservicesuploadadapter~CloudServicesUploadAdapter Cloud Services upload adapter}.
Expand Down
25 changes: 3 additions & 22 deletions tests/_utils/gettoken.js → tests/_utils/gettokenurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,8 @@ import uid from '@ckeditor/ckeditor5-utils/src/uid';
// into a subscription at any moment, allowing you to preserve all uploaded images.
const CLOUD_SERVICES_TOKEN_URL = 'https://j2sns7jmy0.execute-api.eu-central-1.amazonaws.com/prod/token';

export default function getToken() {
return new Promise( ( resolve, reject ) => {
const xhr = new XMLHttpRequest();
const userId = uid();
export default function getTokenUrl() {
const userId = uid();

xhr.open( 'GET', `${ CLOUD_SERVICES_TOKEN_URL }?user.id=${ userId }` );

xhr.onload = () => {
if ( xhr.status >= 200 && xhr.status < 300 ) {
const response = JSON.parse( xhr.responseText );

resolve( response.token );
} else {
reject( new Error( `XHR status: ${ xhr.status }` ) );
}
};

xhr.onerror = err => {
reject( err );
};

xhr.send( null );
} );
return `${ CLOUD_SERVICES_TOKEN_URL }?user.id=${ userId }`;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newest CS should support anonymous users. It means that you don't need to set user.id, and the whole file is not needed.

Copy link
Contributor Author

@ma2ciek ma2ciek Oct 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do something like this in every sample and snippet?

cloudServices: {
	tokenUrl: 'https://j2sns7jmy0.execute-api.eu-central-1.amazonaws.com/prod/token'
},

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"every sample"

Is there more than one? :P

I would go with:

const CLOUD_SERVICES_TOKEN_URL = 'https://j2sns7jmy0.execute-api.eu-central-1.amazonaws.com/prod/token';

//...

cloudServices: {
	tokenUrl: CLOUD_SERVICES_TOKEN_URL
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ach, then I would: export CLOUD_SERVICES_TOKEN_URL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll wait with changes for the correct token URI.

}
32 changes: 10 additions & 22 deletions tests/cloudservicesuploadadapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
import CloudServicesUploadAdapter from '../src/cloudservicesuploadadapter';
import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';

import UploadGatewayMock from './_utils/uploadgatewaymock';
import { createNativeFileMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
import CloudServicesMock from '@ckeditor/ckeditor5-cloudservices/tests/_utils/cloudservicesmock';

// Store original uploader.
const CSUploader = CloudServicesUploadAdapter._UploadGateway;
const CloudServices = CloudServicesUploadAdapter._CloudServices;

describe( 'CloudServicesUploadAdapter', () => {
let div;

before( () => {
// Mock uploader.
CloudServicesUploadAdapter._UploadGateway = UploadGatewayMock;
CloudServicesUploadAdapter._CloudServices = CloudServicesMock;
} );

after( () => {
// Restore original uploader.
CloudServicesUploadAdapter._UploadGateway = CSUploader;
CloudServicesUploadAdapter._CloudServices = CloudServices;
} );

beforeEach( () => {
Expand All @@ -44,48 +49,31 @@ describe( 'CloudServicesUploadAdapter', () => {
.create( div, {
plugins: [ CloudServicesUploadAdapter ],
cloudServices: {
token: 'abc',
tokenUrl: 'abc',
uploadUrl: 'http://upload.mock.url/'
}
} )
.then( editor => {
expect( UploadGatewayMock.lastToken ).to.equal( 'abc' );
expect( UploadGatewayMock.lastToken.value ).to.equal( 'token' );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not check internals of the token class here. I would remove this assertion since this test is not about it.

expect( UploadGatewayMock.lastUploadUrl ).to.equal( 'http://upload.mock.url/' );

return editor.destroy();
} );
} );

it( 'should not set loader if there is no token', () => {
UploadGatewayMock.lastToken = undefined;

return ClassicTestEditor
.create( div, {
plugins: [ CloudServicesUploadAdapter ]
} )
.then( editor => {
expect( UploadGatewayMock.lastToken ).to.be.an( 'undefined' );

return editor.destroy();
} );
} );

it( 'should set the default config.cloudServices.uploadUrl', () => {
it( 'should set the default uploadUrl', () => {
const expectedDefaultUrl = 'https://files.cke-cs.com/upload/';

return ClassicTestEditor
.create( div, {
plugins: [ CloudServicesUploadAdapter ],
cloudServices: {
token: 'abc'
tokenUrl: 'abc'
}
} )
.then( editor => {
expect( UploadGatewayMock.lastToken ).to.equal( 'abc' );
expect( UploadGatewayMock.lastUploadUrl ).to.equal( expectedDefaultUrl );

expect( editor.config.get( 'cloudServices.uploadUrl' ) ).to.equal( expectedDefaultUrl );

return editor.destroy();
} );
} );
Expand All @@ -98,7 +86,7 @@ describe( 'CloudServicesUploadAdapter', () => {
return ClassicTestEditor.create( div, {
plugins: [ CloudServicesUploadAdapter ],
cloudServices: {
token: 'abc',
tokenUrl: 'abc',
uploadUrl: 'http://upload.mock.url/'
}
} ).then( _editor => {
Expand Down
14 changes: 13 additions & 1 deletion tests/easyimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ import UploadGatewayMock from './_utils/uploadgatewaymock';
import { createNativeFileMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';

import CloudServicesMock from '@ckeditor/ckeditor5-cloudservices/tests/_utils/cloudservicesmock';

const CloudServices = CloudServicesUploadAdapter._CloudServices;

describe( 'EasyImage', () => {
before( () => {
CloudServicesUploadAdapter._CloudServices = CloudServicesMock;
} );

after( () => {
CloudServicesUploadAdapter._CloudServices = CloudServices;
} );

it( 'should require other plugins', () => {
const plugins = EasyImage.requires;

Expand Down Expand Up @@ -83,7 +95,7 @@ describe( 'EasyImage', () => {
Paragraph, EasyImage
],
cloudServices: {
token: 'abc',
tokenUrl: 'abc',
uploadUrl: 'http://upload.mock.url/'
}
} )
Expand Down
33 changes: 16 additions & 17 deletions tests/manual/easyimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@ import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'
import EasyImage from '../../src/easyimage';
import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';

import getToken from '../_utils/gettoken';
import getTokenUrl from '../_utils/getTokenUrl';

getToken()
.then( token => {
return ClassicEditor
.create( document.querySelector( '#editor' ), {
cloudServices: {
token
},
plugins: [ ArticlePluginSet, EasyImage ],
toolbar: [ 'headings', 'undo', 'redo', 'insertImage' ],
image: {
toolbar: [ 'imageStyleFull', 'imageStyleSide', '|', 'imageTextAlternative' ]
}
} )
.then( editor => {
window.editor = editor;
} );
const tokenUrl = getTokenUrl();

ClassicEditor
.create( document.querySelector( '#editor' ), {
cloudServices: {
tokenUrl
},
plugins: [ ArticlePluginSet, EasyImage ],
toolbar: [ 'headings', 'undo', 'redo', 'insertImage' ],
image: {
toolbar: [ 'imageStyleFull', 'imageStyleSide', '|', 'imageTextAlternative' ]
}
} )
.then( editor => {
window.editor = editor;
} )
.catch( err => {
console.error( err.stack );
Expand Down