ApiTest.php 47 KB

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