1
0

ApiTest.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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 Joas Schilling <coding@schilljs.com>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Richard Steinmetz <richard@steinmetz.cloud>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Robin McCorkell <robin@mccorkell.me.uk>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author Thomas Müller <thomas.mueller@tmit.eu>
  18. * @author Valdnet <47037905+Valdnet@users.noreply.github.com>
  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 integration 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. /**
  174. * @group RoutingWeirdness
  175. */
  176. public function testCreateShareLink() {
  177. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  178. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  179. $ocs->cleanup();
  180. $data = $result->getData();
  181. $this->assertEquals(\OCP\Constants::PERMISSION_ALL,
  182. $data['permissions']);
  183. $this->assertEmpty($data['expiration']);
  184. $this->assertTrue(is_string($data['token']));
  185. // check for correct link
  186. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  187. $this->assertEquals($url, $data['url']);
  188. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  189. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  190. $ocs->deleteShare($data['id']);
  191. $ocs->cleanup();
  192. }
  193. /**
  194. * @group RoutingWeirdness
  195. */
  196. public function testCreateShareLinkPublicUpload() {
  197. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  198. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
  199. $ocs->cleanup();
  200. $data = $result->getData();
  201. $this->assertEquals(
  202. \OCP\Constants::PERMISSION_READ |
  203. \OCP\Constants::PERMISSION_CREATE |
  204. \OCP\Constants::PERMISSION_UPDATE |
  205. \OCP\Constants::PERMISSION_DELETE |
  206. \OCP\Constants::PERMISSION_SHARE,
  207. $data['permissions']
  208. );
  209. $this->assertEmpty($data['expiration']);
  210. $this->assertTrue(is_string($data['token']));
  211. // check for correct link
  212. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  213. $this->assertEquals($url, $data['url']);
  214. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  215. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  216. $ocs->deleteShare($data['id']);
  217. $ocs->cleanup();
  218. }
  219. public function testEnforceLinkPassword() {
  220. $password = md5(time());
  221. $config = \OC::$server->getConfig();
  222. $config->setAppValue('core', 'shareapi_enforce_links_password', 'yes');
  223. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  224. try {
  225. $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  226. $this->fail();
  227. } catch (OCSForbiddenException $e) {
  228. }
  229. $ocs->cleanup();
  230. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  231. try {
  232. $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '');
  233. $this->fail();
  234. } catch (OCSForbiddenException $e) {
  235. }
  236. $ocs->cleanup();
  237. // share with password should succeed
  238. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  239. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', $password);
  240. $ocs->cleanup();
  241. $data = $result->getData();
  242. // setting new password should succeed
  243. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  244. $ocs->updateShare($data['id'], null, $password);
  245. $ocs->cleanup();
  246. // removing password should fail
  247. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  248. try {
  249. $ocs->updateShare($data['id']);
  250. $this->fail();
  251. } catch (OCSBadRequestException $e) {
  252. }
  253. $ocs->cleanup();
  254. // cleanup
  255. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  256. $ocs->deleteShare($data['id']);
  257. $ocs->cleanup();
  258. $config->setAppValue('core', 'shareapi_enforce_links_password', 'no');
  259. $this->addToAssertionCount(1);
  260. }
  261. /**
  262. * @medium
  263. */
  264. public function testSharePermissions() {
  265. // sharing file to a user should work if shareapi_exclude_groups is set
  266. // to no
  267. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no');
  268. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  269. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  270. $ocs->cleanup();
  271. $data = $result->getData();
  272. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  273. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  274. $ocs->deleteShare($data['id']);
  275. $ocs->cleanup();
  276. // exclude groups, but not the group the user belongs to. Sharing should still work
  277. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'yes');
  278. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups_list', 'admin,group1,group2');
  279. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  280. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  281. $ocs->cleanup();
  282. $data = $result->getData();
  283. $this->shareManager->getShareById('ocinternal:' . $data['id']);
  284. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  285. $ocs->deleteShare($data['id']);
  286. $ocs->cleanup();
  287. // now we exclude the group the user belongs to ('group'), sharing should fail now
  288. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups_list', 'admin,group');
  289. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  290. $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  291. $ocs->cleanup();
  292. // cleanup
  293. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no');
  294. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups_list', '');
  295. $this->addToAssertionCount(1);
  296. }
  297. /**
  298. * @medium
  299. */
  300. public function testGetAllShares() {
  301. $node = $this->userFolder->get($this->filename);
  302. $share = $this->shareManager->newShare();
  303. $share->setNode($node)
  304. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  305. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  306. ->setShareType(IShare::TYPE_USER)
  307. ->setPermissions(19);
  308. $share = $this->shareManager->createShare($share);
  309. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  310. $result = $ocs->getShares();
  311. $ocs->cleanup();
  312. $this->assertTrue(count($result->getData()) === 1);
  313. $this->shareManager->deleteShare($share);
  314. }
  315. public function testGetAllSharesWithMe() {
  316. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2);
  317. $this->logout();
  318. $node1 = $this->userFolder->get($this->filename);
  319. $share1 = $this->shareManager->newShare();
  320. $share1->setNode($node1)
  321. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  322. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  323. ->setShareType(IShare::TYPE_USER)
  324. ->setPermissions(19);
  325. $share1 = $this->shareManager->createShare($share1);
  326. $share1->setStatus(IShare::STATUS_ACCEPTED);
  327. $this->shareManager->updateShare($share1);
  328. $node2 = $this->userFolder->get($this->folder);
  329. $share2 = $this->shareManager->newShare();
  330. $share2->setNode($node2)
  331. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  332. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  333. ->setShareType(IShare::TYPE_USER)
  334. ->setPermissions(31);
  335. $share2 = $this->shareManager->createShare($share2);
  336. $share2->setStatus(IShare::STATUS_ACCEPTED);
  337. $this->shareManager->updateShare($share2);
  338. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  339. $result = $ocs->getShares('true');
  340. $ocs->cleanup();
  341. $this->assertCount(2, $result->getData());
  342. $this->shareManager->deleteShare($share1);
  343. $this->shareManager->deleteShare($share2);
  344. }
  345. /**
  346. * @medium
  347. * @group RoutingWeirdness
  348. */
  349. public function testPublicLinkUrl() {
  350. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  351. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  352. $ocs->cleanup();
  353. $data = $result->getData();
  354. // check if we have a token
  355. $this->assertTrue(is_string($data['token']));
  356. $id = $data['id'];
  357. // check for correct link
  358. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  359. $this->assertEquals($url, $data['url']);
  360. // check for link in getall shares
  361. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  362. $result = $ocs->getShares();
  363. $ocs->cleanup();
  364. $data = $result->getData();
  365. $this->assertEquals($url, current($data)['url']);
  366. // check for path
  367. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  368. $result = $ocs->getShares();
  369. $ocs->cleanup();
  370. $data = $result->getData();
  371. $this->assertEquals($url, current($data)['url']);
  372. // check in share id
  373. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  374. $result = $ocs->getShare($id);
  375. $ocs->cleanup();
  376. $data = $result->getData();
  377. $this->assertEquals($url, current($data)['url']);
  378. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  379. $ocs->deleteShare($id);
  380. $ocs->cleanup();
  381. }
  382. /**
  383. * @medium
  384. * @depends testCreateShareUserFile
  385. * @depends testCreateShareLink
  386. */
  387. public function testGetShareFromSource() {
  388. $node = $this->userFolder->get($this->filename);
  389. $share = $this->shareManager->newShare();
  390. $share->setNode($node)
  391. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  392. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  393. ->setShareType(IShare::TYPE_USER)
  394. ->setPermissions(19);
  395. $share1 = $this->shareManager->createShare($share);
  396. $share = $this->shareManager->newShare();
  397. $share->setNode($node)
  398. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  399. ->setShareType(IShare::TYPE_LINK)
  400. ->setPermissions(1);
  401. $share2 = $this->shareManager->createShare($share);
  402. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  403. $result = $ocs->getShares();
  404. $ocs->cleanup();
  405. // test should return one share created from testCreateShare()
  406. $this->assertTrue(count($result->getData()) === 2);
  407. $this->shareManager->deleteShare($share1);
  408. $this->shareManager->deleteShare($share2);
  409. }
  410. /**
  411. * @medium
  412. * @depends testCreateShareUserFile
  413. * @depends testCreateShareLink
  414. */
  415. public function testGetShareFromSourceWithReshares() {
  416. $node = $this->userFolder->get($this->filename);
  417. $share1 = $this->shareManager->newShare();
  418. $share1->setNode($node)
  419. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  420. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  421. ->setShareType(IShare::TYPE_USER)
  422. ->setPermissions(19);
  423. $share1 = $this->shareManager->createShare($share1);
  424. $share2 = $this->shareManager->newShare();
  425. $share2->setNode($node)
  426. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  427. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  428. ->setShareType(IShare::TYPE_USER)
  429. ->setPermissions(19);
  430. $share2 = $this->shareManager->createShare($share2);
  431. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  432. $result = $ocs->getShares();
  433. $ocs->cleanup();
  434. // test should return one share
  435. $this->assertTrue(count($result->getData()) === 1);
  436. // now also ask for the reshares
  437. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  438. $result = $ocs->getShares('false', 'true', 'false', $this->filename);
  439. $ocs->cleanup();
  440. // now we should get two shares, the initial share and the reshare
  441. $this->assertCount(2, $result->getData());
  442. $this->shareManager->deleteShare($share1);
  443. $this->shareManager->deleteShare($share2);
  444. }
  445. /**
  446. * @medium
  447. * @depends testCreateShareUserFile
  448. */
  449. public function testGetShareFromId() {
  450. $node = $this->userFolder->get($this->filename);
  451. $share1 = $this->shareManager->newShare();
  452. $share1->setNode($node)
  453. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  454. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  455. ->setShareType(IShare::TYPE_USER)
  456. ->setPermissions(19);
  457. $share1 = $this->shareManager->createShare($share1);
  458. // call getShare() with share ID
  459. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  460. $result = $ocs->getShare($share1->getId());
  461. $ocs->cleanup();
  462. // test should return one share created from testCreateShare()
  463. $this->assertEquals(1, count($result->getData()));
  464. $this->shareManager->deleteShare($share1);
  465. }
  466. /**
  467. * @medium
  468. */
  469. public function testGetShareFromFolder() {
  470. $node1 = $this->userFolder->get($this->filename);
  471. $share1 = $this->shareManager->newShare();
  472. $share1->setNode($node1)
  473. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  474. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  475. ->setShareType(IShare::TYPE_USER)
  476. ->setPermissions(19);
  477. $share1 = $this->shareManager->createShare($share1);
  478. $node2 = $this->userFolder->get($this->folder.'/'.$this->filename);
  479. $share2 = $this->shareManager->newShare();
  480. $share2->setNode($node2)
  481. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  482. ->setShareType(IShare::TYPE_LINK)
  483. ->setPermissions(1);
  484. $share2 = $this->shareManager->createShare($share2);
  485. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  486. $result = $ocs->getShares('false', 'false', 'true', $this->folder);
  487. $ocs->cleanup();
  488. // test should return one share within $this->folder
  489. $this->assertTrue(count($result->getData()) === 1);
  490. $this->shareManager->deleteShare($share1);
  491. $this->shareManager->deleteShare($share2);
  492. }
  493. public function testGetShareFromFolderWithFile() {
  494. $node1 = $this->userFolder->get($this->filename);
  495. $share1 = $this->shareManager->newShare();
  496. $share1->setNode($node1)
  497. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  498. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  499. ->setShareType(IShare::TYPE_USER)
  500. ->setPermissions(19);
  501. $share1 = $this->shareManager->createShare($share1);
  502. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  503. try {
  504. $ocs->getShares('false', 'false', 'true', $this->filename);
  505. $this->fail();
  506. } catch (OCSBadRequestException $e) {
  507. $this->assertEquals('Not a directory', $e->getMessage());
  508. }
  509. $ocs->cleanup();
  510. $this->shareManager->deleteShare($share1);
  511. }
  512. /**
  513. * share a folder, than reshare a file within the shared folder and check if we construct the correct path
  514. * @medium
  515. */
  516. public function testGetShareFromFolderReshares() {
  517. $node1 = $this->userFolder->get($this->folder);
  518. $share1 = $this->shareManager->newShare();
  519. $share1->setNode($node1)
  520. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  521. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  522. ->setShareType(IShare::TYPE_USER)
  523. ->setPermissions(31);
  524. $share1 = $this->shareManager->createShare($share1);
  525. $share1->setStatus(IShare::STATUS_ACCEPTED);
  526. $this->shareManager->updateShare($share1);
  527. $node2 = $this->userFolder->get($this->folder.'/'.$this->filename);
  528. $share2 = $this->shareManager->newShare();
  529. $share2->setNode($node2)
  530. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  531. ->setShareType(IShare::TYPE_LINK)
  532. ->setPermissions(1);
  533. $share2 = $this->shareManager->createShare($share2);
  534. $share2->setStatus(IShare::STATUS_ACCEPTED);
  535. $this->shareManager->updateShare($share2);
  536. $node3 = $this->userFolder->get($this->folder.'/'.$this->subfolder.'/'.$this->filename);
  537. $share3 = $this->shareManager->newShare();
  538. $share3->setNode($node3)
  539. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  540. ->setShareType(IShare::TYPE_LINK)
  541. ->setPermissions(1);
  542. $share3 = $this->shareManager->createShare($share3);
  543. $share3->setStatus(IShare::STATUS_ACCEPTED);
  544. $this->shareManager->updateShare($share3);
  545. $testValues = [
  546. ['query' => $this->folder,
  547. 'expectedResult' => $this->folder . $this->filename],
  548. ['query' => $this->folder . $this->subfolder,
  549. 'expectedResult' => $this->folder . $this->subfolder . $this->filename],
  550. ];
  551. foreach ($testValues as $value) {
  552. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  553. $result = $ocs->getShares('false', 'false', 'true', $value['query']);
  554. $ocs->cleanup();
  555. // test should return one share within $this->folder
  556. $data = $result->getData();
  557. $this->assertEquals($value['expectedResult'], $data[0]['path']);
  558. }
  559. // cleanup
  560. $this->shareManager->deleteShare($share1);
  561. $this->shareManager->deleteShare($share2);
  562. $this->shareManager->deleteShare($share3);
  563. }
  564. /**
  565. * reshare a sub folder and check if we get the correct path
  566. * @medium
  567. */
  568. public function testGetShareFromSubFolderReShares() {
  569. $node1 = $this->userFolder->get($this->folder . $this->subfolder);
  570. $share1 = $this->shareManager->newShare();
  571. $share1->setNode($node1)
  572. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  573. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  574. ->setShareType(IShare::TYPE_USER)
  575. ->setPermissions(31);
  576. $share1 = $this->shareManager->createShare($share1);
  577. $share1->setStatus(IShare::STATUS_ACCEPTED);
  578. $this->shareManager->updateShare($share1);
  579. $node2 = \OC::$server->getRootFolder()->getUserFolder(self::TEST_FILES_SHARING_API_USER2)->get($this->subfolder);
  580. $share2 = $this->shareManager->newShare();
  581. $share2->setNode($node2)
  582. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  583. ->setShareType(IShare::TYPE_LINK)
  584. ->setPermissions(1);
  585. $share2 = $this->shareManager->createShare($share2);
  586. $share2->setStatus(IShare::STATUS_ACCEPTED);
  587. $this->shareManager->updateShare($share2);
  588. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  589. $result = $ocs->getShares();
  590. $ocs->cleanup();
  591. // test should return one share within $this->folder
  592. $data = $result->getData();
  593. // we should get exactly one result
  594. $this->assertCount(1, $data);
  595. $this->assertEquals($this->subfolder, $data[0]['path']);
  596. $this->shareManager->deleteShare($share2);
  597. $this->shareManager->deleteShare($share1);
  598. }
  599. /**
  600. * test re-re-share of folder if the path gets constructed correctly
  601. * @medium
  602. */
  603. public function XtestGetShareFromFolderReReShares() {
  604. $node1 = $this->userFolder->get($this->folder . $this->subfolder);
  605. $share1 = $this->shareManager->newShare();
  606. $share1->setNode($node1)
  607. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  608. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  609. ->setShareType(IShare::TYPE_USER)
  610. ->setPermissions(31);
  611. $share1 = $this->shareManager->createShare($share1);
  612. $node2 = $this->userFolder->get($this->folder . $this->subfolder . $this->subsubfolder);
  613. $share2 = $this->shareManager->newShare();
  614. $share2->setNode($node2)
  615. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  616. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  617. ->setShareType(IShare::TYPE_USER)
  618. ->setPermissions(31);
  619. $share2 = $this->shareManager->createShare($share2);
  620. $share3 = $this->shareManager->newShare();
  621. $share3->setNode($node2)
  622. ->setSharedBy(self::TEST_FILES_SHARING_API_USER3)
  623. ->setShareType(IShare::TYPE_LINK)
  624. ->setPermissions(1);
  625. $share3 = $this->shareManager->createShare($share3);
  626. /*
  627. * Test as recipient
  628. */
  629. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER3);
  630. $result = $ocs->getShares();
  631. $ocs->cleanup();
  632. // test should return one share within $this->folder
  633. $data = $result->getData();
  634. // we should get exactly one result
  635. $this->assertCount(1, $data);
  636. $this->assertEquals($this->subsubfolder, $data[0]['path']);
  637. /*
  638. * Test for first owner/initiator
  639. */
  640. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  641. $result = $ocs->getShares();
  642. $ocs->cleanup();
  643. // test should return one share within $this->folder
  644. $data = $result->getData();
  645. // we should get exactly one result
  646. $this->assertCount(1, $data);
  647. $this->assertEquals($this->folder . $this->subfolder, $data[0]['path']);
  648. /*
  649. * Test for second initiator
  650. */
  651. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  652. $result = $ocs->getShares();
  653. $ocs->cleanup();
  654. // test should return one share within $this->folder
  655. $data = $result->getData();
  656. // we should get exactly one result
  657. $this->assertCount(1, $data);
  658. $this->assertEquals($this->subfolder . $this->subsubfolder, $data[0]['path']);
  659. $this->shareManager->deleteShare($share1);
  660. $this->shareManager->deleteShare($share2);
  661. $this->shareManager->deleteShare($share3);
  662. }
  663. /**
  664. * test multiple shared folder if the path gets constructed correctly
  665. * @medium
  666. */
  667. public function testGetShareMultipleSharedFolder() {
  668. $this->setUp();
  669. $node1 = $this->userFolder->get($this->folder . $this->subfolder);
  670. $share1 = $this->shareManager->newShare();
  671. $share1->setNode($node1)
  672. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  673. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  674. ->setShareType(IShare::TYPE_USER)
  675. ->setPermissions(31);
  676. $share1 = $this->shareManager->createShare($share1);
  677. $share1->setStatus(IShare::STATUS_ACCEPTED);
  678. $this->shareManager->updateShare($share1);
  679. $node2 = $this->userFolder->get($this->folder);
  680. $share2 = $this->shareManager->newShare();
  681. $share2->setNode($node2)
  682. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  683. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  684. ->setShareType(IShare::TYPE_USER)
  685. ->setPermissions(31);
  686. $share2 = $this->shareManager->createShare($share2);
  687. $share2->setStatus(IShare::STATUS_ACCEPTED);
  688. $this->shareManager->updateShare($share2);
  689. $share3 = $this->shareManager->newShare();
  690. $share3->setNode($node1)
  691. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  692. ->setShareType(IShare::TYPE_LINK)
  693. ->setPermissions(1);
  694. $share3 = $this->shareManager->createShare($share3);
  695. $share3->setStatus(IShare::STATUS_ACCEPTED);
  696. $this->shareManager->updateShare($share3);
  697. // $request = $this->createRequest(['path' => $this->subfolder]);
  698. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  699. $result1 = $ocs->getShares('false', 'false', 'false', $this->subfolder);
  700. $ocs->cleanup();
  701. // test should return one share within $this->folder
  702. $data1 = $result1->getData();
  703. $this->assertCount(1, $data1);
  704. $s1 = reset($data1);
  705. //$request = $this->createRequest(['path' => $this->folder.$this->subfolder]);
  706. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  707. $result2 = $ocs->getShares('false', 'false', 'false', $this->folder . $this->subfolder);
  708. $ocs->cleanup();
  709. // test should return one share within $this->folder
  710. $data2 = $result2->getData();
  711. $this->assertCount(1, $data2);
  712. $s2 = reset($data2);
  713. $this->assertEquals($this->subfolder, $s1['path']);
  714. $this->assertEquals($this->folder.$this->subfolder, $s2['path']);
  715. $this->shareManager->deleteShare($share1);
  716. $this->shareManager->deleteShare($share2);
  717. $this->shareManager->deleteShare($share3);
  718. }
  719. /**
  720. * test re-re-share of folder if the path gets constructed correctly
  721. * @medium
  722. */
  723. public function testGetShareFromFileReReShares() {
  724. $node1 = $this->userFolder->get($this->folder . $this->subfolder);
  725. $share1 = $this->shareManager->newShare();
  726. $share1->setNode($node1)
  727. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  728. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  729. ->setShareType(IShare::TYPE_USER)
  730. ->setPermissions(31);
  731. $share1 = $this->shareManager->createShare($share1);
  732. $share1->setStatus(IShare::STATUS_ACCEPTED);
  733. $this->shareManager->updateShare($share1);
  734. $user2Folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER2);
  735. $node2 = $user2Folder->get($this->subfolder . $this->filename);
  736. $share2 = $this->shareManager->newShare();
  737. $share2->setNode($node2)
  738. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  739. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  740. ->setShareType(IShare::TYPE_USER)
  741. ->setPermissions(19);
  742. $share2 = $this->shareManager->createShare($share2);
  743. $share2->setStatus(IShare::STATUS_ACCEPTED);
  744. $this->shareManager->updateShare($share2);
  745. $user3Folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER3);
  746. $node3 = $user3Folder->get($this->filename);
  747. $share3 = $this->shareManager->newShare();
  748. $share3->setNode($node3)
  749. ->setSharedBy(self::TEST_FILES_SHARING_API_USER3)
  750. ->setShareType(IShare::TYPE_LINK)
  751. ->setPermissions(1);
  752. $share3 = $this->shareManager->createShare($share3);
  753. $share3->setStatus(IShare::STATUS_ACCEPTED);
  754. $this->shareManager->updateShare($share3);
  755. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER3);
  756. $result = $ocs->getShares();
  757. $ocs->cleanup();
  758. // test should return one share within $this->folder
  759. $data = $result->getData();
  760. // we should get exactly one result
  761. $this->assertCount(1, $data);
  762. $this->assertEquals($this->filename, $data[0]['path']);
  763. $this->shareManager->deleteShare($share1);
  764. $this->shareManager->deleteShare($share2);
  765. $this->shareManager->deleteShare($share3);
  766. }
  767. /**
  768. * @medium
  769. */
  770. public function testGetShareFromUnknownId() {
  771. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER3);
  772. try {
  773. $ocs->getShare(0);
  774. $this->fail();
  775. } catch (OCSNotFoundException $e) {
  776. $this->assertEquals('Wrong share ID, share does not exist', $e->getMessage());
  777. }
  778. $ocs->cleanup();
  779. }
  780. /**
  781. * @medium
  782. * @depends testCreateShareUserFile
  783. * @depends testCreateShareLink
  784. */
  785. public function testUpdateShare() {
  786. $password = md5(time());
  787. $node1 = $this->userFolder->get($this->filename);
  788. $share1 = $this->shareManager->newShare();
  789. $share1->setNode($node1)
  790. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  791. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  792. ->setShareType(IShare::TYPE_USER)
  793. ->setPermissions(19)
  794. ->setAttributes($this->shareManager->newShare()->newAttributes());
  795. $this->assertNotNull($share1->getAttributes());
  796. $share1 = $this->shareManager->createShare($share1);
  797. $this->assertNull($share1->getAttributes());
  798. $this->assertEquals(19, $share1->getPermissions());
  799. // attributes get cleared when empty
  800. $this->assertNull($share1->getAttributes());
  801. $share2 = $this->shareManager->newShare();
  802. $share2->setNode($node1)
  803. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  804. ->setShareType(IShare::TYPE_LINK)
  805. ->setPermissions(1);
  806. $share2 = $this->shareManager->createShare($share2);
  807. $this->assertEquals(1, $share2->getPermissions());
  808. // update permissions
  809. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  810. $ocs->updateShare(
  811. $share1->getId(), 1, null, null, null, null, null, null, null,
  812. '[{"scope": "app1", "key": "attr1", "enabled": true}]'
  813. );
  814. $ocs->cleanup();
  815. $share1 = $this->shareManager->getShareById('ocinternal:' . $share1->getId());
  816. $this->assertEquals(1, $share1->getPermissions());
  817. $this->assertEquals(true, $share1->getAttributes()->getAttribute('app1', 'attr1'));
  818. // update password for link share
  819. $this->assertNull($share2->getPassword());
  820. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  821. $ocs->updateShare($share2->getId(), null, $password);
  822. $ocs->cleanup();
  823. $share2 = $this->shareManager->getShareById('ocinternal:' . $share2->getId());
  824. $this->assertNotNull($share2->getPassword());
  825. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  826. $ocs->updateShare($share2->getId(), null, '');
  827. $ocs->cleanup();
  828. $share2 = $this->shareManager->getShareById('ocinternal:' . $share2->getId());
  829. $this->assertNull($share2->getPassword());
  830. $this->shareManager->deleteShare($share1);
  831. $this->shareManager->deleteShare($share2);
  832. }
  833. /**
  834. * @medium
  835. */
  836. public function testUpdateShareUpload() {
  837. $node1 = $this->userFolder->get($this->folder);
  838. $share1 = $this->shareManager->newShare();
  839. $share1->setNode($node1)
  840. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  841. ->setShareType(IShare::TYPE_LINK)
  842. ->setPermissions(1);
  843. $share1 = $this->shareManager->createShare($share1);
  844. // update public upload
  845. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  846. $ocs->updateShare($share1->getId(), null, null, null, 'true');
  847. $ocs->cleanup();
  848. $share1 = $this->shareManager->getShareById($share1->getFullId());
  849. $this->assertEquals(
  850. \OCP\Constants::PERMISSION_READ |
  851. \OCP\Constants::PERMISSION_CREATE |
  852. \OCP\Constants::PERMISSION_UPDATE |
  853. \OCP\Constants::PERMISSION_DELETE |
  854. \OCP\Constants::PERMISSION_SHARE,
  855. $share1->getPermissions()
  856. );
  857. // cleanup
  858. $this->shareManager->deleteShare($share1);
  859. }
  860. /**
  861. * @medium
  862. */
  863. public function testUpdateShareExpireDate() {
  864. $node1 = $this->userFolder->get($this->folder);
  865. $share1 = $this->shareManager->newShare();
  866. $share1->setNode($node1)
  867. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  868. ->setShareType(IShare::TYPE_LINK)
  869. ->setPermissions(1);
  870. $share1 = $this->shareManager->createShare($share1);
  871. $share1->setStatus(IShare::STATUS_ACCEPTED);
  872. $this->shareManager->updateShare($share1);
  873. $config = \OC::$server->getConfig();
  874. // enforce expire date, by default 7 days after the file was shared
  875. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  876. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  877. $dateWithinRange = new \DateTime();
  878. $dateWithinRange->setTime(0, 0, 0);
  879. $dateWithinRange->add(new \DateInterval('P5D'));
  880. $dateOutOfRange = new \DateTime();
  881. $dateOutOfRange->setTime(0, 0, 0);
  882. $dateOutOfRange->add(new \DateInterval('P8D'));
  883. // update expire date to a valid value
  884. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  885. $ocs->updateShare($share1->getId(), null, null, null, null, $dateWithinRange->format('Y-m-d'));
  886. $ocs->cleanup();
  887. $share1 = $this->shareManager->getShareById($share1->getFullId());
  888. // date should be changed
  889. $this->assertEquals($dateWithinRange, $share1->getExpirationDate());
  890. // update expire date to a value out of range
  891. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  892. try {
  893. $ocs->updateShare($share1->getId());
  894. $this->fail();
  895. } catch (OCSBadRequestException $e) {
  896. }
  897. $ocs->cleanup();
  898. $share1 = $this->shareManager->getShareById($share1->getFullId());
  899. // date shouldn't be changed
  900. $this->assertEquals($dateWithinRange, $share1->getExpirationDate());
  901. // Try to remove expire date
  902. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  903. try {
  904. $ocs->updateShare($share1->getId());
  905. $this->fail();
  906. } catch (OCSBadRequestException $e) {
  907. }
  908. $ocs->cleanup();
  909. $share1 = $this->shareManager->getShareById($share1->getFullId());
  910. // date shouldn't be changed
  911. $this->assertEquals($dateWithinRange, $share1->getExpirationDate());
  912. // cleanup
  913. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  914. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  915. $this->shareManager->deleteShare($share1);
  916. }
  917. /**
  918. * @medium
  919. * @depends testCreateShareUserFile
  920. */
  921. public function testDeleteShare() {
  922. $node1 = $this->userFolder->get($this->filename);
  923. $share1 = $this->shareManager->newShare();
  924. $share1->setNode($node1)
  925. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  926. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  927. ->setShareType(IShare::TYPE_USER)
  928. ->setPermissions(19);
  929. $share1 = $this->shareManager->createShare($share1);
  930. $share2 = $this->shareManager->newShare();
  931. $share2->setNode($node1)
  932. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  933. ->setShareType(IShare::TYPE_LINK)
  934. ->setPermissions(1);
  935. $share2 = $this->shareManager->createShare($share1);
  936. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  937. $ocs->deleteShare($share1->getId());
  938. $ocs->cleanup();
  939. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  940. $ocs->deleteShare($share2->getId());
  941. $ocs->cleanup();
  942. $this->assertEmpty($this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_USER));
  943. $this->assertEmpty($this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_LINK));
  944. }
  945. /**
  946. * test unshare of a reshared file
  947. */
  948. public function testDeleteReshare() {
  949. $node1 = $this->userFolder->get($this->folder);
  950. $share1 = $this->shareManager->newShare();
  951. $share1->setNode($node1)
  952. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  953. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  954. ->setShareType(IShare::TYPE_USER)
  955. ->setPermissions(31);
  956. $share1 = $this->shareManager->createShare($share1);
  957. $share1->setStatus(IShare::STATUS_ACCEPTED);
  958. $this->shareManager->updateShare($share1);
  959. $user2folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER2);
  960. $node2 = $user2folder->get($this->folder.'/'.$this->filename);
  961. $share2 = $this->shareManager->newShare();
  962. $share2->setNode($node2)
  963. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  964. ->setShareType(IShare::TYPE_LINK)
  965. ->setPermissions(1);
  966. $share2 = $this->shareManager->createShare($share2);
  967. $share2->setStatus(IShare::STATUS_ACCEPTED);
  968. $this->shareManager->updateShare($share2);
  969. // test if we can unshare the link again
  970. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  971. $ocs->deleteShare($share2->getId());
  972. $ocs->cleanup();
  973. $this->shareManager->deleteShare($share1);
  974. $this->addToAssertionCount(1);
  975. }
  976. /**
  977. * share a folder which contains a share mount point, should be forbidden
  978. */
  979. public function testShareFolderWithAMountPoint() {
  980. // user 1 shares a folder with user2
  981. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  982. $share = $this->share(
  983. IShare::TYPE_USER,
  984. $this->folder,
  985. self::TEST_FILES_SHARING_API_USER1,
  986. self::TEST_FILES_SHARING_API_USER2,
  987. \OCP\Constants::PERMISSION_ALL
  988. );
  989. $share->setStatus(IShare::STATUS_ACCEPTED);
  990. $this->shareManager->updateShare($share);
  991. // user2 shares a file from the folder as link
  992. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  993. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  994. $view->mkdir("localDir");
  995. // move mount point to the folder "localDir"
  996. $result = $view->rename($this->folder, 'localDir/'.$this->folder);
  997. $this->assertTrue($result !== false);
  998. // try to share "localDir"
  999. $fileInfo2 = $view->getFileInfo('localDir');
  1000. $this->assertTrue($fileInfo2 instanceof \OC\Files\FileInfo);
  1001. $pass = true;
  1002. try {
  1003. $this->share(
  1004. IShare::TYPE_USER,
  1005. 'localDir',
  1006. self::TEST_FILES_SHARING_API_USER2,
  1007. self::TEST_FILES_SHARING_API_USER3,
  1008. \OCP\Constants::PERMISSION_ALL
  1009. );
  1010. } catch (\Exception $e) {
  1011. $pass = false;
  1012. }
  1013. $this->assertFalse($pass);
  1014. //cleanup
  1015. $result = $view->rename('localDir/' . $this->folder, $this->folder);
  1016. $this->assertTrue($result !== false);
  1017. $view->unlink('localDir');
  1018. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  1019. $this->shareManager->deleteShare($share);
  1020. }
  1021. /**
  1022. * Post init mount points hook for mounting simulated ext storage
  1023. */
  1024. public static function initTestMountPointsHook($data) {
  1025. if ($data['user'] === self::TEST_FILES_SHARING_API_USER1) {
  1026. \OC\Files\Filesystem::mount(self::$tempStorage, [], '/' . self::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME);
  1027. }
  1028. }
  1029. /**
  1030. * Tests mounting a folder that is an external storage mount point.
  1031. */
  1032. public function testShareStorageMountPoint() {
  1033. $tempStorage = new \OC\Files\Storage\Temporary([]);
  1034. $tempStorage->file_put_contents('test.txt', 'abcdef');
  1035. $tempStorage->getScanner()->scan('');
  1036. $this->registerMount(self::TEST_FILES_SHARING_API_USER1, $tempStorage, self::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME);
  1037. // logging in will auto-mount the temp storage for user1 as well
  1038. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  1039. // user 1 shares the mount point folder with user2
  1040. $share = $this->share(
  1041. IShare::TYPE_USER,
  1042. $this->folder,
  1043. self::TEST_FILES_SHARING_API_USER1,
  1044. self::TEST_FILES_SHARING_API_USER2,
  1045. \OCP\Constants::PERMISSION_ALL
  1046. );
  1047. $share->setStatus(IShare::STATUS_ACCEPTED);
  1048. $this->shareManager->updateShare($share);
  1049. // user2: check that mount point name appears correctly
  1050. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  1051. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  1052. $this->assertTrue($view->file_exists($this->folder));
  1053. $this->assertTrue($view->file_exists($this->folder . '/test.txt'));
  1054. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  1055. $this->shareManager->deleteShare($share);
  1056. \OC_Hook::clear('OC_Filesystem', 'post_initMountPoints');
  1057. \OC_Hook::clear('\OCA\Files_Sharing\Tests\ApiTest', 'initTestMountPointsHook');
  1058. }
  1059. public function datesProvider() {
  1060. $date = new \DateTime();
  1061. $date->add(new \DateInterval('P5D'));
  1062. return [
  1063. [$date->format('Y-m-d'), true],
  1064. ['abc', false],
  1065. [$date->format('Y-m-d') . 'xyz', false],
  1066. ];
  1067. }
  1068. /**
  1069. * Make sure only ISO 8601 dates are accepted
  1070. *
  1071. * @dataProvider datesProvider
  1072. * @group RoutingWeirdness
  1073. */
  1074. public function testPublicLinkExpireDate($date, $valid) {
  1075. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1076. try {
  1077. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date);
  1078. $this->assertTrue($valid);
  1079. } catch (OCSNotFoundException $e) {
  1080. $this->assertFalse($valid);
  1081. $this->assertEquals('Invalid date, date format must be YYYY-MM-DD', $e->getMessage());
  1082. $ocs->cleanup();
  1083. return;
  1084. }
  1085. $ocs->cleanup();
  1086. $data = $result->getData();
  1087. $this->assertTrue(is_string($data['token']));
  1088. $this->assertEquals($date, substr($data['expiration'], 0, 10));
  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. $this->assertEquals($date, $share->getExpirationDate()->format('Y-m-d'));
  1094. $this->shareManager->deleteShare($share);
  1095. }
  1096. /**
  1097. * @group RoutingWeirdness
  1098. */
  1099. public function testCreatePublicLinkExpireDateValid() {
  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('P5D'));
  1106. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1107. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d'));
  1108. $ocs->cleanup();
  1109. $data = $result->getData();
  1110. $this->assertTrue(is_string($data['token']));
  1111. $this->assertEquals($date->format('Y-m-d') . ' 00:00:00', $data['expiration']);
  1112. // check for correct link
  1113. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  1114. $this->assertEquals($url, $data['url']);
  1115. $share = $this->shareManager->getShareById('ocinternal:'.$data['id']);
  1116. $date->setTime(0, 0, 0);
  1117. $this->assertEquals($date, $share->getExpirationDate());
  1118. $this->shareManager->deleteShare($share);
  1119. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1120. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1121. }
  1122. public function testCreatePublicLinkExpireDateInvalidFuture() {
  1123. $config = \OC::$server->getConfig();
  1124. // enforce expire date, by default 7 days after the file was shared
  1125. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  1126. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  1127. $date = new \DateTime();
  1128. $date->add(new \DateInterval('P8D'));
  1129. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1130. try {
  1131. $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d'));
  1132. $this->fail();
  1133. } catch (OCSException $e) {
  1134. $this->assertEquals(404, $e->getCode());
  1135. $this->assertEquals('Cannot set expiration date more than 7 days in the future', $e->getMessage());
  1136. }
  1137. $ocs->cleanup();
  1138. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1139. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1140. }
  1141. public function XtestCreatePublicLinkExpireDateInvalidPast() {
  1142. $config = \OC::$server->getConfig();
  1143. $date = new \DateTime();
  1144. $date->sub(new \DateInterval('P8D'));
  1145. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1146. try {
  1147. $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d'));
  1148. $this->fail();
  1149. } catch (OCSException $e) {
  1150. $this->assertEquals(404, $e->getCode());
  1151. $this->assertEquals('Expiration date is in the past', $e->getMessage());
  1152. }
  1153. $ocs->cleanup();
  1154. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1155. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1156. }
  1157. /**
  1158. * test for no invisible shares
  1159. * See: https://github.com/owncloud/core/issues/22295
  1160. */
  1161. public function testInvisibleSharesUser() {
  1162. // simulate a post request
  1163. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1164. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  1165. $ocs->cleanup();
  1166. $data = $result->getData();
  1167. $topId = $data['id'];
  1168. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  1169. $ocs->acceptShare($topId);
  1170. $ocs->cleanup();
  1171. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  1172. $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  1173. $ocs->cleanup();
  1174. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1175. $ocs->deleteShare($topId);
  1176. $ocs->cleanup();
  1177. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1178. $result = $ocs->getShares();
  1179. $ocs->cleanup();
  1180. $this->assertEmpty($result->getData());
  1181. }
  1182. /**
  1183. * test for no invisible shares
  1184. * See: https://github.com/owncloud/core/issues/22295
  1185. */
  1186. public function testInvisibleSharesGroup() {
  1187. // simulate a post request
  1188. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1189. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1);
  1190. $ocs->cleanup();
  1191. $data = $result->getData();
  1192. $topId = $data['id'];
  1193. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  1194. $ocs->acceptShare($topId);
  1195. $ocs->cleanup();
  1196. \OC_Util::tearDownFS();
  1197. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  1198. $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  1199. $ocs->cleanup();
  1200. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1201. $ocs->deleteShare($topId);
  1202. $ocs->cleanup();
  1203. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1204. $result = $ocs->getShares();
  1205. $ocs->cleanup();
  1206. $this->assertEmpty($result->getData());
  1207. }
  1208. }