1
0

ApiTest.php 48 KB

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