ApiTest.php 49 KB

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