filesSpec.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. * @author Felix Heidecke <felix@heidecke.me>
  6. * @author Vincent Petry <vincent@nextcloud.com>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. describe('OCA.Files.Files tests', function() {
  25. var Files = OCA.Files.Files;
  26. describe('File name validation', function() {
  27. it('Validates correct file names', function() {
  28. var fileNames = [
  29. 'boringname',
  30. 'something.with.extension',
  31. 'now with spaces',
  32. '.a',
  33. '..a',
  34. '.dotfile',
  35. 'single\'quote',
  36. ' spaces before',
  37. 'spaces after ',
  38. 'allowed chars including the crazy ones $%&_-^@!,()[]{}=;#',
  39. '汉字也能用',
  40. 'und Ümläüte sind auch willkommen'
  41. ];
  42. for ( var i = 0; i < fileNames.length; i++ ) {
  43. var error = false;
  44. try {
  45. expect(Files.isFileNameValid(fileNames[i])).toEqual(true);
  46. }
  47. catch (e) {
  48. error = e;
  49. }
  50. expect(error).toEqual(false);
  51. }
  52. });
  53. it('Detects invalid file names', function() {
  54. var fileNames = [
  55. '',
  56. ' ',
  57. '.',
  58. '..',
  59. ' ..',
  60. '.. ',
  61. '. ',
  62. ' .',
  63. 'foo.part',
  64. 'bar.filepart'
  65. ];
  66. for ( var i = 0; i < fileNames.length; i++ ) {
  67. var threwException = false;
  68. try {
  69. Files.isFileNameValid(fileNames[i]);
  70. console.error('Invalid file name not detected:', fileNames[i]);
  71. }
  72. catch (e) {
  73. threwException = true;
  74. }
  75. expect(threwException).toEqual(true);
  76. }
  77. });
  78. });
  79. describe('getDownloadUrl', function() {
  80. it('returns the ajax download URL when filename and dir specified', function() {
  81. var url = Files.getDownloadUrl('test file.txt', '/subdir');
  82. expect(url).toEqual(OC.getRootPath() + '/remote.php/webdav/subdir/test%20file.txt');
  83. });
  84. it('returns the webdav download URL when filename and root dir specified', function() {
  85. var url = Files.getDownloadUrl('test file.txt', '/');
  86. expect(url).toEqual(OC.getRootPath() + '/remote.php/webdav/test%20file.txt');
  87. });
  88. it('returns the ajax download URL when multiple files specified', function() {
  89. var url = Files.getDownloadUrl(['test file.txt', 'abc.txt'], '/subdir');
  90. 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');
  91. });
  92. });
  93. describe('handleDownload', function() {
  94. var redirectStub;
  95. var cookieStub;
  96. var clock;
  97. var testUrl;
  98. beforeEach(function() {
  99. testUrl = 'http://example.com/owncloud/path/download.php';
  100. redirectStub = sinon.stub(OC, 'redirect');
  101. cookieStub = sinon.stub(OC.Util, 'isCookieSetToValue');
  102. clock = sinon.useFakeTimers();
  103. });
  104. afterEach(function() {
  105. redirectStub.restore();
  106. cookieStub.restore();
  107. clock.restore();
  108. });
  109. it('appends secret to url when no existing parameters', function() {
  110. Files.handleDownload(testUrl);
  111. expect(redirectStub.calledOnce).toEqual(true);
  112. expect(redirectStub.getCall(0).args[0]).toContain(testUrl + '?downloadStartSecret=');
  113. });
  114. it('appends secret to url with existing parameters', function() {
  115. Files.handleDownload(testUrl + '?test=1');
  116. expect(redirectStub.calledOnce).toEqual(true);
  117. expect(redirectStub.getCall(0).args[0]).toContain(testUrl + '?test=1&downloadStartSecret=');
  118. });
  119. it('sets cookie and calls callback when cookie appears', function() {
  120. var callbackStub = sinon.stub();
  121. var token;
  122. Files.handleDownload(testUrl, callbackStub);
  123. expect(redirectStub.calledOnce).toEqual(true);
  124. token = OC.parseQueryString(redirectStub.getCall(0).args[0]).downloadStartSecret;
  125. expect(token).toBeDefined();
  126. expect(cookieStub.calledOnce).toEqual(true);
  127. cookieStub.returns(false);
  128. clock.tick(600);
  129. expect(cookieStub.calledTwice).toEqual(true);
  130. expect(cookieStub.getCall(1).args[0]).toEqual('ocDownloadStarted');
  131. expect(cookieStub.getCall(1).args[1]).toEqual(token);
  132. expect(callbackStub.notCalled).toEqual(true);
  133. cookieStub.returns(true);
  134. clock.tick(2000);
  135. expect(cookieStub.callCount).toEqual(3);
  136. expect(callbackStub.calledOnce).toEqual(true);
  137. });
  138. });
  139. });