share.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Michael Gapczynski
  6. * @copyright 2012 Michael Gapczynski mtgap@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. class Test_Share extends PHPUnit_Framework_TestCase {
  22. protected $itemType;
  23. protected $userBackend;
  24. protected $user1;
  25. protected $user2;
  26. protected $groupBackend;
  27. protected $group1;
  28. protected $group2;
  29. protected $resharing;
  30. protected $dateInFuture;
  31. protected $dateInPast;
  32. public function setUp() {
  33. OC_User::clearBackends();
  34. OC_User::useBackend('dummy');
  35. $this->user1 = uniqid('user1_');
  36. $this->user2 = uniqid('user2_');
  37. $this->user3 = uniqid('user3_');
  38. $this->user4 = uniqid('user4_');
  39. OC_User::createUser($this->user1, 'pass');
  40. OC_User::createUser($this->user2, 'pass');
  41. OC_User::createUser($this->user3, 'pass');
  42. OC_User::createUser($this->user4, 'pass');
  43. OC_User::setUserId($this->user1);
  44. OC_Group::clearBackends();
  45. OC_Group::useBackend(new OC_Group_Dummy);
  46. $this->group1 = uniqid('group_');
  47. $this->group2 = uniqid('group_');
  48. OC_Group::createGroup($this->group1);
  49. OC_Group::createGroup($this->group2);
  50. OC_Group::addToGroup($this->user1, $this->group1);
  51. OC_Group::addToGroup($this->user2, $this->group1);
  52. OC_Group::addToGroup($this->user3, $this->group1);
  53. OC_Group::addToGroup($this->user2, $this->group2);
  54. OC_Group::addToGroup($this->user4, $this->group2);
  55. OCP\Share::registerBackend('test', 'Test_Share_Backend');
  56. OC_Hook::clear('OCP\\Share');
  57. OC::registerShareHooks();
  58. $this->resharing = OC_Appconfig::getValue('core', 'shareapi_allow_resharing', 'yes');
  59. OC_Appconfig::setValue('core', 'shareapi_allow_resharing', 'yes');
  60. // 20 Minutes in the past, 20 minutes in the future.
  61. $now = time();
  62. $dateFormat = 'Y-m-d H:i:s';
  63. $this->dateInPast = date($dateFormat, $now - 20 * 60);
  64. $this->dateInFuture = date($dateFormat, $now + 20 * 60);
  65. }
  66. public function tearDown() {
  67. $query = OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `item_type` = ?');
  68. $query->execute(array('test'));
  69. OC_Appconfig::setValue('core', 'shareapi_allow_resharing', $this->resharing);
  70. }
  71. public function testShareInvalidShareType() {
  72. $message = 'Share type foobar is not valid for test.txt';
  73. try {
  74. OCP\Share::shareItem('test', 'test.txt', 'foobar', $this->user2, OCP\PERMISSION_READ);
  75. } catch (Exception $exception) {
  76. $this->assertEquals($message, $exception->getMessage());
  77. }
  78. }
  79. public function testInvalidItemType() {
  80. $message = 'Sharing backend for foobar not found';
  81. try {
  82. OCP\Share::shareItem('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ);
  83. $this->fail('Exception was expected: '.$message);
  84. } catch (Exception $exception) {
  85. $this->assertEquals($message, $exception->getMessage());
  86. }
  87. try {
  88. OCP\Share::getItemsSharedWith('foobar');
  89. $this->fail('Exception was expected: '.$message);
  90. } catch (Exception $exception) {
  91. $this->assertEquals($message, $exception->getMessage());
  92. }
  93. try {
  94. OCP\Share::getItemSharedWith('foobar', 'test.txt');
  95. $this->fail('Exception was expected: '.$message);
  96. } catch (Exception $exception) {
  97. $this->assertEquals($message, $exception->getMessage());
  98. }
  99. try {
  100. OCP\Share::getItemSharedWithBySource('foobar', 'test.txt');
  101. $this->fail('Exception was expected: '.$message);
  102. } catch (Exception $exception) {
  103. $this->assertEquals($message, $exception->getMessage());
  104. }
  105. try {
  106. OCP\Share::getItemShared('foobar', 'test.txt');
  107. $this->fail('Exception was expected: '.$message);
  108. } catch (Exception $exception) {
  109. $this->assertEquals($message, $exception->getMessage());
  110. }
  111. try {
  112. OCP\Share::unshare('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2);
  113. $this->fail('Exception was expected: '.$message);
  114. } catch (Exception $exception) {
  115. $this->assertEquals($message, $exception->getMessage());
  116. }
  117. try {
  118. OCP\Share::setPermissions('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_UPDATE);
  119. $this->fail('Exception was expected: '.$message);
  120. } catch (Exception $exception) {
  121. $this->assertEquals($message, $exception->getMessage());
  122. }
  123. }
  124. protected function shareUserOneTestFileWithUserTwo() {
  125. OC_User::setUserId($this->user1);
  126. $this->assertTrue(
  127. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ),
  128. 'Failed asserting that user 1 successfully shared text.txt with user 2.'
  129. );
  130. $this->assertContains(
  131. 'test.txt',
  132. OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  133. 'Failed asserting that test.txt is a shared file of user 1.'
  134. );
  135. OC_User::setUserId($this->user2);
  136. $this->assertContains(
  137. 'test.txt',
  138. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  139. 'Failed asserting that user 2 has access to test.txt after initial sharing.'
  140. );
  141. }
  142. public function testShareWithUser() {
  143. // Invalid shares
  144. $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the item owner';
  145. try {
  146. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\PERMISSION_READ);
  147. $this->fail('Exception was expected: '.$message);
  148. } catch (Exception $exception) {
  149. $this->assertEquals($message, $exception->getMessage());
  150. }
  151. $message = 'Sharing test.txt failed, because the user foobar does not exist';
  152. try {
  153. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, 'foobar', OCP\PERMISSION_READ);
  154. $this->fail('Exception was expected: '.$message);
  155. } catch (Exception $exception) {
  156. $this->assertEquals($message, $exception->getMessage());
  157. }
  158. $message = 'Sharing foobar failed, because the sharing backend for test could not find its source';
  159. try {
  160. OCP\Share::shareItem('test', 'foobar', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ);
  161. $this->fail('Exception was expected: '.$message);
  162. } catch (Exception $exception) {
  163. $this->assertEquals($message, $exception->getMessage());
  164. }
  165. // Valid share
  166. $this->shareUserOneTestFileWithUserTwo();
  167. // Attempt to share again
  168. OC_User::setUserId($this->user1);
  169. $message = 'Sharing test.txt failed, because this item is already shared with '.$this->user2;
  170. try {
  171. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ);
  172. $this->fail('Exception was expected: '.$message);
  173. } catch (Exception $exception) {
  174. $this->assertEquals($message, $exception->getMessage());
  175. }
  176. // Attempt to share back
  177. OC_User::setUserId($this->user2);
  178. $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the original sharer';
  179. try {
  180. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\PERMISSION_READ);
  181. $this->fail('Exception was expected: '.$message);
  182. } catch (Exception $exception) {
  183. $this->assertEquals($message, $exception->getMessage());
  184. }
  185. // Unshare
  186. OC_User::setUserId($this->user1);
  187. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
  188. // Attempt reshare without share permission
  189. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ));
  190. OC_User::setUserId($this->user2);
  191. $message = 'Sharing test.txt failed, because resharing is not allowed';
  192. try {
  193. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\PERMISSION_READ);
  194. $this->fail('Exception was expected: '.$message);
  195. } catch (Exception $exception) {
  196. $this->assertEquals($message, $exception->getMessage());
  197. }
  198. // Owner grants share and update permission
  199. OC_User::setUserId($this->user1);
  200. $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE | OCP\PERMISSION_SHARE));
  201. // Attempt reshare with escalated permissions
  202. OC_User::setUserId($this->user2);
  203. $message = 'Sharing test.txt failed, because the permissions exceed permissions granted to '.$this->user2;
  204. try {
  205. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\PERMISSION_READ | OCP\PERMISSION_DELETE);
  206. $this->fail('Exception was expected: '.$message);
  207. } catch (Exception $exception) {
  208. $this->assertEquals($message, $exception->getMessage());
  209. }
  210. // Valid reshare
  211. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE));
  212. $this->assertEquals(array('test.txt'), OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
  213. OC_User::setUserId($this->user3);
  214. $this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
  215. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  216. // Attempt to escalate permissions
  217. OC_User::setUserId($this->user2);
  218. $message = 'Setting permissions for test.txt failed, because the permissions exceed permissions granted to '.$this->user2;
  219. try {
  220. OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\PERMISSION_READ | OCP\PERMISSION_DELETE);
  221. $this->fail('Exception was expected: '.$message);
  222. } catch (Exception $exception) {
  223. $this->assertEquals($message, $exception->getMessage());
  224. }
  225. // Remove update permission
  226. OC_User::setUserId($this->user1);
  227. $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ | OCP\PERMISSION_SHARE));
  228. OC_User::setUserId($this->user2);
  229. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_SHARE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  230. OC_User::setUserId($this->user3);
  231. $this->assertEquals(array(OCP\PERMISSION_READ), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  232. // Remove share permission
  233. OC_User::setUserId($this->user1);
  234. $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ));
  235. OC_User::setUserId($this->user2);
  236. $this->assertEquals(array(OCP\PERMISSION_READ), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  237. OC_User::setUserId($this->user3);
  238. $this->assertFalse(OCP\Share::getItemSharedWith('test', 'test.txt'));
  239. // Reshare again, and then have owner unshare
  240. OC_User::setUserId($this->user1);
  241. $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ | OCP\PERMISSION_SHARE));
  242. OC_User::setUserId($this->user2);
  243. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\PERMISSION_READ));
  244. OC_User::setUserId($this->user1);
  245. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
  246. OC_User::setUserId($this->user2);
  247. $this->assertFalse(OCP\Share::getItemSharedWith('test', 'test.txt'));
  248. OC_User::setUserId($this->user3);
  249. $this->assertFalse(OCP\Share::getItemSharedWith('test', 'test.txt'));
  250. // Attempt target conflict
  251. OC_User::setUserId($this->user1);
  252. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ));
  253. OC_User::setUserId($this->user3);
  254. $this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ));
  255. OC_User::setUserId($this->user2);
  256. $to_test = OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET);
  257. $this->assertEquals(2, count($to_test));
  258. $this->assertTrue(in_array('test.txt', $to_test));
  259. $this->assertTrue(in_array('test1.txt', $to_test));
  260. // Remove user
  261. OC_User::setUserId($this->user1);
  262. OC_User::deleteUser($this->user1);
  263. OC_User::setUserId($this->user2);
  264. $this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  265. }
  266. public function testShareWithUserExpirationExpired() {
  267. $this->shareUserOneTestFileWithUserTwo();
  268. OC_User::setUserId($this->user1);
  269. $this->assertTrue(
  270. OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInPast),
  271. 'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
  272. );
  273. OC_User::setUserId($this->user2);
  274. $this->assertFalse(
  275. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  276. 'Failed asserting that user 2 no longer has access to test.txt after expiration.'
  277. );
  278. }
  279. public function testShareWithUserExpirationValid() {
  280. $this->shareUserOneTestFileWithUserTwo();
  281. OC_User::setUserId($this->user1);
  282. $this->assertTrue(
  283. OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture),
  284. 'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
  285. );
  286. OC_User::setUserId($this->user2);
  287. $this->assertEquals(
  288. array('test.txt'),
  289. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  290. 'Failed asserting that user 2 still has access to test.txt after expiration date has been set.'
  291. );
  292. }
  293. protected function shareUserOneTestFileWithGroupOne() {
  294. OC_User::setUserId($this->user1);
  295. $this->assertTrue(
  296. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\PERMISSION_READ),
  297. 'Failed asserting that user 1 successfully shared text.txt with group 1.'
  298. );
  299. $this->assertContains(
  300. 'test.txt',
  301. OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  302. 'Failed asserting that test.txt is a shared file of user 1.'
  303. );
  304. OC_User::setUserId($this->user2);
  305. $this->assertContains(
  306. 'test.txt',
  307. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  308. 'Failed asserting that user 2 has access to test.txt after initial sharing.'
  309. );
  310. OC_User::setUserId($this->user3);
  311. $this->assertContains(
  312. 'test.txt',
  313. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  314. 'Failed asserting that user 3 has access to test.txt after initial sharing.'
  315. );
  316. }
  317. public function testShareWithGroup() {
  318. // Invalid shares
  319. $message = 'Sharing test.txt failed, because the group foobar does not exist';
  320. try {
  321. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, 'foobar', OCP\PERMISSION_READ);
  322. $this->fail('Exception was expected: '.$message);
  323. } catch (Exception $exception) {
  324. $this->assertEquals($message, $exception->getMessage());
  325. }
  326. $policy = OC_Appconfig::getValue('core', 'shareapi_share_policy', 'global');
  327. OC_Appconfig::setValue('core', 'shareapi_share_policy', 'groups_only');
  328. $message = 'Sharing test.txt failed, because '.$this->user1.' is not a member of the group '.$this->group2;
  329. try {
  330. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group2, OCP\PERMISSION_READ);
  331. $this->fail('Exception was expected: '.$message);
  332. } catch (Exception $exception) {
  333. $this->assertEquals($message, $exception->getMessage());
  334. }
  335. OC_Appconfig::setValue('core', 'shareapi_share_policy', $policy);
  336. // Valid share
  337. $this->shareUserOneTestFileWithGroupOne();
  338. // Attempt to share again
  339. OC_User::setUserId($this->user1);
  340. $message = 'Sharing test.txt failed, because this item is already shared with '.$this->group1;
  341. try {
  342. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\PERMISSION_READ);
  343. $this->fail('Exception was expected: '.$message);
  344. } catch (Exception $exception) {
  345. $this->assertEquals($message, $exception->getMessage());
  346. }
  347. // Attempt to share back to owner of group share
  348. OC_User::setUserId($this->user2);
  349. $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the original sharer';
  350. try {
  351. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\PERMISSION_READ);
  352. $this->fail('Exception was expected: '.$message);
  353. } catch (Exception $exception) {
  354. $this->assertEquals($message, $exception->getMessage());
  355. }
  356. // Attempt to share back to group
  357. $message = 'Sharing test.txt failed, because this item is already shared with '.$this->group1;
  358. try {
  359. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\PERMISSION_READ);
  360. $this->fail('Exception was expected: '.$message);
  361. } catch (Exception $exception) {
  362. $this->assertEquals($message, $exception->getMessage());
  363. }
  364. // Attempt to share back to member of group
  365. $message ='Sharing test.txt failed, because this item is already shared with '.$this->user3;
  366. try {
  367. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\PERMISSION_READ);
  368. $this->fail('Exception was expected: '.$message);
  369. } catch (Exception $exception) {
  370. $this->assertEquals($message, $exception->getMessage());
  371. }
  372. // Unshare
  373. OC_User::setUserId($this->user1);
  374. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1));
  375. // Valid share with same person - user then group
  376. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ | OCP\PERMISSION_DELETE | OCP\PERMISSION_SHARE));
  377. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE));
  378. OC_User::setUserId($this->user2);
  379. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  380. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE | OCP\PERMISSION_DELETE | OCP\PERMISSION_SHARE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  381. OC_User::setUserId($this->user3);
  382. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  383. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  384. // Valid reshare
  385. OC_User::setUserId($this->user2);
  386. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, OCP\PERMISSION_READ));
  387. OC_User::setUserId($this->user4);
  388. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  389. // Unshare from user only
  390. OC_User::setUserId($this->user1);
  391. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
  392. OC_User::setUserId($this->user2);
  393. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  394. OC_User::setUserId($this->user4);
  395. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  396. // Valid share with same person - group then user
  397. OC_User::setUserId($this->user1);
  398. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ | OCP\PERMISSION_DELETE));
  399. OC_User::setUserId($this->user2);
  400. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  401. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE | OCP\PERMISSION_DELETE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  402. // Unshare from group only
  403. OC_User::setUserId($this->user1);
  404. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1));
  405. OC_User::setUserId($this->user2);
  406. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_DELETE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  407. // Attempt user specific target conflict
  408. OC_User::setUserId($this->user3);
  409. $this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\PERMISSION_READ | OCP\PERMISSION_SHARE));
  410. OC_User::setUserId($this->user2);
  411. $to_test = OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET);
  412. $this->assertEquals(2, count($to_test));
  413. $this->assertTrue(in_array('test.txt', $to_test));
  414. $this->assertTrue(in_array('test1.txt', $to_test));
  415. // Valid reshare
  416. $this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, OCP\PERMISSION_READ | OCP\PERMISSION_SHARE));
  417. OC_User::setUserId($this->user4);
  418. $this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  419. // Remove user from group
  420. OC_Group::removeFromGroup($this->user2, $this->group1);
  421. OC_User::setUserId($this->user2);
  422. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  423. OC_User::setUserId($this->user4);
  424. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  425. // Add user to group
  426. OC_Group::addToGroup($this->user4, $this->group1);
  427. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  428. // Unshare from self
  429. $this->assertTrue(OCP\Share::unshareFromSelf('test', 'test.txt'));
  430. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  431. OC_User::setUserId($this->user2);
  432. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  433. // Remove group
  434. OC_Group::deleteGroup($this->group1);
  435. OC_User::setUserId($this->user4);
  436. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  437. OC_User::setUserId($this->user3);
  438. $this->assertEquals(array(), OCP\Share::getItemsShared('test'));
  439. }
  440. public function testShareWithGroupExpirationExpired() {
  441. $this->shareUserOneTestFileWithGroupOne();
  442. OC_User::setUserId($this->user1);
  443. $this->assertTrue(
  444. OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInPast),
  445. 'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
  446. );
  447. OC_User::setUserId($this->user2);
  448. $this->assertFalse(
  449. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  450. 'Failed asserting that user 2 no longer has access to test.txt after expiration.'
  451. );
  452. OC_User::setUserId($this->user3);
  453. $this->assertFalse(
  454. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  455. 'Failed asserting that user 3 no longer has access to test.txt after expiration.'
  456. );
  457. }
  458. public function testShareWithGroupExpirationValid() {
  459. $this->shareUserOneTestFileWithGroupOne();
  460. OC_User::setUserId($this->user1);
  461. $this->assertTrue(
  462. OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture),
  463. 'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
  464. );
  465. OC_User::setUserId($this->user2);
  466. $this->assertEquals(
  467. array('test.txt'),
  468. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  469. 'Failed asserting that user 2 still has access to test.txt after expiration date has been set.'
  470. );
  471. OC_User::setUserId($this->user3);
  472. $this->assertEquals(
  473. array('test.txt'),
  474. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  475. 'Failed asserting that user 3 still has access to test.txt after expiration date has been set.'
  476. );
  477. }
  478. protected function getShareByValidToken($token) {
  479. $row = OCP\Share::getShareByToken($token);
  480. $this->assertInternalType(
  481. 'array',
  482. $row,
  483. "Failed asserting that a share for token $token exists."
  484. );
  485. return $row;
  486. }
  487. public function testShareItemWithLink() {
  488. OC_User::setUserId($this->user1);
  489. $token = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, OCP\PERMISSION_READ);
  490. $this->assertInternalType(
  491. 'string',
  492. $token,
  493. 'Failed asserting that user 1 successfully shared text.txt as link with token.'
  494. );
  495. // testGetShareByTokenNoExpiration
  496. $row = $this->getShareByValidToken($token);
  497. $this->assertEmpty(
  498. $row['expiration'],
  499. 'Failed asserting that the returned row does not have an expiration date.'
  500. );
  501. // testGetShareByTokenExpirationValid
  502. $this->assertTrue(
  503. OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture),
  504. 'Failed asserting that user 1 successfully set a future expiration date for the test.txt share.'
  505. );
  506. $row = $this->getShareByValidToken($token);
  507. $this->assertNotEmpty(
  508. $row['expiration'],
  509. 'Failed asserting that the returned row has an expiration date.'
  510. );
  511. // testGetShareByTokenExpirationExpired
  512. $this->assertTrue(
  513. OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInPast),
  514. 'Failed asserting that user 1 successfully set a past expiration date for the test.txt share.'
  515. );
  516. $this->assertFalse(
  517. OCP\Share::getShareByToken($token),
  518. 'Failed asserting that an expired share could not be found.'
  519. );
  520. }
  521. public function testUnshareAll() {
  522. $this->shareUserOneTestFileWithUserTwo();
  523. $this->shareUserOneTestFileWithGroupOne();
  524. OC_User::setUserId($this->user1);
  525. $this->assertEquals(
  526. array('test.txt', 'test.txt'),
  527. OCP\Share::getItemsShared('test', 'test.txt'),
  528. 'Failed asserting that the test.txt file is shared exactly two times.'
  529. );
  530. $this->assertTrue(
  531. OCP\Share::unshareAll('test', 'test.txt'),
  532. 'Failed asserting that user 1 successfully unshared all shares of the test.txt share.'
  533. );
  534. $this->assertEquals(
  535. array(),
  536. OCP\Share::getItemsShared('test'),
  537. 'Failed asserting that both shares of the test.txt file have been removed.'
  538. );
  539. }
  540. }