1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* bender-ckeditor-plugins: cloudservices,easyimage */
( function() {
var TOKEN_VALUE = 'sample-token-value',
TOKEN_URL = '/mock_token_url',
UPLOAD_URL = 'cs_url',
xhrServer = sinon.fakeServer.create(),
incrementalTokenCount = 0;
bender.editor = {
config: {
cloudServices_uploadUrl: UPLOAD_URL,
cloudServices_tokenUrl: TOKEN_URL
}
};
xhrServer.respondWith( function( req ) {
var respMapping = {
'/empty_token': ''
};
respMapping[ TOKEN_URL ] = TOKEN_VALUE;
respMapping[ UPLOAD_URL ] = JSON.stringify( {
200: 'https://foo/bar.jpg/w_200',
400: 'https://foo/bar.jpg/w_400',
600: 'https://foo/bar.jpg/w_600',
'default': 'https://foo/bar.jpg'
} );
if ( req.url in respMapping ) {
req.respond( 200, {}, respMapping[ req.url ] );
} else if ( req.url === '/incremental_token' ) {
req.respond( 200, {}, TOKEN_VALUE + incrementalTokenCount );
incrementalTokenCount += 1;
} else {
req.respond( 200, {}, 'dummy-response' );
}
} );
bender.test( {
setUp: function() {
bender.tools.ignoreUnsupportedEnvironment( 'easyimage' );
this.cloudservices = CKEDITOR.plugins.cloudservices;
incrementalTokenCount = 0;
xhrServer.respond();
this.editor.config.cloudServices_uploadUrl = UPLOAD_URL;
},
'test plugin exposes loader': function() {
assert.isInstanceOf( Function, this.cloudservices.cloudServicesLoader, 'cloudServicesLoader property type' );
},
'test loader uses config url/token': function() {
var instance = new this.cloudservices.cloudServicesLoader( this.editor, bender.tools.pngBase64 ),
// Stub loader.xhr methods before it's actually called.
listener = this.editor.once( 'fileUploadRequest', this.commonRequestListener, null, null, 0 );
try {
instance.upload();
// Make sure that configured URL has been requested.
sinon.assert.calledWithExactly( instance.xhr.open, 'POST', UPLOAD_URL, true );
// Make sure that proper header has been added.
sinon.assert.calledWithExactly( instance.xhr.setRequestHeader, 'Authorization', TOKEN_VALUE );
assert.areSame( 1, instance.xhr.send.callCount, 'Call count' );
} catch ( e ) {
// Propagate.
throw e;
} finally {
// Always remove listener.
listener.removeListener();
}
},
'test the token is fetched from cloudServices_tokenUrl': function() {
var botDefinition = {
startupData: '<p>foo</p>',
name: 'incremental_token',
config: {
extraPlugins: 'cloudservices',
cloudServices_tokenUrl: '/incremental_token',
cloudServices_uploadUrl: UPLOAD_URL
}
};
CKEDITOR.once( 'instanceCreated', function( evt ) {
// Lower the polling rate.
evt.editor.CLOUD_SERVICES_TOKEN_INTERVAL = 100;
} );
bender.editorBot.create( botDefinition, function( bot ) {
xhrServer.respond();
setTimeout( function() {
resume( function() {
xhrServer.respond();
var instance = new this.cloudservices.cloudServicesLoader( bot.editor, bender.tools.pngBase64 );
instance.upload();
for ( var i = xhrServer.requests.length - 1; i >= 0; i-- ) {
var curReq = xhrServer.requests[ i ];
if ( curReq.method === 'POST' && curReq.url === UPLOAD_URL ) {
objectAssert.hasKey( 'Authorization', curReq.requestHeaders, 'Token is included as a header' );
assert.isMatching( /sample\-token\-value\d+/, curReq.requestHeaders.Authorization, 'Authorization header' );
assert.areNotSame( TOKEN_VALUE + '0', curReq.requestHeaders.Authorization, 'Authorization header value' );
break;
}
}
} );
}, 250 );
wait();
} );
},
'test destroying the editor stops CS token querying': function() {
var customizedTokenUrl = TOKEN_URL + '/editor_to_be_removed',
botDefinition = {
startupData: '<p>foo</p>',
name: 'editor_to_be_removed',
config: {
extraPlugins: 'cloudservices',
cloudServices_tokenUrl: customizedTokenUrl,
cloudServices_uploadUrl: UPLOAD_URL
}
};
function getActiveTokenUrlCount() {
return CKEDITOR.tools.array.filter( xhrServer.requests, function( req ) {
return req.url === customizedTokenUrl;
} ).length;
}
CKEDITOR.once( 'instanceCreated', function( evt ) {
// Lower the polling rate.
evt.editor.CLOUD_SERVICES_TOKEN_INTERVAL = 100;
} );
bender.editorBot.create( botDefinition, function( bot ) {
xhrServer.respond();
// Store initial count of token calls.
var initialCount = getActiveTokenUrlCount();
bot.editor.destroy();
// Wait a bit after the editor was destroyed.
setTimeout( function() {
resume( function() {
xhrServer.respond();
assert.areSame( initialCount, getActiveTokenUrlCount(), 'Token requests count does not increase' );
} );
}, 250 );
wait();
} );
},
'test loader allows url overriding': function() {
var instance = new this.cloudservices.cloudServicesLoader( this.editor, bender.tools.pngBase64 ),
// Stub loader.xhr methods before it's actually called.
listener = this.editor.once( 'fileUploadRequest', this.commonRequestListener, null, null, 0 );
try {
instance.upload( 'my_custom_url' );
sinon.assert.calledWithExactly( instance.xhr.open, 'POST', 'my_custom_url', true );
assert.areSame( 1, instance.xhr.send.callCount, 'Call count' );
} catch ( e ) {
// Propagate.
throw e;
} finally {
// Always remove listener.
listener.removeListener();
}
},
'test loader allows token overriding': function() {
var instance = new this.cloudservices.cloudServicesLoader( this.editor, bender.tools.pngBase64, null, 'different_token' ),
// Stub loader.xhr methods before it's actually called.
listener = this.editor.once( 'fileUploadRequest', this.commonRequestListener, null, null, 0 );
try {
instance.upload();
sinon.assert.calledWithExactly( instance.xhr.setRequestHeader, 'Authorization', 'different_token' );
assert.areSame( 1, instance.xhr.send.callCount, 'Call count' );
} catch ( e ) {
// Propagate.
throw e;
} finally {
// Always remove listener.
listener.removeListener();
}
},
'test no upload URL error': function() {
var instance = new this.cloudservices.cloudServicesLoader( this.editor, bender.tools.pngBase64, null, 'different_token' ),
listener = this.editor.once( 'fileUploadRequest', this.commonRequestListener, null, null, 0 );
this.editor.config.cloudServices_uploadUrl = undefined;
CKEDITOR.once( 'log', function( evt ) {
evt.cancel();
assert.areEqual( 'cloudservices-no-upload-url', evt.data.errorCode, 'There should be URL error log.' );
} );
instance.upload();
listener.removeListener();
},
'test no token error': function() {
var botDefinition = {
startupData: '<p>foo</p>',
name: 'empty_token',
config: {
extraPlugins: 'cloudservices',
cloudServices_tokenUrl: '/empty_token',
cloudServices_uploadUrl: UPLOAD_URL
}
},
plugin = this.cloudservices;
bender.editorBot.create( botDefinition, function( bot ) {
xhrServer.respond();
var instance = new plugin.cloudServicesLoader( bot.editor, bender.tools.pngBase64 ),
listener = CKEDITOR.once( 'log', function( evt ) {
evt.cancel();
assert.areEqual( 'cloudservices-no-token', evt.data.errorCode, 'There should be TOKEN error log.' );
} );
instance.upload();
listener.removeListener();
} );
},
'test no token URL error': function() {
var botDefinition = {
startupData: '<p>foo</p>',
name: 'no_token_url',
config: {
extraPlugins: 'cloudservices',
cloudServices_uploadUrl: UPLOAD_URL
}
},
tokenUrlErrors = 0,
listener = CKEDITOR.on( 'log', function( evt ) {
if ( evt.data.errorCode === 'cloudservices-no-token-url' ) {
tokenUrlErrors += 1;
}
evt.cancel();
} );
bender.editorBot.create( botDefinition, function() {
listener.removeListener();
assert.areSame( 1, tokenUrlErrors, 'URL errors logged' );
} );
},
// Common fileUploadRequest listener reused by tests.
commonRequestListener: function( evt ) {
var loader = evt.data.fileLoader;
sinon.stub( loader.xhr, 'open' );
sinon.stub( loader.xhr, 'send' );
sinon.stub( loader.xhr, 'setRequestHeader' );
}
} );
} )();
|