1
0

ViewOnlyPluginTest.php 5.0 KB

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