ApiTest.php 48 KB

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