ApiTest.php 49 KB

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