fileUploadSpec.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /* global FileList */
  22. describe('OC.Upload tests', function() {
  23. var $dummyUploader;
  24. var testFile;
  25. beforeEach(function() {
  26. testFile = {
  27. name: 'test.txt',
  28. size: 5000, // 5 KB
  29. type: 'text/plain',
  30. lastModifiedDate: new Date()
  31. };
  32. // need a dummy button because file-upload checks on it
  33. $('#testArea').append(
  34. '<input type="file" id="file_upload_start" name="files[]" multiple="multiple">' +
  35. '<input type="hidden" id="upload_limit" name="upload_limit" value="10000000">' + // 10 MB
  36. '<input type="hidden" id="free_space" name="free_space" value="50000000">' + // 50 MB
  37. // TODO: handlebars!
  38. '<div id="new">' +
  39. '<a>New</a>' +
  40. '<ul>' +
  41. '<li data-type="file" data-newname="New text file.txt"><p>Text file</p></li>' +
  42. '</ul>' +
  43. '</div>'
  44. );
  45. $dummyUploader = $('#file_upload_start');
  46. });
  47. afterEach(function() {
  48. delete window.file_upload_param;
  49. $dummyUploader = undefined;
  50. });
  51. describe('Adding files for upload', function() {
  52. var params;
  53. var failStub;
  54. beforeEach(function() {
  55. params = OC.Upload.init();
  56. failStub = sinon.stub();
  57. $dummyUploader.on('fileuploadfail', failStub);
  58. });
  59. afterEach(function() {
  60. params = undefined;
  61. failStub = undefined;
  62. });
  63. /**
  64. * Add file for upload
  65. * @param file file data
  66. */
  67. function addFile(file) {
  68. return params.add.call(
  69. $dummyUploader[0],
  70. {},
  71. {
  72. originalFiles: {},
  73. files: [file]
  74. });
  75. }
  76. it('adds file when size is below limits', function() {
  77. var result = addFile(testFile);
  78. expect(result).toEqual(true);
  79. });
  80. it('adds file when free space is unknown', function() {
  81. var result;
  82. $('#free_space').val(-2);
  83. result = addFile(testFile);
  84. expect(result).toEqual(true);
  85. expect(failStub.notCalled).toEqual(true);
  86. });
  87. it('does not add file if it exceeds upload limit', function() {
  88. var result;
  89. $('#upload_limit').val(1000);
  90. result = addFile(testFile);
  91. expect(result).toEqual(false);
  92. expect(failStub.calledOnce).toEqual(true);
  93. expect(failStub.getCall(0).args[1].textStatus).toEqual('sizeexceedlimit');
  94. expect(failStub.getCall(0).args[1].errorThrown).toEqual(
  95. 'Total file size 5 KB exceeds upload limit 1000 B'
  96. );
  97. });
  98. it('does not add file if it exceeds free space', function() {
  99. var result;
  100. $('#free_space').val(1000);
  101. result = addFile(testFile);
  102. expect(result).toEqual(false);
  103. expect(failStub.calledOnce).toEqual(true);
  104. expect(failStub.getCall(0).args[1].textStatus).toEqual('notenoughspace');
  105. expect(failStub.getCall(0).args[1].errorThrown).toEqual(
  106. 'Not enough free space, you are uploading 5 KB but only 1000 B is left'
  107. );
  108. });
  109. });
  110. describe('Upload conflicts', function() {
  111. var oldFileList;
  112. var conflictDialogStub;
  113. var callbacks;
  114. beforeEach(function() {
  115. oldFileList = FileList;
  116. $('#testArea').append(
  117. '<div id="tableContainer">' +
  118. '<table id="filestable">' +
  119. '<thead><tr>' +
  120. '<th id="headerName" class="hidden column-name">' +
  121. '<input type="checkbox" id="select_all_files" class="select-all">' +
  122. '<a class="name columntitle" data-sort="name"><span>Name</span><span class="sort-indicator"></span></a>' +
  123. '<span id="selectedActionsList" class="selectedActions hidden">' +
  124. '<a href class="download"><img src="actions/download.svg">Download</a>' +
  125. '<a href class="delete-selected">Delete</a></span>' +
  126. '</th>' +
  127. '<th class="hidden column-size"><a class="columntitle" data-sort="size"><span class="sort-indicator"></span></a></th>' +
  128. '<th class="hidden column-mtime"><a class="columntitle" data-sort="mtime"><span class="sort-indicator"></span></a></th>' +
  129. '</tr></thead>' +
  130. '<tbody id="fileList"></tbody>' +
  131. '<tfoot></tfoot>' +
  132. '</table>' +
  133. '</div>'
  134. );
  135. FileList = new OCA.Files.FileList($('#tableContainer'));
  136. FileList.add({name: 'conflict.txt', mimetype: 'text/plain'});
  137. FileList.add({name: 'conflict2.txt', mimetype: 'text/plain'});
  138. conflictDialogStub = sinon.stub(OC.dialogs, 'fileexists');
  139. callbacks = {
  140. onNoConflicts: sinon.stub()
  141. };
  142. });
  143. afterEach(function() {
  144. conflictDialogStub.restore();
  145. FileList.destroy();
  146. FileList = oldFileList;
  147. });
  148. it('does not show conflict dialog when no client side conflict', function() {
  149. var selection = {
  150. // yes, the format of uploads is weird...
  151. uploads: [
  152. {files: [{name: 'noconflict.txt'}]},
  153. {files: [{name: 'noconflict2.txt'}]}
  154. ]
  155. };
  156. OC.Upload.checkExistingFiles(selection, callbacks);
  157. expect(conflictDialogStub.notCalled).toEqual(true);
  158. expect(callbacks.onNoConflicts.calledOnce).toEqual(true);
  159. expect(callbacks.onNoConflicts.calledWith(selection)).toEqual(true);
  160. });
  161. it('shows conflict dialog when no client side conflict', function() {
  162. var selection = {
  163. // yes, the format of uploads is weird...
  164. uploads: [
  165. {files: [{name: 'conflict.txt'}]},
  166. {files: [{name: 'conflict2.txt'}]},
  167. {files: [{name: 'noconflict.txt'}]}
  168. ]
  169. };
  170. var deferred = $.Deferred();
  171. conflictDialogStub.returns(deferred.promise());
  172. deferred.resolve();
  173. OC.Upload.checkExistingFiles(selection, callbacks);
  174. expect(conflictDialogStub.callCount).toEqual(3);
  175. expect(conflictDialogStub.getCall(1).args[0])
  176. .toEqual({files: [ { name: 'conflict.txt' } ]});
  177. expect(conflictDialogStub.getCall(1).args[1])
  178. .toEqual({ name: 'conflict.txt', mimetype: 'text/plain', directory: '/' });
  179. expect(conflictDialogStub.getCall(1).args[2]).toEqual({ name: 'conflict.txt' });
  180. // yes, the dialog must be called several times...
  181. expect(conflictDialogStub.getCall(2).args[0]).toEqual({
  182. files: [ { name: 'conflict2.txt' } ]
  183. });
  184. expect(conflictDialogStub.getCall(2).args[1])
  185. .toEqual({ name: 'conflict2.txt', mimetype: 'text/plain', directory: '/' });
  186. expect(conflictDialogStub.getCall(2).args[2]).toEqual({ name: 'conflict2.txt' });
  187. expect(callbacks.onNoConflicts.calledOnce).toEqual(true);
  188. expect(callbacks.onNoConflicts.calledWith({
  189. uploads: [
  190. {files: [{name: 'noconflict.txt'}]}
  191. ]
  192. })).toEqual(true);
  193. });
  194. });
  195. });