ViewOnlyPluginTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 OCP\Files\File;
  26. use OCP\Files\Storage\IStorage;
  27. use OCP\Share\IAttributes;
  28. use OCP\Share\IShare;
  29. use Psr\Log\LoggerInterface;
  30. use Sabre\DAV\Server;
  31. use Sabre\DAV\Tree;
  32. use Test\TestCase;
  33. use Sabre\HTTP\RequestInterface;
  34. use OCA\DAV\Connector\Sabre\Exception\Forbidden;
  35. class ViewOnlyPluginTest extends TestCase {
  36. private ViewOnlyPlugin $plugin;
  37. /** @var Tree | \PHPUnit\Framework\MockObject\MockObject */
  38. private $tree;
  39. /** @var RequestInterface | \PHPUnit\Framework\MockObject\MockObject */
  40. private $request;
  41. public function setUp(): void {
  42. $this->plugin = new ViewOnlyPlugin(
  43. $this->createMock(LoggerInterface::class)
  44. );
  45. $this->request = $this->createMock(RequestInterface::class);
  46. $this->tree = $this->createMock(Tree::class);
  47. $server = $this->createMock(Server::class);
  48. $server->tree = $this->tree;
  49. $this->plugin->initialize($server);
  50. }
  51. public function testCanGetNonDav(): void {
  52. $this->request->expects($this->once())->method('getPath')->willReturn('files/test/target');
  53. $this->tree->method('getNodeForPath')->willReturn(null);
  54. $this->assertTrue($this->plugin->checkViewOnly($this->request));
  55. }
  56. public function testCanGetNonShared(): void {
  57. $this->request->expects($this->once())->method('getPath')->willReturn('files/test/target');
  58. $davNode = $this->createMock(DavFile::class);
  59. $this->tree->method('getNodeForPath')->willReturn($davNode);
  60. $file = $this->createMock(File::class);
  61. $davNode->method('getNode')->willReturn($file);
  62. $storage = $this->createMock(IStorage::class);
  63. $file->method('getStorage')->willReturn($storage);
  64. $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false);
  65. $this->assertTrue($this->plugin->checkViewOnly($this->request));
  66. }
  67. public function providesDataForCanGet(): array {
  68. return [
  69. // has attribute permissions-download enabled - can get file
  70. [ $this->createMock(File::class), true, true],
  71. // has no attribute permissions-download - can get file
  72. [ $this->createMock(File::class), null, true],
  73. // has attribute permissions-download disabled- cannot get the file
  74. [ $this->createMock(File::class), false, false],
  75. ];
  76. }
  77. /**
  78. * @dataProvider providesDataForCanGet
  79. */
  80. public function testCanGet(File $nodeInfo, ?bool $attrEnabled, bool $expectCanDownloadFile): void {
  81. $this->request->expects($this->once())->method('getPath')->willReturn('files/test/target');
  82. $davNode = $this->createMock(DavFile::class);
  83. $this->tree->method('getNodeForPath')->willReturn($davNode);
  84. $davNode->method('getNode')->willReturn($nodeInfo);
  85. $storage = $this->createMock(SharedStorage::class);
  86. $share = $this->createMock(IShare::class);
  87. $nodeInfo->method('getStorage')->willReturn($storage);
  88. $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
  89. $storage->method('getShare')->willReturn($share);
  90. $extAttr = $this->createMock(IAttributes::class);
  91. $share->method('getAttributes')->willReturn($extAttr);
  92. $extAttr->method('getAttribute')->with('permissions', 'download')->willReturn($attrEnabled);
  93. if (!$expectCanDownloadFile) {
  94. $this->expectException(Forbidden::class);
  95. }
  96. $this->plugin->checkViewOnly($this->request);
  97. }
  98. }