ShareTest.php 8.2 KB

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