ViewOnlyPluginTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2019 ownCloud GmbH
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\DAV;
  8. use OCA\DAV\Connector\Sabre\Exception\Forbidden;
  9. use OCA\DAV\Connector\Sabre\File as DavFile;
  10. use OCA\DAV\DAV\ViewOnlyPlugin;
  11. use OCA\Files_Sharing\SharedStorage;
  12. use OCA\Files_Versions\Sabre\VersionFile;
  13. use OCA\Files_Versions\Versions\IVersion;
  14. use OCP\Files\File;
  15. use OCP\Files\Folder;
  16. use OCP\Files\Storage\ISharedStorage;
  17. use OCP\Files\Storage\IStorage;
  18. use OCP\IUser;
  19. use OCP\Share\IAttributes;
  20. use OCP\Share\IShare;
  21. use Sabre\DAV\Server;
  22. use Sabre\DAV\Tree;
  23. use Sabre\HTTP\RequestInterface;
  24. use Test\TestCase;
  25. class ViewOnlyPluginTest extends TestCase {
  26. private ViewOnlyPlugin $plugin;
  27. /** @var Tree | \PHPUnit\Framework\MockObject\MockObject */
  28. private $tree;
  29. /** @var RequestInterface | \PHPUnit\Framework\MockObject\MockObject */
  30. private $request;
  31. /** @var Folder | \PHPUnit\Framework\MockObject\MockObject */
  32. private $userFolder;
  33. public function setUp(): void {
  34. $this->userFolder = $this->createMock(Folder::class);
  35. $this->plugin = new ViewOnlyPlugin(
  36. $this->userFolder,
  37. );
  38. $this->request = $this->createMock(RequestInterface::class);
  39. $this->tree = $this->createMock(Tree::class);
  40. $server = $this->createMock(Server::class);
  41. $server->tree = $this->tree;
  42. $this->plugin->initialize($server);
  43. }
  44. public function testCanGetNonDav(): void {
  45. $this->request->expects($this->once())->method('getPath')->willReturn('files/test/target');
  46. $this->tree->method('getNodeForPath')->willReturn(null);
  47. $this->assertTrue($this->plugin->checkViewOnly($this->request));
  48. }
  49. public function testCanGetNonShared(): void {
  50. $this->request->expects($this->once())->method('getPath')->willReturn('files/test/target');
  51. $davNode = $this->createMock(DavFile::class);
  52. $this->tree->method('getNodeForPath')->willReturn($davNode);
  53. $file = $this->createMock(File::class);
  54. $davNode->method('getNode')->willReturn($file);
  55. $storage = $this->createMock(IStorage::class);
  56. $file->method('getStorage')->willReturn($storage);
  57. $storage->method('instanceOfStorage')->with(ISharedStorage::class)->willReturn(false);
  58. $this->assertTrue($this->plugin->checkViewOnly($this->request));
  59. }
  60. public function providesDataForCanGet(): array {
  61. return [
  62. // has attribute permissions-download enabled - can get file
  63. [false, true, true],
  64. // has no attribute permissions-download - can get file
  65. [false, null, true],
  66. // has attribute permissions-download disabled- cannot get the file
  67. [false, false, false],
  68. // has attribute permissions-download enabled - can get file version
  69. [true, true, true],
  70. // has no attribute permissions-download - can get file version
  71. [true, null, true],
  72. // has attribute permissions-download disabled- cannot get the file version
  73. [true, false, false],
  74. ];
  75. }
  76. /**
  77. * @dataProvider providesDataForCanGet
  78. */
  79. public function testCanGet(bool $isVersion, ?bool $attrEnabled, bool $expectCanDownloadFile): void {
  80. $nodeInfo = $this->createMock(File::class);
  81. if ($isVersion) {
  82. $davPath = 'versions/alice/versions/117/123456';
  83. $version = $this->createMock(IVersion::class);
  84. $version->expects($this->once())
  85. ->method('getSourceFile')
  86. ->willReturn($nodeInfo);
  87. $davNode = $this->createMock(VersionFile::class);
  88. $davNode->expects($this->once())
  89. ->method('getVersion')
  90. ->willReturn($version);
  91. $currentUser = $this->createMock(IUser::class);
  92. $currentUser->expects($this->once())
  93. ->method('getUID')
  94. ->willReturn('alice');
  95. $nodeInfo->expects($this->once())
  96. ->method('getOwner')
  97. ->willReturn($currentUser);
  98. $nodeInfo = $this->createMock(File::class);
  99. $owner = $this->createMock(IUser::class);
  100. $owner->expects($this->once())
  101. ->method('getUID')
  102. ->willReturn('bob');
  103. $this->userFolder->expects($this->once())
  104. ->method('getById')
  105. ->willReturn([$nodeInfo]);
  106. $this->userFolder->expects($this->once())
  107. ->method('getOwner')
  108. ->willReturn($owner);
  109. } else {
  110. $davPath = 'files/path/to/file.odt';
  111. $davNode = $this->createMock(DavFile::class);
  112. $davNode->method('getNode')->willReturn($nodeInfo);
  113. }
  114. $this->request->expects($this->once())->method('getPath')->willReturn($davPath);
  115. $this->tree->expects($this->once())
  116. ->method('getNodeForPath')
  117. ->with($davPath)
  118. ->willReturn($davNode);
  119. $storage = $this->createMock(SharedStorage::class);
  120. $share = $this->createMock(IShare::class);
  121. $nodeInfo->expects($this->once())
  122. ->method('getStorage')
  123. ->willReturn($storage);
  124. $storage->method('instanceOfStorage')->with(ISharedStorage::class)->willReturn(true);
  125. $storage->method('getShare')->willReturn($share);
  126. $extAttr = $this->createMock(IAttributes::class);
  127. $share->method('getAttributes')->willReturn($extAttr);
  128. $extAttr->expects($this->once())
  129. ->method('getAttribute')
  130. ->with('permissions', 'download')
  131. ->willReturn($attrEnabled);
  132. if (!$expectCanDownloadFile) {
  133. $this->expectException(Forbidden::class);
  134. }
  135. $this->plugin->checkViewOnly($this->request);
  136. }
  137. }