filesSpec.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * ownCloud
  3. *
  4. * @author Vincent Petry
  5. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. describe('OCA.Files.Files tests', function() {
  22. var Files = OCA.Files.Files;
  23. describe('File name validation', function() {
  24. it('Validates correct file names', function() {
  25. var fileNames = [
  26. 'boringname',
  27. 'something.with.extension',
  28. 'now with spaces',
  29. '.a',
  30. '..a',
  31. '.dotfile',
  32. 'single\'quote',
  33. ' spaces before',
  34. 'spaces after ',
  35. 'allowed chars including the crazy ones $%&_-^@!,()[]{}=;#',
  36. '汉字也能用',
  37. 'und Ümläüte sind auch willkommen'
  38. ];
  39. for ( var i = 0; i < fileNames.length; i++ ) {
  40. var error = false;
  41. try {
  42. expect(Files.isFileNameValid(fileNames[i])).toEqual(true);
  43. }
  44. catch (e) {
  45. error = e;
  46. }
  47. expect(error).toEqual(false);
  48. }
  49. });
  50. it('Detects invalid file names', function() {
  51. var fileNames = [
  52. '',
  53. ' ',
  54. '.',
  55. '..',
  56. ' ..',
  57. '.. ',
  58. '. ',
  59. ' .'
  60. ];
  61. for ( var i = 0; i < fileNames.length; i++ ) {
  62. var threwException = false;
  63. try {
  64. Files.isFileNameValid(fileNames[i]);
  65. console.error('Invalid file name not detected:', fileNames[i]);
  66. }
  67. catch (e) {
  68. threwException = true;
  69. }
  70. expect(threwException).toEqual(true);
  71. }
  72. });
  73. });
  74. describe('getDownloadUrl', function() {
  75. it('returns the ajax download URL when filename and dir specified', function() {
  76. var url = Files.getDownloadUrl('test file.txt', '/subdir');
  77. expect(url).toEqual(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2Fsubdir&files=test%20file.txt');
  78. });
  79. it('returns the ajax download URL when filename and root dir specific', function() {
  80. var url = Files.getDownloadUrl('test file.txt', '/');
  81. expect(url).toEqual(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2F&files=test%20file.txt');
  82. });
  83. it('returns the ajax download URL when multiple files specified', function() {
  84. var url = Files.getDownloadUrl(['test file.txt', 'abc.txt'], '/subdir');
  85. expect(url).toEqual(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2Fsubdir&files=%5B%22test%20file.txt%22%2C%22abc.txt%22%5D');
  86. });
  87. });
  88. });