Merge pull request #15893 from owncloud/files-fixnewfilevalidation

Fix file name validation in New menu
This commit is contained in:
Morris Jobke 2015-04-27 22:59:14 +02:00
commit 24bb4e3f6f
2 changed files with 61 additions and 10 deletions

View File

@ -611,7 +611,7 @@ OC.Upload = {
var lastPos; var lastPos;
var checkInput = function () { var checkInput = function () {
var filename = input.val(); var filename = input.val();
if (Files.isFileNameValid(filename)) { if (!Files.isFileNameValid(filename)) {
// Files.isFileNameValid(filename) throws an exception itself // Files.isFileNameValid(filename) throws an exception itself
} else if (FileList.inList(filename)) { } else if (FileList.inList(filename)) {
throw t('files', '{new_name} already exists', {new_name: filename}); throw t('files', '{new_name} already exists', {new_name: filename});
@ -651,12 +651,6 @@ OC.Upload = {
FileList.lastAction(); FileList.lastAction();
} }
var name = FileList.getUniqueName(newname); var name = FileList.getUniqueName(newname);
if (newname !== name) {
FileList.checkName(name, newname, true);
var hidden = true;
} else {
var hidden = false;
}
switch(type) { switch(type) {
case 'file': case 'file':
$.post( $.post(
@ -667,7 +661,7 @@ OC.Upload = {
}, },
function(result) { function(result) {
if (result.status === 'success') { if (result.status === 'success') {
FileList.add(result.data, {hidden: hidden, animate: true, scrollTo: true}); FileList.add(result.data, {animate: true, scrollTo: true});
} else { } else {
OC.dialogs.alert(result.data.message, t('core', 'Could not create file')); OC.dialogs.alert(result.data.message, t('core', 'Could not create file'));
} }
@ -683,7 +677,7 @@ OC.Upload = {
}, },
function(result) { function(result) {
if (result.status === 'success') { if (result.status === 'success') {
FileList.add(result.data, {hidden: hidden, animate: true, scrollTo: true}); FileList.add(result.data, {animate: true, scrollTo: true});
} else { } else {
OC.dialogs.alert(result.data.message, t('core', 'Could not create folder')); OC.dialogs.alert(result.data.message, t('core', 'Could not create folder'));
} }

View File

@ -35,7 +35,14 @@ describe('OC.Upload tests', function() {
$('#testArea').append( $('#testArea').append(
'<input type="file" id="file_upload_start" name="files[]" multiple="multiple">' + '<input type="file" id="file_upload_start" name="files[]" multiple="multiple">' +
'<input type="hidden" id="upload_limit" name="upload_limit" value="10000000">' + // 10 MB '<input type="hidden" id="upload_limit" name="upload_limit" value="10000000">' + // 10 MB
'<input type="hidden" id="free_space" name="free_space" value="50000000">' // 50 MB '<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>'
); );
$dummyUploader = $('#file_upload_start'); $dummyUploader = $('#file_upload_start');
}); });
@ -111,4 +118,54 @@ describe('OC.Upload tests', function() {
); );
}); });
}); });
describe('New file', function() {
var $input;
var currentDirStub;
beforeEach(function() {
OC.Upload.init();
$('#new>a').click();
$('#new li[data-type=file]').click();
$input = $('#new input[type=text]');
currentDirStub = sinon.stub(FileList, 'getCurrentDirectory');
currentDirStub.returns('testdir');
});
afterEach(function() {
currentDirStub.restore();
});
it('sets default text in field', function() {
expect($input.length).toEqual(1);
expect($input.val()).toEqual('New text file.txt');
});
it('creates file when enter is pressed', function() {
$input.val('somefile.txt');
$input.trigger(new $.Event('keyup', {keyCode: 13}));
$input.parent('form').submit();
expect(fakeServer.requests.length).toEqual(2);
var request = fakeServer.requests[1];
expect(request.method).toEqual('POST');
expect(request.url).toEqual(OC.webroot + '/index.php/apps/files/ajax/newfile.php');
var query = OC.parseQueryString(request.requestBody);
expect(query).toEqual({
dir: 'testdir',
filename: 'somefile.txt'
});
});
it('prevents entering invalid file names', function() {
$input.val('..');
$input.trigger(new $.Event('keyup', {keyCode: 13}));
$input.parent('form').submit();
expect(fakeServer.requests.length).toEqual(1);
});
it('prevents entering file names that already exist', function() {
var inListStub = sinon.stub(FileList, 'inList').returns(true);
$input.val('existing.txt');
$input.trigger(new $.Event('keyup', {keyCode: 13}));
$input.parent('form').submit();
expect(fakeServer.requests.length).toEqual(1);
inListStub.restore();
});
});
}); });