filesSpec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2015 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. describe('OCA.Files.Files tests', function() {
  7. var Files = OCA.Files.Files;
  8. describe('File name validation', function() {
  9. it('Validates correct file names', function() {
  10. var fileNames = [
  11. 'boringname',
  12. 'something.with.extension',
  13. 'now with spaces',
  14. '.a',
  15. '..a',
  16. '.dotfile',
  17. 'single\'quote',
  18. ' spaces before',
  19. 'spaces after ',
  20. 'allowed chars including the crazy ones $%&_-^@!,()[]{}=;#',
  21. '汉字也能用',
  22. 'und Ümläüte sind auch willkommen'
  23. ];
  24. for ( var i = 0; i < fileNames.length; i++ ) {
  25. var error = false;
  26. try {
  27. expect(Files.isFileNameValid(fileNames[i])).toEqual(true);
  28. }
  29. catch (e) {
  30. error = e;
  31. }
  32. expect(error).toEqual(false);
  33. }
  34. });
  35. it('Detects invalid file names', function() {
  36. var fileNames = [
  37. '',
  38. ' ',
  39. '.',
  40. '..',
  41. ' ..',
  42. '.. ',
  43. '. ',
  44. ' .',
  45. 'foo.part',
  46. 'bar.filepart'
  47. ];
  48. for ( var i = 0; i < fileNames.length; i++ ) {
  49. var threwException = false;
  50. try {
  51. Files.isFileNameValid(fileNames[i]);
  52. console.error('Invalid file name not detected:', fileNames[i]);
  53. }
  54. catch (e) {
  55. threwException = true;
  56. }
  57. expect(threwException).toEqual(true);
  58. }
  59. });
  60. });
  61. describe('getDownloadUrl', function() {
  62. it('returns the ajax download URL when filename and dir specified', function() {
  63. var url = Files.getDownloadUrl('test file.txt', '/subdir');
  64. expect(url).toEqual(OC.getRootPath() + '/remote.php/webdav/subdir/test%20file.txt');
  65. });
  66. it('returns the webdav download URL when filename and root dir specified', function() {
  67. var url = Files.getDownloadUrl('test file.txt', '/');
  68. expect(url).toEqual(OC.getRootPath() + '/remote.php/webdav/test%20file.txt');
  69. });
  70. it('returns the ajax download URL when multiple files specified', function() {
  71. var url = Files.getDownloadUrl(['test file.txt', 'abc.txt'], '/subdir');
  72. expect(url).toEqual(OC.getRootPath() + '/index.php/apps/files/ajax/download.php?dir=%2Fsubdir&files=%5B%22test%20file.txt%22%2C%22abc.txt%22%5D');
  73. });
  74. });
  75. describe('handleDownload', function() {
  76. var redirectStub;
  77. var cookieStub;
  78. var clock;
  79. var testUrl;
  80. beforeEach(function() {
  81. testUrl = 'http://example.com/owncloud/path/download.php';
  82. redirectStub = sinon.stub(OC, 'redirect');
  83. cookieStub = sinon.stub(OC.Util, 'isCookieSetToValue');
  84. clock = sinon.useFakeTimers();
  85. });
  86. afterEach(function() {
  87. redirectStub.restore();
  88. cookieStub.restore();
  89. clock.restore();
  90. });
  91. it('appends secret to url when no existing parameters', function() {
  92. Files.handleDownload(testUrl);
  93. expect(redirectStub.calledOnce).toEqual(true);
  94. expect(redirectStub.getCall(0).args[0]).toContain(testUrl + '?downloadStartSecret=');
  95. });
  96. it('appends secret to url with existing parameters', function() {
  97. Files.handleDownload(testUrl + '?test=1');
  98. expect(redirectStub.calledOnce).toEqual(true);
  99. expect(redirectStub.getCall(0).args[0]).toContain(testUrl + '?test=1&downloadStartSecret=');
  100. });
  101. it('sets cookie and calls callback when cookie appears', function() {
  102. var callbackStub = sinon.stub();
  103. var token;
  104. Files.handleDownload(testUrl, callbackStub);
  105. expect(redirectStub.calledOnce).toEqual(true);
  106. token = OC.parseQueryString(redirectStub.getCall(0).args[0]).downloadStartSecret;
  107. expect(token).toBeDefined();
  108. expect(cookieStub.calledOnce).toEqual(true);
  109. cookieStub.returns(false);
  110. clock.tick(600);
  111. expect(cookieStub.calledTwice).toEqual(true);
  112. expect(cookieStub.getCall(1).args[0]).toEqual('ocDownloadStarted');
  113. expect(cookieStub.getCall(1).args[1]).toEqual(token);
  114. expect(callbackStub.notCalled).toEqual(true);
  115. cookieStub.returns(true);
  116. clock.tick(2000);
  117. expect(cookieStub.callCount).toEqual(3);
  118. expect(callbackStub.calledOnce).toEqual(true);
  119. });
  120. });
  121. });