From d36a2ff9eeedc9ebb2ebbba5d55cc255281e9929 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Thu, 30 Jan 2014 10:41:04 +0100 Subject: Added .jshintrc - Also fixes a few JSHint warnings in files app - Added "global" comment on top of files app to suppress warning and also inform devs about what globals are use --- apps/files/tests/js/filesSpec.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'apps/files/tests/js/filesSpec.js') diff --git a/apps/files/tests/js/filesSpec.js b/apps/files/tests/js/filesSpec.js index 9d0a2e4f9d7..510b4df0046 100644 --- a/apps/files/tests/js/filesSpec.js +++ b/apps/files/tests/js/filesSpec.js @@ -18,6 +18,8 @@ * License along with this library. If not, see . * */ + +/* global Files */ describe('Files tests', function() { describe('File name validation', function() { it('Validates correct file names', function() { @@ -36,12 +38,14 @@ describe('Files tests', function() { 'und Ümläüte sind auch willkommen' ]; for ( var i = 0; i < fileNames.length; i++ ) { + var error = false; try { expect(Files.isFileNameValid(fileNames[i])).toEqual(true); } catch (e) { - fail(); + error = e; } + expect(error).toEqual(false); } }); it('Detects invalid file names', function() { @@ -69,7 +73,6 @@ describe('Files tests', function() { var threwException = false; try { Files.isFileNameValid(fileNames[i]); - fail(); } catch (e) { threwException = true; -- cgit v1.2.3 From ba1b9df8a9c2da0f46f2b5a587eeb92c8c3d528d Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Thu, 30 Jan 2014 11:42:43 +0100 Subject: Fixed file name validation unit test + added newline - fixed file name validation unit test - added "\n" as forbidden character in isFileNameValid() --- apps/files/js/files.js | 22 +++++++++++++++------- apps/files/tests/js/filesSpec.js | 1 + 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'apps/files/tests/js/filesSpec.js') diff --git a/apps/files/js/files.js b/apps/files/js/files.js index 441c80762f9..d0ca43349e0 100644 --- a/apps/files/js/files.js +++ b/apps/files/js/files.js @@ -79,17 +79,25 @@ var Files = { return fileName; }, - isFileNameValid:function (name) { - if (name === '.') { - throw t('files', '\'.\' is an invalid file name.'); - } else if (name.length === 0) { + /** + * Checks whether the given file name is valid. + * @param name file name to check + * @return true if the file name is valid. + * Throws a string exception with an error message if + * the file name is not valid + */ + isFileNameValid: function (name) { + var trimmedName = name.trim(); + if (trimmedName === '.' || trimmedName === '..') { + throw t('files', '"{name}" is an invalid file name.', {name: name}); + } else if (trimmedName.length === 0) { throw t('files', 'File name cannot be empty.'); } - // check for invalid characters - var invalid_characters = ['\\', '/', '<', '>', ':', '"', '|', '?', '*']; + var invalid_characters = + ['\\', '/', '<', '>', ':', '"', '|', '?', '*', '\n']; for (var i = 0; i < invalid_characters.length; i++) { - if (name.indexOf(invalid_characters[i]) !== -1) { + if (trimmedName.indexOf(invalid_characters[i]) !== -1) { throw t('files', "Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed."); } } diff --git a/apps/files/tests/js/filesSpec.js b/apps/files/tests/js/filesSpec.js index 510b4df0046..018c8ef0f3c 100644 --- a/apps/files/tests/js/filesSpec.js +++ b/apps/files/tests/js/filesSpec.js @@ -73,6 +73,7 @@ describe('Files tests', function() { var threwException = false; try { Files.isFileNameValid(fileNames[i]); + console.error('Invalid file name not detected:', fileNames[i]); } catch (e) { threwException = true; -- cgit v1.2.3