1
0

ApiTest.php 47 KB

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