1
0

ViewOnlyPluginTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @author Piotr Mrowczynski piotr@owncloud.com
  4. *
  5. * @copyright Copyright (c) 2019, ownCloud GmbH
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\DAV\Tests\unit\DAV;
  22. use OCA\DAV\Connector\Sabre\Exception\Forbidden;
  23. use OCA\DAV\Connector\Sabre\File as DavFile;
  24. use OCA\DAV\DAV\ViewOnlyPlugin;
  25. use OCA\Files_Sharing\SharedStorage;
  26. use OCA\Files_Versions\Sabre\VersionFile;
  27. use OCA\Files_Versions\Versions\IVersion;
  28. use OCP\Files\File;
  29. use OCP\Files\Folder;
  30. use OCP\Files\Storage\IStorage;
  31. use OCP\IUser;
  32. use OCP\Share\IAttributes;
  33. use OCP\Share\IShare;
  34. use Sabre\DAV\Server;
  35. use Sabre\DAV\Tree;
  36. use Sabre\HTTP\RequestInterface;
  37. use Test\TestCase;
  38. class ViewOnlyPluginTest extends TestCase {
  39. private ViewOnlyPlugin $plugin;
  40. /** @var Tree | \PHPUnit\Framework\MockObject\MockObject */
  41. private $tree;
  42. /** @var RequestInterface | \PHPUnit\Framework\MockObject\MockObject */
  43. private $request;
  44. /** @var Folder | \PHPUnit\Framework\MockObject\MockObject */
  45. private $userFolder;
  46. public function setUp(): void {
  47. $this->userFolder = $this->createMock(Folder::class);
  48. $this->plugin = new ViewOnlyPlugin(
  49. $this->userFolder,
  50. );
  51. $this->request = $this->createMock(RequestInterface::class);
  52. $this->tree = $this->createMock(Tree::class);
  53. $server = $this->createMock(Server::class);
  54. $server->tree = $this->tree;
  55. $this->plugin->initialize($server);
  56. }
  57. public function testCanGetNonDav(): void {
  58. $this->request->expects($this->once())->method('getPath')->willReturn('files/test/target');
  59. $this->tree->method('getNodeForPath')->willReturn(null);
  60. $this->assertTrue($this->plugin->checkViewOnly($this->request));
  61. }
  62. public function testCanGetNonShared(): void {
  63. $this->request->expects($this->once())->method('getPath')->willReturn('files/test/target');
  64. $davNode = $this->createMock(DavFile::class);
  65. $this->tree->method('getNodeForPath')->willReturn($davNode);
  66. $file = $this->createMock(File::class);
  67. $davNode->method('getNode')->willReturn($file);
  68. $storage = $this->createMock(IStorage::class);
  69. $file->method('getStorage')->willReturn($storage);
  70. $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false);
  71. $this->assertTrue($this->plugin->checkViewOnly($this->request));
  72. }
  73. public function providesDataForCanGet(): array {
  74. return [
  75. // has attribute permissions-download enabled - can get file
  76. [false, true, true],
  77. // has no attribute permissions-download - can get file
  78. [false, null, true],
  79. // has attribute permissions-download disabled- cannot get the file
  80. [false, false, false],
  81. // has attribute permissions-download enabled - can get file version
  82. [true, true, true],
  83. // has no attribute permissions-download - can get file version
  84. [true, null, true],
  85. // has attribute permissions-download disabled- cannot get the file version
  86. [true, false, false],
  87. ];
  88. }
  89. /**
  90. * @dataProvider providesDataForCanGet
  91. */
  92. public function testCanGet(bool $isVersion, ?bool $attrEnabled, bool $expectCanDownloadFile): void {
  93. $nodeInfo = $this->createMock(File::class);
  94. if ($isVersion) {
  95. $davPath = 'versions/alice/versions/117/123456';
  96. $version = $this->createMock(IVersion::class);
  97. $version->expects($this->once())
  98. ->method('getSourceFile')
  99. ->willReturn($nodeInfo);
  100. $davNode = $this->createMock(VersionFile::class);
  101. $davNode->expects($this->once())
  102. ->method('getVersion')
  103. ->willReturn($version);
  104. $currentUser = $this->createMock(IUser::class);
  105. $currentUser->expects($this->once())
  106. ->method('getUID')
  107. ->willReturn('alice');
  108. $nodeInfo->expects($this->once())
  109. ->method('getOwner')
  110. ->willReturn($currentUser);
  111. $nodeInfo = $this->createMock(File::class);
  112. $owner = $this->createMock(IUser::class);
  113. $owner->expects($this->once())
  114. ->method('getUID')
  115. ->willReturn('bob');
  116. $this->userFolder->expects($this->once())
  117. ->method('getById')
  118. ->willReturn([$nodeInfo]);
  119. $this->userFolder->expects($this->once())
  120. ->method('getOwner')
  121. ->willReturn($owner);
  122. } else {
  123. $davPath = 'files/path/to/file.odt';
  124. $davNode = $this->createMock(DavFile::class);
  125. $davNode->method('getNode')->willReturn($nodeInfo);
  126. }
  127. $this->request->expects($this->once())->method('getPath')->willReturn($davPath);
  128. $this->tree->expects($this->once())
  129. ->method('getNodeForPath')
  130. ->with($davPath)
  131. ->willReturn($davNode);
  132. $storage = $this->createMock(SharedStorage::class);
  133. $share = $this->createMock(IShare::class);
  134. $nodeInfo->expects($this->once())
  135. ->method('getStorage')
  136. ->willReturn($storage);
  137. $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
  138. $storage->method('getShare')->willReturn($share);
  139. $extAttr = $this->createMock(IAttributes::class);
  140. $share->method('getAttributes')->willReturn($extAttr);
  141. $extAttr->expects($this->once())
  142. ->method('getAttribute')
  143. ->with('permissions', 'download')
  144. ->willReturn($attrEnabled);
  145. if (!$expectCanDownloadFile) {
  146. $this->expectException(Forbidden::class);
  147. }
  148. $this->plugin->checkViewOnly($this->request);
  149. }
  150. }