Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

filesSpec.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. 'foo.part',
  61. 'bar.filepart'
  62. ];
  63. for ( var i = 0; i < fileNames.length; i++ ) {
  64. var threwException = false;
  65. try {
  66. Files.isFileNameValid(fileNames[i]);
  67. console.error('Invalid file name not detected:', fileNames[i]);
  68. }
  69. catch (e) {
  70. threwException = true;
  71. }
  72. expect(threwException).toEqual(true);
  73. }
  74. });
  75. });
  76. describe('getDownloadUrl', function() {
  77. it('returns the ajax download URL when filename and dir specified', function() {
  78. var url = Files.getDownloadUrl('test file.txt', '/subdir');
  79. expect(url).toEqual(OC.webroot + '/remote.php/webdav/subdir/test%20file.txt');
  80. });
  81. it('returns the webdav download URL when filename and root dir specified', function() {
  82. var url = Files.getDownloadUrl('test file.txt', '/');
  83. expect(url).toEqual(OC.webroot + '/remote.php/webdav/test%20file.txt');
  84. });
  85. it('returns the ajax download URL when multiple files specified', function() {
  86. var url = Files.getDownloadUrl(['test file.txt', 'abc.txt'], '/subdir');
  87. 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');
  88. });
  89. });
  90. describe('handleDownload', function() {
  91. var redirectStub;
  92. var cookieStub;
  93. var clock;
  94. var testUrl;
  95. beforeEach(function() {
  96. testUrl = 'http://example.com/owncloud/path/download.php';
  97. redirectStub = sinon.stub(OC, 'redirect');
  98. cookieStub = sinon.stub(OC.Util, 'isCookieSetToValue');
  99. clock = sinon.useFakeTimers();
  100. });
  101. afterEach(function() {
  102. redirectStub.restore();
  103. cookieStub.restore();
  104. clock.restore();
  105. });
  106. it('appends secret to url when no existing parameters', function() {
  107. Files.handleDownload(testUrl);
  108. expect(redirectStub.calledOnce).toEqual(true);
  109. expect(redirectStub.getCall(0).args[0]).toContain(testUrl + '?downloadStartSecret=');
  110. });
  111. it('appends secret to url with existing parameters', function() {
  112. Files.handleDownload(testUrl + '?test=1');
  113. expect(redirectStub.calledOnce).toEqual(true);
  114. expect(redirectStub.getCall(0).args[0]).toContain(testUrl + '?test=1&downloadStartSecret=');
  115. });
  116. it('sets cookie and calls callback when cookie appears', function() {
  117. var callbackStub = sinon.stub();
  118. var token;
  119. Files.handleDownload(testUrl, callbackStub);
  120. expect(redirectStub.calledOnce).toEqual(true);
  121. token = OC.parseQueryString(redirectStub.getCall(0).args[0]).downloadStartSecret;
  122. expect(token).toBeDefined();
  123. expect(cookieStub.calledOnce).toEqual(true);
  124. cookieStub.returns(false);
  125. clock.tick(600);
  126. expect(cookieStub.calledTwice).toEqual(true);
  127. expect(cookieStub.getCall(1).args[0]).toEqual('ocDownloadStarted');
  128. expect(cookieStub.getCall(1).args[1]).toEqual(token);
  129. expect(callbackStub.notCalled).toEqual(true);
  130. cookieStub.returns(true);
  131. clock.tick(2000);
  132. expect(cookieStub.callCount).toEqual(3);
  133. expect(callbackStub.calledOnce).toEqual(true);
  134. });
  135. });
  136. });