ShareTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. // clear database table
  56. $query = \OCP\DB::prepare('DELETE FROM `*PREFIX*share`');
  57. $query->execute();
  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. \OCP\Share::SHARE_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. \OCP\Share::SHARE_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. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  83. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  84. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  85. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  86. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  87. \OC\Files\Filesystem::unlink($this->filename);
  88. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  89. // both group share and user share should be gone
  90. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  91. // for user3 nothing should change
  92. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  93. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  94. $this->shareManager->deleteShare($share1);
  95. $this->shareManager->deleteShare($share2);
  96. }
  97. /**
  98. * @param \OC\Files\FileInfo[] $content
  99. * @param string[] $expected
  100. */
  101. public function verifyDirContent($content, $expected) {
  102. foreach ($content as $c) {
  103. if (!in_array($c['name'], $expected)) {
  104. $this->assertTrue(false, "folder should only contain '" . implode(',', $expected) . "', found: " .$c['name']);
  105. }
  106. }
  107. }
  108. public function testShareWithDifferentShareFolder() {
  109. $fileinfo = $this->view->getFileInfo($this->filename);
  110. $folderinfo = $this->view->getFileInfo($this->folder);
  111. $share = $this->share(
  112. \OCP\Share::SHARE_TYPE_USER,
  113. $this->filename,
  114. self::TEST_FILES_SHARING_API_USER1,
  115. self::TEST_FILES_SHARING_API_USER2,
  116. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  117. );
  118. \OCA\Files_Sharing\Helper::setShareFolder('/Shared/subfolder');
  119. $share = $this->share(
  120. \OCP\Share::SHARE_TYPE_USER,
  121. $this->folder,
  122. self::TEST_FILES_SHARING_API_USER1,
  123. self::TEST_FILES_SHARING_API_USER2,
  124. \OCP\Constants::PERMISSION_ALL
  125. );
  126. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  127. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  128. $this->assertTrue(\OC\Files\Filesystem::file_exists('/Shared/subfolder/' . $this->folder));
  129. //cleanup
  130. \OC::$server->getConfig()->deleteSystemValue('share_folder');
  131. }
  132. public function testShareWithGroupUniqueName() {
  133. $this->loginHelper(self::TEST_FILES_SHARING_API_USER1);
  134. \OC\Files\Filesystem::file_put_contents('test.txt', 'test');
  135. $share = $this->share(
  136. \OCP\Share::SHARE_TYPE_GROUP,
  137. 'test.txt',
  138. self::TEST_FILES_SHARING_API_USER1,
  139. self::TEST_FILES_SHARING_API_GROUP1,
  140. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  141. );
  142. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  143. $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP);
  144. $share = $shares[0];
  145. $this->assertSame('/test.txt' ,$share->getTarget());
  146. $this->assertSame(19, $share->getPermissions());
  147. \OC\Files\Filesystem::rename('test.txt', 'new test.txt');
  148. $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP);
  149. $share = $shares[0];
  150. $this->assertSame('/new test.txt' ,$share->getTarget());
  151. $this->assertSame(19, $share->getPermissions());
  152. $share->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE);
  153. $this->shareManager->updateShare($share);
  154. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  155. $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP);
  156. $share = $shares[0];
  157. $this->assertSame('/new test.txt' ,$share->getTarget());
  158. $this->assertSame(3, $share->getPermissions());
  159. }
  160. /**
  161. * shared files should never have delete permissions
  162. * @dataProvider dataProviderTestFileSharePermissions
  163. */
  164. public function testFileSharePermissions($permission, $expectedvalid) {
  165. $pass = true;
  166. try {
  167. $this->share(
  168. \OCP\Share::SHARE_TYPE_USER,
  169. $this->filename,
  170. self::TEST_FILES_SHARING_API_USER1,
  171. self::TEST_FILES_SHARING_API_USER2,
  172. $permission
  173. );
  174. } catch (\Exception $e) {
  175. $pass = false;
  176. }
  177. $this->assertEquals($expectedvalid, $pass);
  178. }
  179. public function dataProviderTestFileSharePermissions() {
  180. $permission1 = \OCP\Constants::PERMISSION_ALL;
  181. $permission3 = \OCP\Constants::PERMISSION_READ;
  182. $permission4 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE;
  183. $permission5 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE;
  184. $permission6 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
  185. return array(
  186. array($permission1, false),
  187. array($permission3, true),
  188. array($permission4, true),
  189. array($permission5, false),
  190. array($permission6, false),
  191. );
  192. }
  193. public function testFileOwner() {
  194. $this->share(
  195. \OCP\Share::SHARE_TYPE_USER,
  196. $this->filename,
  197. self::TEST_FILES_SHARING_API_USER1,
  198. self::TEST_FILES_SHARING_API_USER2,
  199. \OCP\Constants::PERMISSION_READ
  200. );
  201. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  202. $info = \OC\Files\Filesystem::getFileInfo($this->filename);
  203. $this->assertSame(self::TEST_FILES_SHARING_API_USER1, $info->getOwner()->getUID());
  204. }
  205. }