2014-03-06 16:49:57 +04:00
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Vincent Petry
|
|
|
|
* @copyright 2014 Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
describe('OC.Upload tests', function() {
|
|
|
|
var $dummyUploader;
|
|
|
|
var testFile;
|
2015-12-16 19:35:53 +03:00
|
|
|
var uploader;
|
|
|
|
var failStub;
|
2018-11-23 00:19:57 +03:00
|
|
|
var progressBarStub;
|
2014-03-06 16:49:57 +04:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
testFile = {
|
|
|
|
name: 'test.txt',
|
|
|
|
size: 5000, // 5 KB
|
|
|
|
type: 'text/plain',
|
|
|
|
lastModifiedDate: new Date()
|
|
|
|
};
|
|
|
|
// need a dummy button because file-upload checks on it
|
|
|
|
$('#testArea').append(
|
|
|
|
'<input type="file" id="file_upload_start" name="files[]" multiple="multiple">' +
|
2015-04-27 17:31:18 +03:00
|
|
|
'<input type="hidden" id="free_space" name="free_space" value="50000000">' + // 50 MB
|
|
|
|
// TODO: handlebars!
|
|
|
|
'<div id="new">' +
|
|
|
|
'<a>New</a>' +
|
|
|
|
'<ul>' +
|
|
|
|
'<li data-type="file" data-newname="New text file.txt"><p>Text file</p></li>' +
|
|
|
|
'</ul>' +
|
|
|
|
'</div>'
|
2014-03-06 16:49:57 +04:00
|
|
|
);
|
|
|
|
$dummyUploader = $('#file_upload_start');
|
2018-11-23 00:19:57 +03:00
|
|
|
progressBarStub = {on: function(){}};
|
|
|
|
uploader = new OC.Uploader($dummyUploader, {progressBar: progressBarStub});
|
2015-12-16 19:35:53 +03:00
|
|
|
failStub = sinon.stub();
|
2016-08-31 17:32:14 +03:00
|
|
|
uploader.on('fail', failStub);
|
2014-03-06 16:49:57 +04:00
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
$dummyUploader = undefined;
|
2015-12-16 19:35:53 +03:00
|
|
|
failStub = undefined;
|
2014-03-06 16:49:57 +04:00
|
|
|
});
|
|
|
|
|
2015-12-16 19:35:53 +03:00
|
|
|
/**
|
|
|
|
* Add file for upload
|
|
|
|
* @param {Array.<File>} files array of file data to simulate upload
|
|
|
|
* @return {Array.<Object>} array of uploadinfo or null if add() returned false
|
|
|
|
*/
|
|
|
|
function addFiles(uploader, files) {
|
|
|
|
return _.map(files, function(file) {
|
|
|
|
var jqXHR = {status: 200};
|
|
|
|
var uploadInfo = {
|
|
|
|
originalFiles: files,
|
|
|
|
files: [file],
|
|
|
|
jqXHR: jqXHR,
|
2018-05-07 12:19:50 +03:00
|
|
|
response: sinon.stub().returns(jqXHR),
|
2018-12-06 14:00:09 +03:00
|
|
|
submit: sinon.stub(),
|
|
|
|
abort: sinon.stub()
|
2015-12-16 19:35:53 +03:00
|
|
|
};
|
|
|
|
if (uploader.fileUploadParam.add.call(
|
2014-03-06 16:49:57 +04:00
|
|
|
$dummyUploader[0],
|
|
|
|
{},
|
2015-12-16 19:35:53 +03:00
|
|
|
uploadInfo
|
|
|
|
)) {
|
|
|
|
return uploadInfo;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
2014-03-06 16:49:57 +04:00
|
|
|
|
2015-12-16 19:35:53 +03:00
|
|
|
describe('Adding files for upload', function() {
|
2019-02-21 01:07:38 +03:00
|
|
|
it('adds file when size is below limits', function(done) {
|
2015-12-16 19:35:53 +03:00
|
|
|
var result = addFiles(uploader, [testFile]);
|
|
|
|
expect(result[0]).not.toEqual(null);
|
2019-02-21 01:07:38 +03:00
|
|
|
result[0].submit.callsFake(function(){
|
|
|
|
expect(result[0].submit.calledOnce).toEqual(true);
|
|
|
|
done();
|
|
|
|
});
|
2014-03-06 16:49:57 +04:00
|
|
|
});
|
2019-02-21 01:07:38 +03:00
|
|
|
it('adds file when free space is unknown', function(done) {
|
2014-03-06 16:49:57 +04:00
|
|
|
var result;
|
|
|
|
$('#free_space').val(-2);
|
|
|
|
|
2015-12-16 19:35:53 +03:00
|
|
|
result = addFiles(uploader, [testFile]);
|
|
|
|
expect(result[0]).not.toEqual(null);
|
2019-02-21 01:07:38 +03:00
|
|
|
result[0].submit.callsFake(function(){
|
|
|
|
expect(result[0].submit.calledOnce).toEqual(true);
|
|
|
|
expect(failStub.notCalled).toEqual(true);
|
|
|
|
done();
|
|
|
|
});
|
2014-03-06 16:49:57 +04:00
|
|
|
});
|
2019-02-21 01:07:38 +03:00
|
|
|
it('does not add file if it exceeds free space', function(done) {
|
2014-03-06 16:49:57 +04:00
|
|
|
var result;
|
|
|
|
$('#free_space').val(1000);
|
|
|
|
|
2019-02-21 01:07:38 +03:00
|
|
|
failStub.callsFake(function(){
|
|
|
|
expect(failStub.calledOnce).toEqual(true);
|
|
|
|
expect(failStub.getCall(0).args[1].textStatus).toEqual('notenoughspace');
|
|
|
|
expect(failStub.getCall(0).args[1].errorThrown).toEqual(
|
|
|
|
'Not enough free space, you are uploading 5 KB but only 1000 B is left'
|
|
|
|
);
|
|
|
|
setTimeout(done, 0);
|
|
|
|
});
|
2015-12-16 19:35:53 +03:00
|
|
|
result = addFiles(uploader, [testFile]);
|
2014-03-06 16:49:57 +04:00
|
|
|
|
2015-12-16 19:35:53 +03:00
|
|
|
expect(result[0]).toEqual(null);
|
2014-03-06 16:49:57 +04:00
|
|
|
});
|
|
|
|
});
|
2015-10-21 12:46:51 +03:00
|
|
|
describe('Upload conflicts', function() {
|
|
|
|
var conflictDialogStub;
|
2015-12-16 19:35:53 +03:00
|
|
|
var fileList;
|
2015-10-21 12:46:51 +03:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
$('#testArea').append(
|
|
|
|
'<div id="tableContainer">' +
|
2018-10-03 22:42:24 +03:00
|
|
|
'<table id="filestable" class="list-container view-grid">' +
|
2015-10-21 12:46:51 +03:00
|
|
|
'<thead><tr>' +
|
|
|
|
'<th id="headerName" class="hidden column-name">' +
|
|
|
|
'<input type="checkbox" id="select_all_files" class="select-all">' +
|
|
|
|
'<a class="name columntitle" data-sort="name"><span>Name</span><span class="sort-indicator"></span></a>' +
|
|
|
|
'<span id="selectedActionsList" class="selectedActions hidden">' +
|
|
|
|
'<a href class="download"><img src="actions/download.svg">Download</a>' +
|
|
|
|
'<a href class="delete-selected">Delete</a></span>' +
|
|
|
|
'</th>' +
|
|
|
|
'<th class="hidden column-size"><a class="columntitle" data-sort="size"><span class="sort-indicator"></span></a></th>' +
|
|
|
|
'<th class="hidden column-mtime"><a class="columntitle" data-sort="mtime"><span class="sort-indicator"></span></a></th>' +
|
|
|
|
'</tr></thead>' +
|
|
|
|
'<tbody id="fileList"></tbody>' +
|
|
|
|
'<tfoot></tfoot>' +
|
|
|
|
'</table>' +
|
|
|
|
'</div>'
|
|
|
|
);
|
2015-12-16 19:35:53 +03:00
|
|
|
fileList = new OCA.Files.FileList($('#tableContainer'));
|
2015-10-21 12:46:51 +03:00
|
|
|
|
2015-12-16 19:35:53 +03:00
|
|
|
fileList.add({name: 'conflict.txt', mimetype: 'text/plain'});
|
|
|
|
fileList.add({name: 'conflict2.txt', mimetype: 'text/plain'});
|
2015-10-21 12:46:51 +03:00
|
|
|
|
|
|
|
conflictDialogStub = sinon.stub(OC.dialogs, 'fileexists');
|
2015-12-16 19:35:53 +03:00
|
|
|
|
|
|
|
uploader = new OC.Uploader($dummyUploader, {
|
2018-11-23 00:19:57 +03:00
|
|
|
progressBar: progressBarStub,
|
2015-12-16 19:35:53 +03:00
|
|
|
fileList: fileList
|
|
|
|
});
|
2016-09-09 13:19:29 +03:00
|
|
|
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
conflictDialogStub.returns(deferred.promise());
|
|
|
|
deferred.resolve();
|
2015-10-21 12:46:51 +03:00
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
conflictDialogStub.restore();
|
|
|
|
|
2015-12-16 19:35:53 +03:00
|
|
|
fileList.destroy();
|
2015-10-21 12:46:51 +03:00
|
|
|
});
|
2019-02-21 01:07:38 +03:00
|
|
|
it('does not show conflict dialog when no client side conflict', function(done) {
|
|
|
|
$('#free_space').val(200000);
|
|
|
|
var counter = 0;
|
|
|
|
var fun = function() {
|
|
|
|
counter++;
|
|
|
|
if(counter != 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
expect(result[0].submit.calledOnce).toEqual(true);
|
|
|
|
expect(result[1].submit.calledOnce).toEqual(true);
|
|
|
|
setTimeout(done, 0);
|
|
|
|
};
|
2015-12-16 19:35:53 +03:00
|
|
|
var result = addFiles(uploader, [{name: 'noconflict.txt'}, {name: 'noconflict2.txt'}]);
|
2019-02-21 01:07:38 +03:00
|
|
|
result[0].submit.callsFake(fun);
|
|
|
|
result[1].submit.callsFake(fun);
|
2015-10-21 12:46:51 +03:00
|
|
|
|
|
|
|
expect(conflictDialogStub.notCalled).toEqual(true);
|
2019-02-21 01:07:38 +03:00
|
|
|
|
2015-10-21 12:46:51 +03:00
|
|
|
});
|
2019-02-21 01:07:38 +03:00
|
|
|
it('shows conflict dialog when no client side conflict', function(done) {
|
|
|
|
var counter = 0;
|
|
|
|
conflictDialogStub.callsFake(function(){
|
|
|
|
counter++;
|
|
|
|
if(counter != 3) {
|
|
|
|
return $.Deferred().resolve().promise();
|
|
|
|
}
|
|
|
|
setTimeout(function() {
|
|
|
|
expect(conflictDialogStub.callCount).toEqual(3);
|
|
|
|
expect(conflictDialogStub.getCall(1).args[0].getFileName())
|
|
|
|
.toEqual('conflict.txt');
|
|
|
|
expect(conflictDialogStub.getCall(1).args[1])
|
|
|
|
.toEqual({ name: 'conflict.txt', mimetype: 'text/plain', directory: '/' });
|
|
|
|
expect(conflictDialogStub.getCall(1).args[2]).toEqual({ name: 'conflict.txt' });
|
|
|
|
|
|
|
|
// yes, the dialog must be called several times...
|
|
|
|
expect(conflictDialogStub.getCall(2).args[0].getFileName()).toEqual('conflict2.txt');
|
|
|
|
expect(conflictDialogStub.getCall(2).args[1])
|
|
|
|
.toEqual({ name: 'conflict2.txt', mimetype: 'text/plain', directory: '/' });
|
|
|
|
expect(conflictDialogStub.getCall(2).args[2]).toEqual({ name: 'conflict2.txt' });
|
|
|
|
|
|
|
|
expect(result[0].submit.calledOnce).toEqual(false);
|
|
|
|
expect(result[1].submit.calledOnce).toEqual(false);
|
|
|
|
expect(result[2].submit.calledOnce).toEqual(true);
|
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
2015-12-16 19:35:53 +03:00
|
|
|
var result = addFiles(uploader, [
|
|
|
|
{name: 'conflict.txt'},
|
|
|
|
{name: 'conflict2.txt'},
|
|
|
|
{name: 'noconflict.txt'}
|
|
|
|
]);
|
2015-10-21 12:46:51 +03:00
|
|
|
|
|
|
|
});
|
2019-02-21 01:07:38 +03:00
|
|
|
it('cancels upload when skipping file in conflict mode', function(done) {
|
2016-09-09 13:19:29 +03:00
|
|
|
var fileData = {name: 'conflict.txt'};
|
|
|
|
var uploadData = addFiles(uploader, [
|
|
|
|
fileData
|
|
|
|
]);
|
|
|
|
|
|
|
|
var upload = new OC.FileUpload(uploader, uploadData[0]);
|
|
|
|
var deleteStub = sinon.stub(upload, 'deleteUpload');
|
2019-02-21 01:07:38 +03:00
|
|
|
deleteStub.callsFake(function(){
|
|
|
|
expect(deleteStub.calledOnce).toEqual(true);
|
|
|
|
done();
|
|
|
|
});
|
2016-09-09 13:19:29 +03:00
|
|
|
|
|
|
|
uploader.onSkip(upload);
|
|
|
|
});
|
2019-02-21 01:07:38 +03:00
|
|
|
it('overwrites file when choosing replace in conflict mode', function(done) {
|
2016-09-09 13:19:29 +03:00
|
|
|
var fileData = {name: 'conflict.txt'};
|
|
|
|
var uploadData = addFiles(uploader, [
|
|
|
|
fileData
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(uploadData[0].submit.notCalled).toEqual(true);
|
|
|
|
|
|
|
|
var upload = new OC.FileUpload(uploader, uploadData[0]);
|
2019-02-21 01:07:38 +03:00
|
|
|
uploadData[0].submit.callsFake(function(){
|
|
|
|
expect(upload.getConflictMode()).toEqual(OC.FileUpload.CONFLICT_MODE_OVERWRITE);
|
|
|
|
expect(uploadData[0].submit.callCount).toEqual(1);
|
|
|
|
done();
|
|
|
|
});
|
2016-09-09 13:19:29 +03:00
|
|
|
uploader.onReplace(upload);
|
|
|
|
});
|
2019-02-21 01:07:38 +03:00
|
|
|
it('autorenames file when choosing replace in conflict mode', function(done) {
|
2016-09-09 13:19:29 +03:00
|
|
|
// needed for _.defer call
|
|
|
|
var clock = sinon.useFakeTimers();
|
|
|
|
var fileData = {name: 'conflict.txt'};
|
|
|
|
var uploadData = addFiles(uploader, [
|
|
|
|
fileData
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(uploadData[0].submit.notCalled).toEqual(true);
|
|
|
|
|
|
|
|
var upload = new OC.FileUpload(uploader, uploadData[0]);
|
|
|
|
var getResponseStatusStub = sinon.stub(upload, 'getResponseStatus');
|
2019-02-21 01:07:38 +03:00
|
|
|
var counter = 0;
|
|
|
|
uploadData[0].submit.callsFake(function(){
|
|
|
|
counter++;
|
|
|
|
if(counter===1)
|
|
|
|
{
|
|
|
|
expect(upload.getConflictMode()).toEqual(OC.FileUpload.CONFLICT_MODE_AUTORENAME);
|
|
|
|
expect(upload.getFileName()).toEqual('conflict (2).txt');
|
|
|
|
expect(uploadData[0].submit.calledOnce).toEqual(true);
|
|
|
|
getResponseStatusStub.returns(412);
|
|
|
|
uploader.fileUploadParam.fail.call($dummyUploader[0], {}, uploadData[0]);
|
|
|
|
clock.tick(500);
|
|
|
|
}
|
|
|
|
if(counter===2)
|
|
|
|
{
|
|
|
|
expect(upload.getFileName()).toEqual('conflict (3).txt');
|
|
|
|
expect(uploadData[0].submit.calledTwice).toEqual(true);
|
|
|
|
|
|
|
|
clock.restore();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
2016-09-09 13:19:29 +03:00
|
|
|
|
|
|
|
uploader.onAutorename(upload);
|
|
|
|
|
|
|
|
// in case of server-side conflict, tries to rename again
|
|
|
|
});
|
2015-10-21 12:46:51 +03:00
|
|
|
});
|
2014-03-06 16:49:57 +04:00
|
|
|
});
|