ApiTest.php 48 KB

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