ShareTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests;
  8. use OCP\Share\IShare;
  9. /**
  10. * Class ShareTest
  11. *
  12. * @group DB
  13. */
  14. class ShareTest extends TestCase {
  15. public const TEST_FOLDER_NAME = '/folder_share_api_test';
  16. private static $tempStorage;
  17. private string $subsubfolder = '';
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->folder = self::TEST_FOLDER_NAME;
  21. $this->subfolder = '/subfolder_share_api_test';
  22. $this->subsubfolder = '/subsubfolder_share_api_test';
  23. $this->filename = '/share-api-test.txt';
  24. // save file with content
  25. $this->view->file_put_contents($this->filename, $this->data);
  26. $this->view->mkdir($this->folder);
  27. $this->view->mkdir($this->folder . $this->subfolder);
  28. $this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
  29. $this->view->file_put_contents($this->folder.$this->filename, $this->data);
  30. $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
  31. }
  32. protected function tearDown(): void {
  33. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  34. $this->view->unlink($this->filename);
  35. $this->view->deleteAll($this->folder);
  36. self::$tempStorage = null;
  37. parent::tearDown();
  38. }
  39. public function testUnshareFromSelf() {
  40. $groupManager = \OC::$server->getGroupManager();
  41. $userManager = \OC::$server->getUserManager();
  42. $testGroup = $groupManager->createGroup('testGroup');
  43. $user1 = $userManager->get(self::TEST_FILES_SHARING_API_USER2);
  44. $user2 = $userManager->get(self::TEST_FILES_SHARING_API_USER3);
  45. $testGroup->addUser($user1);
  46. $testGroup->addUser($user2);
  47. $share1 = $this->share(
  48. IShare::TYPE_USER,
  49. $this->filename,
  50. self::TEST_FILES_SHARING_API_USER1,
  51. self::TEST_FILES_SHARING_API_USER2,
  52. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  53. );
  54. $share2 = $this->share(
  55. IShare::TYPE_GROUP,
  56. $this->filename,
  57. self::TEST_FILES_SHARING_API_USER1,
  58. 'testGroup',
  59. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  60. );
  61. $this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2);
  62. $this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER3);
  63. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  64. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  65. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  66. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  67. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  68. \OC\Files\Filesystem::unlink($this->filename);
  69. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  70. // both group share and user share should be gone
  71. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  72. // for user3 nothing should change
  73. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  74. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  75. $this->shareManager->deleteShare($share1);
  76. $this->shareManager->deleteShare($share2);
  77. }
  78. /**
  79. * @param \OC\Files\FileInfo[] $content
  80. * @param string[] $expected
  81. */
  82. public function verifyDirContent($content, $expected) {
  83. foreach ($content as $c) {
  84. if (!in_array($c['name'], $expected)) {
  85. $this->assertTrue(false, "folder should only contain '" . implode(',', $expected) . "', found: " .$c['name']);
  86. }
  87. }
  88. }
  89. public function testShareWithDifferentShareFolder() {
  90. $fileinfo = $this->view->getFileInfo($this->filename);
  91. $folderinfo = $this->view->getFileInfo($this->folder);
  92. $share = $this->share(
  93. IShare::TYPE_USER,
  94. $this->filename,
  95. self::TEST_FILES_SHARING_API_USER1,
  96. self::TEST_FILES_SHARING_API_USER2,
  97. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  98. );
  99. \OCA\Files_Sharing\Helper::setShareFolder('/Shared/subfolder');
  100. $share = $this->share(
  101. IShare::TYPE_USER,
  102. $this->folder,
  103. self::TEST_FILES_SHARING_API_USER1,
  104. self::TEST_FILES_SHARING_API_USER2,
  105. \OCP\Constants::PERMISSION_ALL
  106. );
  107. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  108. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  109. $this->assertTrue(\OC\Files\Filesystem::file_exists('/Shared/subfolder/' . $this->folder));
  110. //cleanup
  111. \OC::$server->getConfig()->deleteSystemValue('share_folder');
  112. }
  113. public function testShareWithGroupUniqueName() {
  114. $this->markTestSkipped('TODO: Disable because fails on drone');
  115. $this->loginHelper(self::TEST_FILES_SHARING_API_USER1);
  116. \OC\Files\Filesystem::file_put_contents('test.txt', 'test');
  117. $share = $this->share(
  118. IShare::TYPE_GROUP,
  119. 'test.txt',
  120. self::TEST_FILES_SHARING_API_USER1,
  121. self::TEST_FILES_SHARING_API_GROUP1,
  122. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  123. );
  124. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  125. $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_GROUP);
  126. $share = $shares[0];
  127. $this->assertSame('/test.txt', $share->getTarget());
  128. $this->assertSame(19, $share->getPermissions());
  129. \OC\Files\Filesystem::rename('test.txt', 'new test.txt');
  130. $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_GROUP);
  131. $share = $shares[0];
  132. $this->assertSame('/new test.txt', $share->getTarget());
  133. $this->assertSame(19, $share->getPermissions());
  134. $share->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE);
  135. $this->shareManager->updateShare($share);
  136. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  137. $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_GROUP);
  138. $share = $shares[0];
  139. $this->assertSame('/new test.txt', $share->getTarget());
  140. $this->assertSame(3, $share->getPermissions());
  141. }
  142. /**
  143. * shared files should never have delete permissions
  144. * @dataProvider dataProviderTestFileSharePermissions
  145. */
  146. public function testFileSharePermissions($permission, $expectedvalid) {
  147. $pass = true;
  148. try {
  149. $this->share(
  150. IShare::TYPE_USER,
  151. $this->filename,
  152. self::TEST_FILES_SHARING_API_USER1,
  153. self::TEST_FILES_SHARING_API_USER2,
  154. $permission
  155. );
  156. } catch (\Exception $e) {
  157. $pass = false;
  158. }
  159. $this->assertEquals($expectedvalid, $pass);
  160. }
  161. public function dataProviderTestFileSharePermissions() {
  162. $permission1 = \OCP\Constants::PERMISSION_ALL;
  163. $permission3 = \OCP\Constants::PERMISSION_READ;
  164. $permission4 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE;
  165. $permission5 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE;
  166. $permission6 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
  167. return [
  168. [$permission1, false],
  169. [$permission3, true],
  170. [$permission4, true],
  171. [$permission5, false],
  172. [$permission6, false],
  173. ];
  174. }
  175. public function testFileOwner() {
  176. $this->share(
  177. IShare::TYPE_USER,
  178. $this->filename,
  179. self::TEST_FILES_SHARING_API_USER1,
  180. self::TEST_FILES_SHARING_API_USER2,
  181. \OCP\Constants::PERMISSION_READ
  182. );
  183. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  184. $info = \OC\Files\Filesystem::getFileInfo($this->filename);
  185. $this->assertSame(self::TEST_FILES_SHARING_API_USER1, $info->getOwner()->getUID());
  186. }
  187. }