|
6 | 6 | */ |
7 | 7 | namespace OCA\Files_Sharing\Tests\Controller; |
8 | 8 |
|
| 9 | +use OC\Files\Storage\Wrapper\Wrapper; |
9 | 10 | use OCA\Federation\TrustedServers; |
10 | 11 | use OCA\Files_Sharing\Controller\ShareAPIController; |
| 12 | +use OCA\Files_Sharing\External\Storage; |
| 13 | +use OCA\Files_Sharing\SharedStorage; |
11 | 14 | use OCP\App\IAppManager; |
12 | 15 | use OCP\AppFramework\Http\DataResponse; |
13 | 16 | use OCP\AppFramework\OCS\OCSBadRequestException; |
@@ -5299,4 +5302,217 @@ public function testFormatShareWithFederatedShareWithAtInUsername(): void { |
5299 | 5302 |
|
5300 | 5303 | $this->assertTrue($result['is_trusted_server']); |
5301 | 5304 | } |
| 5305 | + |
| 5306 | + public function testOwnerCanAlwaysDownload(): void { |
| 5307 | + $ocs = $this->mockFormatShare(); |
| 5308 | + |
| 5309 | + $share = $this->createMock(IShare::class); |
| 5310 | + $node = $this->createMock(File::class); |
| 5311 | + $userFolder = $this->createMock(Folder::class); |
| 5312 | + $owner = $this->createMock(IUser::class); |
| 5313 | + |
| 5314 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5315 | + $share->method('getNodeId')->willReturn(42); |
| 5316 | + $node->method('getOwner')->willReturn($owner); |
| 5317 | + $owner->method('getUID')->willReturn('sharedByUser'); |
| 5318 | + |
| 5319 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5320 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5321 | + |
| 5322 | + // Expect hideDownload to be set to false since owner can always download |
| 5323 | + $share->expects($this->once())->method('setHideDownload')->with(false); |
| 5324 | + |
| 5325 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5326 | + } |
| 5327 | + |
| 5328 | + public function testParentHideDownloadEnforcedOnChild(): void { |
| 5329 | + $ocs = $this->mockFormatShare(); |
| 5330 | + |
| 5331 | + $share = $this->createMock(IShare::class); |
| 5332 | + $node = $this->createMock(File::class); |
| 5333 | + $userFolder = $this->createMock(Folder::class); |
| 5334 | + $owner = $this->createMock(IUser::class); |
| 5335 | + $storage = $this->createMock(SharedStorage::class); |
| 5336 | + $originalShare = $this->createMock(IShare::class); |
| 5337 | + |
| 5338 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5339 | + $share->method('getNodeId')->willReturn(42); |
| 5340 | + $share->method('getHideDownload')->willReturn(false); // User wants to allow downloads |
| 5341 | + $node->method('getOwner')->willReturn($owner); |
| 5342 | + $owner->method('getUID')->willReturn('differentOwner'); |
| 5343 | + $node->method('getStorage')->willReturn($storage); |
| 5344 | + $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); |
| 5345 | + $storage->method('getInstanceOfStorage')->with(SharedStorage::class)->willReturn($storage); |
| 5346 | + $storage->method('getShare')->willReturn($originalShare); |
| 5347 | + $originalShare->method('getHideDownload')->willReturn(true); // Parent hides download |
| 5348 | + $originalShare->method('getAttributes')->willReturn(null); |
| 5349 | + |
| 5350 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5351 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5352 | + |
| 5353 | + // Should be forced to hide download due to parent restriction |
| 5354 | + $share->expects($this->once())->method('setHideDownload')->with(true); |
| 5355 | + |
| 5356 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5357 | + } |
| 5358 | + |
| 5359 | + public function testUserCanHideWhenParentAllows(): void { |
| 5360 | + $ocs = $this->mockFormatShare(); |
| 5361 | + |
| 5362 | + $share = $this->createMock(IShare::class); |
| 5363 | + $node = $this->createMock(File::class); |
| 5364 | + $userFolder = $this->createMock(Folder::class); |
| 5365 | + $owner = $this->createMock(IUser::class); |
| 5366 | + $storage = $this->createMock(SharedStorage::class); |
| 5367 | + $originalShare = $this->createMock(IShare::class); |
| 5368 | + |
| 5369 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5370 | + $share->method('getNodeId')->willReturn(42); |
| 5371 | + $share->method('getHideDownload')->willReturn(true); // User chooses to hide downloads |
| 5372 | + $node->method('getOwner')->willReturn($owner); |
| 5373 | + $owner->method('getUID')->willReturn('differentOwner'); |
| 5374 | + $node->method('getStorage')->willReturn($storage); |
| 5375 | + $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); |
| 5376 | + $storage->method('getInstanceOfStorage')->with(SharedStorage::class)->willReturn($storage); |
| 5377 | + $storage->method('getShare')->willReturn($originalShare); |
| 5378 | + $originalShare->method('getHideDownload')->willReturn(false); // Parent allows download |
| 5379 | + $originalShare->method('getAttributes')->willReturn(null); |
| 5380 | + |
| 5381 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5382 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5383 | + |
| 5384 | + // Should respect user's choice to hide downloads |
| 5385 | + $share->expects($this->once())->method('setHideDownload')->with(true); |
| 5386 | + |
| 5387 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5388 | + } |
| 5389 | + |
| 5390 | + public function testParentDownloadAttributeInherited(): void { |
| 5391 | + $ocs = $this->mockFormatShare(); |
| 5392 | + |
| 5393 | + $share = $this->createMock(IShare::class); |
| 5394 | + $node = $this->createMock(File::class); |
| 5395 | + $userFolder = $this->createMock(Folder::class); |
| 5396 | + $owner = $this->createMock(IUser::class); |
| 5397 | + $storage = $this->createMock(SharedStorage::class); |
| 5398 | + $originalShare = $this->createMock(IShare::class); |
| 5399 | + $attributes = $this->createMock(\OCP\Share\IAttributes::class); |
| 5400 | + $shareAttributes = $this->createMock(\OCP\Share\IAttributes::class); |
| 5401 | + |
| 5402 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5403 | + $share->method('getNodeId')->willReturn(42); |
| 5404 | + $share->method('getHideDownload')->willReturn(false); // User wants to allow downloads |
| 5405 | + $share->method('getAttributes')->willReturn($shareAttributes); |
| 5406 | + $share->method('newAttributes')->willReturn($shareAttributes); |
| 5407 | + $node->method('getOwner')->willReturn($owner); |
| 5408 | + $owner->method('getUID')->willReturn('differentOwner'); |
| 5409 | + $node->method('getStorage')->willReturn($storage); |
| 5410 | + $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); |
| 5411 | + $storage->method('getInstanceOfStorage')->with(SharedStorage::class)->willReturn($storage); |
| 5412 | + $storage->method('getShare')->willReturn($originalShare); |
| 5413 | + $originalShare->method('getHideDownload')->willReturn(false); |
| 5414 | + $originalShare->method('getAttributes')->willReturn($attributes); |
| 5415 | + $attributes->method('getAttribute')->with('permissions', 'download')->willReturn(false); // Parent forbids download |
| 5416 | + |
| 5417 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5418 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5419 | + |
| 5420 | + // Should be forced to hide download and set download attribute to false |
| 5421 | + $share->expects($this->once())->method('setHideDownload')->with(true); |
| 5422 | + $shareAttributes->expects($this->once())->method('setAttribute')->with('permissions', 'download', false); |
| 5423 | + $share->expects($this->once())->method('setAttributes')->with($shareAttributes); |
| 5424 | + |
| 5425 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5426 | + } |
| 5427 | + |
| 5428 | + public function testFederatedStorageRespectsUserChoice(): void { |
| 5429 | + $ocs = $this->mockFormatShare(); |
| 5430 | + |
| 5431 | + $share = $this->createMock(IShare::class); |
| 5432 | + $node = $this->createMock(File::class); |
| 5433 | + $userFolder = $this->createMock(Folder::class); |
| 5434 | + $owner = $this->createMock(IUser::class); |
| 5435 | + $storage = $this->createMock(\OCA\Files_Sharing\External\Storage::class); |
| 5436 | + |
| 5437 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5438 | + $share->method('getNodeId')->willReturn(42); |
| 5439 | + $share->method('getHideDownload')->willReturn(true); // User chooses to hide downloads |
| 5440 | + $node->method('getOwner')->willReturn($owner); |
| 5441 | + $owner->method('getUID')->willReturn('differentOwner'); |
| 5442 | + $node->method('getStorage')->willReturn($storage); |
| 5443 | + $storage->method('instanceOfStorage')->willReturnMap([ |
| 5444 | + [SharedStorage::class, false], |
| 5445 | + [\OCA\Files_Sharing\External\Storage::class, true] |
| 5446 | + ]); |
| 5447 | + |
| 5448 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5449 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5450 | + |
| 5451 | + // For federated storage, should respect user's choice |
| 5452 | + $share->expects($this->once())->method('setHideDownload')->with(true); |
| 5453 | + |
| 5454 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5455 | + } |
| 5456 | + |
| 5457 | + public function testUserAllowsDownloadWhenParentPermits(): void { |
| 5458 | + $ocs = $this->mockFormatShare(); |
| 5459 | + |
| 5460 | + $share = $this->createMock(IShare::class); |
| 5461 | + $node = $this->createMock(File::class); |
| 5462 | + $userFolder = $this->createMock(Folder::class); |
| 5463 | + $owner = $this->createMock(IUser::class); |
| 5464 | + $storage = $this->createMock(SharedStorage::class); |
| 5465 | + $originalShare = $this->createMock(IShare::class); |
| 5466 | + |
| 5467 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5468 | + $share->method('getNodeId')->willReturn(42); |
| 5469 | + $share->method('getHideDownload')->willReturn(false); // User wants to allow downloads |
| 5470 | + $node->method('getOwner')->willReturn($owner); |
| 5471 | + $owner->method('getUID')->willReturn('differentOwner'); |
| 5472 | + $node->method('getStorage')->willReturn($storage); |
| 5473 | + $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); |
| 5474 | + $storage->method('getInstanceOfStorage')->with(SharedStorage::class)->willReturn($storage); |
| 5475 | + $storage->method('getShare')->willReturn($originalShare); |
| 5476 | + $originalShare->method('getHideDownload')->willReturn(false); // Parent allows download |
| 5477 | + $originalShare->method('getAttributes')->willReturn(null); |
| 5478 | + |
| 5479 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5480 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5481 | + |
| 5482 | + // Should allow downloads as both user and parent permit it |
| 5483 | + $share->expects($this->once())->method('setHideDownload')->with(false); |
| 5484 | + |
| 5485 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5486 | + } |
| 5487 | + |
| 5488 | + public function testWrapperStorageUnwrapped(): void { |
| 5489 | + $ocs = $this->mockFormatShare(); |
| 5490 | + |
| 5491 | + $share = $this->createMock(IShare::class); |
| 5492 | + $node = $this->createMock(File::class); |
| 5493 | + $userFolder = $this->createMock(Folder::class); |
| 5494 | + $owner = $this->createMock(IUser::class); |
| 5495 | + $wrapperStorage = $this->createMock(Wrapper::class); |
| 5496 | + $innerStorage = $this->createMock(SharedStorage::class); |
| 5497 | + $originalShare = $this->createMock(IShare::class); |
| 5498 | + |
| 5499 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5500 | + $share->method('getNodeId')->willReturn(42); |
| 5501 | + $share->method('getHideDownload')->willReturn(false); |
| 5502 | + $node->method('getOwner')->willReturn($owner); |
| 5503 | + $owner->method('getUID')->willReturn('differentOwner'); |
| 5504 | + $node->method('getStorage')->willReturn($wrapperStorage); |
| 5505 | + $wrapperStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); |
| 5506 | + $wrapperStorage->method('getInstanceOfStorage')->with(SharedStorage::class)->willReturn($innerStorage); |
| 5507 | + $innerStorage->method('getShare')->willReturn($originalShare); |
| 5508 | + $originalShare->method('getHideDownload')->willReturn(false); |
| 5509 | + $originalShare->method('getAttributes')->willReturn(null); |
| 5510 | + |
| 5511 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5512 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5513 | + |
| 5514 | + $share->expects($this->once())->method('setHideDownload')->with(false); |
| 5515 | + |
| 5516 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5517 | + } |
5302 | 5518 | } |
0 commit comments