ApiTest.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  9. * @author Georg Ehrke <oc.list@georgehrke.com>
  10. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  11. * @author Joas Schilling <coding@schilljs.com>
  12. * @author Julius Härtl <jus@bitgrid.net>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Richard Steinmetz <richard@steinmetz.cloud>
  15. * @author Robin Appelman <robin@icewind.nl>
  16. * @author Robin McCorkell <robin@mccorkell.me.uk>
  17. * @author Roeland Jago Douma <roeland@famdouma.nl>
  18. * @author Thomas Müller <thomas.mueller@tmit.eu>
  19. * @author Vincent Petry <vincent@nextcloud.com>
  20. *
  21. * @license AGPL-3.0
  22. *
  23. * This code is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License, version 3,
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License, version 3,
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>
  34. *
  35. */
  36. namespace OCA\Files_Sharing\Tests;
  37. use OC\Files\Cache\Scanner;
  38. use OCA\Files_Sharing\Controller\ShareAPIController;
  39. use OCP\App\IAppManager;
  40. use OCP\AppFramework\OCS\OCSBadRequestException;
  41. use OCP\AppFramework\OCS\OCSException;
  42. use OCP\AppFramework\OCS\OCSForbiddenException;
  43. use OCP\AppFramework\OCS\OCSNotFoundException;
  44. use OCP\IConfig;
  45. use OCP\IL10N;
  46. use OCP\IPreview;
  47. use OCP\IRequest;
  48. use OCP\IServerContainer;
  49. use OCP\Share\IShare;
  50. use OCP\UserStatus\IManager as IUserStatusManager;
  51. /**
  52. * Class ApiTest
  53. *
  54. * @group DB
  55. * TODO: convert to real intergration tests
  56. */
  57. class ApiTest extends TestCase {
  58. public const TEST_FOLDER_NAME = '/folder_share_api_test';
  59. public const APP_NAME = 'files_sharing';
  60. private static $tempStorage;
  61. /** @var \OCP\Files\Folder */
  62. private $userFolder;
  63. /** @var string */
  64. private $subsubfolder;
  65. protected function setUp(): void {
  66. parent::setUp();
  67. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no');
  68. \OC::$server->getConfig()->setAppValue('core', 'shareapi_expire_after_n_days', '7');
  69. $this->folder = self::TEST_FOLDER_NAME;
  70. $this->subfolder = '/subfolder_share_api_test';
  71. $this->subsubfolder = '/subsubfolder_share_api_test';
  72. $this->filename = '/share-api-test.txt';
  73. // save file with content
  74. $this->view->file_put_contents($this->filename, $this->data);
  75. $this->view->mkdir($this->folder);
  76. $this->view->mkdir($this->folder . $this->subfolder);
  77. $this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
  78. $this->view->file_put_contents($this->folder.$this->filename, $this->data);
  79. $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
  80. $mount = $this->view->getMount($this->filename);
  81. $mount->getStorage()->getScanner()->scan('', Scanner::SCAN_RECURSIVE);
  82. $this->userFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  83. }
  84. protected function tearDown(): void {
  85. if ($this->view instanceof \OC\Files\View) {
  86. $this->view->unlink($this->filename);
  87. $this->view->deleteAll($this->folder);
  88. }
  89. self::$tempStorage = null;
  90. parent::tearDown();
  91. }
  92. /**
  93. * @param string $userId The userId of the caller
  94. * @return \OCA\Files_Sharing\Controller\ShareAPIController
  95. */
  96. private function createOCS($userId) {
  97. $l = $this->getMockBuilder(IL10N::class)->getMock();
  98. $l->method('t')
  99. ->willReturnCallback(function ($text, $parameters = []) {
  100. return vsprintf($text, $parameters);
  101. });
  102. $config = $this->createMock(IConfig::class);
  103. $appManager = $this->createMock(IAppManager::class);
  104. $serverContainer = $this->createMock(IServerContainer::class);
  105. $userStatusManager = $this->createMock(IUserStatusManager::class);
  106. $previewManager = $this->createMock(IPreview::class);
  107. return new ShareAPIController(
  108. self::APP_NAME,
  109. $this->getMockBuilder(IRequest::class)->getMock(),
  110. $this->shareManager,
  111. \OC::$server->getGroupManager(),
  112. \OC::$server->getUserManager(),
  113. \OC::$server->getRootFolder(),
  114. \OC::$server->getURLGenerator(),
  115. $userId,
  116. $l,
  117. $config,
  118. $appManager,
  119. $serverContainer,
  120. $userStatusManager,
  121. $previewManager
  122. );
  123. }
  124. public function testCreateShareUserFile() {
  125. $this->setUp(); // for some reasons phpunit refuses to do this for us only for this test
  126. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  127. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  128. $ocs->cleanup();
  129. $data = $result->getData();
  130. $this->assertEquals(19, $data['permissions']);
  131. $this->assertEmpty($data['expiration']);
  132. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  133. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  134. $ocs->deleteShare($data['id']);
  135. $ocs->cleanup();
  136. }
  137. public function testCreateShareUserFolder() {
  138. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  139. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  140. $ocs->cleanup();
  141. $data = $result->getData();
  142. $this->assertEquals(31, $data['permissions']);
  143. $this->assertEmpty($data['expiration']);
  144. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  145. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  146. $ocs->deleteShare($data['id']);
  147. $ocs->cleanup();
  148. }
  149. public function testCreateShareGroupFile() {
  150. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  151. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1);
  152. $ocs->cleanup();
  153. $data = $result->getData();
  154. $this->assertEquals(19, $data['permissions']);
  155. $this->assertEmpty($data['expiration']);
  156. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  157. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  158. $ocs->deleteShare($data['id']);
  159. $ocs->cleanup();
  160. }
  161. public function testCreateShareGroupFolder() {
  162. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  163. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1);
  164. $ocs->cleanup();
  165. $data = $result->getData();
  166. $this->assertEquals(31, $data['permissions']);
  167. $this->assertEmpty($data['expiration']);
  168. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  169. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  170. $ocs->deleteShare($data['id']);
  171. $ocs->cleanup();
  172. }
  173. public function testCreateShareLink() {
  174. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  175. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  176. $ocs->cleanup();
  177. $data = $result->getData();
  178. $this->assertEquals(\OCP\Constants::PERMISSION_READ |
  179. \OCP\Constants::PERMISSION_SHARE,
  180. $data['permissions']);
  181. $this->assertEmpty($data['expiration']);
  182. $this->assertTrue(is_string($data['token']));
  183. // check for correct link
  184. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  185. $this->assertEquals($url, $data['url']);
  186. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  187. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  188. $ocs->deleteShare($data['id']);
  189. $ocs->cleanup();
  190. }
  191. public function testCreateShareLinkPublicUpload() {
  192. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  193. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
  194. $ocs->cleanup();
  195. $data = $result->getData();
  196. $this->assertEquals(
  197. \OCP\Constants::PERMISSION_READ |
  198. \OCP\Constants::PERMISSION_CREATE |
  199. \OCP\Constants::PERMISSION_UPDATE |
  200. \OCP\Constants::PERMISSION_DELETE |
  201. \OCP\Constants::PERMISSION_SHARE,
  202. $data['permissions']
  203. );
  204. $this->assertEmpty($data['expiration']);
  205. $this->assertTrue(is_string($data['token']));
  206. // check for correct link
  207. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  208. $this->assertEquals($url, $data['url']);
  209. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  210. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  211. $ocs->deleteShare($data['id']);
  212. $ocs->cleanup();
  213. }
  214. public function testEnforceLinkPassword() {
  215. $password = md5(time());
  216. $config = \OC::$server->getConfig();
  217. $config->setAppValue('core', 'shareapi_enforce_links_password', 'yes');
  218. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  219. try {
  220. $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  221. $this->fail();
  222. } catch (OCSForbiddenException $e) {
  223. }
  224. $ocs->cleanup();
  225. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  226. try {
  227. $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '');
  228. $this->fail();
  229. } catch (OCSForbiddenException $e) {
  230. }
  231. $ocs->cleanup();
  232. // share with password should succeed
  233. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  234. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', $password);
  235. $ocs->cleanup();
  236. $data = $result->getData();
  237. // setting new password should succeed
  238. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  239. $ocs->updateShare($data['id'], null, $password);
  240. $ocs->cleanup();
  241. // removing password should fail
  242. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  243. try {
  244. $ocs->updateShare($data['id']);
  245. $this->fail();
  246. } catch (OCSBadRequestException $e) {
  247. }
  248. $ocs->cleanup();
  249. // cleanup
  250. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  251. $ocs->deleteShare($data['id']);
  252. $ocs->cleanup();
  253. $config->setAppValue('core', 'shareapi_enforce_links_password', 'no');
  254. $this->addToAssertionCount(1);
  255. }
  256. /**
  257. * @medium
  258. */
  259. public function testSharePermissions() {
  260. // sharing file to a user should work if shareapi_exclude_groups is set
  261. // to no
  262. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no');
  263. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  264. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  265. $ocs->cleanup();
  266. $data = $result->getData();
  267. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  268. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  269. $ocs->deleteShare($data['id']);
  270. $ocs->cleanup();
  271. // exclude groups, but not the group the user belongs to. Sharing should still work
  272. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'yes');
  273. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups_list', 'admin,group1,group2');
  274. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  275. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  276. $ocs->cleanup();
  277. $data = $result->getData();
  278. $this->shareManager->getShareById('ocinternal:' . $data['id']);
  279. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  280. $ocs->deleteShare($data['id']);
  281. $ocs->cleanup();
  282. // now we exclude the group the user belongs to ('group'), sharing should fail now
  283. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups_list', 'admin,group');
  284. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  285. $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  286. $ocs->cleanup();
  287. // cleanup
  288. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no');
  289. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups_list', '');
  290. $this->addToAssertionCount(1);
  291. }
  292. /**
  293. * @medium
  294. */
  295. public function testGetAllShares() {
  296. $node = $this->userFolder->get($this->filename);
  297. $share = $this->shareManager->newShare();
  298. $share->setNode($node)
  299. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  300. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  301. ->setShareType(IShare::TYPE_USER)
  302. ->setPermissions(19);
  303. $share = $this->shareManager->createShare($share);
  304. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  305. $result = $ocs->getShares();
  306. $ocs->cleanup();
  307. $this->assertTrue(count($result->getData()) === 1);
  308. $this->shareManager->deleteShare($share);
  309. }
  310. public function testGetAllSharesWithMe() {
  311. $node1 = $this->userFolder->get($this->filename);
  312. $share1 = $this->shareManager->newShare();
  313. $share1->setNode($node1)
  314. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  315. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  316. ->setShareType(IShare::TYPE_USER)
  317. ->setPermissions(19);
  318. $share1 = $this->shareManager->createShare($share1);
  319. $share1->setStatus(IShare::STATUS_ACCEPTED);
  320. $this->shareManager->updateShare($share1);
  321. $node2 = $this->userFolder->get($this->folder);
  322. $share2 = $this->shareManager->newShare();
  323. $share2->setNode($node2)
  324. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  325. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  326. ->setShareType(IShare::TYPE_USER)
  327. ->setPermissions(31);
  328. $share2 = $this->shareManager->createShare($share2);
  329. $share2->setStatus(IShare::STATUS_ACCEPTED);
  330. $this->shareManager->updateShare($share2);
  331. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  332. $result = $ocs->getShares('true');
  333. $ocs->cleanup();
  334. $this->assertCount(2, $result->getData());
  335. $this->shareManager->deleteShare($share1);
  336. $this->shareManager->deleteShare($share2);
  337. }
  338. /**
  339. * @medium
  340. */
  341. public function testPublicLinkUrl() {
  342. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  343. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  344. $ocs->cleanup();
  345. $data = $result->getData();
  346. // check if we have a token
  347. $this->assertTrue(is_string($data['token']));
  348. $id = $data['id'];
  349. // check for correct link
  350. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  351. $this->assertEquals($url, $data['url']);
  352. // check for link in getall shares
  353. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  354. $result = $ocs->getShares();
  355. $ocs->cleanup();
  356. $data = $result->getData();
  357. $this->assertEquals($url, current($data)['url']);
  358. // check for path
  359. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  360. $result = $ocs->getShares();
  361. $ocs->cleanup();
  362. $data = $result->getData();
  363. $this->assertEquals($url, current($data)['url']);
  364. // check in share id
  365. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  366. $result = $ocs->getShare($id);
  367. $ocs->cleanup();
  368. $data = $result->getData();
  369. $this->assertEquals($url, current($data)['url']);
  370. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  371. $ocs->deleteShare($id);
  372. $ocs->cleanup();
  373. }
  374. /**
  375. * @medium
  376. * @depends testCreateShareUserFile
  377. * @depends testCreateShareLink
  378. */
  379. public function testGetShareFromSource() {
  380. $node = $this->userFolder->get($this->filename);
  381. $share = $this->shareManager->newShare();
  382. $share->setNode($node)
  383. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  384. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  385. ->setShareType(IShare::TYPE_USER)
  386. ->setPermissions(19);
  387. $share1 = $this->shareManager->createShare($share);
  388. $share = $this->shareManager->newShare();
  389. $share->setNode($node)
  390. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  391. ->setShareType(IShare::TYPE_LINK)
  392. ->setPermissions(1);
  393. $share2 = $this->shareManager->createShare($share);
  394. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  395. $result = $ocs->getShares();
  396. $ocs->cleanup();
  397. // test should return one share created from testCreateShare()
  398. $this->assertTrue(count($result->getData()) === 2);
  399. $this->shareManager->deleteShare($share1);
  400. $this->shareManager->deleteShare($share2);
  401. }
  402. /**
  403. * @medium
  404. * @depends testCreateShareUserFile
  405. * @depends testCreateShareLink
  406. */
  407. public function testGetShareFromSourceWithReshares() {
  408. $node = $this->userFolder->get($this->filename);
  409. $share1 = $this->shareManager->newShare();
  410. $share1->setNode($node)
  411. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  412. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  413. ->setShareType(IShare::TYPE_USER)
  414. ->setPermissions(19);
  415. $share1 = $this->shareManager->createShare($share1);
  416. $share2 = $this->shareManager->newShare();
  417. $share2->setNode($node)
  418. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  419. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  420. ->setShareType(IShare::TYPE_USER)
  421. ->setPermissions(19);
  422. $share2 = $this->shareManager->createShare($share2);
  423. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  424. $result = $ocs->getShares();
  425. $ocs->cleanup();
  426. // test should return one share
  427. $this->assertTrue(count($result->getData()) === 1);
  428. // now also ask for the reshares
  429. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  430. $result = $ocs->getShares('false', 'true', 'false', $this->filename);
  431. $ocs->cleanup();
  432. // now we should get two shares, the initial share and the reshare
  433. $this->assertCount(2, $result->getData());
  434. $this->shareManager->deleteShare($share1);
  435. $this->shareManager->deleteShare($share2);
  436. }
  437. /**
  438. * @medium
  439. * @depends testCreateShareUserFile
  440. */
  441. public function testGetShareFromId() {
  442. $node = $this->userFolder->get($this->filename);
  443. $share1 = $this->shareManager->newShare();
  444. $share1->setNode($node)
  445. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  446. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  447. ->setShareType(IShare::TYPE_USER)
  448. ->setPermissions(19);
  449. $share1 = $this->shareManager->createShare($share1);
  450. // call getShare() with share ID
  451. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  452. $result = $ocs->getShare($share1->getId());
  453. $ocs->cleanup();
  454. // test should return one share created from testCreateShare()
  455. $this->assertEquals(1, count($result->getData()));
  456. $this->shareManager->deleteShare($share1);
  457. }
  458. /**
  459. * @medium
  460. */
  461. public function testGetShareFromFolder() {
  462. $node1 = $this->userFolder->get($this->filename);
  463. $share1 = $this->shareManager->newShare();
  464. $share1->setNode($node1)
  465. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  466. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  467. ->setShareType(IShare::TYPE_USER)
  468. ->setPermissions(19);
  469. $share1 = $this->shareManager->createShare($share1);
  470. $node2 = $this->userFolder->get($this->folder.'/'.$this->filename);
  471. $share2 = $this->shareManager->newShare();
  472. $share2->setNode($node2)
  473. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  474. ->setShareType(IShare::TYPE_LINK)
  475. ->setPermissions(1);
  476. $share2 = $this->shareManager->createShare($share2);
  477. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  478. $result = $ocs->getShares('false', 'false', 'true', $this->folder);
  479. $ocs->cleanup();
  480. // test should return one share within $this->folder
  481. $this->assertTrue(count($result->getData()) === 1);
  482. $this->shareManager->deleteShare($share1);
  483. $this->shareManager->deleteShare($share2);
  484. }
  485. public function testGetShareFromFolderWithFile() {
  486. $node1 = $this->userFolder->get($this->filename);
  487. $share1 = $this->shareManager->newShare();
  488. $share1->setNode($node1)
  489. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  490. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  491. ->setShareType(IShare::TYPE_USER)
  492. ->setPermissions(19);
  493. $share1 = $this->shareManager->createShare($share1);
  494. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  495. try {
  496. $ocs->getShares('false', 'false', 'true', $this->filename);
  497. $this->fail();
  498. } catch (OCSBadRequestException $e) {
  499. $this->assertEquals('Not a directory', $e->getMessage());
  500. }
  501. $ocs->cleanup();
  502. $this->shareManager->deleteShare($share1);
  503. }
  504. /**
  505. * share a folder, than reshare a file within the shared folder and check if we construct the correct path
  506. * @medium
  507. */
  508. public function testGetShareFromFolderReshares() {
  509. $node1 = $this->userFolder->get($this->folder);
  510. $share1 = $this->shareManager->newShare();
  511. $share1->setNode($node1)
  512. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  513. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  514. ->setShareType(IShare::TYPE_USER)
  515. ->setPermissions(31);
  516. $share1 = $this->shareManager->createShare($share1);
  517. $share1->setStatus(IShare::STATUS_ACCEPTED);
  518. $this->shareManager->updateShare($share1);
  519. $node2 = $this->userFolder->get($this->folder.'/'.$this->filename);
  520. $share2 = $this->shareManager->newShare();
  521. $share2->setNode($node2)
  522. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  523. ->setShareType(IShare::TYPE_LINK)
  524. ->setPermissions(1);
  525. $share2 = $this->shareManager->createShare($share2);
  526. $share2->setStatus(IShare::STATUS_ACCEPTED);
  527. $this->shareManager->updateShare($share2);
  528. $node3 = $this->userFolder->get($this->folder.'/'.$this->subfolder.'/'.$this->filename);
  529. $share3 = $this->shareManager->newShare();
  530. $share3->setNode($node3)
  531. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  532. ->setShareType(IShare::TYPE_LINK)
  533. ->setPermissions(1);
  534. $share3 = $this->shareManager->createShare($share3);
  535. $share3->setStatus(IShare::STATUS_ACCEPTED);
  536. $this->shareManager->updateShare($share3);
  537. $testValues = [
  538. ['query' => $this->folder,
  539. 'expectedResult' => $this->folder . $this->filename],
  540. ['query' => $this->folder . $this->subfolder,
  541. 'expectedResult' => $this->folder . $this->subfolder . $this->filename],
  542. ];
  543. foreach ($testValues as $value) {
  544. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  545. $result = $ocs->getShares('false', 'false', 'true', $value['query']);
  546. $ocs->cleanup();
  547. // test should return one share within $this->folder
  548. $data = $result->getData();
  549. $this->assertEquals($value['expectedResult'], $data[0]['path']);
  550. }
  551. // cleanup
  552. $this->shareManager->deleteShare($share1);
  553. $this->shareManager->deleteShare($share2);
  554. $this->shareManager->deleteShare($share3);
  555. }
  556. /**
  557. * reshare a sub folder and check if we get the correct path
  558. * @medium
  559. */
  560. public function testGetShareFromSubFolderReShares() {
  561. $node1 = $this->userFolder->get($this->folder . $this->subfolder);
  562. $share1 = $this->shareManager->newShare();
  563. $share1->setNode($node1)
  564. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  565. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  566. ->setShareType(IShare::TYPE_USER)
  567. ->setPermissions(31);
  568. $share1 = $this->shareManager->createShare($share1);
  569. $share1->setStatus(IShare::STATUS_ACCEPTED);
  570. $this->shareManager->updateShare($share1);
  571. $node2 = \OC::$server->getRootFolder()->getUserFolder(self::TEST_FILES_SHARING_API_USER2)->get($this->subfolder);
  572. $share2 = $this->shareManager->newShare();
  573. $share2->setNode($node2)
  574. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  575. ->setShareType(IShare::TYPE_LINK)
  576. ->setPermissions(1);
  577. $share2 = $this->shareManager->createShare($share2);
  578. $share2->setStatus(IShare::STATUS_ACCEPTED);
  579. $this->shareManager->updateShare($share2);
  580. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  581. $result = $ocs->getShares();
  582. $ocs->cleanup();
  583. // test should return one share within $this->folder
  584. $data = $result->getData();
  585. // we should get exactly one result
  586. $this->assertCount(1, $data);
  587. $this->assertEquals($this->subfolder, $data[0]['path']);
  588. $this->shareManager->deleteShare($share2);
  589. $this->shareManager->deleteShare($share1);
  590. }
  591. /**
  592. * test re-re-share of folder if the path gets constructed correctly
  593. * @medium
  594. */
  595. public function XtestGetShareFromFolderReReShares() {
  596. $node1 = $this->userFolder->get($this->folder . $this->subfolder);
  597. $share1 = $this->shareManager->newShare();
  598. $share1->setNode($node1)
  599. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  600. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  601. ->setShareType(IShare::TYPE_USER)
  602. ->setPermissions(31);
  603. $share1 = $this->shareManager->createShare($share1);
  604. $node2 = $this->userFolder->get($this->folder . $this->subfolder . $this->subsubfolder);
  605. $share2 = $this->shareManager->newShare();
  606. $share2->setNode($node2)
  607. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  608. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  609. ->setShareType(IShare::TYPE_USER)
  610. ->setPermissions(31);
  611. $share2 = $this->shareManager->createShare($share2);
  612. $share3 = $this->shareManager->newShare();
  613. $share3->setNode($node2)
  614. ->setSharedBy(self::TEST_FILES_SHARING_API_USER3)
  615. ->setShareType(IShare::TYPE_LINK)
  616. ->setPermissions(1);
  617. $share3 = $this->shareManager->createShare($share3);
  618. /*
  619. * Test as recipient
  620. */
  621. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER3);
  622. $result = $ocs->getShares();
  623. $ocs->cleanup();
  624. // test should return one share within $this->folder
  625. $data = $result->getData();
  626. // we should get exactly one result
  627. $this->assertCount(1, $data);
  628. $this->assertEquals($this->subsubfolder, $data[0]['path']);
  629. /*
  630. * Test for first owner/initiator
  631. */
  632. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  633. $result = $ocs->getShares();
  634. $ocs->cleanup();
  635. // test should return one share within $this->folder
  636. $data = $result->getData();
  637. // we should get exactly one result
  638. $this->assertCount(1, $data);
  639. $this->assertEquals($this->folder . $this->subfolder, $data[0]['path']);
  640. /*
  641. * Test for second initiator
  642. */
  643. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  644. $result = $ocs->getShares();
  645. $ocs->cleanup();
  646. // test should return one share within $this->folder
  647. $data = $result->getData();
  648. // we should get exactly one result
  649. $this->assertCount(1, $data);
  650. $this->assertEquals($this->subfolder . $this->subsubfolder, $data[0]['path']);
  651. $this->shareManager->deleteShare($share1);
  652. $this->shareManager->deleteShare($share2);
  653. $this->shareManager->deleteShare($share3);
  654. }
  655. /**
  656. * test multiple shared folder if the path gets constructed correctly
  657. * @medium
  658. */
  659. public function testGetShareMultipleSharedFolder() {
  660. $this->setUp();
  661. $node1 = $this->userFolder->get($this->folder . $this->subfolder);
  662. $share1 = $this->shareManager->newShare();
  663. $share1->setNode($node1)
  664. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  665. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  666. ->setShareType(IShare::TYPE_USER)
  667. ->setPermissions(31);
  668. $share1 = $this->shareManager->createShare($share1);
  669. $share1->setStatus(IShare::STATUS_ACCEPTED);
  670. $this->shareManager->updateShare($share1);
  671. $node2 = $this->userFolder->get($this->folder);
  672. $share2 = $this->shareManager->newShare();
  673. $share2->setNode($node2)
  674. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  675. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  676. ->setShareType(IShare::TYPE_USER)
  677. ->setPermissions(31);
  678. $share2 = $this->shareManager->createShare($share2);
  679. $share2->setStatus(IShare::STATUS_ACCEPTED);
  680. $this->shareManager->updateShare($share2);
  681. $share3 = $this->shareManager->newShare();
  682. $share3->setNode($node1)
  683. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  684. ->setShareType(IShare::TYPE_LINK)
  685. ->setPermissions(1);
  686. $share3 = $this->shareManager->createShare($share3);
  687. $share3->setStatus(IShare::STATUS_ACCEPTED);
  688. $this->shareManager->updateShare($share3);
  689. // $request = $this->createRequest(['path' => $this->subfolder]);
  690. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  691. $result1 = $ocs->getShares('false','false','false', $this->subfolder);
  692. $ocs->cleanup();
  693. // test should return one share within $this->folder
  694. $data1 = $result1->getData();
  695. $this->assertCount(1, $data1);
  696. $s1 = reset($data1);
  697. //$request = $this->createRequest(['path' => $this->folder.$this->subfolder]);
  698. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  699. $result2 = $ocs->getShares('false', 'false', 'false', $this->folder . $this->subfolder);
  700. $ocs->cleanup();
  701. // test should return one share within $this->folder
  702. $data2 = $result2->getData();
  703. $this->assertCount(1, $data2);
  704. $s2 = reset($data2);
  705. $this->assertEquals($this->subfolder, $s1['path']);
  706. $this->assertEquals($this->folder.$this->subfolder, $s2['path']);
  707. $this->shareManager->deleteShare($share1);
  708. $this->shareManager->deleteShare($share2);
  709. $this->shareManager->deleteShare($share3);
  710. }
  711. /**
  712. * test re-re-share of folder if the path gets constructed correctly
  713. * @medium
  714. */
  715. public function testGetShareFromFileReReShares() {
  716. $node1 = $this->userFolder->get($this->folder . $this->subfolder);
  717. $share1 = $this->shareManager->newShare();
  718. $share1->setNode($node1)
  719. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  720. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  721. ->setShareType(IShare::TYPE_USER)
  722. ->setPermissions(31);
  723. $share1 = $this->shareManager->createShare($share1);
  724. $share1->setStatus(IShare::STATUS_ACCEPTED);
  725. $this->shareManager->updateShare($share1);
  726. $user2Folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER2);
  727. $node2 = $user2Folder->get($this->subfolder . $this->filename);
  728. $share2 = $this->shareManager->newShare();
  729. $share2->setNode($node2)
  730. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  731. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  732. ->setShareType(IShare::TYPE_USER)
  733. ->setPermissions(19);
  734. $share2 = $this->shareManager->createShare($share2);
  735. $share2->setStatus(IShare::STATUS_ACCEPTED);
  736. $this->shareManager->updateShare($share2);
  737. $user3Folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER3);
  738. $node3 = $user3Folder->get($this->filename);
  739. $share3 = $this->shareManager->newShare();
  740. $share3->setNode($node3)
  741. ->setSharedBy(self::TEST_FILES_SHARING_API_USER3)
  742. ->setShareType(IShare::TYPE_LINK)
  743. ->setPermissions(1);
  744. $share3 = $this->shareManager->createShare($share3);
  745. $share3->setStatus(IShare::STATUS_ACCEPTED);
  746. $this->shareManager->updateShare($share3);
  747. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER3);
  748. $result = $ocs->getShares();
  749. $ocs->cleanup();
  750. // test should return one share within $this->folder
  751. $data = $result->getData();
  752. // we should get exactly one result
  753. $this->assertCount(1, $data);
  754. $this->assertEquals($this->filename, $data[0]['path']);
  755. $this->shareManager->deleteShare($share1);
  756. $this->shareManager->deleteShare($share2);
  757. $this->shareManager->deleteShare($share3);
  758. }
  759. /**
  760. * @medium
  761. */
  762. public function testGetShareFromUnknownId() {
  763. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER3);
  764. try {
  765. $ocs->getShare(0);
  766. $this->fail();
  767. } catch (OCSNotFoundException $e) {
  768. $this->assertEquals('Wrong share ID, share doesn\'t exist', $e->getMessage());
  769. }
  770. $ocs->cleanup();
  771. }
  772. /**
  773. * @medium
  774. * @depends testCreateShareUserFile
  775. * @depends testCreateShareLink
  776. */
  777. public function testUpdateShare() {
  778. $password = md5(time());
  779. $node1 = $this->userFolder->get($this->filename);
  780. $share1 = $this->shareManager->newShare();
  781. $share1->setNode($node1)
  782. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  783. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  784. ->setShareType(IShare::TYPE_USER)
  785. ->setPermissions(19);
  786. $share1 = $this->shareManager->createShare($share1);
  787. $share2 = $this->shareManager->newShare();
  788. $share2->setNode($node1)
  789. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  790. ->setShareType(IShare::TYPE_LINK)
  791. ->setPermissions(1);
  792. $share2 = $this->shareManager->createShare($share2);
  793. // update permissions
  794. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  795. $ocs->updateShare($share1->getId(), 1);
  796. $ocs->cleanup();
  797. $share1 = $this->shareManager->getShareById('ocinternal:' . $share1->getId());
  798. $this->assertEquals(1, $share1->getPermissions());
  799. // update password for link share
  800. $this->assertNull($share2->getPassword());
  801. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  802. $ocs->updateShare($share2->getId(), null, $password);
  803. $ocs->cleanup();
  804. $share2 = $this->shareManager->getShareById('ocinternal:' . $share2->getId());
  805. $this->assertNotNull($share2->getPassword());
  806. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  807. $ocs->updateShare($share2->getId(), null, '');
  808. $ocs->cleanup();
  809. $share2 = $this->shareManager->getShareById('ocinternal:' . $share2->getId());
  810. $this->assertNull($share2->getPassword());
  811. $this->shareManager->deleteShare($share1);
  812. $this->shareManager->deleteShare($share2);
  813. }
  814. /**
  815. * @medium
  816. */
  817. public function testUpdateShareUpload() {
  818. $node1 = $this->userFolder->get($this->folder);
  819. $share1 = $this->shareManager->newShare();
  820. $share1->setNode($node1)
  821. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  822. ->setShareType(IShare::TYPE_LINK)
  823. ->setPermissions(1);
  824. $share1 = $this->shareManager->createShare($share1);
  825. // update public upload
  826. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  827. $ocs->updateShare($share1->getId(), null, null, null, 'true');
  828. $ocs->cleanup();
  829. $share1 = $this->shareManager->getShareById($share1->getFullId());
  830. $this->assertEquals(
  831. \OCP\Constants::PERMISSION_READ |
  832. \OCP\Constants::PERMISSION_CREATE |
  833. \OCP\Constants::PERMISSION_UPDATE |
  834. \OCP\Constants::PERMISSION_DELETE |
  835. \OCP\Constants::PERMISSION_SHARE,
  836. $share1->getPermissions()
  837. );
  838. // cleanup
  839. $this->shareManager->deleteShare($share1);
  840. }
  841. /**
  842. * @medium
  843. */
  844. public function testUpdateShareExpireDate() {
  845. $node1 = $this->userFolder->get($this->folder);
  846. $share1 = $this->shareManager->newShare();
  847. $share1->setNode($node1)
  848. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  849. ->setShareType(IShare::TYPE_LINK)
  850. ->setPermissions(1);
  851. $share1 = $this->shareManager->createShare($share1);
  852. $share1->setStatus(IShare::STATUS_ACCEPTED);
  853. $this->shareManager->updateShare($share1);
  854. $config = \OC::$server->getConfig();
  855. // enforce expire date, by default 7 days after the file was shared
  856. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  857. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  858. $dateWithinRange = new \DateTime();
  859. $dateWithinRange->setTime(0,0,0);
  860. $dateWithinRange->add(new \DateInterval('P5D'));
  861. $dateOutOfRange = new \DateTime();
  862. $dateOutOfRange->setTime(0,0,0);
  863. $dateOutOfRange->add(new \DateInterval('P8D'));
  864. // update expire date to a valid value
  865. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  866. $ocs->updateShare($share1->getId(), null, null, null, null, $dateWithinRange->format('Y-m-d'));
  867. $ocs->cleanup();
  868. $share1 = $this->shareManager->getShareById($share1->getFullId());
  869. // date should be changed
  870. $this->assertEquals($dateWithinRange, $share1->getExpirationDate());
  871. // update expire date to a value out of range
  872. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  873. try {
  874. $ocs->updateShare($share1->getId());
  875. $this->fail();
  876. } catch (OCSBadRequestException $e) {
  877. }
  878. $ocs->cleanup();
  879. $share1 = $this->shareManager->getShareById($share1->getFullId());
  880. // date shouldn't be changed
  881. $this->assertEquals($dateWithinRange, $share1->getExpirationDate());
  882. // Try to remove expire date
  883. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  884. try {
  885. $ocs->updateShare($share1->getId());
  886. $this->fail();
  887. } catch (OCSBadRequestException $e) {
  888. }
  889. $ocs->cleanup();
  890. $share1 = $this->shareManager->getShareById($share1->getFullId());
  891. // date shouldn't be changed
  892. $this->assertEquals($dateWithinRange, $share1->getExpirationDate());
  893. // cleanup
  894. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  895. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  896. $this->shareManager->deleteShare($share1);
  897. }
  898. /**
  899. * @medium
  900. * @depends testCreateShareUserFile
  901. */
  902. public function testDeleteShare() {
  903. $node1 = $this->userFolder->get($this->filename);
  904. $share1 = $this->shareManager->newShare();
  905. $share1->setNode($node1)
  906. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  907. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  908. ->setShareType(IShare::TYPE_USER)
  909. ->setPermissions(19);
  910. $share1 = $this->shareManager->createShare($share1);
  911. $share2 = $this->shareManager->newShare();
  912. $share2->setNode($node1)
  913. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  914. ->setShareType(IShare::TYPE_LINK)
  915. ->setPermissions(1);
  916. $share2 = $this->shareManager->createShare($share1);
  917. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  918. $ocs->deleteShare($share1->getId());
  919. $ocs->cleanup();
  920. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  921. $ocs->deleteShare($share2->getId());
  922. $ocs->cleanup();
  923. $this->assertEmpty($this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_USER));
  924. $this->assertEmpty($this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_LINK));
  925. }
  926. /**
  927. * test unshare of a reshared file
  928. */
  929. public function testDeleteReshare() {
  930. $node1 = $this->userFolder->get($this->folder);
  931. $share1 = $this->shareManager->newShare();
  932. $share1->setNode($node1)
  933. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  934. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  935. ->setShareType(IShare::TYPE_USER)
  936. ->setPermissions(31);
  937. $share1 = $this->shareManager->createShare($share1);
  938. $share1->setStatus(IShare::STATUS_ACCEPTED);
  939. $this->shareManager->updateShare($share1);
  940. $user2folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER2);
  941. $node2 = $user2folder->get($this->folder.'/'.$this->filename);
  942. $share2 = $this->shareManager->newShare();
  943. $share2->setNode($node2)
  944. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  945. ->setShareType(IShare::TYPE_LINK)
  946. ->setPermissions(1);
  947. $share2 = $this->shareManager->createShare($share2);
  948. $share2->setStatus(IShare::STATUS_ACCEPTED);
  949. $this->shareManager->updateShare($share2);
  950. // test if we can unshare the link again
  951. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  952. $ocs->deleteShare($share2->getId());
  953. $ocs->cleanup();
  954. $this->shareManager->deleteShare($share1);
  955. $this->addToAssertionCount(1);
  956. }
  957. /**
  958. * share a folder which contains a share mount point, should be forbidden
  959. */
  960. public function testShareFolderWithAMountPoint() {
  961. // user 1 shares a folder with user2
  962. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  963. $share = $this->share(
  964. IShare::TYPE_USER,
  965. $this->folder,
  966. self::TEST_FILES_SHARING_API_USER1,
  967. self::TEST_FILES_SHARING_API_USER2,
  968. \OCP\Constants::PERMISSION_ALL
  969. );
  970. $share->setStatus(IShare::STATUS_ACCEPTED);
  971. $this->shareManager->updateShare($share);
  972. // user2 shares a file from the folder as link
  973. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  974. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  975. $view->mkdir("localDir");
  976. // move mount point to the folder "localDir"
  977. $result = $view->rename($this->folder, 'localDir/'.$this->folder);
  978. $this->assertTrue($result !== false);
  979. // try to share "localDir"
  980. $fileInfo2 = $view->getFileInfo('localDir');
  981. $this->assertTrue($fileInfo2 instanceof \OC\Files\FileInfo);
  982. $pass = true;
  983. try {
  984. $this->share(
  985. IShare::TYPE_USER,
  986. 'localDir',
  987. self::TEST_FILES_SHARING_API_USER2,
  988. self::TEST_FILES_SHARING_API_USER3,
  989. \OCP\Constants::PERMISSION_ALL
  990. );
  991. } catch (\Exception $e) {
  992. $pass = false;
  993. }
  994. $this->assertFalse($pass);
  995. //cleanup
  996. $result = $view->rename('localDir/' . $this->folder, $this->folder);
  997. $this->assertTrue($result !== false);
  998. $view->unlink('localDir');
  999. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  1000. $this->shareManager->deleteShare($share);
  1001. }
  1002. /**
  1003. * Post init mount points hook for mounting simulated ext storage
  1004. */
  1005. public static function initTestMountPointsHook($data) {
  1006. if ($data['user'] === self::TEST_FILES_SHARING_API_USER1) {
  1007. \OC\Files\Filesystem::mount(self::$tempStorage, [], '/' . self::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME);
  1008. }
  1009. }
  1010. /**
  1011. * Tests mounting a folder that is an external storage mount point.
  1012. */
  1013. public function testShareStorageMountPoint() {
  1014. $tempStorage = new \OC\Files\Storage\Temporary([]);
  1015. $tempStorage->file_put_contents('test.txt', 'abcdef');
  1016. $tempStorage->getScanner()->scan('');
  1017. $this->registerMount(self::TEST_FILES_SHARING_API_USER1, $tempStorage, self::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME);
  1018. // logging in will auto-mount the temp storage for user1 as well
  1019. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  1020. // user 1 shares the mount point folder with user2
  1021. $share = $this->share(
  1022. IShare::TYPE_USER,
  1023. $this->folder,
  1024. self::TEST_FILES_SHARING_API_USER1,
  1025. self::TEST_FILES_SHARING_API_USER2,
  1026. \OCP\Constants::PERMISSION_ALL
  1027. );
  1028. $share->setStatus(IShare::STATUS_ACCEPTED);
  1029. $this->shareManager->updateShare($share);
  1030. // user2: check that mount point name appears correctly
  1031. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  1032. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  1033. $this->assertTrue($view->file_exists($this->folder));
  1034. $this->assertTrue($view->file_exists($this->folder . '/test.txt'));
  1035. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  1036. $this->shareManager->deleteShare($share);
  1037. \OC_Hook::clear('OC_Filesystem', 'post_initMountPoints');
  1038. \OC_Hook::clear('\OCA\Files_Sharing\Tests\ApiTest', 'initTestMountPointsHook');
  1039. }
  1040. public function datesProvider() {
  1041. $date = new \DateTime();
  1042. $date->add(new \DateInterval('P5D'));
  1043. return [
  1044. [$date->format('Y-m-d'), true],
  1045. ['abc', false],
  1046. [$date->format('Y-m-d') . 'xyz', false],
  1047. ];
  1048. }
  1049. /**
  1050. * Make sure only ISO 8601 dates are accepted
  1051. *
  1052. * @dataProvider datesProvider
  1053. */
  1054. public function testPublicLinkExpireDate($date, $valid) {
  1055. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1056. try {
  1057. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date);
  1058. $this->assertTrue($valid);
  1059. } catch (OCSNotFoundException $e) {
  1060. $this->assertFalse($valid);
  1061. $this->assertEquals('Invalid date, date format must be YYYY-MM-DD', $e->getMessage());
  1062. $ocs->cleanup();
  1063. return;
  1064. }
  1065. $ocs->cleanup();
  1066. $data = $result->getData();
  1067. $this->assertTrue(is_string($data['token']));
  1068. $this->assertEquals($date, substr($data['expiration'], 0, 10));
  1069. // check for correct link
  1070. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  1071. $this->assertEquals($url, $data['url']);
  1072. $share = $this->shareManager->getShareById('ocinternal:'.$data['id']);
  1073. $this->assertEquals($date, $share->getExpirationDate()->format('Y-m-d'));
  1074. $this->shareManager->deleteShare($share);
  1075. }
  1076. public function testCreatePublicLinkExpireDateValid() {
  1077. $config = \OC::$server->getConfig();
  1078. // enforce expire date, by default 7 days after the file was shared
  1079. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  1080. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  1081. $date = new \DateTime();
  1082. $date->add(new \DateInterval('P5D'));
  1083. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1084. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d'));
  1085. $ocs->cleanup();
  1086. $data = $result->getData();
  1087. $this->assertTrue(is_string($data['token']));
  1088. $this->assertEquals($date->format('Y-m-d') . ' 00:00:00', $data['expiration']);
  1089. // check for correct link
  1090. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  1091. $this->assertEquals($url, $data['url']);
  1092. $share = $this->shareManager->getShareById('ocinternal:'.$data['id']);
  1093. $date->setTime(0,0,0);
  1094. $this->assertEquals($date, $share->getExpirationDate());
  1095. $this->shareManager->deleteShare($share);
  1096. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1097. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1098. }
  1099. public function testCreatePublicLinkExpireDateInvalidFuture() {
  1100. $config = \OC::$server->getConfig();
  1101. // enforce expire date, by default 7 days after the file was shared
  1102. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  1103. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  1104. $date = new \DateTime();
  1105. $date->add(new \DateInterval('P8D'));
  1106. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1107. try {
  1108. $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d'));
  1109. $this->fail();
  1110. } catch (OCSException $e) {
  1111. $this->assertEquals(404, $e->getCode());
  1112. $this->assertEquals('Can’t set expiration date more than 7 days in the future', $e->getMessage());
  1113. }
  1114. $ocs->cleanup();
  1115. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1116. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1117. }
  1118. public function XtestCreatePublicLinkExpireDateInvalidPast() {
  1119. $config = \OC::$server->getConfig();
  1120. $date = new \DateTime();
  1121. $date->sub(new \DateInterval('P8D'));
  1122. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1123. try {
  1124. $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d'));
  1125. $this->fail();
  1126. } catch (OCSException $e) {
  1127. $this->assertEquals(404, $e->getCode());
  1128. $this->assertEquals('Expiration date is in the past', $e->getMessage());
  1129. }
  1130. $ocs->cleanup();
  1131. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1132. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1133. }
  1134. /**
  1135. * test for no invisible shares
  1136. * See: https://github.com/owncloud/core/issues/22295
  1137. */
  1138. public function testInvisibleSharesUser() {
  1139. // simulate a post request
  1140. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1141. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  1142. $ocs->cleanup();
  1143. $data = $result->getData();
  1144. $topId = $data['id'];
  1145. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  1146. $ocs->acceptShare($topId);
  1147. $ocs->cleanup();
  1148. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  1149. $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  1150. $ocs->cleanup();
  1151. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1152. $ocs->deleteShare($topId);
  1153. $ocs->cleanup();
  1154. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1155. $result = $ocs->getShares();
  1156. $ocs->cleanup();
  1157. $this->assertEmpty($result->getData());
  1158. }
  1159. /**
  1160. * test for no invisible shares
  1161. * See: https://github.com/owncloud/core/issues/22295
  1162. */
  1163. public function testInvisibleSharesGroup() {
  1164. // simulate a post request
  1165. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1166. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1);
  1167. $ocs->cleanup();
  1168. $data = $result->getData();
  1169. $topId = $data['id'];
  1170. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  1171. $ocs->acceptShare($topId);
  1172. $ocs->cleanup();
  1173. \OC_Util::tearDownFS();
  1174. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  1175. $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  1176. $ocs->cleanup();
  1177. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1178. $ocs->deleteShare($topId);
  1179. $ocs->cleanup();
  1180. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1181. $result = $ocs->getShares();
  1182. $ocs->cleanup();
  1183. $this->assertEmpty($result->getData());
  1184. }
  1185. }