ApiTest.php 49 KB

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