Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix js unit tests for webdav put upload changes
  • Loading branch information
Vincent Petry committed Sep 3, 2016
commit 1871f7b04ea315004db8617860bb44922824970e
15 changes: 12 additions & 3 deletions apps/files/js/file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
OC.FileUpload = function(uploader, data) {
this.uploader = uploader;
this.data = data;
var path = OC.joinPaths(this.uploader.fileList.getCurrentDirectory(), this.getFile().name);
var path = '';
if (this.uploader.fileList) {
path = OC.joinPaths(this.uploader.fileList.getCurrentDirectory(), this.getFile().name);
} else {
path = this.getFile().name;
}
this.id = 'web-file-upload-' + md5(path) + '-' + (new Date()).getTime();
};
OC.FileUpload.CONFLICT_MODE_DETECT = 0;
Expand Down Expand Up @@ -364,6 +369,9 @@ OC.Uploader.prototype = _.extend({
* also see article @link http://blog.new-bamboo.co.uk/2012/01/10/ridiculously-simple-ajax-uploads-with-formdata
*/
_supportAjaxUploadWithProgress: function() {
if (window.TESTING) {
return true;
}
return supportFileAPI() && supportAjaxUploadProgressEvents() && supportFormData();

// Is the File API supported?
Expand Down Expand Up @@ -718,7 +726,6 @@ OC.Uploader.prototype = _.extend({
dropZone: options.dropZone, // restrict dropZone to content div
autoUpload: false,
sequentialUploads: true,
maxChunkSize: 10000000,
//singleFileUploads is on by default, so the data.files array will always have length 1
/**
* on first add of every selection
Expand Down Expand Up @@ -924,7 +931,9 @@ OC.Uploader.prototype = _.extend({
OC.Notification.showTemporary(data.errorThrown, {timeout: 10});
}

upload.deleteUpload();
if (upload) {
upload.deleteUpload();
}
},
/**
* called for every successful upload
Expand Down
8 changes: 4 additions & 4 deletions apps/files/js/filelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2685,16 +2685,16 @@
// add target dir
data.targetDir = dir;
} else {
// we are dropping somewhere inside the file list, which will
// upload the file to the current directory
data.targetDir = self.getCurrentDirectory();

// cancel uploads to current dir if no permission
var isCreatable = (self.getDirectoryPermissions() & OC.PERMISSION_CREATE) !== 0;
if (!isCreatable) {
self._showPermissionDeniedNotification();
return false;
}

// we are dropping somewhere inside the file list, which will
// upload the file to the current directory
data.targetDir = self.getCurrentDirectory();
}
});
uploader.on('add', function(e, data) {
Expand Down
2 changes: 1 addition & 1 deletion apps/files/tests/js/fileUploadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('OC.Upload tests', function() {
$dummyUploader = $('#file_upload_start');
uploader = new OC.Uploader($dummyUploader);
failStub = sinon.stub();
$dummyUploader.on('fileuploadfail', failStub);
uploader.on('fail', failStub);
});
afterEach(function() {
$dummyUploader = undefined;
Expand Down
86 changes: 36 additions & 50 deletions apps/files/tests/js/filelistSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2546,15 +2546,12 @@ describe('OCA.Files.FileList tests', function() {
* that were registered on the magic upload object
*/
describe('file upload', function() {
var $uploader;
var uploadData;
var uploader;

beforeEach(function() {
// note: this isn't the real blueimp file uploader from jquery.fileupload
// but it makes it possible to simulate the event triggering to
// test the response of the handlers
$uploader = $('#file_upload_start');
fileList.setFiles(testFiles);
uploader = fileList._uploader;
// simulate data structure from jquery.upload
uploadData = {
files: [{
Expand All @@ -2564,7 +2561,7 @@ describe('OCA.Files.FileList tests', function() {
});

afterEach(function() {
$uploader = null;
uploader = null;
uploadData = null;
});

Expand All @@ -2588,11 +2585,7 @@ describe('OCA.Files.FileList tests', function() {
* @return event object including the result
*/
function addFile(data) {
var ev = new $.Event('fileuploadadd', {});
// using triggerHandler instead of trigger so we can pass
// extra data
$uploader.triggerHandler(ev, data || {});
return ev;
uploader.trigger('add', {}, data || {});
}

it('sets target dir to the current directory', function() {
Expand All @@ -2614,82 +2607,79 @@ describe('OCA.Files.FileList tests', function() {
target: $target
}
};
var ev = new $.Event('fileuploaddrop', eventData);
// using triggerHandler instead of trigger so we can pass
// extra data
$uploader.triggerHandler(ev, data || {});
return ev;
uploader.trigger('drop', eventData, data || {});
return !!data.targetDir;
}

it('drop on a tr or crumb outside file list does not trigger upload', function() {
var $anotherTable = $('<table><tbody><tr><td>outside<div class="crumb">crumb</div></td></tr></table>');
var ev;
$('#testArea').append($anotherTable);
ev = dropOn($anotherTable.find('tr'), uploadData);
expect(ev.result).toEqual(false);
expect(ev).toEqual(false);

ev = dropOn($anotherTable.find('.crumb'));
expect(ev.result).toEqual(false);
ev = dropOn($anotherTable.find('.crumb'), uploadData);
expect(ev).toEqual(false);
});
it('drop on an element outside file list container does not trigger upload', function() {
var $anotherEl = $('<div>outside</div>');
var ev;
$('#testArea').append($anotherEl);
ev = dropOn($anotherEl);
ev = dropOn($anotherEl, uploadData);

expect(ev.result).toEqual(false);
expect(ev).toEqual(false);
});
it('drop on an element inside the table triggers upload', function() {
var ev;
ev = dropOn(fileList.$fileList.find('th:first'), uploadData);

expect(ev.result).not.toEqual(false);
expect(ev).not.toEqual(false);
expect(uploadData.targetDir).toEqual('/subdir');
});
it('drop on an element on the table container triggers upload', function() {
var ev;
ev = dropOn($('#app-content-files'), uploadData);

expect(ev.result).not.toEqual(false);
expect(ev).not.toEqual(false);
expect(uploadData.targetDir).toEqual('/subdir');
});
it('drop on an element inside the table does not trigger upload if no upload permission', function() {
$('#permissions').val(0);
var ev;
ev = dropOn(fileList.$fileList.find('th:first'));
ev = dropOn(fileList.$fileList.find('th:first'), uploadData);

expect(ev.result).toEqual(false);
expect(ev).toEqual(false);
expect(notificationStub.calledOnce).toEqual(true);
});
it('drop on an folder does not trigger upload if no upload permission on that folder', function() {
var $tr = fileList.findFileEl('somedir');
var ev;
$tr.data('permissions', OC.PERMISSION_READ);
ev = dropOn($tr);
ev = dropOn($tr, uploadData);

expect(ev.result).toEqual(false);
expect(ev).toEqual(false);
expect(notificationStub.calledOnce).toEqual(true);
});
it('drop on a file row inside the table triggers upload to current folder', function() {
var ev;
ev = dropOn(fileList.findFileEl('One.txt').find('td:first'), uploadData);

expect(ev.result).not.toEqual(false);
expect(ev).not.toEqual(false);
expect(uploadData.targetDir).toEqual('/subdir');
});
it('drop on a folder row inside the table triggers upload to target folder', function() {
var ev;
ev = dropOn(fileList.findFileEl('somedir').find('td:eq(2)'), uploadData);

expect(ev.result).not.toEqual(false);
expect(ev).not.toEqual(false);
expect(uploadData.targetDir).toEqual('/subdir/somedir');
});
it('drop on a breadcrumb inside the table triggers upload to target folder', function() {
var ev;
fileList.changeDirectory('a/b/c/d');
ev = dropOn(fileList.$el.find('.crumb:eq(2)'), uploadData);

expect(ev.result).not.toEqual(false);
expect(ev).not.toEqual(false);
expect(uploadData.targetDir).toEqual('/a/b');
});
it('renders upload indicator element for folders only', function() {
Expand All @@ -2713,8 +2703,7 @@ describe('OCA.Files.FileList tests', function() {
it('fetches folder info', function() {
var fetchInfoStub = sinon.stub(fileList, 'addAndFetchFileInfo');

var ev = new $.Event('fileuploadcreatedfolder', {});
$uploader.triggerHandler(ev, '/subdir/newfolder');
uploader.trigger('createdfolder', {}, '/subdir/newfolder');

expect(fetchInfoStub.calledOnce).toEqual(true);
expect(fetchInfoStub.getCall(0).args[0]).toEqual('newfolder');
Expand All @@ -2737,19 +2726,16 @@ describe('OCA.Files.FileList tests', function() {


function createUpload(name, dir) {
var data = {
files: [{
name: name
}],
upload: {
getFileName: sinon.stub().returns(name),
getFullPath: sinon.stub().returns(dir)
},
jqXHR: {
status: 200
var jqXHR = {
status: 200
};
return {
getFileName: sinon.stub().returns(name),
getFullPath: sinon.stub().returns(dir),
data: {
jqXHR: jqXHR
}
}
return data;
};
}

/**
Expand All @@ -2758,12 +2744,12 @@ describe('OCA.Files.FileList tests', function() {
* @return event object including the result
*/
function addFile(data) {
var ev = new $.Event('fileuploaddone', {});
// using triggerHandler instead of trigger so we can pass
// extra data
var ev = new $.Event('done', {
jqXHR: {status: 200}
});
var deferred = $.Deferred();
fetchInfoStub.returns(deferred.promise());
$uploader.triggerHandler(ev, data || {});
uploader.trigger('done', ev, data || {});
return deferred;
}

Expand All @@ -2778,7 +2764,7 @@ describe('OCA.Files.FileList tests', function() {
var def1 = addFile(createUpload('upload.txt', '/subdir'));
var def2 = addFile(createUpload('upload2.txt', '/subdir'));
var def3 = addFile(createUpload('upload3.txt', '/another'));
$uploader.triggerHandler(new $.Event('fileuploadstop'));
uploader.trigger('stop', {});

expect(highlightStub.notCalled).toEqual(true);
def1.resolve();
Expand All @@ -2794,7 +2780,7 @@ describe('OCA.Files.FileList tests', function() {
var statStub = sinon.stub(fileList, 'updateStorageStatistics');
addFile(createUpload('upload.txt', '/subdir'));
expect(statStub.notCalled).toEqual(true);
$uploader.triggerHandler(new $.Event('fileuploadstop'));
uploader.trigger('stop', {});
expect(statStub.calledOnce).toEqual(true);
statStub.restore();
});
Expand Down