1
0

PermissionsTest.php 5.9 KB

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