ShareTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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->loginHelper(self::TEST_FILES_SHARING_API_USER1);
  131. \OC\Files\Filesystem::file_put_contents('test.txt', 'test');
  132. $share = $this->share(
  133. \OCP\Share::SHARE_TYPE_GROUP,
  134. 'test.txt',
  135. self::TEST_FILES_SHARING_API_USER1,
  136. self::TEST_FILES_SHARING_API_GROUP1,
  137. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  138. );
  139. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  140. $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP);
  141. $share = $shares[0];
  142. $this->assertSame('/test.txt' ,$share->getTarget());
  143. $this->assertSame(19, $share->getPermissions());
  144. \OC\Files\Filesystem::rename('test.txt', 'new test.txt');
  145. $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP);
  146. $share = $shares[0];
  147. $this->assertSame('/new test.txt' ,$share->getTarget());
  148. $this->assertSame(19, $share->getPermissions());
  149. $share->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE);
  150. $this->shareManager->updateShare($share);
  151. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  152. $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP);
  153. $share = $shares[0];
  154. $this->assertSame('/new test.txt' ,$share->getTarget());
  155. $this->assertSame(3, $share->getPermissions());
  156. }
  157. /**
  158. * shared files should never have delete permissions
  159. * @dataProvider dataProviderTestFileSharePermissions
  160. */
  161. public function testFileSharePermissions($permission, $expectedvalid) {
  162. $pass = true;
  163. try {
  164. $this->share(
  165. \OCP\Share::SHARE_TYPE_USER,
  166. $this->filename,
  167. self::TEST_FILES_SHARING_API_USER1,
  168. self::TEST_FILES_SHARING_API_USER2,
  169. $permission
  170. );
  171. } catch (\Exception $e) {
  172. $pass = false;
  173. }
  174. $this->assertEquals($expectedvalid, $pass);
  175. }
  176. public function dataProviderTestFileSharePermissions() {
  177. $permission1 = \OCP\Constants::PERMISSION_ALL;
  178. $permission3 = \OCP\Constants::PERMISSION_READ;
  179. $permission4 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE;
  180. $permission5 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE;
  181. $permission6 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
  182. return array(
  183. array($permission1, false),
  184. array($permission3, true),
  185. array($permission4, true),
  186. array($permission5, false),
  187. array($permission6, false),
  188. );
  189. }
  190. public function testFileOwner() {
  191. $this->share(
  192. \OCP\Share::SHARE_TYPE_USER,
  193. $this->filename,
  194. self::TEST_FILES_SHARING_API_USER1,
  195. self::TEST_FILES_SHARING_API_USER2,
  196. \OCP\Constants::PERMISSION_READ
  197. );
  198. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  199. $info = \OC\Files\Filesystem::getFileInfo($this->filename);
  200. $this->assertSame(self::TEST_FILES_SHARING_API_USER1, $info->getOwner()->getUID());
  201. }
  202. }