ApiTest.php 48 KB

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