ManagerTest.php 77.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
/**
 * @author Roeland Jago Douma <rullzer@owncloud.com>
 *
 * @copyright Copyright (c) 2015, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * 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.
 *
 * 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, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */
namespace Test\Share20;

23
use OCP\Files\IRootFolder;
24
use OCP\IUserManager;
25
use OCP\Share\Exceptions\ShareNotFound;
26
27
use OCP\Share\IProviderFactory;
use OCP\Share\IShare;
28
29
30
use OC\Share20\Manager;
use OC\Share20\Exception;

31
use OC\Share20\Share;
32
use OCP\IL10N;
33
use OCP\ILogger;
34
use OCP\IConfig;
35
use OCP\Share\IShareProvider;
36
37
38
39
use OCP\Security\ISecureRandom;
use OCP\Security\IHasher;
use OCP\Files\Mount\IMountManager;
use OCP\IGroupManager;
40

41
42
43
44
45
46
/**
 * Class ManagerTest
 *
 * @package Test\Share20
 * @group DB
 */
47
48
49
50
51
52
class ManagerTest extends \Test\TestCase {

	/** @var Manager */
	protected $manager;
	/** @var ILogger */
	protected $logger;
53
54
55
56
57
58
	/** @var IConfig */
	protected $config;
	/** @var ISecureRandom */
	protected $secureRandom;
	/** @var IHasher */
	protected $hasher;
59
	/** @var IShareProvider | \PHPUnit_Framework_MockObject_MockObject */
60
	protected $defaultProvider;
61
62
63
64
	/** @var  IMountManager */
	protected $mountManager;
	/** @var  IGroupManager */
	protected $groupManager;
65
66
	/** @var IL10N */
	protected $l;
67
	/** @var DummyFactory */
68
	protected $factory;
69
70
	/** @var IUserManager */
	protected $userManager;
71
72
	/** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
	protected $rootFolder;
73

74
75
76
	public function setUp() {
		
		$this->logger = $this->getMock('\OCP\ILogger');
77
78
79
80
81
		$this->config = $this->getMock('\OCP\IConfig');
		$this->secureRandom = $this->getMock('\OCP\Security\ISecureRandom');
		$this->hasher = $this->getMock('\OCP\Security\IHasher');
		$this->mountManager = $this->getMock('\OCP\Files\Mount\IMountManager');
		$this->groupManager = $this->getMock('\OCP\IGroupManager');
82
		$this->userManager = $this->getMock('\OCP\IUserManager');
83
		$this->rootFolder = $this->getMock('\OCP\Files\IRootFolder');
84

85
86
87
88
89
90
		$this->l = $this->getMock('\OCP\IL10N');
		$this->l->method('t')
			->will($this->returnCallback(function($text, $parameters = []) {
 				return vsprintf($text, $parameters);
 			}));

91
		$this->factory = new DummyFactory(\OC::$server);
92

93
94
		$this->manager = new Manager(
			$this->logger,
95
96
97
98
			$this->config,
			$this->secureRandom,
			$this->hasher,
			$this->mountManager,
99
			$this->groupManager,
100
			$this->l,
101
			$this->factory,
102
103
			$this->userManager,
			$this->rootFolder
104
		);
105

106
107
108
		$this->defaultProvider = $this->getMockBuilder('\OC\Share20\DefaultShareProvider')
			->disableOriginalConstructor()
			->getMock();
109
		$this->defaultProvider->method('identifier')->willReturn('default');
110
111
112
		$this->factory->setProvider($this->defaultProvider);


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	}

	/**
	 * @return \PHPUnit_Framework_MockObject_MockBuilder
	 */
	private function createManagerMock() {
		return 	$this->getMockBuilder('\OC\Share20\Manager')
			->setConstructorArgs([
				$this->logger,
				$this->config,
				$this->secureRandom,
				$this->hasher,
				$this->mountManager,
				$this->groupManager,
				$this->l,
128
				$this->factory,
129
130
				$this->userManager,
				$this->rootFolder
131
132
133
			]);
	}

134
	/**
Roeland Jago Douma's avatar
Roeland Jago Douma committed
135
	 * @expectedException \InvalidArgumentException
136
137
	 */
	public function testDeleteNoShareId() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
138
		$share = $this->manager->newShare();
139
140
141
142

		$this->manager->deleteShare($share);
	}

143
144
145
146
147
148
149
150
	public function dataTestDelete() {
		$user = $this->getMock('\OCP\IUser');
		$user->method('getUID')->willReturn('sharedWithUser');

		$group = $this->getMock('\OCP\IGroup');
		$group->method('getGID')->willReturn('sharedWithGroup');
	
		return [
151
152
153
154
			[\OCP\Share::SHARE_TYPE_USER, 'sharedWithUser'],
			[\OCP\Share::SHARE_TYPE_GROUP, 'sharedWithGroup'],
			[\OCP\Share::SHARE_TYPE_LINK, ''],
			[\OCP\Share::SHARE_TYPE_REMOTE, 'foo@bar.com'],
155
156
157
158
159
160
		];
	}

	/**
	 * @dataProvider dataTestDelete
	 */
161
	public function testDelete($shareType, $sharedWith) {
162
		$manager = $this->createManagerMock()
163
164
165
166
167
168
			->setMethods(['getShareById', 'deleteChildren'])
			->getMock();

		$path = $this->getMock('\OCP\Files\File');
		$path->method('getId')->willReturn(1);

169
170
171
172
173
		$share = $this->manager->newShare();
		$share->setId(42)
			->setProviderId('prov')
			->setShareType($shareType)
			->setSharedWith($sharedWith)
174
			->setSharedBy('sharedBy')
175
176
			->setNode($path)
			->setTarget('myTarget');
177
178

		$manager->expects($this->once())->method('deleteChildren')->with($share);
179

180
		$this->defaultProvider
181
182
183
184
			->expects($this->once())
			->method('delete')
			->with($share);

185
186
187
188
189
190
191
192
193
		$hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock();
		\OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
		\OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');

		$hookListnerExpectsPre = [
			'id' => 42,
			'itemType' => 'file',
			'itemSource' => 1,
			'shareType' => $shareType,
194
			'shareWith' => $sharedWith,
195
196
197
198
199
200
201
202
203
204
205
			'itemparent' => null,
			'uidOwner' => 'sharedBy',
			'fileSource' => 1,
			'fileTarget' => 'myTarget',
		];

		$hookListnerExpectsPost = [
			'id' => 42,
			'itemType' => 'file',
			'itemSource' => 1,
			'shareType' => $shareType,
206
			'shareWith' => $sharedWith,
207
208
209
210
211
212
213
214
215
216
			'itemparent' => null,
			'uidOwner' => 'sharedBy',
			'fileSource' => 1,
			'fileTarget' => 'myTarget',
			'deletedShares' => [
				[
					'id' => 42,
					'itemType' => 'file',
					'itemSource' => 1,
					'shareType' => $shareType,
217
					'shareWith' => $sharedWith,
218
219
220
221
222
223
224
225
226
					'itemparent' => null,
					'uidOwner' => 'sharedBy',
					'fileSource' => 1,
					'fileTarget' => 'myTarget',
				],
			],
		];


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
		$hookListner
			->expects($this->exactly(1))
			->method('pre')
			->with($hookListnerExpectsPre);
		$hookListner
			->expects($this->exactly(1))
			->method('post')
			->with($hookListnerExpectsPost);

		$manager->deleteShare($share);
	}

	public function testDeleteLazyShare() {
		$manager = $this->createManagerMock()
			->setMethods(['getShareById', 'deleteChildren'])
			->getMock();

		$share = $this->manager->newShare();
		$share->setId(42)
			->setProviderId('prov')
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setSharedWith('sharedWith')
			->setSharedBy('sharedBy')
			->setShareOwner('shareOwner')
			->setTarget('myTarget')
			->setNodeId(1)
			->setNodeType('file');

		$this->rootFolder->expects($this->never())->method($this->anything());

		$manager->expects($this->once())->method('deleteChildren')->with($share);

		$this->defaultProvider
			->expects($this->once())
			->method('delete')
			->with($share);

		$hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock();
		\OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
		\OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');

		$hookListnerExpectsPre = [
			'id' => 42,
			'itemType' => 'file',
			'itemSource' => 1,
			'shareType' => \OCP\Share::SHARE_TYPE_USER,
			'shareWith' => 'sharedWith',
			'itemparent' => null,
			'uidOwner' => 'sharedBy',
			'fileSource' => 1,
			'fileTarget' => 'myTarget',
		];

		$hookListnerExpectsPost = [
			'id' => 42,
			'itemType' => 'file',
			'itemSource' => 1,
			'shareType' => \OCP\Share::SHARE_TYPE_USER,
			'shareWith' => 'sharedWith',
			'itemparent' => null,
			'uidOwner' => 'sharedBy',
			'fileSource' => 1,
			'fileTarget' => 'myTarget',
			'deletedShares' => [
				[
					'id' => 42,
					'itemType' => 'file',
					'itemSource' => 1,
					'shareType' => \OCP\Share::SHARE_TYPE_USER,
					'shareWith' => 'sharedWith',
					'itemparent' => null,
					'uidOwner' => 'sharedBy',
					'fileSource' => 1,
					'fileTarget' => 'myTarget',
				],
			],
		];


306
307
308
309
310
311
312
313
314
315
316
317
318
		$hookListner
			->expects($this->exactly(1))
			->method('pre')
			->with($hookListnerExpectsPre);
		$hookListner
			->expects($this->exactly(1))
			->method('post')
			->with($hookListnerExpectsPost);

		$manager->deleteShare($share);
	}

	public function testDeleteNested() {
319
		$manager = $this->createManagerMock()
320
321
322
323
324
325
			->setMethods(['getShareById'])
			->getMock();

		$path = $this->getMock('\OCP\Files\File');
		$path->method('getId')->willReturn(1);

326
327
328
329
		$share1 = $this->manager->newShare();
		$share1->setId(42)
			->setProviderId('prov')
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
330
331
			->setSharedWith('sharedWith1')
			->setSharedBy('sharedBy1')
332
333
334
335
336
337
338
			->setNode($path)
			->setTarget('myTarget1');

		$share2 = $this->manager->newShare();
		$share2->setId(43)
			->setProviderId('prov')
			->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
339
340
			->setSharedWith('sharedWith2')
			->setSharedBy('sharedBy2')
341
342
343
344
345
346
347
348
			->setNode($path)
			->setTarget('myTarget2')
			->setParent(42);

		$share3 = $this->manager->newShare();
		$share3->setId(44)
			->setProviderId('prov')
			->setShareType(\OCP\Share::SHARE_TYPE_LINK)
349
			->setSharedBy('sharedBy3')
350
351
352
			->setNode($path)
			->setTarget('myTarget3')
			->setParent(43);
353

354
		$this->defaultProvider
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
			->method('getChildren')
			->will($this->returnValueMap([
				[$share1, [$share2]],
				[$share2, [$share3]],
				[$share3, []],
			]));

		$this->defaultProvider
			->method('delete')
			->withConsecutive($share3, $share2, $share1);

		$hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock();
		\OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
		\OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');

		$hookListnerExpectsPre = [
			'id' => 42,
			'itemType' => 'file',
			'itemSource' => 1,
			'shareType' => \OCP\Share::SHARE_TYPE_USER,
			'shareWith' => 'sharedWith1',
			'itemparent' => null,
			'uidOwner' => 'sharedBy1',
			'fileSource' => 1,
			'fileTarget' => 'myTarget1',
		];

		$hookListnerExpectsPost = [
			'id' => 42,
			'itemType' => 'file',
			'itemSource' => 1,
			'shareType' => \OCP\Share::SHARE_TYPE_USER,
			'shareWith' => 'sharedWith1',
			'itemparent' => null,
			'uidOwner' => 'sharedBy1',
			'fileSource' => 1,
			'fileTarget' => 'myTarget1',
			'deletedShares' => [
				[
					'id' => 44,
					'itemType' => 'file',
					'itemSource' => 1,
					'shareType' => \OCP\Share::SHARE_TYPE_LINK,
					'shareWith' => '',
					'itemparent' => 43,
					'uidOwner' => 'sharedBy3',
					'fileSource' => 1,
					'fileTarget' => 'myTarget3',
				],
				[
					'id' => 43,
					'itemType' => 'file',
					'itemSource' => 1,
					'shareType' => \OCP\Share::SHARE_TYPE_GROUP,
					'shareWith' => 'sharedWith2',
					'itemparent' => 42,
					'uidOwner' => 'sharedBy2',
					'fileSource' => 1,
					'fileTarget' => 'myTarget2',
				],
				[
					'id' => 42,
					'itemType' => 'file',
					'itemSource' => 1,
					'shareType' => \OCP\Share::SHARE_TYPE_USER,
					'shareWith' => 'sharedWith1',
					'itemparent' => null,
					'uidOwner' => 'sharedBy1',
					'fileSource' => 1,
					'fileTarget' => 'myTarget1',
				],
			],
		];

		$hookListner
			->expects($this->exactly(1))
			->method('pre')
			->with($hookListnerExpectsPre);
		$hookListner
			->expects($this->exactly(1))
			->method('post')
			->with($hookListnerExpectsPost);

		$manager->deleteShare($share1);
	}

	public function testDeleteChildren() {
442
		$manager = $this->createManagerMock()
443
444
445
			->setMethods(['deleteShare'])
			->getMock();

446
		$share = $this->getMock('\OCP\Share\IShare');
447
		$share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
448

449
		$child1 = $this->getMock('\OCP\Share\IShare');
450
		$child1->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
451
		$child2 = $this->getMock('\OCP\Share\IShare');
452
		$child2->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
453
		$child3 = $this->getMock('\OCP\Share\IShare');
454
		$child3->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
455
456
457
458
459
460
461

		$shares = [
			$child1,
			$child2,
			$child3,
		];

462
		$this->defaultProvider
463
464
465
466
467
468
469
470
471
			->expects($this->exactly(4))
			->method('getChildren')
			->will($this->returnCallback(function($_share) use ($share, $shares) {
				if ($_share === $share) {
					return $shares;
				}
				return [];
			}));

472
		$this->defaultProvider
473
474
475
476
477
478
			->expects($this->exactly(3))
			->method('delete')
			->withConsecutive($child1, $child2, $child3);

		$result = $this->invokePrivate($manager, 'deleteChildren', [$share]);
		$this->assertSame($shares, $result);
479
480
	}

481
	public function testGetShareById() {
482
		$share = $this->getMock('\OCP\Share\IShare');
483
484
485
486
487
488
489

		$this->defaultProvider
			->expects($this->once())
			->method('getShareById')
			->with(42)
			->willReturn($share);

490
		$this->assertEquals($share, $this->manager->getShareById('default:42'));
491
	}
492

493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
	/**
	 * @expectedException \OCP\Share\Exceptions\ShareNotFound
	 */
	public function testGetExpiredShareById() {
		$manager = $this->createManagerMock()
			->setMethods(['deleteShare'])
			->getMock();

		$date = new \DateTime();
		$date->setTime(0,0,0);

		$share = $this->manager->newShare();
		$share->setExpirationDate($date)
			->setShareType(\OCP\Share::SHARE_TYPE_LINK);

		$this->defaultProvider->expects($this->once())
			->method('getShareById')
			->with('42')
			->willReturn($share);

		$manager->expects($this->once())
			->method('deleteShare')
			->with($share);

		$manager->getShareById('default:42');
	}

520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
	/**
	 * @expectedException        InvalidArgumentException
	 * @expectedExceptionMessage Passwords are enforced for link shares
	 */
	public function testVerifyPasswordNullButEnforced() {
		$this->config->method('getAppValue')->will($this->returnValueMap([
			['core', 'shareapi_enforce_links_password', 'no', 'yes'],
		]));

		$this->invokePrivate($this->manager, 'verifyPassword', [null]);
	}

	public function testVerifyPasswordNull() {
		$this->config->method('getAppValue')->will($this->returnValueMap([
				['core', 'shareapi_enforce_links_password', 'no', 'no'],
		]));

		$result = $this->invokePrivate($this->manager, 'verifyPassword', [null]);
		$this->assertNull($result);
	}

	public function testVerifyPasswordHook() {
		$this->config->method('getAppValue')->will($this->returnValueMap([
				['core', 'shareapi_enforce_links_password', 'no', 'no'],
		]));

		$hookListner = $this->getMockBuilder('Dummy')->setMethods(['listner'])->getMock();
		\OCP\Util::connectHook('\OC\Share', 'verifyPassword', $hookListner, 'listner');

		$hookListner->expects($this->once())
			->method('listner')
			->with([
				'password' => 'password',
				'accepted' => true,
				'message' => ''
			]);

		$result = $this->invokePrivate($this->manager, 'verifyPassword', ['password']);
		$this->assertNull($result);
	}

	/**
	 * @expectedException        Exception
	 * @expectedExceptionMessage password not accepted
	 */
	public function testVerifyPasswordHookFails() {
		$this->config->method('getAppValue')->will($this->returnValueMap([
				['core', 'shareapi_enforce_links_password', 'no', 'no'],
		]));

		$dummy = new DummyPassword();
		\OCP\Util::connectHook('\OC\Share', 'verifyPassword', $dummy, 'listner');
		$this->invokePrivate($this->manager, 'verifyPassword', ['password']);
	}

	public function createShare($id, $type, $path, $sharedWith, $sharedBy, $shareOwner,
		$permissions, $expireDate = null, $password = null) {
577
		$share = $this->getMock('\OCP\Share\IShare');
578
579
580
581

		$share->method('getShareType')->willReturn($type);
		$share->method('getSharedWith')->willReturn($sharedWith);
		$share->method('getSharedBy')->willReturn($sharedBy);
Roeland Jago Douma's avatar
Roeland Jago Douma committed
582
		$share->method('getShareOwner')->willReturn($shareOwner);
583
		$share->method('getNode')->willReturn($path);
584
585
586
587
588
589
590
591
		$share->method('getPermissions')->willReturn($permissions);
		$share->method('getExpirationDate')->willReturn($expireDate);
		$share->method('getPassword')->willReturn($password);

		return $share;
	}

	public function dataGeneralChecks() {
592
593
594
		$user0 = 'user0';
		$user2 = 'user1';
		$group0 = 'group0';
595
596
597
598
599

		$file = $this->getMock('\OCP\Files\File');
		$node = $this->getMock('\OCP\Files\Node');

		$data = [
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $file, $group0, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK,  $file, $user2, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK,  $file, $group0, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK,  $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
			[$this->createShare(null, -1, $file, null, $user0, $user0, 31, null, null), 'unkown share type', true],

			[$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $file, $user2, null, $user0, 31, null, null), 'SharedBy should be set', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, $group0, null, $user0, 31, null, null), 'SharedBy should be set', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK,  $file, null, null, $user0, 31, null, null), 'SharedBy should be set', true],

			[$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $file, $user0, $user0, $user0, 31, null, null), 'Can\'t share with yourself', true],

			[$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  null, $user2, $user0, $user0, 31, null, null), 'Path should be set', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, null, $group0, $user0, $user0, 31, null, null), 'Path should be set', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK,  null, null, $user0, $user0, 31, null, null), 'Path should be set', true],

			[$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $node, $user2, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $node, $group0, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
			[$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK,  $node, null, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
624
625
626
627
		];

		$nonShareAble = $this->getMock('\OCP\Files\Folder');
		$nonShareAble->method('isShareable')->willReturn(false);
628
		$nonShareAble->method('getPath')->willReturn('path');
629

630
631
632
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $nonShareAble, $user2, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $nonShareAble, $group0, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK,  $nonShareAble, null, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
633
634
635
636

		$limitedPermssions = $this->getMock('\OCP\Files\File');
		$limitedPermssions->method('isShareable')->willReturn(true);
		$limitedPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
637
		$limitedPermssions->method('getPath')->willReturn('path');
638

639
640
641
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $limitedPermssions, $user2, $user0, $user0, null, null, null), 'A share requires permissions', true];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, null, null, null), 'A share requires permissions', true];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK,  $limitedPermssions, null, $user0, $user0, null, null, null), 'A share requires permissions', true];
642

643
644
645
		$mount = $this->getMock('OC\Files\Mount\MoveableMount');
		$limitedPermssions->method('getMountPoint')->willReturn($mount);

646
647
648
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $limitedPermssions, $user2, $user0, $user0, 31, null, null), 'Cannot increase permissions of path', true];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, 17, null, null), 'Cannot increase permissions of path', true];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK,  $limitedPermssions, null, $user0, $user0, 3, null, null), 'Cannot increase permissions of path', true];
649

Roeland Jago Douma's avatar
Roeland Jago Douma committed
650
		$nonMoveableMountPermssions = $this->getMock('\OCP\Files\Folder');
651
652
653
654
655
656
657
		$nonMoveableMountPermssions->method('isShareable')->willReturn(true);
		$nonMoveableMountPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
		$nonMoveableMountPermssions->method('getPath')->willReturn('path');

		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $nonMoveableMountPermssions, $user2, $user0, $user0, 11, null, null), 'Cannot increase permissions of path', false];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $nonMoveableMountPermssions, $group0, $user0, $user0, 11, null, null), 'Cannot increase permissions of path', false];

Roeland Jago Douma's avatar
Roeland Jago Douma committed
658
659
660
661
662
663
664
665
666
		$rootFolder = $this->getMock('\OCP\Files\Folder');
		$rootFolder->method('isShareable')->willReturn(true);
		$rootFolder->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
		$rootFolder->method('getPath')->willReturn('myrootfolder');

		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $rootFolder, $user2, $user0, $user0, 30, null, null), 'You can\'t share your root folder', true];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $rootFolder, $group0, $user0, $user0, 2, null, null), 'You can\'t share your root folder', true];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK,  $rootFolder, null, $user0, $user0, 16, null, null), 'You can\'t share your root folder', true];

667
668
669
670
		$allPermssions = $this->getMock('\OCP\Files\Folder');
		$allPermssions->method('isShareable')->willReturn(true);
		$allPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);

671
672
673
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $allPermssions, $user2, $user0, $user0, 30, null, null), 'Shares need at least read permissions', true];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 2, null, null), 'Shares need at least read permissions', true];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK,  $allPermssions, null, $user0, $user0, 16, null, null), 'Shares need at least read permissions', true];
674

675
676
677
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER,  $allPermssions, $user2, $user0, $user0, 31, null, null), null, false];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 3, null, null), null, false];
		$data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK,  $allPermssions, null, $user0, $user0, 17, null, null), null, false];
678
679
680
681
682
683
684
685
686
687
688
689
690

		return $data;
	}

	/**
	 * @dataProvider dataGeneralChecks
	 *
	 * @param $share
	 * @param $exceptionMessage
	 */
	public function testGeneralChecks($share, $exceptionMessage, $exception) {
		$thrown = null;

691
692
693
694
695
696
697
698
699
		$this->userManager->method('userExists')->will($this->returnValueMap([
			['user0', true],
			['user1', true],
		]));

		$this->groupManager->method('groupExists')->will($this->returnValueMap([
			['group0', true],
		]));

700
		$userFolder = $this->getMock('\OCP\Files\Folder');
Roeland Jago Douma's avatar
Roeland Jago Douma committed
701
		$userFolder->method('getPath')->willReturn('myrootfolder');
702
703
		$this->rootFolder->method('getUserFolder')->willReturn($userFolder);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
704

705
706
707
		try {
			$this->invokePrivate($this->manager, 'generalCreateChecks', [$share]);
			$thrown = false;
708
		} catch (\OCP\Share\Exceptions\GenericShareException $e) {
709
710
			$this->assertEquals($exceptionMessage, $e->getHint());
			$thrown = true;
711
712
713
714
715
716
717
718
		} catch(\InvalidArgumentException $e) {
			$this->assertEquals($exceptionMessage, $e->getMessage());
			$thrown = true;
		}

		$this->assertSame($exception, $thrown);
	}

719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
	/**
	 * @expectedException \InvalidArgumentException
	 * @expectedExceptionMessage You can't share your root folder
	 */
	public function testGeneralCheckShareRoot() {
		$thrown = null;

		$this->userManager->method('userExists')->will($this->returnValueMap([
			['user0', true],
			['user1', true],
		]));

		$userFolder = $this->getMock('\OCP\Files\Folder');
		$userFolder->method('isSubNode')->with($userFolder)->willReturn(false);
		$this->rootFolder->method('getUserFolder')->willReturn($userFolder);

		$share = $this->manager->newShare();

		$share->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setSharedWith('user0')
			->setSharedBy('user1')
			->setNode($userFolder);

		$this->invokePrivate($this->manager, 'generalCreateChecks', [$share]);
	}

745
	/**
746
	 * @expectedException \OCP\Share\Exceptions\GenericShareException
747
748
	 * @expectedExceptionMessage Expiration date is in the past
	 */
Roeland Jago Douma's avatar
Roeland Jago Douma committed
749
	public function testvalidateExpirationDateInPast() {
750
751
752
753
754

		// Expire date in the past
		$past = new \DateTime();
		$past->sub(new \DateInterval('P1D'));

Roeland Jago Douma's avatar
Roeland Jago Douma committed
755
756
757
758
		$share = $this->manager->newShare();
		$share->setExpirationDate($past);

		$this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
759
760
761
762
763
764
	}

	/**
	 * @expectedException InvalidArgumentException
	 * @expectedExceptionMessage Expiration date is enforced
	 */
Roeland Jago Douma's avatar
Roeland Jago Douma committed
765
	public function testvalidateExpirationDateEnforceButNotSet() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
766
		$share = $this->manager->newShare();
767
		$share->setProviderId('foo')->setId('bar');
Roeland Jago Douma's avatar
Roeland Jago Douma committed
768

769
770
		$this->config->method('getAppValue')
			->will($this->returnValueMap([
771
				['core', 'shareapi_default_expire_date', 'no', 'yes'],
772
773
774
				['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
			]));

Roeland Jago Douma's avatar
Roeland Jago Douma committed
775
		$this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
776
777
	}

778
779
780
781
782
783
784
785
786
787
788
789
790
791
	public function testvalidateExpirationDateEnforceButNotEnabledAndNotSet() {
		$share = $this->manager->newShare();
		$share->setProviderId('foo')->setId('bar');

		$this->config->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
			]));

		$this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);

		$this->assertNull($share->getExpirationDate());
	}

792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
	public function testvalidateExpirationDateEnforceButNotSetNewShare() {
		$share = $this->manager->newShare();

		$this->config->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
				['core', 'shareapi_expire_after_n_days', '7', '3'],
				['core', 'shareapi_default_expire_date', 'no', 'yes'],
			]));

		$expected = new \DateTime();
		$expected->setTime(0,0,0);
		$expected->add(new \DateInterval('P3D'));

		$this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);

		$this->assertNotNull($share->getExpirationDate());
		$this->assertEquals($expected, $share->getExpirationDate());
	}

Roeland Jago Douma's avatar
Roeland Jago Douma committed
812
	public function testvalidateExpirationDateEnforceToFarIntoFuture() {
813
814
815
816
		// Expire date in the past
		$future = new \DateTime();
		$future->add(new \DateInterval('P7D'));

Roeland Jago Douma's avatar
Roeland Jago Douma committed
817
818
819
		$share = $this->manager->newShare();
		$share->setExpirationDate($future);

820
821
822
823
824
825
		$this->config->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
				['core', 'shareapi_expire_after_n_days', '7', '3'],
			]));

826
		try {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
827
			$this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
828
		} catch (\OCP\Share\Exceptions\GenericShareException $e) {
829
830
831
832
			$this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getMessage());
			$this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getHint());
			$this->assertEquals(404, $e->getCode());
		}
833
834
	}

Roeland Jago Douma's avatar
Roeland Jago Douma committed
835
	public function testvalidateExpirationDateEnforceValid() {
836
837
838
839
		// Expire date in the past
		$future = new \DateTime();
		$future->add(new \DateInterval('P2D'));
		$future->setTime(0,0,0);
Roeland Jago Douma's avatar
Roeland Jago Douma committed
840
841

		$expected = clone $future;
842
843
		$future->setTime(1,2,3);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
844
845
846
		$share = $this->manager->newShare();
		$share->setExpirationDate($future);

847
848
849
850
851
852
		$this->config->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
				['core', 'shareapi_expire_after_n_days', '7', '3'],
			]));

Roeland Jago Douma's avatar
Roeland Jago Douma committed
853
854
855
856
857
858
		$hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
		\OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate',  $hookListner, 'listener');
		$hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($future) {
			return $data['expirationDate'] == $future;
		}));

859
		$this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
860

861
		$this->assertEquals($expected, $share->getExpirationDate());
862
863
	}

Roeland Jago Douma's avatar
Roeland Jago Douma committed
864
	public function testvalidateExpirationDateNoDateNoDefaultNull() {
865
866
867
		$date = new \DateTime();
		$date->add(new \DateInterval('P5D'));

Roeland Jago Douma's avatar
Roeland Jago Douma committed
868
869
870
871
872
873
874
875
876
		$expected = clone $date;
		$expected->setTime(0,0,0);

		$share = $this->manager->newShare();
		$share->setExpirationDate($date);

		$hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
		\OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate',  $hookListner, 'listener');
		$hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
877
			return $data['expirationDate'] == $expected && $data['passwordSet'] === false;
Roeland Jago Douma's avatar
Roeland Jago Douma committed
878
		}));
879

880
		$this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
Roeland Jago Douma's avatar
Roeland Jago Douma committed
881

882
		$this->assertEquals($expected, $share->getExpirationDate());
883
884
	}

Roeland Jago Douma's avatar
Roeland Jago Douma committed
885
	public function testvalidateExpirationDateNoDateNoDefault() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
886
887
888
		$hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
		\OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate',  $hookListner, 'listener');
		$hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) {
889
			return $data['expirationDate'] === null && $data['passwordSet'] === true;
Roeland Jago Douma's avatar
Roeland Jago Douma committed
890
891
892
		}));

		$share = $this->manager->newShare();
893
		$share->setPassword('password');
Roeland Jago Douma's avatar
Roeland Jago Douma committed
894

895
		$this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
896

897
		$this->assertNull($share->getExpirationDate());
898
899
	}

Roeland Jago Douma's avatar
Roeland Jago Douma committed
900
	public function testvalidateExpirationDateNoDateDefault() {
901
902
903
904
		$future = new \DateTime();
		$future->add(new \DateInterval('P3D'));
		$future->setTime(0,0,0);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
905
906
907
908
909
		$expected = clone $future;

		$share = $this->manager->newShare();
		$share->setExpirationDate($future);

910
911
912
913
914
915
		$this->config->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_default_expire_date', 'no', 'yes'],
				['core', 'shareapi_expire_after_n_days', '7', '3'],
			]));

Roeland Jago Douma's avatar
Roeland Jago Douma committed
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
		$hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
		\OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate',  $hookListner, 'listener');
		$hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
			return $data['expirationDate'] == $expected;
		}));

		$this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);

		$this->assertEquals($expected, $share->getExpirationDate());
	}

	public function testValidateExpirationDateHookModification() {
		$nextWeek = new \DateTime();
		$nextWeek->add(new \DateInterval('P7D'));
		$nextWeek->setTime(0,0,0);
931

Roeland Jago Douma's avatar
Roeland Jago Douma committed
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
968
		$save = clone $nextWeek;

		$hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
		\OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate',  $hookListner, 'listener');
		$hookListner->expects($this->once())->method('listener')->will($this->returnCallback(function ($data) {
			$data['expirationDate']->sub(new \DateInterval('P2D'));
		}));

		$share = $this->manager->newShare();
		$share->setExpirationDate($nextWeek);

		$this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);

		$save->sub(new \DateInterval('P2D'));
		$this->assertEquals($save, $share->getExpirationDate());
	}

	/**
	 * @expectedException \Exception
	 * @expectedExceptionMessage Invalid date!
	 */
	public function testValidateExpirationDateHookException() {
		$nextWeek = new \DateTime();
		$nextWeek->add(new \DateInterval('P7D'));
		$nextWeek->setTime(0,0,0);

		$share = $this->manager->newShare();
		$share->setExpirationDate($nextWeek);

		$hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
		\OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate',  $hookListner, 'listener');
		$hookListner->expects($this->once())->method('listener')->will($this->returnCallback(function ($data) {
			$data['accepted'] = false;
			$data['message'] = 'Invalid date!';
		}));

		$this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
969
970
	}

971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
	public function testValidateExpirationDateExistingShareNoDefault() {
		$share = $this->manager->newShare();

		$share->setId('42')->setProviderId('foo');

		$this->config->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_default_expire_date', 'no', 'yes'],
				['core', 'shareapi_expire_after_n_days', '7', '6'],
			]));

		$this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);

		$this->assertEquals(null, $share->getExpirationDate());
	}

987
988
989
990
991
	/**
	 * @expectedException Exception
	 * @expectedExceptionMessage Only sharing with group members is allowed
	 */
	public function testUserCreateChecksShareWithGroupMembersOnlyDifferentGroups() {
992
		$share = $this->manager->newShare();
993
994
995

		$sharedBy = $this->getMock('\OCP\IUser');
		$sharedWith = $this->getMock('\OCP\IUser');
996
		$share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
997
998
999
1000
1001
1002
1003
1004
1005
1006

		$this->groupManager
			->method('getUserGroupIds')
			->will(
				$this->returnValueMap([
					[$sharedBy, ['group1']],
					[$sharedWith, ['group2']],
				])
			);

1007
1008
1009
1010
1011
		$this->userManager->method('get')->will($this->returnValueMap([
			['sharedBy', $sharedBy],
			['sharedWith', $sharedWith],
		]));

1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
		$this->config
			->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
			]));

		$this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
	}

	public function testUserCreateChecksShareWithGroupMembersOnlySharedGroup() {
1022
		$share = $this->manager->newShare();
1023
1024
1025

		$sharedBy = $this->getMock('\OCP\IUser');
		$sharedWith = $this->getMock('\OCP\IUser');
1026
		$share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
1027
1028

		$path = $this->getMock('\OCP\Files\Node');
1029
		$share->setNode($path);
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039

		$this->groupManager
			->method('getUserGroupIds')
			->will(
				$this->returnValueMap([
					[$sharedBy, ['group1', 'group3']],
					[$sharedWith, ['group2', 'group3']],
				])
			);

1040
1041
1042
1043
1044
		$this->userManager->method('get')->will($this->returnValueMap([
			['sharedBy', $sharedBy],
			['sharedWith', $sharedWith],
		]));

1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
		$this->config
			->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
			]));

		$this->defaultProvider
			->method('getSharesByPath')
			->with($path)
			->willReturn([]);

		$this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
	}

	/**
	 * @expectedException Exception
	 * @expectedExceptionMessage  Path already shared with this user
	 */
	public function testUserCreateChecksIdenticalShareExists() {
1064
1065
		$share  = $this->manager->newShare();
		$share2 = $this->manager->newShare();
1066
1067
1068

		$sharedWith = $this->getMock('\OCP\IUser');
		$path = $this->getMock('\OCP\Files\Node');
1069

1070
		$share->setSharedWith('sharedWith')->setNode($path)
1071
1072
			->setProviderId('foo')->setId('bar');

1073
		$share2->setSharedWith('sharedWith')->setNode($path)
1074
			->setProviderId('foo')->setId('baz');
1075
1076
1077
1078

		$this->defaultProvider
			->method('getSharesByPath')
			->with($path)
1079
			->willReturn([$share2]);
1080
1081
1082
1083
1084
1085
1086
1087

		$this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
	}

	/**
	 * @expectedException Exception
	 * @expectedExceptionMessage  Path already shared with this user
	 */
1088
 	public function testUserCreateChecksIdenticalPathSharedViaGroup() {
1089
		$share  = $this->manager->newShare();
1090

1091
		$sharedWith = $this->getMock('\OCP\IUser');
1092
1093
1094
1095
		$sharedWith->method('getUID')->willReturn('sharedWith');

		$this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);

1096
		$path = $this->getMock('\OCP\Files\Node');
1097

1098
		$share->setSharedWith('sharedWith')
1099
			->setNode($path)
1100
			->setShareOwner('shareOwner')
1101
1102
			->setProviderId('foo')
			->setId('bar');
1103

1104
		$share2 = $this->manager->newShare();
1105
		$share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
1106
			->setShareOwner('shareOwner2')
1107
			->setProviderId('foo')
1108
1109
			->setId('baz')
			->setSharedWith('group');
1110
1111
1112
1113
1114
1115

		$group = $this->getMock('\OCP\IGroup');
		$group->method('inGroup')
			->with($sharedWith)
			->willReturn(true);

1116
		$this->groupManager->method('get')->with('group')->willReturn($group);
1117
1118
1119
1120
1121
1122
1123
1124
1125

		$this->defaultProvider
			->method('getSharesByPath')
			->with($path)
			->willReturn([$share2]);

		$this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
	}

1126
	public function testUserCreateChecksIdenticalPathNotSharedWithUser() {
1127
		$share = $this->manager->newShare();
1128
1129
		$sharedWith = $this->getMock('\OCP\IUser');
		$path = $this->getMock('\OCP\Files\Node');
1130
		$share->setSharedWith('sharedWith')
1131
			->setNode($path)
1132
			->setShareOwner('shareOwner')
1133
1134
			->setProviderId('foo')
			->setId('bar');
1135

1136
1137
		$this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);

1138
		$share2 = $this->manager->newShare();
1139
		$share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
1140
			->setShareOwner('shareOwner2')
1141
1142
			->setProviderId('foo')
			->setId('baz');
1143
1144
1145
1146
1147
1148

		$group = $this->getMock('\OCP\IGroup');
		$group->method('inGroup')
			->with($sharedWith)
			->willReturn(false);

1149
1150
1151
		$this->groupManager->method('get')->with('group')->willReturn($group);

		$share2->setSharedWith('group');
1152
1153
1154
1155
1156
1157
1158
1159
1160

		$this->defaultProvider
			->method('getSharesByPath')
			->with($path)
			->willReturn([$share2]);

		$this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
	}

1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
	/**
	 * @expectedException Exception
	 * @expectedExceptionMessage Group sharing is now allowed
	 */
	public function testGroupCreateChecksShareWithGroupMembersGroupSharingNotAllowed() {
		$share = $this->manager->newShare();

		$this->config
			->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_allow_group_sharing', 'yes', 'no'],
			]));

		$this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
	}

1177
1178
1179
1180
1181
	/**
	 * @expectedException Exception
	 * @expectedExceptionMessage Only sharing within your own groups is allowed
	 */
	public function testGroupCreateChecksShareWithGroupMembersOnlyNotInGroup() {
1182
		$share = $this->manager->newShare();
1183

1184
1185
1186
		$user = $this->getMock('\OCP\IUser');
		$group = $this->getMock('\OCP\IGroup');
		$share->setSharedBy('user')->setSharedWith('group');
1187

1188
1189
1190
1191
		$group->method('inGroup')->with($user)->willReturn(false);

		$this->groupManager->method('get')->with('group')->willReturn($group);
		$this->userManager->method('get')->with('user')->willReturn($user);
1192
1193
1194
1195
1196

		$this->config
			->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
1197
				['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
1198
1199
1200
1201
1202
1203
			]));

		$this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
	}

	public function testGroupCreateChecksShareWithGroupMembersOnlyInGroup() {
1204
		$share = $this->manager->newShare();
1205

1206
1207
1208
		$user = $this->getMock('\OCP\IUser');
		$group = $this->getMock('\OCP\IGroup');
		$share->setSharedBy('user')->setSharedWith('group');
1209

1210
1211
1212
1213
		$this->userManager->method('get')->with('user')->willReturn($user);
		$this->groupManager->method('get')->with('group')->willReturn($group);

		$group->method('inGroup')->with($user)->willReturn(true);
1214
1215

		$path = $this->getMock('\OCP\Files\Node');
1216
		$share->setNode($path);
1217
1218
1219
1220
1221
1222
1223
1224
1225

		$this->defaultProvider->method('getSharesByPath')
			->with($path)
			->willReturn([]);

		$this->config
			->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
1226
				['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
			]));

		$this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
	}

	/**
	 * @expectedException Exception
	 * @expectedExceptionMessage Path already shared with this group
	 */
	public function testGroupCreateChecksPathAlreadySharedWithSameGroup() {
1237
		$share = $this->manager->newShare();
1238
1239

		$path = $this->getMock('\OCP\Files\Node');
1240
		$share->setSharedWith('sharedWith')
1241
			->setNode($path)
1242
1243
			->setProviderId('foo')
			->setId('bar');
1244

1245
		$share2 = $this->manager->newShare();
1246
		$share2->setSharedWith('sharedWith')
1247
1248
			->setProviderId('foo')
			->setId('baz');
1249
1250
1251
1252
1253

		$this->defaultProvider->method('getSharesByPath')
			->with($path)
			->willReturn([$share2]);

1254
1255
1256
1257
1258
1259
		$this->config
			->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
			]));

1260
1261
1262
1263
		$this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
	}

	public function testGroupCreateChecksPathAlreadySharedWithDifferentGroup() {
1264
		$share = $this->manager->newShare();
1265

1266
		$share->setSharedWith('sharedWith');
1267
1268

		$path = $this->getMock('\OCP\Files\Node');
1269
		$share->setNode($path);
1270

1271
		$share2 = $this->manager->newShare();
1272
		$share2->setSharedWith('sharedWith2');
1273
1274
1275
1276
1277

		$this->defaultProvider->method('getSharesByPath')
			->with($path)
			->willReturn([$share2]);

1278
1279
1280
1281
1282
1283
		$this->config
			->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
			]));

1284
1285
1286
1287
1288
1289
1290
1291
		$this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
	}

	/**
	 * @expectedException Exception
	 * @expectedExceptionMessage Link sharing not allowed
	 */
	public function testLinkCreateChecksNoLinkSharesAllowed() {
1292
		$share = $this->manager->newShare();
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307

		$this->config
			->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_allow_links', 'yes', 'no'],
			]));

		$this->invokePrivate($this->manager, 'linkCreateChecks', [$share]);
	}

	/**
	 * @expectedException Exception
	 * @expectedExceptionMessage Link shares can't have reshare permissions
	 */
	public function testLinkCreateChecksSharePermissions() {
1308
		$share = $this->manager->newShare();
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325

		$share->setPermissions(\OCP\Constants::PERMISSION_SHARE);

		$this->config
			->method('getAppValue')
			->will($this->returnValueMap([
				['core', 'shareapi_allow_links', 'yes', 'yes'],
			]));

		$this->invokePrivate($this->manager, 'linkCreateChecks', [$share]);
	}

	/**
	 * @expectedException Exception
	 * @expectedExceptionMessage Public upload not allowed
	 */
	public function testLinkCreateChecksNoPublicUpload() {
1326
		$share = $this->manager->newShare();