ApiTest.php 45 KB

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