1
0

ShareTest.php 8.3 KB

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