You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.php 30KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Bjoern Schiessle
  6. * @copyright 2013 Bjoern Schiessle <schiessle@owncloud.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. require_once __DIR__ . '/base.php';
  23. use OCA\Files\Share;
  24. /**
  25. * Class Test_Files_Sharing_Api
  26. */
  27. class Test_Files_Sharing_Api extends Test_Files_Sharing_Base {
  28. const TEST_FOLDER_NAME = '/folder_share_api_test';
  29. private static $tempStorage;
  30. function setUp() {
  31. parent::setUp();
  32. $this->folder = self::TEST_FOLDER_NAME;
  33. $this->subfolder = '/subfolder_share_api_test';
  34. $this->subsubfolder = '/subsubfolder_share_api_test';
  35. $this->filename = '/share-api-test.txt';
  36. // save file with content
  37. $this->view->file_put_contents($this->filename, $this->data);
  38. $this->view->mkdir($this->folder);
  39. $this->view->mkdir($this->folder . $this->subfolder);
  40. $this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
  41. $this->view->file_put_contents($this->folder.$this->filename, $this->data);
  42. $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
  43. }
  44. function tearDown() {
  45. $this->view->unlink($this->filename);
  46. $this->view->deleteAll($this->folder);
  47. self::$tempStorage = null;
  48. parent::tearDown();
  49. }
  50. /**
  51. * @medium
  52. */
  53. function testCreateShare() {
  54. // share to user
  55. // simulate a post request
  56. $_POST['path'] = $this->filename;
  57. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  58. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  59. $result = Share\Api::createShare(array());
  60. $this->assertTrue($result->succeeded());
  61. $data = $result->getData();
  62. $share = $this->getShareFromId($data['id']);
  63. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  64. $this->assertTrue(!empty($items));
  65. // share link
  66. // simulate a post request
  67. $_POST['path'] = $this->folder;
  68. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  69. $result = Share\Api::createShare(array());
  70. // check if API call was successful
  71. $this->assertTrue($result->succeeded());
  72. $data = $result->getData();
  73. // check if we have a token
  74. $this->assertTrue(is_string($data['token']));
  75. $share = $this->getShareFromId($data['id']);
  76. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  77. $this->assertTrue(!empty($items));
  78. $fileinfo = $this->view->getFileInfo($this->filename);
  79. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  80. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  81. $fileinfo = $this->view->getFileInfo($this->folder);
  82. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  83. }
  84. /**
  85. * @medium
  86. * @depends testCreateShare
  87. */
  88. function testGetAllShares() {
  89. $fileinfo = $this->view->getFileInfo($this->filename);
  90. \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  91. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  92. $result = Share\Api::getAllShares(array());
  93. $this->assertTrue($result->succeeded());
  94. // test should return two shares created from testCreateShare()
  95. $this->assertTrue(count($result->getData()) === 1);
  96. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  97. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  98. }
  99. /**
  100. * @medium
  101. * @depends testCreateShare
  102. */
  103. function testGetShareFromSource() {
  104. $fileInfo = $this->view->getFileInfo($this->filename);
  105. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  106. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  107. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  108. null, 1);
  109. $_GET['path'] = $this->filename;
  110. $result = Share\Api::getAllShares(array());
  111. $this->assertTrue($result->succeeded());
  112. // test should return one share created from testCreateShare()
  113. $this->assertTrue(count($result->getData()) === 2);
  114. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  115. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  116. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  117. }
  118. /**
  119. * @medium
  120. * @depends testCreateShare
  121. */
  122. function testGetShareFromSourceWithReshares() {
  123. $fileInfo = $this->view->getFileInfo($this->filename);
  124. // share the file as user1 to user2
  125. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  126. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  127. // login as user2 and reshare the file to user3
  128. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  129. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  130. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  131. // login as user1 again
  132. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  133. $_GET['path'] = $this->filename;
  134. $result = Share\Api::getAllShares(array());
  135. $this->assertTrue($result->succeeded());
  136. // test should return one share
  137. $this->assertTrue(count($result->getData()) === 1);
  138. // now also ask for the reshares
  139. $_GET['reshares'] = 'true';
  140. $result = Share\Api::getAllShares(array());
  141. $this->assertTrue($result->succeeded());
  142. // now we should get two shares, the initial share and the reshare
  143. $this->assertTrue(count($result->getData()) === 2);
  144. // unshare files again
  145. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  146. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  147. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
  148. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  149. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  150. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  151. }
  152. /**
  153. * @medium
  154. * @depends testCreateShare
  155. */
  156. function testGetShareFromId() {
  157. $fileInfo = $this->view->getFileInfo($this->filename);
  158. $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  159. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  160. // share was successful?
  161. $this->assertTrue($result);
  162. // get item to determine share ID
  163. $result = \OCP\Share::getItemShared('file', $fileInfo['fileid']);
  164. $this->assertEquals(1, count($result));
  165. // get first element
  166. $share = reset($result);
  167. // call getShare() with share ID
  168. $params = array('id' => $share['id']);
  169. $result = Share\Api::getShare($params);
  170. $this->assertTrue($result->succeeded());
  171. // test should return one share created from testCreateShare()
  172. $this->assertEquals(1, count($result->getData()));
  173. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  174. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  175. }
  176. /**
  177. * @medium
  178. */
  179. function testGetShareFromFolder() {
  180. $fileInfo1 = $this->view->getFileInfo($this->filename);
  181. $fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
  182. $result = \OCP\Share::shareItem('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  183. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  184. // share was successful?
  185. $this->assertTrue($result);
  186. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  187. null, 1);
  188. // share was successful?
  189. $this->assertTrue(is_string($result));
  190. $_GET['path'] = $this->folder;
  191. $_GET['subfiles'] = 'true';
  192. $result = Share\Api::getAllShares(array());
  193. $this->assertTrue($result->succeeded());
  194. // test should return one share within $this->folder
  195. $this->assertTrue(count($result->getData()) === 1);
  196. \OCP\Share::unshare('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  197. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  198. \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  199. }
  200. /**
  201. * @brief share a folder, than reshare a file within the shared folder and check if we construct the correct path
  202. * @medium
  203. */
  204. function testGetShareFromFolderReshares() {
  205. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  206. $fileInfo1 = $this->view->getFileInfo($this->folder);
  207. $fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
  208. $fileInfo3 = $this->view->getFileInfo($this->folder.'/' . $this->subfolder . '/' .$this->filename);
  209. // share root folder to user2
  210. $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  211. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  212. // share was successful?
  213. $this->assertTrue($result);
  214. // login as user2
  215. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  216. // share file in root folder
  217. $result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  218. // share was successful?
  219. $this->assertTrue(is_string($result));
  220. // share file in subfolder
  221. $result = \OCP\Share::shareItem('file', $fileInfo3['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  222. // share was successful?
  223. $this->assertTrue(is_string($result));
  224. $testValues=array(
  225. array('query' => $this->folder,
  226. 'expectedResult' => $this->folder . $this->filename),
  227. array('query' => $this->folder . $this->subfolder,
  228. 'expectedResult' => $this->folder . $this->subfolder . $this->filename),
  229. );
  230. foreach ($testValues as $value) {
  231. $_GET['path'] = $value['query'];
  232. $_GET['subfiles'] = 'true';
  233. $result = Share\Api::getAllShares(array());
  234. $this->assertTrue($result->succeeded());
  235. // test should return one share within $this->folder
  236. $data = $result->getData();
  237. $this->assertEquals($value['expectedResult'], $data[0]['path']);
  238. }
  239. // cleanup
  240. \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  241. \OCP\Share::unshare('file', $fileInfo3['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  242. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  243. \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  244. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  245. }
  246. /**
  247. * @brief reshare a sub folder and check if we get the correct path
  248. * @medium
  249. */
  250. function testGetShareFromSubFolderReShares() {
  251. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  252. $fileInfo = $this->view->getFileInfo($this->folder . $this->subfolder);
  253. // share sub-folder to user2
  254. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  255. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  256. // share was successful?
  257. $this->assertTrue($result);
  258. // login as user2
  259. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  260. // reshare subfolder
  261. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  262. // share was successful?
  263. $this->assertTrue(is_string($result));
  264. $_GET['path'] = '/';
  265. $_GET['subfiles'] = 'true';
  266. $result = Share\Api::getAllShares(array());
  267. $this->assertTrue($result->succeeded());
  268. // test should return one share within $this->folder
  269. $data = $result->getData();
  270. // we should get exactly one result
  271. $this->assertEquals(1, count($data));
  272. $expectedPath = $this->subfolder;
  273. $this->assertEquals($expectedPath, $data[0]['path']);
  274. // cleanup
  275. $result = \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  276. $this->assertTrue($result);
  277. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  278. $result = \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  279. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  280. $this->assertTrue($result);
  281. }
  282. /**
  283. * @brief test re-re-share of folder if the path gets constructed correctly
  284. * @medium
  285. */
  286. function testGetShareFromFolderReReShares() {
  287. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  288. $fileInfo1 = $this->view->getFileInfo($this->folder . $this->subfolder);
  289. $fileInfo2 = $this->view->getFileInfo($this->folder . $this->subfolder . $this->subsubfolder);
  290. // share sub-folder to user2
  291. $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  292. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  293. // share was successful?
  294. $this->assertTrue($result);
  295. // login as user2
  296. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  297. // reshare subsubfolder
  298. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  299. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  300. // share was successful?
  301. $this->assertTrue($result);
  302. // login as user3
  303. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  304. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  305. // share was successful?
  306. $this->assertTrue(is_string($result));
  307. $_GET['path'] = '/';
  308. $_GET['subfiles'] = 'true';
  309. $result = Share\Api::getAllShares(array());
  310. $this->assertTrue($result->succeeded());
  311. // test should return one share within $this->folder
  312. $data = $result->getData();
  313. // we should get exactly one result
  314. $this->assertEquals(1, count($data));
  315. $expectedPath = $this->subsubfolder;
  316. $this->assertEquals($expectedPath, $data[0]['path']);
  317. // cleanup
  318. $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  319. $this->assertTrue($result);
  320. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  321. $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  322. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
  323. $this->assertTrue($result);
  324. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  325. $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  326. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  327. $this->assertTrue($result);
  328. }
  329. /**
  330. * @brief test multiple shared folder if the path gets constructed correctly
  331. * @medium
  332. */
  333. function testGetShareMultipleSharedFolder() {
  334. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  335. $fileInfo1 = $this->view->getFileInfo($this->folder);
  336. $fileInfo2 = $this->view->getFileInfo($this->folder . $this->subfolder);
  337. // share sub-folder to user2
  338. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  339. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  340. // share was successful?
  341. $this->assertTrue($result);
  342. // share folder to user2
  343. $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  344. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  345. // share was successful?
  346. $this->assertTrue($result);
  347. // login as user2
  348. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  349. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  350. // share was successful?
  351. $this->assertTrue(is_string($result));
  352. // ask for subfolder
  353. $expectedPath1 = $this->subfolder;
  354. $_GET['path'] = $expectedPath1;
  355. $result1 = Share\Api::getAllShares(array());
  356. $this->assertTrue($result1->succeeded());
  357. // test should return one share within $this->folder
  358. $data1 = $result1->getData();
  359. $share1 = reset($data1);
  360. // ask for folder/subfolder
  361. $expectedPath2 = $this->folder . $this->subfolder;
  362. $_GET['path'] = $expectedPath2;
  363. $result2 = Share\Api::getAllShares(array());
  364. $this->assertTrue($result2->succeeded());
  365. // test should return one share within $this->folder
  366. $data2 = $result2->getData();
  367. $share2 = reset($data2);
  368. // validate results
  369. // we should get exactly one result each time
  370. $this->assertEquals(1, count($data1));
  371. $this->assertEquals(1, count($data2));
  372. $this->assertEquals($expectedPath1, $share1['path']);
  373. $this->assertEquals($expectedPath2, $share2['path']);
  374. // cleanup
  375. $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  376. $this->assertTrue($result);
  377. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  378. $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  379. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  380. $this->assertTrue($result);
  381. $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  382. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  383. $this->assertTrue($result);
  384. }
  385. /**
  386. * @brief test re-re-share of folder if the path gets constructed correctly
  387. * @medium
  388. */
  389. function testGetShareFromFileReReShares() {
  390. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  391. $fileInfo1 = $this->view->getFileInfo($this->folder . $this->subfolder);
  392. $fileInfo2 = $this->view->getFileInfo($this->folder. $this->subfolder . $this->filename);
  393. // share sub-folder to user2
  394. $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  395. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  396. // share was successful?
  397. $this->assertTrue($result);
  398. // login as user2
  399. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  400. // reshare subsubfolder
  401. $result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  402. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  403. // share was successful?
  404. $this->assertTrue($result);
  405. // login as user3
  406. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  407. $result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  408. // share was successful?
  409. $this->assertTrue(is_string($result));
  410. $_GET['path'] = '/';
  411. $_GET['subfiles'] = 'true';
  412. $result = Share\Api::getAllShares(array());
  413. $this->assertTrue($result->succeeded());
  414. // test should return one share within $this->folder
  415. $data = $result->getData();
  416. // we should get exactly one result
  417. $this->assertEquals(1, count($data));
  418. $expectedPath = $this->filename;
  419. $this->assertEquals($expectedPath, $data[0]['path']);
  420. // cleanup
  421. $result = \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  422. $this->assertTrue($result);
  423. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  424. $result = \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  425. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
  426. $this->assertTrue($result);
  427. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  428. $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  429. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  430. $this->assertTrue($result);
  431. }
  432. /**
  433. * @medium
  434. */
  435. function testGetShareFromUnknownId() {
  436. $params = array('id' => 0);
  437. $result = Share\Api::getShare($params);
  438. $this->assertEquals(404, $result->getStatusCode());
  439. $meta = $result->getMeta();
  440. $this->assertEquals('share doesn\'t exist', $meta['message']);
  441. }
  442. /**
  443. * @medium
  444. * @depends testCreateShare
  445. */
  446. function testUpdateShare() {
  447. $fileInfo = $this->view->getFileInfo($this->filename);
  448. $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  449. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  450. // share was successful?
  451. $this->assertTrue($result);
  452. $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  453. null, 1);
  454. // share was successful?
  455. $this->assertTrue(is_string($result));
  456. $items = \OCP\Share::getItemShared('file', null);
  457. // make sure that we found a link share and a user share
  458. $this->assertEquals(count($items), 2);
  459. $linkShare = null;
  460. $userShare = null;
  461. foreach ($items as $item) {
  462. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  463. $linkShare = $item;
  464. }
  465. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_USER) {
  466. $userShare = $item;
  467. }
  468. }
  469. // make sure that we found a link share and a user share
  470. $this->assertTrue(is_array($linkShare));
  471. $this->assertTrue(is_array($userShare));
  472. // update permissions
  473. $this->assertEquals('31', $userShare['permissions']);
  474. $params = array();
  475. $params['id'] = $userShare['id'];
  476. $params['_put'] = array();
  477. $params['_put']['permissions'] = 1;
  478. $result = Share\Api::updateShare($params);
  479. $meta = $result->getMeta();
  480. $this->assertTrue($result->succeeded(), $meta['message']);
  481. $items = \OCP\Share::getItemShared('file', $userShare['file_source']);
  482. $newUserShare = null;
  483. foreach ($items as $item) {
  484. if ($item['share_with'] === \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2) {
  485. $newUserShare = $item;
  486. break;
  487. }
  488. }
  489. $this->assertTrue(is_array($newUserShare));
  490. $this->assertEquals('1', $newUserShare['permissions']);
  491. // update password for link share
  492. $this->assertTrue(empty($linkShare['share_with']));
  493. $params = array();
  494. $params['id'] = $linkShare['id'];
  495. $params['_put'] = array();
  496. $params['_put']['password'] = 'foo';
  497. $result = Share\Api::updateShare($params);
  498. $this->assertTrue($result->succeeded());
  499. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  500. $newLinkShare = null;
  501. foreach ($items as $item) {
  502. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  503. $newLinkShare = $item;
  504. break;
  505. }
  506. }
  507. $this->assertTrue(is_array($newLinkShare));
  508. $this->assertTrue(!empty($newLinkShare['share_with']));
  509. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  510. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  511. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  512. }
  513. /**
  514. * @medium
  515. */
  516. function testUpdateShareUpload() {
  517. $fileInfo = $this->view->getFileInfo($this->folder);
  518. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  519. null, 1);
  520. // share was successful?
  521. $this->assertTrue(is_string($result));
  522. $items = \OCP\Share::getItemShared('file', null);
  523. // make sure that we found a link share and a user share
  524. $this->assertEquals(count($items), 1);
  525. $linkShare = null;
  526. foreach ($items as $item) {
  527. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  528. $linkShare = $item;
  529. }
  530. }
  531. // make sure that we found a link share
  532. $this->assertTrue(is_array($linkShare));
  533. // update public upload
  534. $params = array();
  535. $params['id'] = $linkShare['id'];
  536. $params['_put'] = array();
  537. $params['_put']['publicUpload'] = 'true';
  538. $result = Share\Api::updateShare($params);
  539. $this->assertTrue($result->succeeded());
  540. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  541. $updatedLinkShare = null;
  542. foreach ($items as $item) {
  543. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  544. $updatedLinkShare = $item;
  545. break;
  546. }
  547. }
  548. $this->assertTrue(is_array($updatedLinkShare));
  549. $this->assertEquals(7, $updatedLinkShare['permissions']);
  550. // cleanup
  551. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  552. }
  553. /**
  554. * @medium
  555. * @depends testCreateShare
  556. */
  557. function testDeleteShare() {
  558. $fileInfo = $this->view->getFileInfo($this->filename);
  559. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  560. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  561. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  562. null, 1);
  563. $items = \OCP\Share::getItemShared('file', null);
  564. $this->assertEquals(2, count($items));
  565. foreach ($items as $item) {
  566. $result = Share\Api::deleteShare(array('id' => $item['id']));
  567. $this->assertTrue($result->succeeded());
  568. }
  569. $itemsAfterDelete = \OCP\Share::getItemShared('file', null);
  570. $this->assertTrue(empty($itemsAfterDelete));
  571. }
  572. /**
  573. * @brief test unshare of a reshared file
  574. */
  575. function testDeleteReshare() {
  576. // user 1 shares a folder with user2
  577. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  578. $fileInfo1 = $this->view->getFileInfo($this->folder);
  579. $fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
  580. $result1 = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  581. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  582. $this->assertTrue($result1);
  583. // user2 shares a file from the folder as link
  584. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  585. $result2 = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  586. $this->assertTrue(is_string($result2));
  587. // test if we can unshare the link again
  588. $items = \OCP\Share::getItemShared('file', null);
  589. $this->assertEquals(1, count($items));
  590. $item = reset($items);
  591. $result3 = Share\Api::deleteShare(array('id' => $item['id']));
  592. $this->assertTrue($result3->succeeded());
  593. // cleanup
  594. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  595. $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  596. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  597. $this->assertTrue($result);
  598. }
  599. /**
  600. * @brief share a folder which contains a share mount point, should be forbidden
  601. */
  602. public function testShareFolderWithAMountPoint() {
  603. // user 1 shares a folder with user2
  604. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  605. $fileInfo = $this->view->getFileInfo($this->folder);
  606. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  607. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  608. $this->assertTrue($result);
  609. // user2 shares a file from the folder as link
  610. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  611. $view = new \OC\Files\View('/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2 . '/files');
  612. $view->mkdir("localDir");
  613. // move mount point to the folder "localDir"
  614. $result = $view->rename($this->folder, 'localDir/'.$this->folder);
  615. $this->assertTrue($result !== false);
  616. // try to share "localDir"
  617. $fileInfo2 = $view->getFileInfo('localDir');
  618. $this->assertTrue($fileInfo2 instanceof \OC\Files\FileInfo);
  619. try {
  620. $result2 = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  621. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  622. } catch (\Exception $e) {
  623. $result2 = false;
  624. }
  625. $this->assertFalse($result2);
  626. //cleanup
  627. $result = $view->rename('localDir/' . $this->folder, $this->folder);
  628. $this->assertTrue($result !== false);
  629. $view->unlink('localDir');
  630. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  631. \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  632. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  633. }
  634. /**
  635. * Post init mount points hook for mounting simulated ext storage
  636. */
  637. public static function initTestMountPointsHook($data) {
  638. if ($data['user'] === \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1) {
  639. \OC\Files\Filesystem::mount(self::$tempStorage, array(), '/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME);
  640. }
  641. }
  642. /**
  643. * Tests mounting a folder that is an external storage mount point.
  644. */
  645. public function testShareStorageMountPoint() {
  646. self::$tempStorage = new \OC\Files\Storage\Temporary(array());
  647. self::$tempStorage->file_put_contents('test.txt', 'abcdef');
  648. self::$tempStorage->getScanner()->scan('');
  649. // needed because the sharing code sometimes switches the user internally and mounts the user's
  650. // storages. In our case the temp storage isn't mounted automatically, so doing it in the post hook
  651. // (similar to how ext storage works)
  652. OCP\Util::connectHook('OC_Filesystem', 'post_initMountPoints', '\Test_Files_Sharing_Api', 'initTestMountPointsHook');
  653. // logging in will auto-mount the temp storage for user1 as well
  654. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  655. $fileInfo = $this->view->getFileInfo($this->folder);
  656. // user 1 shares the mount point folder with user2
  657. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  658. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  659. $this->assertTrue($result);
  660. // user2: check that mount point name appears correctly
  661. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  662. $view = new \OC\Files\View('/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2 . '/files');
  663. $this->assertTrue($view->file_exists($this->folder));
  664. $this->assertTrue($view->file_exists($this->folder . '/test.txt'));
  665. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  666. \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  667. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  668. \OC_Hook::clear('OC_Filesystem', 'post_initMountPoints', '\Test_Files_Sharing_Api', 'initTestMountPointsHook');
  669. }
  670. /**
  671. * @expectedException \Exception
  672. */
  673. public function testShareNonExisting() {
  674. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  675. $id = PHP_INT_MAX - 1;
  676. \OCP\Share::shareItem('file', $id, \OCP\Share::SHARE_TYPE_LINK, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  677. }
  678. /**
  679. * @expectedException \Exception
  680. */
  681. public function testShareNotOwner() {
  682. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  683. \OC\Files\Filesystem::file_put_contents('foo.txt', 'bar');
  684. $info = \OC\Files\Filesystem::getFileInfo('foo.txt');
  685. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  686. \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_LINK, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  687. }
  688. }