ShareTest.php 7.3 KB

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