|
8 | 8 |
|
9 | 9 | namespace OCA\Files_Sharing\Tests\Controller; |
10 | 10 |
|
| 11 | +use OC\Files\Storage\Wrapper\Wrapper; |
11 | 12 | use OCA\Federation\TrustedServers; |
12 | 13 | use OCA\Files_Sharing\Controller\ShareAPIController; |
| 14 | +use OCA\Files_Sharing\External\Storage; |
| 15 | +use OCA\Files_Sharing\SharedStorage; |
13 | 16 | use OCP\App\IAppManager; |
14 | 17 | use OCP\AppFramework\Http\DataResponse; |
15 | 18 | use OCP\AppFramework\OCS\OCSBadRequestException; |
@@ -5432,4 +5435,217 @@ public function testFormatShareWithFederatedShareWithAtInUsername(): void { |
5432 | 5435 |
|
5433 | 5436 | $this->assertTrue($result['is_trusted_server']); |
5434 | 5437 | } |
| 5438 | + |
| 5439 | + public function testOwnerCanAlwaysDownload(): void { |
| 5440 | + $ocs = $this->mockFormatShare(); |
| 5441 | + |
| 5442 | + $share = $this->createMock(IShare::class); |
| 5443 | + $node = $this->createMock(File::class); |
| 5444 | + $userFolder = $this->createMock(Folder::class); |
| 5445 | + $owner = $this->createMock(IUser::class); |
| 5446 | + |
| 5447 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5448 | + $share->method('getNodeId')->willReturn(42); |
| 5449 | + $node->method('getOwner')->willReturn($owner); |
| 5450 | + $owner->method('getUID')->willReturn('sharedByUser'); |
| 5451 | + |
| 5452 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5453 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5454 | + |
| 5455 | + // Expect hideDownload to be set to false since owner can always download |
| 5456 | + $share->expects($this->once())->method('setHideDownload')->with(false); |
| 5457 | + |
| 5458 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5459 | + } |
| 5460 | + |
| 5461 | + public function testParentHideDownloadEnforcedOnChild(): void { |
| 5462 | + $ocs = $this->mockFormatShare(); |
| 5463 | + |
| 5464 | + $share = $this->createMock(IShare::class); |
| 5465 | + $node = $this->createMock(File::class); |
| 5466 | + $userFolder = $this->createMock(Folder::class); |
| 5467 | + $owner = $this->createMock(IUser::class); |
| 5468 | + $storage = $this->createMock(SharedStorage::class); |
| 5469 | + $originalShare = $this->createMock(IShare::class); |
| 5470 | + |
| 5471 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5472 | + $share->method('getNodeId')->willReturn(42); |
| 5473 | + $share->method('getHideDownload')->willReturn(false); // User wants to allow downloads |
| 5474 | + $node->method('getOwner')->willReturn($owner); |
| 5475 | + $owner->method('getUID')->willReturn('differentOwner'); |
| 5476 | + $node->method('getStorage')->willReturn($storage); |
| 5477 | + $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); |
| 5478 | + $storage->method('getInstanceOfStorage')->with(SharedStorage::class)->willReturn($storage); |
| 5479 | + $storage->method('getShare')->willReturn($originalShare); |
| 5480 | + $originalShare->method('getHideDownload')->willReturn(true); // Parent hides download |
| 5481 | + $originalShare->method('getAttributes')->willReturn(null); |
| 5482 | + |
| 5483 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5484 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5485 | + |
| 5486 | + // Should be forced to hide download due to parent restriction |
| 5487 | + $share->expects($this->once())->method('setHideDownload')->with(true); |
| 5488 | + |
| 5489 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5490 | + } |
| 5491 | + |
| 5492 | + public function testUserCanHideWhenParentAllows(): void { |
| 5493 | + $ocs = $this->mockFormatShare(); |
| 5494 | + |
| 5495 | + $share = $this->createMock(IShare::class); |
| 5496 | + $node = $this->createMock(File::class); |
| 5497 | + $userFolder = $this->createMock(Folder::class); |
| 5498 | + $owner = $this->createMock(IUser::class); |
| 5499 | + $storage = $this->createMock(SharedStorage::class); |
| 5500 | + $originalShare = $this->createMock(IShare::class); |
| 5501 | + |
| 5502 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5503 | + $share->method('getNodeId')->willReturn(42); |
| 5504 | + $share->method('getHideDownload')->willReturn(true); // User chooses to hide downloads |
| 5505 | + $node->method('getOwner')->willReturn($owner); |
| 5506 | + $owner->method('getUID')->willReturn('differentOwner'); |
| 5507 | + $node->method('getStorage')->willReturn($storage); |
| 5508 | + $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); |
| 5509 | + $storage->method('getInstanceOfStorage')->with(SharedStorage::class)->willReturn($storage); |
| 5510 | + $storage->method('getShare')->willReturn($originalShare); |
| 5511 | + $originalShare->method('getHideDownload')->willReturn(false); // Parent allows download |
| 5512 | + $originalShare->method('getAttributes')->willReturn(null); |
| 5513 | + |
| 5514 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5515 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5516 | + |
| 5517 | + // Should respect user's choice to hide downloads |
| 5518 | + $share->expects($this->once())->method('setHideDownload')->with(true); |
| 5519 | + |
| 5520 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5521 | + } |
| 5522 | + |
| 5523 | + public function testParentDownloadAttributeInherited(): void { |
| 5524 | + $ocs = $this->mockFormatShare(); |
| 5525 | + |
| 5526 | + $share = $this->createMock(IShare::class); |
| 5527 | + $node = $this->createMock(File::class); |
| 5528 | + $userFolder = $this->createMock(Folder::class); |
| 5529 | + $owner = $this->createMock(IUser::class); |
| 5530 | + $storage = $this->createMock(SharedStorage::class); |
| 5531 | + $originalShare = $this->createMock(IShare::class); |
| 5532 | + $attributes = $this->createMock(\OCP\Share\IAttributes::class); |
| 5533 | + $shareAttributes = $this->createMock(\OCP\Share\IAttributes::class); |
| 5534 | + |
| 5535 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5536 | + $share->method('getNodeId')->willReturn(42); |
| 5537 | + $share->method('getHideDownload')->willReturn(false); // User wants to allow downloads |
| 5538 | + $share->method('getAttributes')->willReturn($shareAttributes); |
| 5539 | + $share->method('newAttributes')->willReturn($shareAttributes); |
| 5540 | + $node->method('getOwner')->willReturn($owner); |
| 5541 | + $owner->method('getUID')->willReturn('differentOwner'); |
| 5542 | + $node->method('getStorage')->willReturn($storage); |
| 5543 | + $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); |
| 5544 | + $storage->method('getInstanceOfStorage')->with(SharedStorage::class)->willReturn($storage); |
| 5545 | + $storage->method('getShare')->willReturn($originalShare); |
| 5546 | + $originalShare->method('getHideDownload')->willReturn(false); |
| 5547 | + $originalShare->method('getAttributes')->willReturn($attributes); |
| 5548 | + $attributes->method('getAttribute')->with('permissions', 'download')->willReturn(false); // Parent forbids download |
| 5549 | + |
| 5550 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5551 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5552 | + |
| 5553 | + // Should be forced to hide download and set download attribute to false |
| 5554 | + $share->expects($this->once())->method('setHideDownload')->with(true); |
| 5555 | + $shareAttributes->expects($this->once())->method('setAttribute')->with('permissions', 'download', false); |
| 5556 | + $share->expects($this->once())->method('setAttributes')->with($shareAttributes); |
| 5557 | + |
| 5558 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5559 | + } |
| 5560 | + |
| 5561 | + public function testFederatedStorageRespectsUserChoice(): void { |
| 5562 | + $ocs = $this->mockFormatShare(); |
| 5563 | + |
| 5564 | + $share = $this->createMock(IShare::class); |
| 5565 | + $node = $this->createMock(File::class); |
| 5566 | + $userFolder = $this->createMock(Folder::class); |
| 5567 | + $owner = $this->createMock(IUser::class); |
| 5568 | + $storage = $this->createMock(\OCA\Files_Sharing\External\Storage::class); |
| 5569 | + |
| 5570 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5571 | + $share->method('getNodeId')->willReturn(42); |
| 5572 | + $share->method('getHideDownload')->willReturn(true); // User chooses to hide downloads |
| 5573 | + $node->method('getOwner')->willReturn($owner); |
| 5574 | + $owner->method('getUID')->willReturn('differentOwner'); |
| 5575 | + $node->method('getStorage')->willReturn($storage); |
| 5576 | + $storage->method('instanceOfStorage')->willReturnMap([ |
| 5577 | + [SharedStorage::class, false], |
| 5578 | + [\OCA\Files_Sharing\External\Storage::class, true] |
| 5579 | + ]); |
| 5580 | + |
| 5581 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5582 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5583 | + |
| 5584 | + // For federated storage, should respect user's choice |
| 5585 | + $share->expects($this->once())->method('setHideDownload')->with(true); |
| 5586 | + |
| 5587 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5588 | + } |
| 5589 | + |
| 5590 | + public function testUserAllowsDownloadWhenParentPermits(): void { |
| 5591 | + $ocs = $this->mockFormatShare(); |
| 5592 | + |
| 5593 | + $share = $this->createMock(IShare::class); |
| 5594 | + $node = $this->createMock(File::class); |
| 5595 | + $userFolder = $this->createMock(Folder::class); |
| 5596 | + $owner = $this->createMock(IUser::class); |
| 5597 | + $storage = $this->createMock(SharedStorage::class); |
| 5598 | + $originalShare = $this->createMock(IShare::class); |
| 5599 | + |
| 5600 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5601 | + $share->method('getNodeId')->willReturn(42); |
| 5602 | + $share->method('getHideDownload')->willReturn(false); // User wants to allow downloads |
| 5603 | + $node->method('getOwner')->willReturn($owner); |
| 5604 | + $owner->method('getUID')->willReturn('differentOwner'); |
| 5605 | + $node->method('getStorage')->willReturn($storage); |
| 5606 | + $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); |
| 5607 | + $storage->method('getInstanceOfStorage')->with(SharedStorage::class)->willReturn($storage); |
| 5608 | + $storage->method('getShare')->willReturn($originalShare); |
| 5609 | + $originalShare->method('getHideDownload')->willReturn(false); // Parent allows download |
| 5610 | + $originalShare->method('getAttributes')->willReturn(null); |
| 5611 | + |
| 5612 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5613 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5614 | + |
| 5615 | + // Should allow downloads as both user and parent permit it |
| 5616 | + $share->expects($this->once())->method('setHideDownload')->with(false); |
| 5617 | + |
| 5618 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5619 | + } |
| 5620 | + |
| 5621 | + public function testWrapperStorageUnwrapped(): void { |
| 5622 | + $ocs = $this->mockFormatShare(); |
| 5623 | + |
| 5624 | + $share = $this->createMock(IShare::class); |
| 5625 | + $node = $this->createMock(File::class); |
| 5626 | + $userFolder = $this->createMock(Folder::class); |
| 5627 | + $owner = $this->createMock(IUser::class); |
| 5628 | + $wrapperStorage = $this->createMock(Wrapper::class); |
| 5629 | + $innerStorage = $this->createMock(SharedStorage::class); |
| 5630 | + $originalShare = $this->createMock(IShare::class); |
| 5631 | + |
| 5632 | + $share->method('getSharedBy')->willReturn('sharedByUser'); |
| 5633 | + $share->method('getNodeId')->willReturn(42); |
| 5634 | + $share->method('getHideDownload')->willReturn(false); |
| 5635 | + $node->method('getOwner')->willReturn($owner); |
| 5636 | + $owner->method('getUID')->willReturn('differentOwner'); |
| 5637 | + $node->method('getStorage')->willReturn($wrapperStorage); |
| 5638 | + $wrapperStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); |
| 5639 | + $wrapperStorage->method('getInstanceOfStorage')->with(SharedStorage::class)->willReturn($innerStorage); |
| 5640 | + $innerStorage->method('getShare')->willReturn($originalShare); |
| 5641 | + $originalShare->method('getHideDownload')->willReturn(false); |
| 5642 | + $originalShare->method('getAttributes')->willReturn(null); |
| 5643 | + |
| 5644 | + $userFolder->method('getById')->with(42)->willReturn([$node]); |
| 5645 | + $this->rootFolder->method('getUserFolder')->with('sharedByUser')->willReturn($userFolder); |
| 5646 | + |
| 5647 | + $share->expects($this->once())->method('setHideDownload')->with(false); |
| 5648 | + |
| 5649 | + $this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]); |
| 5650 | + } |
5435 | 5651 | } |
0 commit comments