ViewOnlyPluginTest.php 4.9 KB

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