api.php 43.6 KB
Newer Older
1
2
<?php
/**
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
3
4
5
6
7
8
9
10
 * @author Björn Schießle <schiessle@owncloud.com>
 * @author Joas Schilling <nickvergessen@owncloud.com>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Robin Appelman <icewind@owncloud.com>
 * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 * @author Thomas Müller <thomas.mueller@tmit.eu>
 * @author Vincent Petry <pvince81@owncloud.com>
11
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
12
13
 * @copyright Copyright (c) 2015, ownCloud, Inc.
 * @license AGPL-3.0
14
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
15
16
17
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
18
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
21
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
23
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
24
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
27
 *
 */
28

29
use OCA\Files\Share;
30
use OCA\Files_sharing\Tests\TestCase;
31
32

/**
33
 * Class Test_Files_Sharing_Api
34
 */
35
class Test_Files_Sharing_Api extends TestCase {
36

37
38
39
40
	const TEST_FOLDER_NAME = '/folder_share_api_test';

	private static $tempStorage;

41
	protected function setUp() {
42
		parent::setUp();
43

Robin Appelman's avatar
Robin Appelman committed
44
45
46
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'no');
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_expire_after_n_days', '7');

47
		$this->folder = self::TEST_FOLDER_NAME;
48
		$this->subfolder  = '/subfolder_share_api_test';
49
		$this->subsubfolder = '/subsubfolder_share_api_test';
50

51
		$this->filename = '/share-api-test.txt';
52
53
54
55

		// save file with content
		$this->view->file_put_contents($this->filename, $this->data);
		$this->view->mkdir($this->folder);
56
57
58
59
		$this->view->mkdir($this->folder . $this->subfolder);
		$this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
		$this->view->file_put_contents($this->folder.$this->filename, $this->data);
		$this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
60
61
	}

62
	protected function tearDown() {
Morris Jobke's avatar
Morris Jobke committed
63
64
65
66
		if($this->view instanceof \OC\Files\View) {
			$this->view->unlink($this->filename);
			$this->view->deleteAll($this->folder);
		}
67

68
69
		self::$tempStorage = null;

70
		parent::tearDown();
71
72
73
74
75
76
77
78
79
80
81
82
83
84
	}

	/**
	 * @medium
	 */
	function testCreateShare() {

		// share to user

		// simulate a post request
		$_POST['path'] = $this->filename;
		$_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;

85
		$result = \OCA\Files_Sharing\API\Local::createShare(array());
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

		$this->assertTrue($result->succeeded());
		$data = $result->getData();

		$share = $this->getShareFromId($data['id']);

		$items = \OCP\Share::getItemShared('file', $share['item_source']);

		$this->assertTrue(!empty($items));

		// share link

		// simulate a post request
		$_POST['path'] = $this->folder;
		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;

102
		$result = \OCA\Files_Sharing\API\Local::createShare(array());
103
104
105
106
107
108
109
110
111

		// check if API call was successful
		$this->assertTrue($result->succeeded());

		$data = $result->getData();

		// check if we have a token
		$this->assertTrue(is_string($data['token']));

Roeland Jago Douma's avatar
Roeland Jago Douma committed
112
113
114
115
116
		// check for correct link
		$url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
		$this->assertEquals($url, $data['url']);


117
118
119
120
121
122
		$share = $this->getShareFromId($data['id']);

		$items = \OCP\Share::getItemShared('file', $share['item_source']);

		$this->assertTrue(!empty($items));

123
124
125
126
127
128
129
		$fileinfo = $this->view->getFileInfo($this->filename);

		\OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

		$fileinfo = $this->view->getFileInfo($this->folder);

Bjoern Schiessle's avatar
Bjoern Schiessle committed
130
		\OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
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
	/**
	 * @medium
	 */
	public function testCreateShareInvalidPermissions() {

		// simulate a post request
		$_POST['path'] = $this->filename;
		$_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
		$_POST['permissions'] = \OCP\Constants::PERMISSION_SHARE;

		$result = \OCA\Files_Sharing\API\Local::createShare([]);

		// share was successful?
		$this->assertFalse($result->succeeded());
		$this->assertEquals(400, $result->getStatusCode());

		$shares = \OCP\Share::getItemShared('file', null);
		$this->assertCount(0, $shares);

		$fileinfo = $this->view->getFileInfo($this->filename);
		\OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
	}


159
160
161
162
163
164
165
166
	function testEnfoceLinkPassword() {

		$appConfig = \OC::$server->getAppConfig();
		$appConfig->setValue('core', 'shareapi_enforce_links_password', 'yes');

		// don't allow to share link without a password
		$_POST['path'] = $this->folder;
		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
167

168

169
		$result = \OCA\Files_Sharing\API\Local::createShare(array());
170
171
		$this->assertFalse($result->succeeded());

172

173
174
175
176
177
		// don't allow to share link without a empty password
		$_POST['path'] = $this->folder;
		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
		$_POST['password'] = '';

178
		$result = \OCA\Files_Sharing\API\Local::createShare(array());
179
180
181
182
183
184
185
		$this->assertFalse($result->succeeded());

		// share with password should succeed
		$_POST['path'] = $this->folder;
		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
		$_POST['password'] = 'foo';

186
		$result = \OCA\Files_Sharing\API\Local::createShare(array());
187
188
189
190
191
192
193
194
195
196
		$this->assertTrue($result->succeeded());

		$data = $result->getData();

		// setting new password should succeed
		$params = array();
		$params['id'] = $data['id'];
		$params['_put'] = array();
		$params['_put']['password'] = 'bar';

197
		$result = \OCA\Files_Sharing\API\Local::updateShare($params);
198
199
200
201
202
203
204
205
		$this->assertTrue($result->succeeded());

		// removing password should fail
		$params = array();
		$params['id'] = $data['id'];
		$params['_put'] = array();
		$params['_put']['password'] = '';

206
		$result = \OCA\Files_Sharing\API\Local::updateShare($params);
207
208
209
210
211
212
		$this->assertFalse($result->succeeded());

		// cleanup
		$fileinfo = $this->view->getFileInfo($this->folder);
		\OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
		$appConfig->setValue('core', 'shareapi_enforce_links_password', 'no');
213
214
	}

215
216
217
218
219
220
221
	/**
	 * @medium
	*/
	function testSharePermissions() {

		// sharing file to a user should work if shareapi_exclude_groups is set
		// to no
222
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'no');
223
224
225
226
		$_POST['path'] = $this->filename;
		$_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;

227
		$result = \OCA\Files_Sharing\API\Local::createShare(array());
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

		$this->assertTrue($result->succeeded());
		$data = $result->getData();

		$share = $this->getShareFromId($data['id']);

		$items = \OCP\Share::getItemShared('file', $share['item_source']);

		$this->assertTrue(!empty($items));

		$fileinfo = $this->view->getFileInfo($this->filename);

		$result = \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

		$this->assertTrue($result);

		// exclude groups, but not the group the user belongs to. Sharing should still work
246
247
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'yes');
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', 'admin,group1,group2');
248
249
250
251
252

		$_POST['path'] = $this->filename;
		$_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;

253
		$result = \OCA\Files_Sharing\API\Local::createShare(array());
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

		$this->assertTrue($result->succeeded());
		$data = $result->getData();

		$share = $this->getShareFromId($data['id']);

		$items = \OCP\Share::getItemShared('file', $share['item_source']);

		$this->assertTrue(!empty($items));

		$fileinfo = $this->view->getFileInfo($this->filename);

		$result = \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

		$this->assertTrue($result);

		// now we exclude the group the user belongs to ('group'), sharing should fail now
272
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', 'admin,group');
273
274
275
276
277

		$_POST['path'] = $this->filename;
		$_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;

278
		$result = \OCA\Files_Sharing\API\Local::createShare(array());
279
280
281
282

		$this->assertFalse($result->succeeded());

		// cleanup
283
284
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'no');
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', '');
285
286
	}

287

288
289
290
291
292
293
	/**
	 * @medium
	 * @depends testCreateShare
	 */
	function testGetAllShares() {

294
295
296
297
298
		$fileinfo = $this->view->getFileInfo($this->filename);

		\OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
		\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

299
		$result = \OCA\Files_Sharing\API\Local::getAllShares(array());
300
301
302

		$this->assertTrue($result->succeeded());

Robin McCorkell's avatar
Robin McCorkell committed
303
		// test should return two shares created from testCreateShare()
304
305
306
307
		$this->assertTrue(count($result->getData()) === 1);

		\OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
308
309
310
311
312
313
	}

	/**
	 * @medium
	 * @depends testCreateShare
	 */
Bjoern Schiessle's avatar
Bjoern Schiessle committed
314
	function testGetShareFromSource() {
315
316
317

		$fileInfo = $this->view->getFileInfo($this->filename);

318
319
320
321
322
323
		\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
				null, 1);

324
		$_GET['path'] = $this->filename;
325

326
		$result = \OCA\Files_Sharing\API\Local::getAllShares(array());
327
328
329

		$this->assertTrue($result->succeeded());

Robin McCorkell's avatar
Robin McCorkell committed
330
		// test should return one share created from testCreateShare()
331
332
333
334
335
336
		$this->assertTrue(count($result->getData()) === 2);

		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
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
	/**
	 * @medium
	 * @depends testCreateShare
	 */
	function testGetShareFromSourceWithReshares() {

		$fileInfo = $this->view->getFileInfo($this->filename);

		// share the file as user1 to user2
		\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		// login as user2 and reshare the file to user3
		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

		\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);

		// login as user1 again
		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);

		$_GET['path'] = $this->filename;

363
		$result = \OCA\Files_Sharing\API\Local::getAllShares(array());
364
365
366
367
368
369
370
371
372

		$this->assertTrue($result->succeeded());

		// test should return one share
		$this->assertTrue(count($result->getData()) === 1);

		// now also ask for the reshares
		$_GET['reshares'] = 'true';

373
		$result = \OCA\Files_Sharing\API\Local::getAllShares(array());
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

		$this->assertTrue($result->succeeded());

		// now we should get two shares, the initial share and the reshare
		$this->assertTrue(count($result->getData()) === 2);

		// unshare files again

		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);

		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);

		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

	}

394
395
396
	/**
	 * @medium
	 * @depends testCreateShare
Bjoern Schiessle's avatar
Bjoern Schiessle committed
397
398
399
400
401
402
	 */
	function testGetShareFromId() {

		$fileInfo = $this->view->getFileInfo($this->filename);

		$result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
403
			\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
Bjoern Schiessle's avatar
Bjoern Schiessle committed
404
405
406
407

		// share was successful?
		$this->assertTrue($result);

Bjoern Schiessle's avatar
Bjoern Schiessle committed
408
		// get item to determine share ID
Bjoern Schiessle's avatar
Bjoern Schiessle committed
409
410
		$result = \OCP\Share::getItemShared('file', $fileInfo['fileid']);

411
		$this->assertEquals(1, count($result));
Bjoern Schiessle's avatar
Bjoern Schiessle committed
412
413
414
415

		// get first element
		$share = reset($result);

Bjoern Schiessle's avatar
Bjoern Schiessle committed
416
		// call getShare() with share ID
Bjoern Schiessle's avatar
Bjoern Schiessle committed
417
		$params = array('id' => $share['id']);
418
		$result = \OCA\Files_Sharing\API\Local::getShare($params);
Bjoern Schiessle's avatar
Bjoern Schiessle committed
419
420
421

		$this->assertTrue($result->succeeded());

422
		// test should return one share created from testCreateShare()
423
		$this->assertEquals(1, count($result->getData()));
Bjoern Schiessle's avatar
Bjoern Schiessle committed
424
425

		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
426
427
428
429
			\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

	}

430
431
432
433
434
435
436
437
438
439
440
441
442
443
	/**
	 * @medium
	 */
	function testGetShareFromFolder() {

		$fileInfo1 = $this->view->getFileInfo($this->filename);
		$fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);

		$result = \OCP\Share::shareItem('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		// share was successful?
		$this->assertTrue($result);

Bjoern Schiessle's avatar
Bjoern Schiessle committed
444
		$result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK,
445
446
447
448
449
				null, 1);

		// share was successful?
		$this->assertTrue(is_string($result));

Bjoern Schiessle's avatar
Bjoern Schiessle committed
450
451
		$_GET['path'] = $this->folder;
		$_GET['subfiles'] = 'true';
452

453
		$result = \OCA\Files_Sharing\API\Local::getAllShares(array());
454
455
456

		$this->assertTrue($result->succeeded());

Robin McCorkell's avatar
Robin McCorkell committed
457
		// test should return one share within $this->folder
458
459
460
461
462
		$this->assertTrue(count($result->getData()) === 1);

		\OCP\Share::unshare('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

Bjoern Schiessle's avatar
Bjoern Schiessle committed
463
		\OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
464
465
466

	}

467
	/**
468
	 * share a folder, than reshare a file within the shared folder and check if we construct the correct path
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
	 * @medium
	 */
	function testGetShareFromFolderReshares() {

		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

		$fileInfo1 = $this->view->getFileInfo($this->folder);
		$fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
		$fileInfo3 = $this->view->getFileInfo($this->folder.'/' . $this->subfolder . '/' .$this->filename);

		// share root folder to user2
		$result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		// share was successful?
		$this->assertTrue($result);

		// login as user2
		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

		// share file in root folder
		$result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
		// share was successful?
		$this->assertTrue(is_string($result));

		// share file in subfolder
		$result = \OCP\Share::shareItem('file', $fileInfo3['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
		// share was successful?
		$this->assertTrue(is_string($result));

		$testValues=array(
Bjoern Schiessle's avatar
Bjoern Schiessle committed
500
501
502
503
			array('query' => $this->folder,
				'expectedResult' => $this->folder . $this->filename),
			array('query' => $this->folder . $this->subfolder,
				'expectedResult' => $this->folder . $this->subfolder . $this->filename),
504
505
506
507
508
509
		);
		foreach ($testValues as $value) {

			$_GET['path'] = $value['query'];
			$_GET['subfiles'] = 'true';

510
			$result = \OCA\Files_Sharing\API\Local::getAllShares(array());
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531

			$this->assertTrue($result->succeeded());

			// test should return one share within $this->folder
			$data = $result->getData();

			$this->assertEquals($value['expectedResult'], $data[0]['path']);
		}

		// cleanup

		\OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
		\OCP\Share::unshare('file', $fileInfo3['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);

		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

		\OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

	}

532
	/**
533
	 * reshare a sub folder and check if we get the correct path
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
	 * @medium
	 */
	function testGetShareFromSubFolderReShares() {

		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

		$fileInfo = $this->view->getFileInfo($this->folder . $this->subfolder);

		// share sub-folder to user2
		$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		// share was successful?
		$this->assertTrue($result);

		// login as user2
		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

		// reshare subfolder
		$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);

		// share was successful?
		$this->assertTrue(is_string($result));

Bjoern Schiessle's avatar
Bjoern Schiessle committed
558
		$_GET['path'] = '/';
559
560
		$_GET['subfiles'] = 'true';

561
		$result = \OCA\Files_Sharing\API\Local::getAllShares(array());
562
563
564
565
566
567
568
569
570

		$this->assertTrue($result->succeeded());

		// test should return one share within $this->folder
		$data = $result->getData();

		// we should get exactly one result
		$this->assertEquals(1, count($data));

Bjoern Schiessle's avatar
Bjoern Schiessle committed
571
		$expectedPath = $this->subfolder;
572
573
574
575
576
577
578
579
580
581
582
583
584
585
		$this->assertEquals($expectedPath, $data[0]['path']);

		// cleanup
		$result = \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
		$this->assertTrue($result);

		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
		$result = \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
		$this->assertTrue($result);

	}

	/**
586
	 * test re-re-share of folder if the path gets constructed correctly
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
	 * @medium
	 */
	function testGetShareFromFolderReReShares() {

		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

		$fileInfo1 = $this->view->getFileInfo($this->folder . $this->subfolder);
		$fileInfo2 = $this->view->getFileInfo($this->folder . $this->subfolder . $this->subsubfolder);

		// share sub-folder to user2
		$result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		// share was successful?
		$this->assertTrue($result);

		// login as user2
		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

		// reshare subsubfolder
		$result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
		// share was successful?
		$this->assertTrue($result);

		// login as user3
		self::loginHelper(self::TEST_FILES_SHARING_API_USER3);

		$result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
		// share was successful?
		$this->assertTrue(is_string($result));


Bjoern Schiessle's avatar
Bjoern Schiessle committed
620
		$_GET['path'] = '/';
621
622
		$_GET['subfiles'] = 'true';

623
		$result = \OCA\Files_Sharing\API\Local::getAllShares(array());
624
625
626
627
628
629
630
631
632

		$this->assertTrue($result->succeeded());

		// test should return one share within $this->folder
		$data = $result->getData();

		// we should get exactly one result
		$this->assertEquals(1, count($data));

Bjoern Schiessle's avatar
Bjoern Schiessle committed
633
		$expectedPath = $this->subsubfolder;
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
		$this->assertEquals($expectedPath, $data[0]['path']);


		// cleanup
		$result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
		$this->assertTrue($result);

		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
		$result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
		$this->assertTrue($result);

		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
		$result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
		$this->assertTrue($result);

	}

653
	/**
654
	 * test multiple shared folder if the path gets constructed correctly
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
	 * @medium
	 */
	function testGetShareMultipleSharedFolder() {

		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

		$fileInfo1 = $this->view->getFileInfo($this->folder);
		$fileInfo2 = $this->view->getFileInfo($this->folder . $this->subfolder);


		// share sub-folder to user2
		$result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		// share was successful?
		$this->assertTrue($result);

		// share folder to user2
		$result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		// share was successful?
		$this->assertTrue($result);


		// login as user2
		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

		$result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
		// share was successful?
		$this->assertTrue(is_string($result));


Bjoern Schiessle's avatar
Bjoern Schiessle committed
688
689
		// ask for subfolder
		$expectedPath1 = $this->subfolder;
690
691
		$_GET['path'] = $expectedPath1;

692
		$result1 = \OCA\Files_Sharing\API\Local::getAllShares(array());
693
694
695
696
697
698
699

		$this->assertTrue($result1->succeeded());

		// test should return one share within $this->folder
		$data1 = $result1->getData();
		$share1 = reset($data1);

Bjoern Schiessle's avatar
Bjoern Schiessle committed
700
701
		// ask for folder/subfolder
		$expectedPath2 = $this->folder . $this->subfolder;
702
703
		$_GET['path'] = $expectedPath2;

704
		$result2 = \OCA\Files_Sharing\API\Local::getAllShares(array());
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735

		$this->assertTrue($result2->succeeded());

		// test should return one share within $this->folder
		$data2 = $result2->getData();
		$share2 = reset($data2);


		// validate results
		// we should get exactly one result each time
		$this->assertEquals(1, count($data1));
		$this->assertEquals(1, count($data2));

		$this->assertEquals($expectedPath1, $share1['path']);
		$this->assertEquals($expectedPath2, $share2['path']);


		// cleanup
		$result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
		$this->assertTrue($result);

		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
		$result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
		$this->assertTrue($result);
		$result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
		$this->assertTrue($result);

	}

736
	/**
737
	 * test re-re-share of folder if the path gets constructed correctly
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
	 * @medium
	 */
	function testGetShareFromFileReReShares() {

		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

		$fileInfo1 = $this->view->getFileInfo($this->folder . $this->subfolder);
		$fileInfo2 = $this->view->getFileInfo($this->folder. $this->subfolder . $this->filename);

		// share sub-folder to user2
		$result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		// share was successful?
		$this->assertTrue($result);

		// login as user2
		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

		// reshare subsubfolder
		$result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
		// share was successful?
		$this->assertTrue($result);

		// login as user3
		self::loginHelper(self::TEST_FILES_SHARING_API_USER3);

		$result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
		// share was successful?
		$this->assertTrue(is_string($result));


Bjoern Schiessle's avatar
Bjoern Schiessle committed
771
		$_GET['path'] = '/';
772
773
		$_GET['subfiles'] = 'true';

774
		$result = \OCA\Files_Sharing\API\Local::getAllShares(array());
775
776
777
778
779
780
781
782
783

		$this->assertTrue($result->succeeded());

		// test should return one share within $this->folder
		$data = $result->getData();

		// we should get exactly one result
		$this->assertEquals(1, count($data));

Bjoern Schiessle's avatar
Bjoern Schiessle committed
784
		$expectedPath = $this->filename;
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
		$this->assertEquals($expectedPath, $data[0]['path']);


		// cleanup
		$result = \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
		$this->assertTrue($result);

		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
		$result = \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
		$this->assertTrue($result);

		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
		$result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
		$this->assertTrue($result);

	}

804
805
806
807
808
809
810
	/**
	 * @medium
	 */
	function testGetShareFromUnknownId() {

		$params = array('id' => 0);

811
		$result = \OCA\Files_Sharing\API\Local::getShare($params);
812
813

		$this->assertEquals(404, $result->getStatusCode());
Robin McCorkell's avatar
Robin McCorkell committed
814
		$meta = $result->getMeta();
815
		$this->assertEquals('share doesn\'t exist', $meta['message']);
Bjoern Schiessle's avatar
Bjoern Schiessle committed
816
817
818
819
820
821

	}

	/**
	 * @medium
	 * @depends testCreateShare
822
823
824
	 */
	function testUpdateShare() {

825
826
		$fileInfo = $this->view->getFileInfo($this->filename);

827
		$result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
828
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);
829

830
831
832
833
		// share was successful?
		$this->assertTrue($result);

		$result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
834
835
				null, 1);

836
837
838
		// share was successful?
		$this->assertTrue(is_string($result));

839
840
		$items = \OCP\Share::getItemShared('file', null);

841
842
843
		// make sure that we found a link share and a user share
		$this->assertEquals(count($items), 2);

844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
		$linkShare = null;
		$userShare = null;

		foreach ($items as $item) {
			if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
				$linkShare = $item;
			}
			if ($item['share_type'] === \OCP\Share::SHARE_TYPE_USER) {
				$userShare = $item;
			}
		}

		// make sure that we found a link share and a user share
		$this->assertTrue(is_array($linkShare));
		$this->assertTrue(is_array($userShare));

860
861
		// check if share have expected permissions, single shared files never have
		// delete permissions
862
		$this->assertEquals(\OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_DELETE, $userShare['permissions']);
863

864
		// update permissions
865
866
867
868
869
870

		$params = array();
		$params['id'] = $userShare['id'];
		$params['_put'] = array();
		$params['_put']['permissions'] = 1;

871
		$result = \OCA\Files_Sharing\API\Local::updateShare($params);
872

Robin McCorkell's avatar
Robin McCorkell committed
873
		$meta = $result->getMeta();
874
		$this->assertTrue($result->succeeded(), $meta['message']);
875
876
877
878
879
880
881
882
883
884
885
886
887

		$items = \OCP\Share::getItemShared('file', $userShare['file_source']);

		$newUserShare = null;
		foreach ($items as $item) {
			if ($item['share_with'] === \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2) {
				$newUserShare = $item;
				break;
			}
		}

		$this->assertTrue(is_array($newUserShare));

888
		$this->assertEquals('1', $newUserShare['permissions']);
889
890
891
892
893
894
895
896
897
898

		// update password for link share

		$this->assertTrue(empty($linkShare['share_with']));

		$params = array();
		$params['id'] = $linkShare['id'];
		$params['_put'] = array();
		$params['_put']['password'] = 'foo';

899
		$result = \OCA\Files_Sharing\API\Local::updateShare($params);
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915

		$this->assertTrue($result->succeeded());

		$items = \OCP\Share::getItemShared('file', $linkShare['file_source']);

		$newLinkShare = null;
		foreach ($items as $item) {
			if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
				$newLinkShare = $item;
				break;
			}
		}

		$this->assertTrue(is_array($newLinkShare));
		$this->assertTrue(!empty($newLinkShare['share_with']));

916
917
918
919
920
		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);

921
922
	}

923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
	/**
	 * @medium
	 * @depends testCreateShare
	 */
	public function testUpdateShareInvalidPermissions() {

		$fileInfo = $this->view->getFileInfo($this->filename);

		$result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);

		// share was successful?
		$this->assertTrue($result);

		$share = \OCP\Share::getItemShared('file', null);
		$this->assertCount(1, $share);
		$share = reset($share);

		// check if share have expected permissions, single shared files never have
		// delete permissions
		$this->assertEquals(\OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_DELETE, $share['permissions']);

		// update permissions
		$params = [];
		$params['id'] = $share['id'];
		$params['_put'] = [];
		$params['_put']['permissions'] = \OCP\Constants::PERMISSION_SHARE;

		$result = \OCA\Files_Sharing\API\Local::updateShare($params);

		//Updating should fail with 400
		$this->assertFalse($result->succeeded());
		$this->assertEquals(400, $result->getStatusCode());

		$share = \OCP\Share::getItemShared('file', $share['file_source']);
		$share = reset($share);

		//Permissions should not have changed!
		$this->assertEquals(\OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_DELETE, $share['permissions']);

		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
	}


Bjoern Schiessle's avatar
Bjoern Schiessle committed
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
	/**
	 * @medium
	 */
	function testUpdateShareUpload() {

		$fileInfo = $this->view->getFileInfo($this->folder);

		$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
				null, 1);

		// share was successful?
		$this->assertTrue(is_string($result));

		$items = \OCP\Share::getItemShared('file', null);

		// make sure that we found a link share and a user share
984
		$this->assertEquals(1, count($items));
Bjoern Schiessle's avatar
Bjoern Schiessle committed
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001

		$linkShare = null;

		foreach ($items as $item) {
			if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
				$linkShare = $item;
			}
		}

		// make sure that we found a link share
		$this->assertTrue(is_array($linkShare));

		// update public upload

		$params = array();
		$params['id'] = $linkShare['id'];
		$params['_put'] = array();
Bjoern Schiessle's avatar
Bjoern Schiessle committed
1002
		$params['_put']['publicUpload'] = 'true';
Bjoern Schiessle's avatar
Bjoern Schiessle committed
1003

1004
		$result = \OCA\Files_Sharing\API\Local::updateShare($params);
Bjoern Schiessle's avatar
Bjoern Schiessle committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026

		$this->assertTrue($result->succeeded());

		$items = \OCP\Share::getItemShared('file', $linkShare['file_source']);

		$updatedLinkShare = null;
		foreach ($items as $item) {
			if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
				$updatedLinkShare = $item;
				break;
			}
		}

		$this->assertTrue(is_array($updatedLinkShare));
		$this->assertEquals(7, $updatedLinkShare['permissions']);

		// cleanup

		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);

	}

1027
1028
1029
1030
1031
1032
	/**
	 * @medium
	 */
	function testUpdateShareExpireDate() {

		$fileInfo = $this->view->getFileInfo($this->folder);
1033
		$config = \OC::$server->getConfig();
1034
1035

		// enforce expire date, by default 7 days after the file was shared
1036
1037
		$config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
		$config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062

		$dateWithinRange = new \DateTime();
		$dateWithinRange->add(new \DateInterval('P5D'));
		$dateOutOfRange = new \DateTime();
		$dateOutOfRange->add(new \DateInterval('P8D'));

		$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
				null, 1);

		// share was successful?
		$this->assertTrue(is_string($result));

		$items = \OCP\Share::getItemShared('file', null);

		// make sure that we found a link share
		$this->assertEquals(1, count($items));

		$linkShare = reset($items);

		// update expire date to a valid value
		$params = array();
		$params['id'] = $linkShare['id'];
		$params['_put'] = array();
		$params['_put']['expireDate'] = $dateWithinRange->format('Y-m-d');

1063
		$result = \OCA\Files_Sharing\API\Local::updateShare($params);
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080

		$this->assertTrue($result->succeeded());

		$items = \OCP\Share::getItemShared('file', $linkShare['file_source']);

		$updatedLinkShare = reset($items);

		// date should be changed
		$this->assertTrue(is_array($updatedLinkShare));
		$this->assertEquals($dateWithinRange->format('Y-m-d') . ' 00:00:00', $updatedLinkShare['expiration']);

		// update expire date to a value out of range
		$params = array();
		$params['id'] = $linkShare['id'];
		$params['_put'] = array();
		$params['_put']['expireDate'] = $dateOutOfRange->format('Y-m-d');

1081
		$result = \OCA\Files_Sharing\API\Local::updateShare($params);
1082
1083

		$this->assertFalse($result->succeeded());
Roeland Jago Douma's avatar
Roeland Jago Douma committed
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101

		$items = \OCP\Share::getItemShared('file', $linkShare['file_source']);

		$updatedLinkShare = reset($items);

		// date shouldn't be changed
		$this->assertTrue(is_array($updatedLinkShare));
		$this->assertEquals($dateWithinRange->format('Y-m-d') . ' 00:00:00', $updatedLinkShare['expiration']);


		// Try to remove expire date
		$params = array();
		$params['id'] = $linkShare['id'];
		$params['_put'] = ['expireDate' => ''];

		$result = \OCA\Files_Sharing\API\Local::updateShare($params);

		$this->assertFalse($result->succeeded());
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111

		$items = \OCP\Share::getItemShared('file', $linkShare['file_source']);

		$updatedLinkShare = reset($items);

		// date shouldn't be changed
		$this->assertTrue(is_array($updatedLinkShare));
		$this->assertEquals($dateWithinRange->format('Y-m-d') . ' 00:00:00', $updatedLinkShare['expiration']);

		// cleanup
1112
1113
		$config->setAppValue('core', 'shareapi_default_expire_date', 'no');
		$config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
1114
1115
1116
1117
		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);

	}

1118
1119
1120
1121
1122
	/**
	 * @medium
	 * @depends testCreateShare
	 */
	function testDeleteShare() {
1123
1124
1125
1126
1127
1128
1129
1130
1131

		$fileInfo = $this->view->getFileInfo($this->filename);

		\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
				null, 1);

1132
1133
		$items = \OCP\Share::getItemShared('file', null);

1134
1135
		$this->assertEquals(2, count($items));

1136
		foreach ($items as $item) {
1137
			$result = \OCA\Files_Sharing\API\Local::deleteShare(array('id' => $item['id']));
1138
1139
1140
1141
1142
1143
1144

			$this->assertTrue($result->succeeded());
		}

		$itemsAfterDelete = \OCP\Share::getItemShared('file', null);

		$this->assertTrue(empty($itemsAfterDelete));
1145

1146
	}
1147
1148

	/**
1149
	 * test unshare of a reshared file
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
	 */
	function testDeleteReshare() {

		// user 1 shares a folder with user2
		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);

		$fileInfo1 = $this->view->getFileInfo($this->folder);
		$fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);

		$result1 = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		$this->assertTrue($result1);

		// user2 shares a file from the folder as link
		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

		$result2 = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);

		$this->assertTrue(is_string($result2));

		// test if we can unshare the link again
		$items = \OCP\Share::getItemShared('file', null);
		$this->assertEquals(1, count($items));

		$item = reset($items);
1176
		$result3 = \OCA\Files_Sharing\API\Local::deleteShare(array('id' => $item['id']));
1177
1178
1179

		$this->assertTrue($result3->succeeded());

1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
		// cleanup
		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);

		$result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

		$this->assertTrue($result);



	}

	/**
1193
	 * share a folder which contains a share mount point, should be forbidden
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
	 */
	public function testShareFolderWithAMountPoint() {
		// user 1 shares a folder with user2
		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);

		$fileInfo = $this->view->getFileInfo($this->folder);

		$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		$this->assertTrue($result);

		// user2 shares a file from the folder as link
		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

		$view = new \OC\Files\View('/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2 . '/files');
		$view->mkdir("localDir");

		// move mount point to the folder "localDir"
		$result = $view->rename($this->folder, 'localDir/'.$this->folder);
		$this->assertTrue($result !== false);

		// try to share "localDir"
		$fileInfo2 = $view->getFileInfo('localDir');

		$this->assertTrue($fileInfo2 instanceof \OC\Files\FileInfo);

		try {
			$result2 = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
					\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
		} catch (\Exception $e) {
			$result2 = false;
		}

		$this->assertFalse($result2);

		//cleanup

		$result = $view->rename('localDir/' . $this->folder, $this->folder);
		$this->assertTrue($result !== false);
		$view->unlink('localDir');

		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);

		\OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
1240
	}
1241

1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
	/**
	 * Post init mount points hook for mounting simulated ext storage
	 */
	public static function initTestMountPointsHook($data) {
		if ($data['user'] === \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1) {
			\OC\Files\Filesystem::mount(self::$tempStorage, array(), '/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME);
		}
	}

	/**
	 * Tests mounting a folder that is an external storage mount point.
	 */
	public function testShareStorageMountPoint() {
		self::$tempStorage = new \OC\Files\Storage\Temporary(array());
		self::$tempStorage->file_put_contents('test.txt', 'abcdef');
		self::$tempStorage->getScanner()->scan('');

		// needed because the sharing code sometimes switches the user internally and mounts the user's
		// storages. In our case the temp storage isn't mounted automatically, so doing it in the post hook
		// (similar to how ext storage works)
		OCP\Util::connectHook('OC_Filesystem', 'post_initMountPoints', '\Test_Files_Sharing_Api', 'initTestMountPointsHook');

		// logging in will auto-mount the temp storage for user1 as well
		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);

		$fileInfo = $this->view->getFileInfo($this->folder);

		// user 1 shares the mount point folder with user2
		$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

		$this->assertTrue($result);

		// user2: check that mount point name appears correctly
		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

		$view = new \OC\Files\View('/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2 . '/files');

		$this->assertTrue($view->file_exists($this->folder));
		$this->assertTrue($view->file_exists($this->folder . '/test.txt'));

		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);

		\OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
			\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);

		\OC_Hook::clear('OC_Filesystem', 'post_initMountPoints', '\Test_Files_Sharing_Api', 'initTestMountPointsHook');
	}
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
	/**
	 * @expectedException \Exception
	 */
	public function testShareNonExisting() {
		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);

		$id = PHP_INT_MAX - 1;
		\OCP\Share::shareItem('file', $id, \OCP\Share::SHARE_TYPE_LINK, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
	}

	/**
	 * @expectedException \Exception
	 */
	public function testShareNotOwner() {
		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
		\OC\Files\Filesystem::file_put_contents('foo.txt', 'bar');
		$info = \OC\Files\Filesystem::getFileInfo('foo.txt');

		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);

		\OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_LINK, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
	}
1312
1313
1314

	public function testDefaultExpireDate() {
		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
Morris Jobke's avatar
Morris Jobke committed
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327

		// TODO drop this once all code paths use the DI version - otherwise
		// the cache inside this config object is out of date because
		// OC_Appconfig is used and bypasses this cache which lead to integrity
		// constraint violations
		$config = \OC::$server->getConfig();
		$config->deleteAppValue('core', 'shareapi_default_expire_date');
		$config->deleteAppValue('core', 'shareapi_enforce_expire_date');
		$config->deleteAppValue('core', 'shareapi_expire_after_n_days');

		$config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
		$config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
		$config->setAppValue('core', 'shareapi_expire_after_n_days', '2');
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341

		// default expire date is set to 2 days
		// the time when the share was created is set to 3 days in the past
		// user defined expire date is set to +2 days from now on
		// -> link should be already expired by the default expire date but the user
		//    share should still exists.
		$now = time();
		$dateFormat = 'Y-m-d H:i:s';
		$shareCreated = $now - 3 * 24 * 60 * 60;
		$expireDate = date($dateFormat, $now + 2 * 24 * 60 * 60);

		$info = OC\Files\Filesystem::getFileInfo($this->filename);
		$this->assertTrue($info instanceof \OC\Files\FileInfo);

1342
		$result = \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
1343
1344
1345
1346
1347
		$this->assertTrue(is_string($result));

		$result = \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
		$this->assertTrue($result);

1348
		$result = \OCP\Share::setExpirationDate('file', $info->getId() , $expireDate, $now);
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
		$this->assertTrue($result);

		//manipulate stime so that both shares are older then the default expire date
		$statement = "UPDATE `*PREFIX*share` SET `stime` = ? WHERE `share_type` = ?";
		$query = \OCP\DB::prepare($statement);
		$result = $query->execute(array($shareCreated, \OCP\Share::SHARE_TYPE_LINK));
		$this->assertSame(1, $result);
		$query = \OCP\DB::prepare($statement);
		$result = $query->execute(array($shareCreated, \OCP\Share::SHARE_TYPE_USER));
		$this->assertSame(1, $result);

		// now the link share should expire because of enforced default expire date
		// the user share should still exist
		$result = \OCP\Share::getItemShared('file', $info->getId());
		$this->assertTrue(is_array($result));
		$this->assertSame(1, count($result));
		$share = reset($result);
		$this->assertSame(\OCP\Share::SHARE_TYPE_USER, $share['share_type']);

		//cleanup
		$result = \OCP\Share::unshare('file', $info->getId(), \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
		$this->assertTrue($result);
Morris Jobke's avatar
Morris Jobke committed
1371
1372
		$config->setAppValue('core', 'shareapi_default_expire_date', 'no');
		$config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
1373
1374

	}
1375
}