PermissionsTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /**
  32. * Class PermissionsTest
  33. *
  34. * @group DB
  35. */
  36. class PermissionsTest extends TestCase {
  37. /** @var Storage */
  38. private $sharedStorageRestrictedShare;
  39. /** @var Storage */
  40. private $sharedCacheRestrictedShare;
  41. /** @var View */
  42. private $secondView;
  43. /** @var Storage */
  44. private $ownerStorage;
  45. /** @var Storage */
  46. private $sharedStorage;
  47. /** @var Cache */
  48. private $sharedCache;
  49. /** @var Cache */
  50. private $ownerCache;
  51. protected function setUp() {
  52. parent::setUp();
  53. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  54. // prepare user1's dir structure
  55. $textData = "dummy file data\n";
  56. $this->view->mkdir('container');
  57. $this->view->mkdir('container/shareddir');
  58. $this->view->mkdir('container/shareddir/subdir');
  59. $this->view->mkdir('container/shareddirrestricted');
  60. $this->view->mkdir('container/shareddirrestricted/subdir');
  61. $this->view->file_put_contents('container/shareddir/textfile.txt', $textData);
  62. $this->view->file_put_contents('container/shareddirrestricted/textfile1.txt', $textData);
  63. list($this->ownerStorage, $internalPath) = $this->view->resolvePath('');
  64. $this->ownerCache = $this->ownerStorage->getCache();
  65. $this->ownerStorage->getScanner()->scan('');
  66. // share "shareddir" with user2
  67. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  68. $node = $rootFolder->get('container/shareddir');
  69. $share = $this->shareManager->newShare();
  70. $share->setNode($node)
  71. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  72. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  73. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  74. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  75. $this->shareManager->createShare($share);
  76. $node = $rootFolder->get('container/shareddirrestricted');
  77. $share = $this->shareManager->newShare();
  78. $share->setNode($node)
  79. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  80. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  81. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  82. ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  83. $this->shareManager->createShare($share);
  84. // login as user2
  85. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  86. // retrieve the shared storage
  87. $this->secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  88. list($this->sharedStorage, $internalPath) = $this->secondView->resolvePath('files/shareddir');
  89. list($this->sharedStorageRestrictedShare, $internalPath) = $this->secondView->resolvePath('files/shareddirrestricted');
  90. $this->sharedCache = $this->sharedStorage->getCache();
  91. $this->sharedCacheRestrictedShare = $this->sharedStorageRestrictedShare->getCache();
  92. }
  93. protected function tearDown() {
  94. if ($this->sharedCache) {
  95. $this->sharedCache->clear();
  96. }
  97. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  98. $shares = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, \OCP\Share::SHARE_TYPE_USER);
  99. foreach ($shares as $share) {
  100. $this->shareManager->deleteShare($share);
  101. }
  102. $this->view->deleteAll('container');
  103. $this->ownerCache->clear();
  104. parent::tearDown();
  105. }
  106. /**
  107. * Test that the permissions of shared directory are returned correctly
  108. */
  109. function testGetPermissions() {
  110. $sharedDirPerms = $this->sharedStorage->getPermissions('shareddir');
  111. $this->assertEquals(31, $sharedDirPerms);
  112. $sharedDirPerms = $this->sharedStorage->getPermissions('shareddir/textfile.txt');
  113. $this->assertEquals(31, $sharedDirPerms);
  114. $sharedDirRestrictedPerms = $this->sharedStorageRestrictedShare->getPermissions('shareddirrestricted');
  115. $this->assertEquals(7, $sharedDirRestrictedPerms);
  116. $sharedDirRestrictedPerms = $this->sharedStorageRestrictedShare->getPermissions('shareddirrestricted/textfile.txt');
  117. $this->assertEquals(7, $sharedDirRestrictedPerms);
  118. }
  119. /**
  120. * Test that the permissions of shared directory are returned correctly
  121. */
  122. function testGetDirectoryPermissions() {
  123. $contents = $this->secondView->getDirectoryContent('files/shareddir');
  124. $this->assertEquals('subdir', $contents[0]['name']);
  125. $this->assertEquals(31, $contents[0]['permissions']);
  126. $this->assertEquals('textfile.txt', $contents[1]['name']);
  127. // 27 is correct because create is reserved to folders only - requires more unit tests overall to ensure this
  128. $this->assertEquals(27, $contents[1]['permissions']);
  129. $contents = $this->secondView->getDirectoryContent('files/shareddirrestricted');
  130. $this->assertEquals('subdir', $contents[0]['name']);
  131. $this->assertEquals(7, $contents[0]['permissions']);
  132. $this->assertEquals('textfile1.txt', $contents[1]['name']);
  133. // 3 is correct because create is reserved to folders only
  134. $this->assertEquals(3, $contents[1]['permissions']);
  135. }
  136. }