aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/tests/js/shareSpec.js
blob: 2cf5dc47b6349b5e005180fd9276e53b8f643392 (plain)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */

describe('OCA.Sharing.Util tests', function() {
	var oldFileListPrototype;
	var fileList;
	var testFiles;

	function getImageUrl($el) {
		// might be slightly different cross-browser
		var url = $el.css('background-image');
		var r = url.match(/url\(['"]?([^'")]*)['"]?\)/);
		if (!r) {
			return url;
		}
		return r[1];
	}

	beforeEach(function() {
		// back up prototype, as it will be extended by
		// the sharing code
		oldFileListPrototype = _.extend({}, OCA.Files.FileList.prototype);

		var $content = $('<div id="content"></div>');
		$('#testArea').append($content);
		// dummy file list
		var $div = $(
			'<div>' +
			'<table id="filestable">' +
			'<thead></thead>' +
			'<tbody id="fileList"></tbody>' +
			'</table>' +
			'</div>');
		$('#content').append($div);

		var fileActions = new OCA.Files.FileActions();
		OCA.Sharing.Util.initialize(fileActions);
		fileList = new OCA.Files.FileList(
			$div, {
				fileActions : fileActions
			}
		);

		testFiles = [{
			id: 1,
			type: 'file',
			name: 'One.txt',
			path: '/subdir',
			mimetype: 'text/plain',
			size: 12,
			permissions: OC.PERMISSION_ALL,
			etag: 'abc',
			shareOwner: 'User One',
			isShareMountPoint: false
		}];

		OCA.Sharing.sharesLoaded = true;
		OC.Share.statuses = {
			1: {link: false, path: '/subdir'}
		};
	});
	afterEach(function() {
		OCA.Files.FileList.prototype = oldFileListPrototype;
		delete OCA.Sharing.sharesLoaded;
		delete OC.Share.droppedDown;
		fileList.destroy();
		fileList = null;
		OC.Share.statuses = {};
		OC.Share.currentShares = {};
	});

	describe('Sharing data in table row', function() {
		// TODO: test data-permissions, data-share-owner, etc
	});
	describe('Share action icon', function() {
		beforeEach(function() {
			OC.Share.statuses = {1: {link: false, path: '/subdir'}};
			OCA.Sharing.sharesLoaded = true;
		});
		afterEach(function() {
			OC.Share.statuses = {};
			OCA.Sharing.sharesLoaded = false;
		});
		it('do not shows share text when not shared', function() {
			var $action, $tr;
			OC.Share.statuses = {};
			fileList.setFiles([{
				id: 1,
				type: 'dir',
				name: 'One',
				path: '/subdir',
				mimetype: 'text/plain',
				size: 12,
				permissions: OC.PERMISSION_ALL,
				etag: 'abc'
			}]);
			$tr = fileList.$el.find('tbody tr:first');
			$action = $tr.find('.action-share');
			expect($action.hasClass('permanent')).toEqual(false);
			expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
			expect(OC.basename(getImageUrl($tr.find('.filename')))).toEqual('folder.svg');
			expect($action.find('img').length).toEqual(1);
		});
		it('shows simple share text with share icon', function() {
			var $action, $tr;
			fileList.setFiles([{
				id: 1,
				type: 'dir',
				name: 'One',
				path: '/subdir',
				mimetype: 'text/plain',
				size: 12,
				permissions: OC.PERMISSION_ALL,
				etag: 'abc'
			}]);
			$tr = fileList.$el.find('tbody tr:first');
			$action = $tr.find('.action-share');
			expect($action.hasClass('permanent')).toEqual(true);
			expect($action.find('>span').text()).toEqual('Shared');
			expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
			expect(OC.basename(getImageUrl($tr.find('.filename')))).toEqual('folder-shared.svg');
			expect($action.find('img').length).toEqual(1);
		});
		it('shows simple share text with public icon when shared with link', function() {
			var $action, $tr;
			OC.Share.statuses = {1: {link: true, path: '/subdir'}};
			fileList.setFiles([{
				id: 1,
				type: 'dir',
				name: 'One',
				path: '/subdir',
				mimetype: 'text/plain',
				size: 12,
				permissions: OC.PERMISSION_ALL,
				etag: 'abc'
			}]);
			$tr = fileList.$el.find('tbody tr:first');
			$action = $tr.find('.action-share');
			expect($action.hasClass('permanent')).toEqual(true);
			expect($action.find('>span').text()).toEqual('Shared');
			expect(OC.basename($action.find('img').attr('src'))).toEqual('public.svg');
			expect(OC.basename(getImageUrl($tr.find('.filename')))).toEqual('folder-public.svg');
			expect($action.find('img').length).toEqual(1);
		});
		it('shows owner name when owner is available', function() {
			var $action, $tr;
			fileList.setFiles([{
				id: 1,
				type: 'dir',
				name: 'One.txt',
				path: '/subdir',
				mimetype: 'text/plain',
				size: 12,
				permissions: OC.PERMISSION_ALL,
				shareOwner: 'User One',
				etag: 'abc'
			}]);
			$tr = fileList.$el.find('tbody tr:first');
			$action = $tr.find('.action-share');
			expect($action.hasClass('permanent')).toEqual(true);
			expect($action.find('>span').text()).toEqual('User One');
			expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
			expect(OC.basename(getImageUrl($tr.find('.filename')))).toEqual('folder-shared.svg');
		});
		it('shows recipients when recipients are available', function() {
			var $action, $tr;
			fileList.setFiles([{
				id: 1,
				type: 'dir',
				name: 'One.txt',
				path: '/subdir',
				mimetype: 'text/plain',
				size: 12,
				permissions: OC.PERMISSION_ALL,
				recipientsDisplayName: 'User One, User Two',
				etag: 'abc'
			}]);
			$tr = fileList.$el.find('tbody tr:first');
			$action = $tr.find('.action-share');
			expect($action.hasClass('permanent')).toEqual(true);
			expect($action.find('>span').text()).toEqual('Shared with User One, User Two');
			expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
			expect(OC.basename(getImageUrl($tr.find('.filename')))).toEqual('folder-shared.svg');
			expect($action.find('img').length).toEqual(1);
		});
		it('shows static share text when file shared with user that has no share permission', function() {
			var $action, $tr;
			fileList.setFiles([{
				id: 1,
				type: 'dir',
				name: 'One',
				path: '/subdir',
				mimetype: 'text/plain',
				size: 12,
				permissions: OC.PERMISSION_CREATE,
				etag: 'abc',
				shareOwner: 'User One'
			}]);
			$tr = fileList.$el.find('tbody tr:first');
			expect($tr.find('.action-share').length).toEqual(0);
			$action = $tr.find('.action-share-notification');
			expect($action.hasClass('permanent')).toEqual(true);
			expect($action.find('>span').text().trim()).toEqual('User One');
			expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
			expect(OC.basename(getImageUrl($tr.find('.filename')))).toEqual('folder-shared.svg');
			expect($action.find('img').length).toEqual(1);
		});
	});
	describe('Share action', function() {
		var showDropDownStub;

		function makeDummyShareItem(displayName) {
			return {
				share_with_displayname: displayName
			};
		}

		beforeEach(function() {
			showDropDownStub = sinon.stub(OC.Share, 'showDropDown', function() {
				$('#testArea').append($('<div id="dropdown"></div>'));
			});
		});
		afterEach(function() {
			showDropDownStub.restore();
		});
		it('adds share icon after sharing a non-shared file', function() {
			var $action, $tr;
			OC.Share.statuses = {};
			fileList.setFiles([{
				id: 1,
				type: 'file',
				name: 'One.txt',
				path: '/subdir',
				mimetype: 'text/plain',
				size: 12,
				permissions: OC.PERMISSION_ALL,
				etag: 'abc'
			}]);
			$action = fileList.$el.find('tbody tr:first .action-share');
			$tr = fileList.$el.find('tr:first');

			expect($action.hasClass('permanent')).toEqual(false);

			$tr.find('.action-share').click();

			expect(showDropDownStub.calledOnce).toEqual(true);

			// simulate what the dropdown does
			var shares = {};
			OC.Share.itemShares[OC.Share.SHARE_TYPE_USER] = ['user1', 'user2'];
			OC.Share.itemShares[OC.Share.SHARE_TYPE_GROUP] = ['group1', 'group2'];
			shares[OC.Share.SHARE_TYPE_USER] = _.map(['User One', 'User Two'], makeDummyShareItem);
			shares[OC.Share.SHARE_TYPE_GROUP] = _.map(['Group One', 'Group Two'], makeDummyShareItem);
			$('#dropdown').trigger(new $.Event('sharesChanged', {shares: shares}));

			expect($tr.attr('data-share-recipients')).toEqual('Group One, Group Two, User One, User Two');

			OC.Share.updateIcon('file', 1);
			expect($action.hasClass('permanent')).toEqual(true);
			expect($action.find('>span').text()).toEqual('Shared with Group One, Group Two, User One, User Two');
			expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
		});
		it('updates share icon after updating shares of a file', function() {
			var $action, $tr;
			OC.Share.statuses = {1: {link: false, path: '/subdir'}};
			fileList.setFiles([{
				id: 1,
				type: 'file',
				name: 'One.txt',
				path: '/subdir',
				mimetype: 'text/plain',
				size: 12,
				permissions: OC.PERMISSION_ALL,
				etag: 'abc'
			}]);
			$action = fileList.$el.find('tbody tr:first .action-share');
			$tr = fileList.$el.find('tr:first');

			expect($action.hasClass('permanent')).toEqual(true);

			$tr.find('.action-share').click();

			expect(showDropDownStub.calledOnce).toEqual(true);

			// simulate what the dropdown does
			var shares = {};
			OC.Share.itemShares[OC.Share.SHARE_TYPE_USER] = ['user1', 'user2', 'user3'];
			shares[OC.Share.SHARE_TYPE_USER] = _.map(['User One', 'User Two', 'User Three'], makeDummyShareItem);
			$('#dropdown').trigger(new $.Event('sharesChanged', {shares: shares}));

			expect($tr.attr('data-share-recipients')).toEqual('User One, User Three, User Two');

			OC.Share.updateIcon('file', 1);

			expect($action.hasClass('permanent')).toEqual(true);
			expect($action.find('>span').text()).toEqual('Shared with User One, User Three, User Two');
			expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
		});
		it('removes share icon after removing all shares from a file', function() {
			var $action, $tr;
			OC.Share.statuses = {1: {link: false, path: '/subdir'}};
			fileList.setFiles([{
				id: 1,
				type: 'file',
				name: 'One.txt',
				path: '/subdir',
				mimetype: 'text/plain',
				size: 12,
				permissions: OC.PERMISSION_ALL,
				etag: 'abc',
				recipients: 'User One, User Two'
			}]);
			$action = fileList.$el.find('tbody tr:first .action-share');
			$tr = fileList.$el.find('tr:first');

			expect($action.hasClass('permanent')).toEqual(true);

			$tr.find('.action-share').click();

			expect(showDropDownStub.calledOnce).toEqual(true);

			// simulate what the dropdown does
			OC.Share.itemShares = {};
			$('#dropdown').trigger(new $.Event('sharesChanged', {shares: {}}));

			expect($tr.attr('data-share-recipients')).not.toBeDefined();

			OC.Share.updateIcon('file', 1);
			expect($action.hasClass('permanent')).toEqual(false);
		});
		it('keep share text after updating reshare', function() {
			var $action, $tr;
			OC.Share.statuses = {1: {link: false, path: '/subdir'}};
			fileList.setFiles([{
				id: 1,
				type: 'file',
				name: 'One.txt',
				path: '/subdir',
				mimetype: 'text/plain',
				size: 12,
				permissions: OC.PERMISSION_ALL,
				etag: 'abc',
				shareOwner: 'User One'
			}]);
			$action = fileList.$el.find('tbody tr:first .action-share');
			$tr = fileList.$el.find('tr:first');

			expect($action.hasClass('permanent')).toEqual(true);

			$tr.find('.action-share').click();

			expect(showDropDownStub.calledOnce).toEqual(true);

			// simulate what the dropdown does
			var shares = {};
			OC.Share.itemShares[OC.Share.SHARE_TYPE_USER] = ['user2'];
			shares[OC.Share.SHARE_TYPE_USER] = _.map(['User Two'], makeDummyShareItem);
			$('#dropdown').trigger(new $.Event('sharesChanged', {shares: shares}));

			expect($tr.attr('data-share-recipients')).toEqual('User Two');

			OC.Share.updateIcon('file', 1);

			expect($action.hasClass('permanent')).toEqual(true);
			expect($action.find('>span').text()).toEqual('User One');
			expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
		});
		it('keep share text after unsharing reshare', function() {
			var $action, $tr;
			OC.Share.statuses = {1: {link: false, path: '/subdir'}};
			fileList.setFiles([{
				id: 1,
				type: 'file',
				name: 'One.txt',
				path: '/subdir',
				mimetype: 'text/plain',
				size: 12,
				permissions: OC.PERMISSION_ALL,
				etag: 'abc',
				shareOwner: 'User One',
				recipients: 'User Two'
			}]);
			$action = fileList.$el.find('tbody tr:first .action-share');
			$tr = fileList.$el.find('tr:first');

			expect($action.hasClass('permanent')).toEqual(true);

			$tr.find('.action-share').click();

			expect(showDropDownStub.calledOnce).toEqual(true);

			// simulate what the dropdown does
			OC.Share.itemShares = {};
			$('#dropdown').trigger(new $.Event('sharesChanged', {shares: {}}));

			expect($tr.attr('data-share-recipients')).not.toBeDefined();

			OC.Share.updateIcon('file', 1);

			expect($action.hasClass('permanent')).toEqual(true);
			expect($action.find('>span').text()).toEqual('User One');
			expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
		});
	});
	describe('formatRecipients', function() {
		it('returns a single recipient when one passed', function() {
			expect(OCA.Sharing.Util.formatRecipients(['User one']))
				.toEqual('User one');
		});
		it('returns two recipients when two passed', function() {
			expect(OCA.Sharing.Util.formatRecipients(['User one', 'User two']))
				.toEqual('User one, User two');
		});
		it('returns four recipients with plus when five passed', function() {
			var recipients = [
				'User one',
				'User two',
				'User three',
				'User four',
				'User five'
			];
			expect(OCA.Sharing.Util.formatRecipients(recipients))
				.toEqual('User four, User one, User three, User two, +1');
		});
		it('returns four recipients with plus when ten passed', function() {
			var recipients = [
				'User one',
				'User two',
				'User three',
				'User four',
				'User five',
				'User six',
				'User seven',
				'User eight',
				'User nine',
				'User ten'
			];
			expect(OCA.Sharing.Util.formatRecipients(recipients))
				.toEqual('User four, User one, User three, User two, +6');
		});
		it('returns four recipients with plus when four passed with counter', function() {
			var recipients = [
				'User one',
				'User two',
				'User three',
				'User four'
			];
			expect(OCA.Sharing.Util.formatRecipients(recipients, 10))
				.toEqual('User four, User one, User three, User two, +6');
		});
	});
	
});