1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/**
* @copyright 2014 Vincent Petry <pvince81@owncloud.com>
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Jan-Christoph Borchardt <hey@jancborchardt.net>
* @author Julius Härtl <jus@bitgrid.net>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Tomasz Grobelny <tomasz@grobelny.net>
* @author Vincent Petry <vincent@nextcloud.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
describe('OC.Upload tests', function() {
var $dummyUploader;
var testFile;
var uploader;
var failStub;
var progressBarStub;
beforeEach(function() {
testFile = {
name: 'test.txt',
size: 5000, // 5 KB
type: 'text/plain',
lastModifiedDate: new Date()
};
// need a dummy button because file-upload checks on it
$('#testArea').append(
'<input type="file" id="file_upload_start" name="files[]" multiple="multiple">' +
'<input type="hidden" id="free_space" name="free_space" value="50000000">' + // 50 MB
// TODO: handlebars!
'<div id="new">' +
'<a>New</a>' +
'<ul>' +
'<li data-type="file" data-newname="New text file.txt"><p>Text file</p></li>' +
'</ul>' +
'</div>'
);
$dummyUploader = $('#file_upload_start');
progressBarStub = {on: function(){}};
uploader = new OC.Uploader($dummyUploader, {progressBar: progressBarStub});
failStub = sinon.stub();
uploader.on('fail', failStub);
});
afterEach(function() {
$dummyUploader = undefined;
failStub = undefined;
});
/**
* Add file for upload
* @param {Array.<File>} files array of file data to simulate upload
* @return {Array.<Object>} array of uploadinfo or null if add() returned false
*/
function addFiles(uploader, files) {
return _.map(files, function(file) {
var jqXHR = {status: 200};
var uploadInfo = {
originalFiles: files,
files: [file],
jqXHR: jqXHR,
response: sinon.stub().returns(jqXHR),
targetDir: "/",
submit: sinon.stub(),
abort: sinon.stub()
};
if (uploader.fileUploadParam.add.call(
$dummyUploader[0],
{},
uploadInfo
)) {
return uploadInfo;
}
return null;
});
}
describe('Adding files for upload', function() {
it('adds file when size is below limits', function(done) {
var result = addFiles(uploader, [testFile]);
expect(result[0]).not.toEqual(null);
result[0].submit.callsFake(function(){
expect(result[0].submit.calledOnce).toEqual(true);
done();
});
});
it('adds file when free space is unknown', function(done) {
var result;
$('#free_space').val(-2);
result = addFiles(uploader, [testFile]);
expect(result[0]).not.toEqual(null);
result[0].submit.callsFake(function(){
expect(result[0].submit.calledOnce).toEqual(true);
expect(failStub.notCalled).toEqual(true);
done();
});
});
it('does not add file if it exceeds free space', function(done) {
var result;
$('#free_space').val(1000);
failStub.callsFake(function(){
expect(failStub.calledOnce).toEqual(true);
expect(failStub.getCall(0).args[1].textStatus).toEqual('notenoughspace');
expect(failStub.getCall(0).args[1].errorThrown).toEqual(
'Not enough free space, you are uploading 5 KB but only 1 KB is left'
);
setTimeout(done, 0);
});
result = addFiles(uploader, [testFile]);
expect(result[0]).toEqual(null);
});
});
describe('Upload conflicts', function() {
var conflictDialogStub;
var clock;
var fileList;
beforeEach(function() {
$('#testArea').append(
'<div id="tableContainer">' +
'<table class="files-filestable list-container view-grid">' +
'<thead><tr>' +
'<th class="hidden column-name">' +
'<input type="checkbox" id="select_all_files" class="select-all">' +
'<a class="name columntitle" data-sort="name"><span>Name</span><span class="sort-indicator"></span></a>' +
'<span class="selectedActions hidden">' +
'<a href class="download"><img src="actions/download.svg">Download</a>' +
'<a href class="delete-selected">Delete</a></span>' +
'</th>' +
'<th class="hidden column-size"><a class="columntitle" data-sort="size"><span class="sort-indicator"></span></a></th>' +
'<th class="hidden column-mtime"><a class="columntitle" data-sort="mtime"><span class="sort-indicator"></span></a></th>' +
'</tr></thead>' +
'<tbody class="files-fileList"></tbody>' +
'<tfoot></tfoot>' +
'</table>' +
'</div>'
);
fileList = new OCA.Files.FileList($('#tableContainer'));
fileList.add({name: 'conflict.txt', mimetype: 'text/plain'});
fileList.add({name: 'conflict2.txt', mimetype: 'text/plain'});
conflictDialogStub = sinon.stub(OC.dialogs, 'fileexists');
uploader = new OC.Uploader($dummyUploader, {
progressBar: progressBarStub,
fileList: fileList
});
var deferred = $.Deferred();
conflictDialogStub.returns(deferred.promise());
deferred.resolve();
});
afterEach(function() {
if (clock) {
clock.restore();
clock = undefined
}
conflictDialogStub.restore();
fileList.destroy();
});
it('does not show conflict dialog when no client side conflict', function(done) {
$('#free_space').val(200000);
var counter = 0;
var fun = function() {
counter++;
if(counter != 2) {
return;
}
expect(result[0].submit.calledOnce).toEqual(true);
expect(result[1].submit.calledOnce).toEqual(true);
setTimeout(done, 0);
};
var result = addFiles(uploader, [{name: 'noconflict.txt'}, {name: 'noconflict2.txt'}]);
result[0].submit.callsFake(fun);
result[1].submit.callsFake(fun);
expect(conflictDialogStub.notCalled).toEqual(true);
});
it('shows conflict dialog when no client side conflict', function(done) {
var counter = 0;
conflictDialogStub.callsFake(function(){
counter++;
if(counter != 3) {
return $.Deferred().resolve().promise();
}
setTimeout(function() {
expect(conflictDialogStub.callCount).toEqual(3);
expect(conflictDialogStub.getCall(1).args[0].getFileName())
.toEqual('conflict.txt');
expect(conflictDialogStub.getCall(1).args[1])
.toEqual({ name: 'conflict.txt', mimetype: 'text/plain', directory: '/' });
expect(conflictDialogStub.getCall(1).args[2]).toEqual({ name: 'conflict.txt' });
// yes, the dialog must be called several times...
expect(conflictDialogStub.getCall(2).args[0].getFileName()).toEqual('conflict2.txt');
expect(conflictDialogStub.getCall(2).args[1])
.toEqual({ name: 'conflict2.txt', mimetype: 'text/plain', directory: '/' });
expect(conflictDialogStub.getCall(2).args[2]).toEqual({ name: 'conflict2.txt' });
expect(result[0].submit.calledOnce).toEqual(false);
expect(result[1].submit.calledOnce).toEqual(false);
expect(result[2].submit.calledOnce).toEqual(true);
done();
}, 10);
});
var result = addFiles(uploader, [
{name: 'conflict.txt'},
{name: 'conflict2.txt'},
{name: 'noconflict.txt'}
]);
});
it('cancels upload when skipping file in conflict mode', function(done) {
var fileData = {name: 'conflict.txt'};
var uploadData = addFiles(uploader, [
fileData
]);
var upload = new OC.FileUpload(uploader, uploadData[0]);
var deleteStub = sinon.stub(upload, 'deleteUpload');
deleteStub.callsFake(function(){
expect(deleteStub.calledOnce).toEqual(true);
done();
});
uploader.onSkip(upload);
});
it('overwrites file when choosing replace in conflict mode', function(done) {
var fileData = {name: 'conflict.txt'};
var uploadData = addFiles(uploader, [
fileData
]);
expect(uploadData[0].submit.notCalled).toEqual(true);
var upload = new OC.FileUpload(uploader, uploadData[0]);
uploadData[0].submit.callsFake(function(){
expect(upload.getConflictMode()).toEqual(OC.FileUpload.CONFLICT_MODE_OVERWRITE);
expect(uploadData[0].submit.callCount).toEqual(1);
done();
});
uploader.onReplace(upload);
});
it('autorenames file when choosing replace in conflict mode', function(done) {
var fileData = {name: 'conflict.txt'};
var uploadData = addFiles(uploader, [
fileData
]);
expect(uploadData[0].submit.notCalled).toEqual(true);
var upload = new OC.FileUpload(uploader, uploadData[0]);
var getResponseStatusStub = sinon.stub(upload, 'getResponseStatus');
var counter = 0;
uploadData[0].submit.callsFake(function(){
counter++;
if(counter===1)
{
expect(upload.getConflictMode()).toEqual(OC.FileUpload.CONFLICT_MODE_AUTORENAME);
expect(upload.getFileName()).toEqual('conflict (2).txt');
expect(uploadData[0].submit.calledOnce).toEqual(true);
getResponseStatusStub.returns(412);
uploader.fileUploadParam.fail.call($dummyUploader[0], {}, uploadData[0]);
}
if(counter===2)
{
_.defer(function() {
expect(upload.getFileName()).toEqual('conflict (3).txt');
expect(uploadData[0].submit.calledTwice).toEqual(true);
done();
})
}
});
uploader.onAutorename(upload);
// in case of server-side conflict, tries to rename again
});
});
});
|