1
0

PermissionsTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_Sharing\Tests;
  29. use OC\Files\Cache\Cache;
  30. use OC\Files\Storage\Storage;
  31. use OC\Files\View;
  32. use OCP\Share\IShare;
  33. /**
  34. * Class PermissionsTest
  35. *
  36. * @group DB
  37. */
  38. class PermissionsTest extends TestCase {
  39. /** @var Storage */
  40. private $sharedStorageRestrictedShare;
  41. /** @var Storage */
  42. private $sharedCacheRestrictedShare;
  43. /** @var View */
  44. private $secondView;
  45. /** @var Storage */
  46. private $ownerStorage;
  47. /** @var Storage */
  48. private $sharedStorage;
  49. /** @var Cache */
  50. private $sharedCache;
  51. /** @var Cache */
  52. private $ownerCache;
  53. protected function setUp(): void {
  54. parent::setUp();
  55. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  56. // prepare user1's dir structure
  57. $textData = "dummy file data\n";
  58. $this->view->mkdir('container');
  59. $this->view->mkdir('container/shareddir');
  60. $this->view->mkdir('container/shareddir/subdir');
  61. $this->view->mkdir('container/shareddirrestricted');
  62. $this->view->mkdir('container/shareddirrestricted/subdir');
  63. $this->view->file_put_contents('container/shareddir/textfile.txt', $textData);
  64. $this->view->file_put_contents('container/shareddirrestricted/textfile1.txt', $textData);
  65. list($this->ownerStorage, $internalPath) = $this->view->resolvePath('');
  66. $this->ownerCache = $this->ownerStorage->getCache();
  67. $this->ownerStorage->getScanner()->scan('');
  68. // share "shareddir" with user2
  69. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  70. $node = $rootFolder->get('container/shareddir');
  71. $share = $this->shareManager->newShare();
  72. $share->setNode($node)
  73. ->setShareType(IShare::TYPE_USER)
  74. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  75. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  76. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  77. $share = $this->shareManager->createShare($share);
  78. $share->setStatus(IShare::STATUS_ACCEPTED);
  79. $this->shareManager->updateShare($share);
  80. $node = $rootFolder->get('container/shareddirrestricted');
  81. $share = $this->shareManager->newShare();
  82. $share->setNode($node)
  83. ->setShareType(IShare::TYPE_USER)
  84. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  85. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  86. ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  87. $share = $this->shareManager->createShare($share);
  88. $share->setStatus(IShare::STATUS_ACCEPTED);
  89. $this->shareManager->updateShare($share);
  90. // login as user2
  91. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  92. // retrieve the shared storage
  93. $this->secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  94. list($this->sharedStorage, $internalPath) = $this->secondView->resolvePath('files/shareddir');
  95. list($this->sharedStorageRestrictedShare, $internalPath) = $this->secondView->resolvePath('files/shareddirrestricted');
  96. $this->sharedCache = $this->sharedStorage->getCache();
  97. $this->sharedCacheRestrictedShare = $this->sharedStorageRestrictedShare->getCache();
  98. }
  99. protected function tearDown(): void {
  100. if ($this->sharedCache) {
  101. $this->sharedCache->clear();
  102. }
  103. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  104. $shares = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER);
  105. foreach ($shares as $share) {
  106. $this->shareManager->deleteShare($share);
  107. }
  108. $this->view->deleteAll('container');
  109. $this->ownerCache->clear();
  110. parent::tearDown();
  111. }
  112. /**
  113. * Test that the permissions of shared directory are returned correctly
  114. */
  115. public function testGetPermissions() {
  116. $sharedDirPerms = $this->sharedStorage->getPermissions('');
  117. $this->assertEquals(31, $sharedDirPerms);
  118. $sharedDirPerms = $this->sharedStorage->getPermissions('textfile.txt');
  119. $this->assertEquals(27, $sharedDirPerms);
  120. $sharedDirRestrictedPerms = $this->sharedStorageRestrictedShare->getPermissions('');
  121. $this->assertEquals(15, $sharedDirRestrictedPerms);
  122. $sharedDirRestrictedPerms = $this->sharedStorageRestrictedShare->getPermissions('textfile1.txt');
  123. $this->assertEquals(3, $sharedDirRestrictedPerms);
  124. }
  125. /**
  126. * Test that the permissions of shared directory are returned correctly
  127. */
  128. public function testGetDirectoryPermissions() {
  129. $contents = $this->secondView->getDirectoryContent('files/shareddir');
  130. $this->assertEquals('subdir', $contents[0]['name']);
  131. $this->assertEquals(31, $contents[0]['permissions']);
  132. $this->assertEquals('textfile.txt', $contents[1]['name']);
  133. // 27 is correct because create is reserved to folders only - requires more unit tests overall to ensure this
  134. $this->assertEquals(27, $contents[1]['permissions']);
  135. $contents = $this->secondView->getDirectoryContent('files/shareddirrestricted');
  136. $this->assertEquals('subdir', $contents[0]['name']);
  137. $this->assertEquals(7, $contents[0]['permissions']);
  138. $this->assertEquals('textfile1.txt', $contents[1]['name']);
  139. // 3 is correct because create is reserved to folders only
  140. $this->assertEquals(3, $contents[1]['permissions']);
  141. }
  142. }