ShareTest.php 8.1 KB

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