api.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @copyright Copyright (c) 2015, ownCloud, Inc.
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. use OCA\Files\Share;
  29. use OCA\Files_sharing\Tests\TestCase;
  30. /**
  31. * Class Test_Files_Sharing_Api
  32. */
  33. class Test_Files_Sharing_Api extends TestCase {
  34. const TEST_FOLDER_NAME = '/folder_share_api_test';
  35. private static $tempStorage;
  36. protected function setUp() {
  37. parent::setUp();
  38. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'no');
  39. \OC::$server->getAppConfig()->setValue('core', 'shareapi_expire_after_n_days', '7');
  40. $this->folder = self::TEST_FOLDER_NAME;
  41. $this->subfolder = '/subfolder_share_api_test';
  42. $this->subsubfolder = '/subsubfolder_share_api_test';
  43. $this->filename = '/share-api-test.txt';
  44. // save file with content
  45. $this->view->file_put_contents($this->filename, $this->data);
  46. $this->view->mkdir($this->folder);
  47. $this->view->mkdir($this->folder . $this->subfolder);
  48. $this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
  49. $this->view->file_put_contents($this->folder.$this->filename, $this->data);
  50. $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
  51. }
  52. protected function tearDown() {
  53. if($this->view instanceof \OC\Files\View) {
  54. $this->view->unlink($this->filename);
  55. $this->view->deleteAll($this->folder);
  56. }
  57. self::$tempStorage = null;
  58. parent::tearDown();
  59. }
  60. /**
  61. * @medium
  62. */
  63. function testCreateShare() {
  64. // share to user
  65. // simulate a post request
  66. $_POST['path'] = $this->filename;
  67. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  68. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  69. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  70. $this->assertTrue($result->succeeded());
  71. $data = $result->getData();
  72. $share = $this->getShareFromId($data['id']);
  73. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  74. $this->assertTrue(!empty($items));
  75. // share link
  76. // simulate a post request
  77. $_POST['path'] = $this->folder;
  78. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  79. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  80. // check if API call was successful
  81. $this->assertTrue($result->succeeded());
  82. $data = $result->getData();
  83. // check if we have a token
  84. $this->assertTrue(is_string($data['token']));
  85. // check for correct link
  86. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  87. $this->assertEquals($url, $data['url']);
  88. $share = $this->getShareFromId($data['id']);
  89. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  90. $this->assertTrue(!empty($items));
  91. $fileinfo = $this->view->getFileInfo($this->filename);
  92. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  93. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  94. $fileinfo = $this->view->getFileInfo($this->folder);
  95. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  96. }
  97. /**
  98. * @medium
  99. */
  100. public function testCreateShareInvalidPermissions() {
  101. // simulate a post request
  102. $_POST['path'] = $this->filename;
  103. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  104. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  105. $_POST['permissions'] = \OCP\Constants::PERMISSION_SHARE;
  106. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  107. // share was successful?
  108. $this->assertFalse($result->succeeded());
  109. $this->assertEquals(400, $result->getStatusCode());
  110. $shares = \OCP\Share::getItemShared('file', null);
  111. $this->assertCount(0, $shares);
  112. $fileinfo = $this->view->getFileInfo($this->filename);
  113. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  114. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  115. }
  116. function testEnfoceLinkPassword() {
  117. $appConfig = \OC::$server->getAppConfig();
  118. $appConfig->setValue('core', 'shareapi_enforce_links_password', 'yes');
  119. // don't allow to share link without a password
  120. $_POST['path'] = $this->folder;
  121. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  122. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  123. $this->assertFalse($result->succeeded());
  124. // don't allow to share link without a empty password
  125. $_POST['path'] = $this->folder;
  126. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  127. $_POST['password'] = '';
  128. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  129. $this->assertFalse($result->succeeded());
  130. // share with password should succeed
  131. $_POST['path'] = $this->folder;
  132. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  133. $_POST['password'] = 'foo';
  134. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  135. $this->assertTrue($result->succeeded());
  136. $data = $result->getData();
  137. // setting new password should succeed
  138. $params = array();
  139. $params['id'] = $data['id'];
  140. $params['_put'] = array();
  141. $params['_put']['password'] = 'bar';
  142. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  143. $this->assertTrue($result->succeeded());
  144. // removing password should fail
  145. $params = array();
  146. $params['id'] = $data['id'];
  147. $params['_put'] = array();
  148. $params['_put']['password'] = '';
  149. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  150. $this->assertFalse($result->succeeded());
  151. // cleanup
  152. $fileinfo = $this->view->getFileInfo($this->folder);
  153. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  154. $appConfig->setValue('core', 'shareapi_enforce_links_password', 'no');
  155. }
  156. /**
  157. * @medium
  158. */
  159. function testSharePermissions() {
  160. // sharing file to a user should work if shareapi_exclude_groups is set
  161. // to no
  162. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'no');
  163. $_POST['path'] = $this->filename;
  164. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  165. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  166. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  167. $this->assertTrue($result->succeeded());
  168. $data = $result->getData();
  169. $share = $this->getShareFromId($data['id']);
  170. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  171. $this->assertTrue(!empty($items));
  172. $fileinfo = $this->view->getFileInfo($this->filename);
  173. $result = \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  174. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  175. $this->assertTrue($result);
  176. // exclude groups, but not the group the user belongs to. Sharing should still work
  177. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'yes');
  178. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', 'admin,group1,group2');
  179. $_POST['path'] = $this->filename;
  180. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  181. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  182. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  183. $this->assertTrue($result->succeeded());
  184. $data = $result->getData();
  185. $share = $this->getShareFromId($data['id']);
  186. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  187. $this->assertTrue(!empty($items));
  188. $fileinfo = $this->view->getFileInfo($this->filename);
  189. $result = \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  190. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  191. $this->assertTrue($result);
  192. // now we exclude the group the user belongs to ('group'), sharing should fail now
  193. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', 'admin,group');
  194. $_POST['path'] = $this->filename;
  195. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  196. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  197. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  198. $this->assertFalse($result->succeeded());
  199. // cleanup
  200. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'no');
  201. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', '');
  202. }
  203. /**
  204. * @medium
  205. * @depends testCreateShare
  206. */
  207. function testGetAllShares() {
  208. $fileinfo = $this->view->getFileInfo($this->filename);
  209. \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  210. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  211. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  212. $this->assertTrue($result->succeeded());
  213. // test should return two shares created from testCreateShare()
  214. $this->assertTrue(count($result->getData()) === 1);
  215. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  216. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  217. }
  218. function testGetAllSharesWithMe() {
  219. $fileinfo1 = $this->view->getFileInfo($this->filename);
  220. $fileinfo2 = $this->view->getFileInfo($this->folder.$this->filename);
  221. \OCP\Share::shareItem('file', $fileinfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  222. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  223. \OCP\Share::shareItem('folder', $fileinfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  224. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  225. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  226. $_GET['shared_with_me'] = 1;
  227. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  228. $this->assertTrue($result->succeeded());
  229. $this->assertTrue(count($result->getData()) === 2);
  230. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  231. \OCP\Share::unshare('file', $fileinfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  232. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  233. \OCP\Share::unshare('folder', $fileinfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  234. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  235. }
  236. /**
  237. * @medium
  238. * @depends testCreateShare
  239. */
  240. function testPublicLinkUrl() {
  241. // simulate a post request
  242. $_POST['path'] = $this->folder;
  243. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  244. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  245. $this->assertTrue($result->succeeded());
  246. $data = $result->getData();
  247. // check if we have a token
  248. $this->assertTrue(is_string($data['token']));
  249. $id = $data['id'];
  250. // check for correct link
  251. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  252. $this->assertEquals($url, $data['url']);
  253. // check for link in getall shares
  254. $result = \OCA\Files_Sharing\API\Local::getAllShares([]);
  255. $this->assertTrue($result->succeeded());
  256. $data = $result->getData();
  257. $this->assertEquals($url, current($data)['url']);
  258. // check for path
  259. $_GET['path'] = $this->folder;
  260. $result = \OCA\Files_Sharing\API\Local::getAllShares([]);
  261. $this->assertTrue($result->succeeded());
  262. $data = $result->getData();
  263. $this->assertEquals($url, current($data)['url']);
  264. // check in share id
  265. $result = \OCA\Files_Sharing\API\Local::getShare(['id' => $id]);
  266. $this->assertTrue($result->succeeded());
  267. $data = $result->getData();
  268. $this->assertEquals($url, current($data)['url']);
  269. //Clean up share
  270. $fileinfo = $this->view->getFileInfo($this->folder);
  271. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  272. }
  273. /**
  274. * @medium
  275. * @depends testCreateShare
  276. */
  277. function testGetShareFromSource() {
  278. $fileInfo = $this->view->getFileInfo($this->filename);
  279. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  280. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  281. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  282. null, 1);
  283. $_GET['path'] = $this->filename;
  284. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  285. $this->assertTrue($result->succeeded());
  286. // test should return one share created from testCreateShare()
  287. $this->assertTrue(count($result->getData()) === 2);
  288. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  289. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  290. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  291. }
  292. /**
  293. * @medium
  294. * @depends testCreateShare
  295. */
  296. function testGetShareFromSourceWithReshares() {
  297. $fileInfo = $this->view->getFileInfo($this->filename);
  298. // share the file as user1 to user2
  299. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  300. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  301. // login as user2 and reshare the file to user3
  302. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  303. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  304. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  305. // login as user1 again
  306. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  307. $_GET['path'] = $this->filename;
  308. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  309. $this->assertTrue($result->succeeded());
  310. // test should return one share
  311. $this->assertTrue(count($result->getData()) === 1);
  312. // now also ask for the reshares
  313. $_GET['reshares'] = 'true';
  314. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  315. $this->assertTrue($result->succeeded());
  316. // now we should get two shares, the initial share and the reshare
  317. $this->assertTrue(count($result->getData()) === 2);
  318. // unshare files again
  319. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  320. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  321. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
  322. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  323. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  324. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  325. }
  326. /**
  327. * @medium
  328. * @depends testCreateShare
  329. */
  330. function testGetShareFromId() {
  331. $fileInfo = $this->view->getFileInfo($this->filename);
  332. $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  333. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  334. // share was successful?
  335. $this->assertTrue($result);
  336. // get item to determine share ID
  337. $result = \OCP\Share::getItemShared('file', $fileInfo['fileid']);
  338. $this->assertEquals(1, count($result));
  339. // get first element
  340. $share = reset($result);
  341. // call getShare() with share ID
  342. $params = array('id' => $share['id']);
  343. $result = \OCA\Files_Sharing\API\Local::getShare($params);
  344. $this->assertTrue($result->succeeded());
  345. // test should return one share created from testCreateShare()
  346. $this->assertEquals(1, count($result->getData()));
  347. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  348. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  349. }
  350. /**
  351. * @medium
  352. */
  353. function testGetShareFromFolder() {
  354. $fileInfo1 = $this->view->getFileInfo($this->filename);
  355. $fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
  356. $result = \OCP\Share::shareItem('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  357. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  358. // share was successful?
  359. $this->assertTrue($result);
  360. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  361. null, 1);
  362. // share was successful?
  363. $this->assertTrue(is_string($result));
  364. $_GET['path'] = $this->folder;
  365. $_GET['subfiles'] = 'true';
  366. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  367. $this->assertTrue($result->succeeded());
  368. // test should return one share within $this->folder
  369. $this->assertTrue(count($result->getData()) === 1);
  370. \OCP\Share::unshare('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  371. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  372. \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  373. }
  374. function testGetShareFromFolderWithFile() {
  375. $fileInfo1 = $this->view->getFileInfo($this->filename);
  376. $result = \OCP\Share::shareItem('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  377. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  378. // share was successful?
  379. $this->assertTrue($result);
  380. $_GET = [
  381. 'path' => $this->filename,
  382. 'subfiles' => 1
  383. ];
  384. $result = \OCA\Files_Sharing\API\Local::getAllShares([]);
  385. $this->assertFalse($result->succeeded());
  386. $this->assertEquals(400, $result->getStatusCode());
  387. $this->assertEquals('not a directory', $result->getMeta()['message']);
  388. \OCP\Share::unshare('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  389. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  390. }
  391. /**
  392. * share a folder, than reshare a file within the shared folder and check if we construct the correct path
  393. * @medium
  394. */
  395. function testGetShareFromFolderReshares() {
  396. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  397. $fileInfo1 = $this->view->getFileInfo($this->folder);
  398. $fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
  399. $fileInfo3 = $this->view->getFileInfo($this->folder.'/' . $this->subfolder . '/' .$this->filename);
  400. // share root folder to user2
  401. $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  402. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  403. // share was successful?
  404. $this->assertTrue($result);
  405. // login as user2
  406. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  407. // share file in root folder
  408. $result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  409. // share was successful?
  410. $this->assertTrue(is_string($result));
  411. // share file in subfolder
  412. $result = \OCP\Share::shareItem('file', $fileInfo3['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  413. // share was successful?
  414. $this->assertTrue(is_string($result));
  415. $testValues=array(
  416. array('query' => $this->folder,
  417. 'expectedResult' => $this->folder . $this->filename),
  418. array('query' => $this->folder . $this->subfolder,
  419. 'expectedResult' => $this->folder . $this->subfolder . $this->filename),
  420. );
  421. foreach ($testValues as $value) {
  422. $_GET['path'] = $value['query'];
  423. $_GET['subfiles'] = 'true';
  424. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  425. $this->assertTrue($result->succeeded());
  426. // test should return one share within $this->folder
  427. $data = $result->getData();
  428. $this->assertEquals($value['expectedResult'], $data[0]['path']);
  429. }
  430. // cleanup
  431. \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  432. \OCP\Share::unshare('file', $fileInfo3['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  433. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  434. \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  435. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  436. }
  437. /**
  438. * reshare a sub folder and check if we get the correct path
  439. * @medium
  440. */
  441. function testGetShareFromSubFolderReShares() {
  442. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  443. $fileInfo = $this->view->getFileInfo($this->folder . $this->subfolder);
  444. // share sub-folder to user2
  445. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  446. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  447. // share was successful?
  448. $this->assertTrue($result);
  449. // login as user2
  450. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  451. // reshare subfolder
  452. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  453. // share was successful?
  454. $this->assertTrue(is_string($result));
  455. $_GET['path'] = '/';
  456. $_GET['subfiles'] = 'true';
  457. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  458. $this->assertTrue($result->succeeded());
  459. // test should return one share within $this->folder
  460. $data = $result->getData();
  461. // we should get exactly one result
  462. $this->assertEquals(1, count($data));
  463. $expectedPath = $this->subfolder;
  464. $this->assertEquals($expectedPath, $data[0]['path']);
  465. // cleanup
  466. $result = \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  467. $this->assertTrue($result);
  468. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  469. $result = \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  470. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  471. $this->assertTrue($result);
  472. }
  473. /**
  474. * test re-re-share of folder if the path gets constructed correctly
  475. * @medium
  476. */
  477. function testGetShareFromFolderReReShares() {
  478. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  479. $fileInfo1 = $this->view->getFileInfo($this->folder . $this->subfolder);
  480. $fileInfo2 = $this->view->getFileInfo($this->folder . $this->subfolder . $this->subsubfolder);
  481. // share sub-folder to user2
  482. $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  483. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  484. // share was successful?
  485. $this->assertTrue($result);
  486. // login as user2
  487. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  488. // reshare subsubfolder
  489. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  490. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  491. // share was successful?
  492. $this->assertTrue($result);
  493. // login as user3
  494. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  495. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  496. // share was successful?
  497. $this->assertTrue(is_string($result));
  498. $_GET['path'] = '/';
  499. $_GET['subfiles'] = 'true';
  500. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  501. $this->assertTrue($result->succeeded());
  502. // test should return one share within $this->folder
  503. $data = $result->getData();
  504. // we should get exactly one result
  505. $this->assertEquals(1, count($data));
  506. $expectedPath = $this->subsubfolder;
  507. $this->assertEquals($expectedPath, $data[0]['path']);
  508. // cleanup
  509. $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  510. $this->assertTrue($result);
  511. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  512. $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  513. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
  514. $this->assertTrue($result);
  515. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  516. $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  517. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  518. $this->assertTrue($result);
  519. }
  520. /**
  521. * test multiple shared folder if the path gets constructed correctly
  522. * @medium
  523. */
  524. function testGetShareMultipleSharedFolder() {
  525. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  526. $fileInfo1 = $this->view->getFileInfo($this->folder);
  527. $fileInfo2 = $this->view->getFileInfo($this->folder . $this->subfolder);
  528. // share sub-folder to user2
  529. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  530. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  531. // share was successful?
  532. $this->assertTrue($result);
  533. // share folder to user2
  534. $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  535. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  536. // share was successful?
  537. $this->assertTrue($result);
  538. // login as user2
  539. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  540. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  541. // share was successful?
  542. $this->assertTrue(is_string($result));
  543. // ask for subfolder
  544. $expectedPath1 = $this->subfolder;
  545. $_GET['path'] = $expectedPath1;
  546. $result1 = \OCA\Files_Sharing\API\Local::getAllShares(array());
  547. $this->assertTrue($result1->succeeded());
  548. // test should return one share within $this->folder
  549. $data1 = $result1->getData();
  550. $share1 = reset($data1);
  551. // ask for folder/subfolder
  552. $expectedPath2 = $this->folder . $this->subfolder;
  553. $_GET['path'] = $expectedPath2;
  554. $result2 = \OCA\Files_Sharing\API\Local::getAllShares(array());
  555. $this->assertTrue($result2->succeeded());
  556. // test should return one share within $this->folder
  557. $data2 = $result2->getData();
  558. $share2 = reset($data2);
  559. // validate results
  560. // we should get exactly one result each time
  561. $this->assertEquals(1, count($data1));
  562. $this->assertEquals(1, count($data2));
  563. $this->assertEquals($expectedPath1, $share1['path']);
  564. $this->assertEquals($expectedPath2, $share2['path']);
  565. // cleanup
  566. $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  567. $this->assertTrue($result);
  568. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  569. $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  570. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  571. $this->assertTrue($result);
  572. $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  573. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  574. $this->assertTrue($result);
  575. }
  576. /**
  577. * test re-re-share of folder if the path gets constructed correctly
  578. * @medium
  579. */
  580. function testGetShareFromFileReReShares() {
  581. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  582. $fileInfo1 = $this->view->getFileInfo($this->folder . $this->subfolder);
  583. $fileInfo2 = $this->view->getFileInfo($this->folder. $this->subfolder . $this->filename);
  584. // share sub-folder to user2
  585. $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  586. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  587. // share was successful?
  588. $this->assertTrue($result);
  589. // login as user2
  590. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  591. // reshare subsubfolder
  592. $result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  593. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  594. // share was successful?
  595. $this->assertTrue($result);
  596. // login as user3
  597. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  598. $result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  599. // share was successful?
  600. $this->assertTrue(is_string($result));
  601. $_GET['path'] = '/';
  602. $_GET['subfiles'] = 'true';
  603. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  604. $this->assertTrue($result->succeeded());
  605. // test should return one share within $this->folder
  606. $data = $result->getData();
  607. // we should get exactly one result
  608. $this->assertEquals(1, count($data));
  609. $expectedPath = $this->filename;
  610. $this->assertEquals($expectedPath, $data[0]['path']);
  611. // cleanup
  612. $result = \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  613. $this->assertTrue($result);
  614. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  615. $result = \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  616. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
  617. $this->assertTrue($result);
  618. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  619. $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  620. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  621. $this->assertTrue($result);
  622. }
  623. /**
  624. * @medium
  625. */
  626. function testGetShareFromUnknownId() {
  627. $params = array('id' => 0);
  628. $result = \OCA\Files_Sharing\API\Local::getShare($params);
  629. $this->assertEquals(404, $result->getStatusCode());
  630. $meta = $result->getMeta();
  631. $this->assertEquals('share doesn\'t exist', $meta['message']);
  632. }
  633. /**
  634. * @medium
  635. * @depends testCreateShare
  636. */
  637. function testUpdateShare() {
  638. $fileInfo = $this->view->getFileInfo($this->filename);
  639. $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  640. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);
  641. // share was successful?
  642. $this->assertTrue($result);
  643. $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  644. null, 1);
  645. // share was successful?
  646. $this->assertTrue(is_string($result));
  647. $items = \OCP\Share::getItemShared('file', null);
  648. // make sure that we found a link share and a user share
  649. $this->assertEquals(count($items), 2);
  650. $linkShare = null;
  651. $userShare = null;
  652. foreach ($items as $item) {
  653. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  654. $linkShare = $item;
  655. }
  656. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_USER) {
  657. $userShare = $item;
  658. }
  659. }
  660. // make sure that we found a link share and a user share
  661. $this->assertTrue(is_array($linkShare));
  662. $this->assertTrue(is_array($userShare));
  663. // check if share have expected permissions, single shared files never have
  664. // delete permissions
  665. $this->assertEquals(\OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_DELETE, $userShare['permissions']);
  666. // update permissions
  667. $params = array();
  668. $params['id'] = $userShare['id'];
  669. $params['_put'] = array();
  670. $params['_put']['permissions'] = 1;
  671. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  672. $meta = $result->getMeta();
  673. $this->assertTrue($result->succeeded(), $meta['message']);
  674. $items = \OCP\Share::getItemShared('file', $userShare['file_source']);
  675. $newUserShare = null;
  676. foreach ($items as $item) {
  677. if ($item['share_with'] === \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2) {
  678. $newUserShare = $item;
  679. break;
  680. }
  681. }
  682. $this->assertTrue(is_array($newUserShare));
  683. $this->assertEquals('1', $newUserShare['permissions']);
  684. // update password for link share
  685. $this->assertTrue(empty($linkShare['share_with']));
  686. $params = array();
  687. $params['id'] = $linkShare['id'];
  688. $params['_put'] = array();
  689. $params['_put']['password'] = 'foo';
  690. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  691. $this->assertTrue($result->succeeded());
  692. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  693. $newLinkShare = null;
  694. foreach ($items as $item) {
  695. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  696. $newLinkShare = $item;
  697. break;
  698. }
  699. }
  700. $this->assertTrue(is_array($newLinkShare));
  701. $this->assertTrue(!empty($newLinkShare['share_with']));
  702. // Remove password for link share
  703. $params = array();
  704. $params['id'] = $linkShare['id'];
  705. $params['_put'] = array();
  706. $params['_put']['password'] = '';
  707. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  708. $this->assertTrue($result->succeeded());
  709. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  710. $newLinkShare = null;
  711. foreach ($items as $item) {
  712. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  713. $newLinkShare = $item;
  714. break;
  715. }
  716. }
  717. $this->assertTrue(is_array($newLinkShare));
  718. $this->assertTrue(empty($newLinkShare['share_with']));
  719. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  720. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  721. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  722. }
  723. /**
  724. * @medium
  725. * @depends testCreateShare
  726. */
  727. public function testUpdateShareInvalidPermissions() {
  728. $fileInfo = $this->view->getFileInfo($this->filename);
  729. $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  730. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);
  731. // share was successful?
  732. $this->assertTrue($result);
  733. $share = \OCP\Share::getItemShared('file', null);
  734. $this->assertCount(1, $share);
  735. $share = reset($share);
  736. // check if share have expected permissions, single shared files never have
  737. // delete permissions
  738. $this->assertEquals(\OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_DELETE, $share['permissions']);
  739. // update permissions
  740. $params = [];
  741. $params['id'] = $share['id'];
  742. $params['_put'] = [];
  743. $params['_put']['permissions'] = \OCP\Constants::PERMISSION_SHARE;
  744. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  745. //Updating should fail with 400
  746. $this->assertFalse($result->succeeded());
  747. $this->assertEquals(400, $result->getStatusCode());
  748. $share = \OCP\Share::getItemShared('file', $share['file_source']);
  749. $share = reset($share);
  750. //Permissions should not have changed!
  751. $this->assertEquals(\OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_DELETE, $share['permissions']);
  752. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  753. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  754. }
  755. /**
  756. * @medium
  757. */
  758. function testUpdateShareUpload() {
  759. $fileInfo = $this->view->getFileInfo($this->folder);
  760. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  761. null, 1);
  762. // share was successful?
  763. $this->assertTrue(is_string($result));
  764. $items = \OCP\Share::getItemShared('file', null);
  765. // make sure that we found a link share and a user share
  766. $this->assertEquals(1, count($items));
  767. $linkShare = null;
  768. foreach ($items as $item) {
  769. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  770. $linkShare = $item;
  771. }
  772. }
  773. // make sure that we found a link share
  774. $this->assertTrue(is_array($linkShare));
  775. // update public upload
  776. $params = array();
  777. $params['id'] = $linkShare['id'];
  778. $params['_put'] = array();
  779. $params['_put']['publicUpload'] = 'true';
  780. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  781. $this->assertTrue($result->succeeded());
  782. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  783. $updatedLinkShare = null;
  784. foreach ($items as $item) {
  785. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  786. $updatedLinkShare = $item;
  787. break;
  788. }
  789. }
  790. $this->assertTrue(is_array($updatedLinkShare));
  791. $this->assertEquals(7, $updatedLinkShare['permissions']);
  792. // cleanup
  793. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  794. }
  795. /**
  796. * @medium
  797. */
  798. function testUpdateShareExpireDate() {
  799. $fileInfo = $this->view->getFileInfo($this->folder);
  800. $config = \OC::$server->getConfig();
  801. // enforce expire date, by default 7 days after the file was shared
  802. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  803. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  804. $dateWithinRange = new \DateTime();
  805. $dateWithinRange->add(new \DateInterval('P5D'));
  806. $dateOutOfRange = new \DateTime();
  807. $dateOutOfRange->add(new \DateInterval('P8D'));
  808. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  809. null, 1);
  810. // share was successful?
  811. $this->assertTrue(is_string($result));
  812. $items = \OCP\Share::getItemShared('file', null);
  813. // make sure that we found a link share
  814. $this->assertEquals(1, count($items));
  815. $linkShare = reset($items);
  816. // update expire date to a valid value
  817. $params = array();
  818. $params['id'] = $linkShare['id'];
  819. $params['_put'] = array();
  820. $params['_put']['expireDate'] = $dateWithinRange->format('Y-m-d');
  821. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  822. $this->assertTrue($result->succeeded());
  823. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  824. $updatedLinkShare = reset($items);
  825. // date should be changed
  826. $this->assertTrue(is_array($updatedLinkShare));
  827. $this->assertEquals($dateWithinRange->format('Y-m-d') . ' 00:00:00', $updatedLinkShare['expiration']);
  828. // update expire date to a value out of range
  829. $params = array();
  830. $params['id'] = $linkShare['id'];
  831. $params['_put'] = array();
  832. $params['_put']['expireDate'] = $dateOutOfRange->format('Y-m-d');
  833. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  834. $this->assertFalse($result->succeeded());
  835. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  836. $updatedLinkShare = reset($items);
  837. // date shouldn't be changed
  838. $this->assertTrue(is_array($updatedLinkShare));
  839. $this->assertEquals($dateWithinRange->format('Y-m-d') . ' 00:00:00', $updatedLinkShare['expiration']);
  840. // Try to remove expire date
  841. $params = array();
  842. $params['id'] = $linkShare['id'];
  843. $params['_put'] = ['expireDate' => ''];
  844. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  845. $this->assertFalse($result->succeeded());
  846. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  847. $updatedLinkShare = reset($items);
  848. // date shouldn't be changed
  849. $this->assertTrue(is_array($updatedLinkShare));
  850. $this->assertEquals($dateWithinRange->format('Y-m-d') . ' 00:00:00', $updatedLinkShare['expiration']);
  851. // cleanup
  852. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  853. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  854. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  855. }
  856. /**
  857. * @medium
  858. * @depends testCreateShare
  859. */
  860. function testDeleteShare() {
  861. $fileInfo = $this->view->getFileInfo($this->filename);
  862. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  863. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  864. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  865. null, 1);
  866. $items = \OCP\Share::getItemShared('file', null);
  867. $this->assertEquals(2, count($items));
  868. foreach ($items as $item) {
  869. $result = \OCA\Files_Sharing\API\Local::deleteShare(array('id' => $item['id']));
  870. $this->assertTrue($result->succeeded());
  871. }
  872. $itemsAfterDelete = \OCP\Share::getItemShared('file', null);
  873. $this->assertTrue(empty($itemsAfterDelete));
  874. }
  875. /**
  876. * test unshare of a reshared file
  877. */
  878. function testDeleteReshare() {
  879. // user 1 shares a folder with user2
  880. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  881. $fileInfo1 = $this->view->getFileInfo($this->folder);
  882. $fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
  883. $result1 = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  884. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  885. $this->assertTrue($result1);
  886. // user2 shares a file from the folder as link
  887. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  888. $result2 = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  889. $this->assertTrue(is_string($result2));
  890. // test if we can unshare the link again
  891. $items = \OCP\Share::getItemShared('file', null);
  892. $this->assertEquals(1, count($items));
  893. $item = reset($items);
  894. $result3 = \OCA\Files_Sharing\API\Local::deleteShare(array('id' => $item['id']));
  895. $this->assertTrue($result3->succeeded());
  896. // cleanup
  897. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  898. $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  899. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  900. $this->assertTrue($result);
  901. }
  902. /**
  903. * share a folder which contains a share mount point, should be forbidden
  904. */
  905. public function testShareFolderWithAMountPoint() {
  906. // user 1 shares a folder with user2
  907. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  908. $fileInfo = $this->view->getFileInfo($this->folder);
  909. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  910. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  911. $this->assertTrue($result);
  912. // user2 shares a file from the folder as link
  913. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  914. $view = new \OC\Files\View('/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2 . '/files');
  915. $view->mkdir("localDir");
  916. // move mount point to the folder "localDir"
  917. $result = $view->rename($this->folder, 'localDir/'.$this->folder);
  918. $this->assertTrue($result !== false);
  919. // try to share "localDir"
  920. $fileInfo2 = $view->getFileInfo('localDir');
  921. $this->assertTrue($fileInfo2 instanceof \OC\Files\FileInfo);
  922. try {
  923. $result2 = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  924. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  925. } catch (\Exception $e) {
  926. $result2 = false;
  927. }
  928. $this->assertFalse($result2);
  929. //cleanup
  930. $result = $view->rename('localDir/' . $this->folder, $this->folder);
  931. $this->assertTrue($result !== false);
  932. $view->unlink('localDir');
  933. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  934. \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  935. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  936. }
  937. /**
  938. * Post init mount points hook for mounting simulated ext storage
  939. */
  940. public static function initTestMountPointsHook($data) {
  941. if ($data['user'] === \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1) {
  942. \OC\Files\Filesystem::mount(self::$tempStorage, array(), '/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME);
  943. }
  944. }
  945. /**
  946. * Tests mounting a folder that is an external storage mount point.
  947. */
  948. public function testShareStorageMountPoint() {
  949. self::$tempStorage = new \OC\Files\Storage\Temporary(array());
  950. self::$tempStorage->file_put_contents('test.txt', 'abcdef');
  951. self::$tempStorage->getScanner()->scan('');
  952. // needed because the sharing code sometimes switches the user internally and mounts the user's
  953. // storages. In our case the temp storage isn't mounted automatically, so doing it in the post hook
  954. // (similar to how ext storage works)
  955. OCP\Util::connectHook('OC_Filesystem', 'post_initMountPoints', '\Test_Files_Sharing_Api', 'initTestMountPointsHook');
  956. // logging in will auto-mount the temp storage for user1 as well
  957. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  958. $fileInfo = $this->view->getFileInfo($this->folder);
  959. // user 1 shares the mount point folder with user2
  960. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  961. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  962. $this->assertTrue($result);
  963. // user2: check that mount point name appears correctly
  964. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  965. $view = new \OC\Files\View('/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2 . '/files');
  966. $this->assertTrue($view->file_exists($this->folder));
  967. $this->assertTrue($view->file_exists($this->folder . '/test.txt'));
  968. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  969. \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  970. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  971. \OC_Hook::clear('OC_Filesystem', 'post_initMountPoints', '\Test_Files_Sharing_Api', 'initTestMountPointsHook');
  972. }
  973. /**
  974. * @expectedException \Exception
  975. */
  976. public function testShareNonExisting() {
  977. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  978. $id = PHP_INT_MAX - 1;
  979. \OCP\Share::shareItem('file', $id, \OCP\Share::SHARE_TYPE_LINK, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  980. }
  981. /**
  982. * @expectedException \Exception
  983. */
  984. public function testShareNotOwner() {
  985. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  986. \OC\Files\Filesystem::file_put_contents('foo.txt', 'bar');
  987. $info = \OC\Files\Filesystem::getFileInfo('foo.txt');
  988. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  989. \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_LINK, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  990. }
  991. public function testDefaultExpireDate() {
  992. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  993. // TODO drop this once all code paths use the DI version - otherwise
  994. // the cache inside this config object is out of date because
  995. // OC_Appconfig is used and bypasses this cache which lead to integrity
  996. // constraint violations
  997. $config = \OC::$server->getConfig();
  998. $config->deleteAppValue('core', 'shareapi_default_expire_date');
  999. $config->deleteAppValue('core', 'shareapi_enforce_expire_date');
  1000. $config->deleteAppValue('core', 'shareapi_expire_after_n_days');
  1001. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  1002. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  1003. $config->setAppValue('core', 'shareapi_expire_after_n_days', '2');
  1004. // default expire date is set to 2 days
  1005. // the time when the share was created is set to 3 days in the past
  1006. // user defined expire date is set to +2 days from now on
  1007. // -> link should be already expired by the default expire date but the user
  1008. // share should still exists.
  1009. $now = time();
  1010. $dateFormat = 'Y-m-d H:i:s';
  1011. $shareCreated = $now - 3 * 24 * 60 * 60;
  1012. $expireDate = date($dateFormat, $now + 2 * 24 * 60 * 60);
  1013. $info = OC\Files\Filesystem::getFileInfo($this->filename);
  1014. $this->assertTrue($info instanceof \OC\Files\FileInfo);
  1015. $result = \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
  1016. $this->assertTrue(is_string($result));
  1017. $result = \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  1018. $this->assertTrue($result);
  1019. $result = \OCP\Share::setExpirationDate('file', $info->getId() , $expireDate, $now);
  1020. $this->assertTrue($result);
  1021. //manipulate stime so that both shares are older then the default expire date
  1022. $statement = "UPDATE `*PREFIX*share` SET `stime` = ? WHERE `share_type` = ?";
  1023. $query = \OCP\DB::prepare($statement);
  1024. $result = $query->execute(array($shareCreated, \OCP\Share::SHARE_TYPE_LINK));
  1025. $this->assertSame(1, $result);
  1026. $query = \OCP\DB::prepare($statement);
  1027. $result = $query->execute(array($shareCreated, \OCP\Share::SHARE_TYPE_USER));
  1028. $this->assertSame(1, $result);
  1029. // now the link share should expire because of enforced default expire date
  1030. // the user share should still exist
  1031. $result = \OCP\Share::getItemShared('file', $info->getId());
  1032. $this->assertTrue(is_array($result));
  1033. $this->assertSame(1, count($result));
  1034. $share = reset($result);
  1035. $this->assertSame(\OCP\Share::SHARE_TYPE_USER, $share['share_type']);
  1036. //cleanup
  1037. $result = \OCP\Share::unshare('file', $info->getId(), \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  1038. $this->assertTrue($result);
  1039. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1040. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1041. }
  1042. }