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

Commit

Permalink
Merge pull request #8 from ckeditor/t/7
Browse files Browse the repository at this point in the history
Other: Aligned code to the new CloudeServices API and use `ckeditor5-cloudservices` package. Closes #7.

Feature: The plugin will now automatically refresh token when it will expire.

BREAKING CHANGE: Easy Image plugin do not use `cloudeservices.token` config anymore. The proper config now is `cloudeservices.tokenUrl`with the URL to the token server.
  • Loading branch information
Piotr Jasiun authored Oct 31, 2017
2 parents 063c0b7 + 7490e97 commit ce800f7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 94 deletions.
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.2.0",
"@ckeditor/ckeditor5-cloudservices": "ckeditor/ckeditor5-cloudservices",
"@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
16 changes: 6 additions & 10 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,24 +27,19 @@ export default class CloudServicesUploadAdapter extends Plugin {
* @inheritDoc
*/
static get requires() {
return [ FileRepository ];
return [ FileRepository, CloudServices ];
}

/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const config = editor.config;

config.define( 'cloudServices.uploadUrl', 'https://files.cke-cs.com/upload/' );
const cloudServices = editor.plugins.get( 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 @@ -102,7 +98,7 @@ CloudServicesUploadAdapter._UploadGateway = UploadGateway;
* ClassicEditor
* .create( editorElement, {
* cloudServices: {
* token: '...'
* tokenUrl: '...'
* }
* } )
* .then( ... )
Expand Down
40 changes: 0 additions & 40 deletions tests/_utils/gettoken.js

This file was deleted.

35 changes: 11 additions & 24 deletions tests/cloudservicesuploadadapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
import CloudServicesUploadAdapter from '../src/cloudservicesuploadadapter';
import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
import CloudServices from '@ckeditor/ckeditor5-cloudservices/src/cloudservices';

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

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

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

before( () => {
// Mock uploader.
CloudServices.Token = TokenMock;
CloudServicesUploadAdapter._UploadGateway = UploadGatewayMock;
} );

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

Expand All @@ -39,53 +43,36 @@ describe( 'CloudServicesUploadAdapter', () => {
describe( 'init()', () => {
it( 'should set loader', () => {
UploadGatewayMock.lastToken = undefined;
TokenMock.initialToken = 'token';

return ClassicTestEditor
.create( div, {
plugins: [ CloudServicesUploadAdapter ],
cloudServices: {
token: 'abc',
tokenUrl: 'abc',
uploadUrl: 'http://upload.mock.url/'
}
} )
.then( editor => {
expect( UploadGatewayMock.lastToken ).to.equal( 'abc' );
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 +85,7 @@ describe( 'CloudServicesUploadAdapter', () => {
return ClassicTestEditor.create( div, {
plugins: [ CloudServicesUploadAdapter ],
cloudServices: {
token: 'abc',
tokenUrl: 'abc',
uploadUrl: 'http://upload.mock.url/'
}
} ).then( _editor => {
Expand Down
20 changes: 18 additions & 2 deletions tests/easyimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,20 @@ import UploadGatewayMock from './_utils/uploadgatewaymock';
import { createNativeFileMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';

import CloudServices from '@ckeditor/ckeditor5-cloudservices/src/cloudservices';
import TokenMock from '@ckeditor/ckeditor5-cloudservices/tests/_utils/tokenmock';

const Token = CloudServices.Token

describe( 'EasyImage', () => {
before( () => {
CloudServices.Token = TokenMock;
} );

after( () => {
CloudServices.Token = Token;
} );

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

Expand All @@ -29,7 +42,10 @@ describe( 'EasyImage', () => {

return ClassicTestEditor
.create( div, {
plugins: [ EasyImage ]
plugins: [ EasyImage ],
cloudServices: {
tokenUrl: 'abc',
}
} )
.then( editor => {
const easyImage = editor.plugins.get( EasyImage );
Expand Down Expand Up @@ -83,7 +99,7 @@ describe( 'EasyImage', () => {
Paragraph, EasyImage
],
cloudServices: {
token: 'abc',
tokenUrl: 'abc',
uploadUrl: 'http://upload.mock.url/'
}
} )
Expand Down
31 changes: 14 additions & 17 deletions tests/manual/easyimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@ 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 { TOKEN_URL } from '@ckeditor/ckeditor5-cloudservices/tests/_utils/cloudservices-config';

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;
} );
ClassicEditor
.create( document.querySelector( '#editor' ), {
cloudServices: {
tokenUrl: TOKEN_URL
},
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

0 comments on commit ce800f7

Please sign in to comment.