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\IServerContainer;
  51. use OCP\Share\IShare;
  52. use OCP\UserStatus\IManager as IUserStatusManager;
  53. /**
  54. * Class ApiTest
  55. *
  56. * @group DB
  57. * TODO: convert to real integration tests
  58. */
  59. class ApiTest extends TestCase {
  60. public const TEST_FOLDER_NAME = '/folder_share_api_test';
  61. public const APP_NAME = 'files_sharing';
  62. private static $tempStorage;
  63. /** @var \OCP\Files\Folder */
  64. private $userFolder;
  65. /** @var string */
  66. private $subsubfolder;
  67. protected function setUp(): void {
  68. parent::setUp();
  69. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no');
  70. \OC::$server->getConfig()->setAppValue('core', 'shareapi_expire_after_n_days', '7');
  71. Filesystem::getLoader()->removeStorageWrapper('sharing_mask');
  72. $this->folder = self::TEST_FOLDER_NAME;
  73. $this->subfolder = '/subfolder_share_api_test';
  74. $this->subsubfolder = '/subsubfolder_share_api_test';
  75. $this->filename = '/share-api-test.txt';
  76. // save file with content
  77. $this->view->file_put_contents($this->filename, $this->data);
  78. $this->view->mkdir($this->folder);
  79. $this->view->mkdir($this->folder . $this->subfolder);
  80. $this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
  81. $this->view->file_put_contents($this->folder.$this->filename, $this->data);
  82. $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
  83. $mount = $this->view->getMount($this->filename);
  84. $mount->getStorage()->getScanner()->scan('', Scanner::SCAN_RECURSIVE);
  85. $this->userFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  86. }
  87. protected function tearDown(): void {
  88. if ($this->view instanceof \OC\Files\View) {
  89. $this->view->unlink($this->filename);
  90. $this->view->deleteAll($this->folder);
  91. }
  92. self::$tempStorage = null;
  93. parent::tearDown();
  94. }
  95. /**
  96. * @param string $userId The userId of the caller
  97. * @return \OCA\Files_Sharing\Controller\ShareAPIController
  98. */
  99. private function createOCS($userId) {
  100. $l = $this->getMockBuilder(IL10N::class)->getMock();
  101. $l->method('t')
  102. ->willReturnCallback(function ($text, $parameters = []) {
  103. return vsprintf($text, $parameters);
  104. });
  105. $config = $this->createMock(IConfig::class);
  106. $appManager = $this->createMock(IAppManager::class);
  107. $serverContainer = $this->createMock(IServerContainer::class);
  108. $userStatusManager = $this->createMock(IUserStatusManager::class);
  109. $previewManager = $this->createMock(IPreview::class);
  110. $dateTimeZone = $this->createMock(IDateTimeZone::class);
  111. $dateTimeZone->method('getTimeZone')->willReturn(new \DateTimeZone(date_default_timezone_get()));
  112. return new ShareAPIController(
  113. self::APP_NAME,
  114. $this->getMockBuilder(IRequest::class)->getMock(),
  115. $this->shareManager,
  116. \OC::$server->getGroupManager(),
  117. \OC::$server->getUserManager(),
  118. \OC::$server->getRootFolder(),
  119. \OC::$server->getURLGenerator(),
  120. $userId,
  121. $l,
  122. $config,
  123. $appManager,
  124. $serverContainer,
  125. $userStatusManager,
  126. $previewManager,
  127. $dateTimeZone,
  128. );
  129. }
  130. public function testCreateShareUserFile() {
  131. $this->setUp(); // for some reasons phpunit refuses to do this for us only for this test
  132. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  133. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  134. $ocs->cleanup();
  135. $data = $result->getData();
  136. $this->assertEquals(19, $data['permissions']);
  137. $this->assertEmpty($data['expiration']);
  138. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  139. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  140. $ocs->deleteShare($data['id']);
  141. $ocs->cleanup();
  142. }
  143. public function testCreateShareUserFolder() {
  144. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  145. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  146. $ocs->cleanup();
  147. $data = $result->getData();
  148. $this->assertEquals(31, $data['permissions']);
  149. $this->assertEmpty($data['expiration']);
  150. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  151. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  152. $ocs->deleteShare($data['id']);
  153. $ocs->cleanup();
  154. }
  155. public function testCreateShareGroupFile() {
  156. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  157. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1);
  158. $ocs->cleanup();
  159. $data = $result->getData();
  160. $this->assertEquals(19, $data['permissions']);
  161. $this->assertEmpty($data['expiration']);
  162. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  163. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  164. $ocs->deleteShare($data['id']);
  165. $ocs->cleanup();
  166. }
  167. public function testCreateShareGroupFolder() {
  168. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  169. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1);
  170. $ocs->cleanup();
  171. $data = $result->getData();
  172. $this->assertEquals(31, $data['permissions']);
  173. $this->assertEmpty($data['expiration']);
  174. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  175. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  176. $ocs->deleteShare($data['id']);
  177. $ocs->cleanup();
  178. }
  179. /**
  180. * @group RoutingWeirdness
  181. */
  182. public function testCreateShareLink() {
  183. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  184. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  185. $ocs->cleanup();
  186. $data = $result->getData();
  187. $this->assertEquals(\OCP\Constants::PERMISSION_ALL,
  188. $data['permissions']);
  189. $this->assertEmpty($data['expiration']);
  190. $this->assertTrue(is_string($data['token']));
  191. // check for correct link
  192. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  193. $this->assertEquals($url, $data['url']);
  194. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  195. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  196. $ocs->deleteShare($data['id']);
  197. $ocs->cleanup();
  198. }
  199. /**
  200. * @group RoutingWeirdness
  201. */
  202. public function testCreateShareLinkPublicUpload() {
  203. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  204. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
  205. $ocs->cleanup();
  206. $data = $result->getData();
  207. $this->assertEquals(
  208. \OCP\Constants::PERMISSION_READ |
  209. \OCP\Constants::PERMISSION_CREATE |
  210. \OCP\Constants::PERMISSION_UPDATE |
  211. \OCP\Constants::PERMISSION_DELETE |
  212. \OCP\Constants::PERMISSION_SHARE,
  213. $data['permissions']
  214. );
  215. $this->assertEmpty($data['expiration']);
  216. $this->assertTrue(is_string($data['token']));
  217. // check for correct link
  218. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  219. $this->assertEquals($url, $data['url']);
  220. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  221. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  222. $ocs->deleteShare($data['id']);
  223. $ocs->cleanup();
  224. }
  225. public function testEnforceLinkPassword() {
  226. $password = md5(time());
  227. $config = \OC::$server->getConfig();
  228. $config->setAppValue('core', 'shareapi_enforce_links_password', 'yes');
  229. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  230. try {
  231. $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  232. $this->fail();
  233. } catch (OCSForbiddenException $e) {
  234. }
  235. $ocs->cleanup();
  236. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  237. try {
  238. $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '');
  239. $this->fail();
  240. } catch (OCSForbiddenException $e) {
  241. }
  242. $ocs->cleanup();
  243. // share with password should succeed
  244. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  245. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', $password);
  246. $ocs->cleanup();
  247. $data = $result->getData();
  248. // setting new password should succeed
  249. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  250. $ocs->updateShare($data['id'], null, $password);
  251. $ocs->cleanup();
  252. // removing password should fail
  253. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  254. try {
  255. $ocs->updateShare($data['id']);
  256. $this->fail();
  257. } catch (OCSBadRequestException $e) {
  258. }
  259. $ocs->cleanup();
  260. // cleanup
  261. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  262. $ocs->deleteShare($data['id']);
  263. $ocs->cleanup();
  264. $config->setAppValue('core', 'shareapi_enforce_links_password', 'no');
  265. $this->addToAssertionCount(1);
  266. }
  267. /**
  268. * @medium
  269. */
  270. public function testSharePermissions() {
  271. // sharing file to a user should work if shareapi_exclude_groups is set
  272. // to no
  273. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no');
  274. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  275. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  276. $ocs->cleanup();
  277. $data = $result->getData();
  278. $this->shareManager->getShareById('ocinternal:'.$data['id']);
  279. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  280. $ocs->deleteShare($data['id']);
  281. $ocs->cleanup();
  282. // exclude groups, but not the group the user belongs to. Sharing should still work
  283. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'yes');
  284. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups_list', 'admin,group1,group2');
  285. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  286. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  287. $ocs->cleanup();
  288. $data = $result->getData();
  289. $this->shareManager->getShareById('ocinternal:' . $data['id']);
  290. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  291. $ocs->deleteShare($data['id']);
  292. $ocs->cleanup();
  293. // now we exclude the group the user belongs to ('group'), sharing should fail now
  294. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups_list', 'admin,group');
  295. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  296. $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  297. $ocs->cleanup();
  298. // cleanup
  299. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no');
  300. \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups_list', '');
  301. $this->addToAssertionCount(1);
  302. }
  303. /**
  304. * @medium
  305. */
  306. public function testGetAllShares() {
  307. $node = $this->userFolder->get($this->filename);
  308. $share = $this->shareManager->newShare();
  309. $share->setNode($node)
  310. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  311. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  312. ->setShareType(IShare::TYPE_USER)
  313. ->setPermissions(19);
  314. $share = $this->shareManager->createShare($share);
  315. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  316. $result = $ocs->getShares();
  317. $ocs->cleanup();
  318. $this->assertTrue(count($result->getData()) === 1);
  319. $this->shareManager->deleteShare($share);
  320. }
  321. public function testGetAllSharesWithMe() {
  322. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2);
  323. $this->logout();
  324. $node1 = $this->userFolder->get($this->filename);
  325. $share1 = $this->shareManager->newShare();
  326. $share1->setNode($node1)
  327. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  328. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  329. ->setShareType(IShare::TYPE_USER)
  330. ->setPermissions(19);
  331. $share1 = $this->shareManager->createShare($share1);
  332. $share1->setStatus(IShare::STATUS_ACCEPTED);
  333. $this->shareManager->updateShare($share1);
  334. $node2 = $this->userFolder->get($this->folder);
  335. $share2 = $this->shareManager->newShare();
  336. $share2->setNode($node2)
  337. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  338. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  339. ->setShareType(IShare::TYPE_USER)
  340. ->setPermissions(31);
  341. $share2 = $this->shareManager->createShare($share2);
  342. $share2->setStatus(IShare::STATUS_ACCEPTED);
  343. $this->shareManager->updateShare($share2);
  344. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  345. $result = $ocs->getShares('true');
  346. $ocs->cleanup();
  347. $this->assertCount(2, $result->getData());
  348. $this->shareManager->deleteShare($share1);
  349. $this->shareManager->deleteShare($share2);
  350. }
  351. /**
  352. * @medium
  353. * @group RoutingWeirdness
  354. */
  355. public function testPublicLinkUrl() {
  356. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  357. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  358. $ocs->cleanup();
  359. $data = $result->getData();
  360. // check if we have a token
  361. $this->assertTrue(is_string($data['token']));
  362. $id = $data['id'];
  363. // check for correct link
  364. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  365. $this->assertEquals($url, $data['url']);
  366. // check for link in getall shares
  367. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  368. $result = $ocs->getShares();
  369. $ocs->cleanup();
  370. $data = $result->getData();
  371. $this->assertEquals($url, current($data)['url']);
  372. // check for path
  373. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  374. $result = $ocs->getShares();
  375. $ocs->cleanup();
  376. $data = $result->getData();
  377. $this->assertEquals($url, current($data)['url']);
  378. // check in share id
  379. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  380. $result = $ocs->getShare($id);
  381. $ocs->cleanup();
  382. $data = $result->getData();
  383. $this->assertEquals($url, current($data)['url']);
  384. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  385. $ocs->deleteShare($id);
  386. $ocs->cleanup();
  387. }
  388. /**
  389. * @medium
  390. * @depends testCreateShareUserFile
  391. * @depends testCreateShareLink
  392. */
  393. public function testGetShareFromSource() {
  394. $node = $this->userFolder->get($this->filename);
  395. $share = $this->shareManager->newShare();
  396. $share->setNode($node)
  397. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  398. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  399. ->setShareType(IShare::TYPE_USER)
  400. ->setPermissions(19);
  401. $share1 = $this->shareManager->createShare($share);
  402. $share = $this->shareManager->newShare();
  403. $share->setNode($node)
  404. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  405. ->setShareType(IShare::TYPE_LINK)
  406. ->setPermissions(1);
  407. $share2 = $this->shareManager->createShare($share);
  408. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  409. $result = $ocs->getShares();
  410. $ocs->cleanup();
  411. // test should return one share created from testCreateShare()
  412. $this->assertTrue(count($result->getData()) === 2);
  413. $this->shareManager->deleteShare($share1);
  414. $this->shareManager->deleteShare($share2);
  415. }
  416. /**
  417. * @medium
  418. * @depends testCreateShareUserFile
  419. * @depends testCreateShareLink
  420. */
  421. public function testGetShareFromSourceWithReshares() {
  422. $node = $this->userFolder->get($this->filename);
  423. $share1 = $this->shareManager->newShare();
  424. $share1->setNode($node)
  425. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  426. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  427. ->setShareType(IShare::TYPE_USER)
  428. ->setPermissions(19);
  429. $share1 = $this->shareManager->createShare($share1);
  430. $share2 = $this->shareManager->newShare();
  431. $share2->setNode($node)
  432. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  433. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  434. ->setShareType(IShare::TYPE_USER)
  435. ->setPermissions(19);
  436. $share2 = $this->shareManager->createShare($share2);
  437. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  438. $result = $ocs->getShares();
  439. $ocs->cleanup();
  440. // test should return one share
  441. $this->assertTrue(count($result->getData()) === 1);
  442. // now also ask for the reshares
  443. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  444. $result = $ocs->getShares('false', 'true', 'false', $this->filename);
  445. $ocs->cleanup();
  446. // now we should get two shares, the initial share and the reshare
  447. $this->assertCount(2, $result->getData());
  448. $this->shareManager->deleteShare($share1);
  449. $this->shareManager->deleteShare($share2);
  450. }
  451. /**
  452. * @medium
  453. * @depends testCreateShareUserFile
  454. */
  455. public function testGetShareFromId() {
  456. $node = $this->userFolder->get($this->filename);
  457. $share1 = $this->shareManager->newShare();
  458. $share1->setNode($node)
  459. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  460. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  461. ->setShareType(IShare::TYPE_USER)
  462. ->setPermissions(19);
  463. $share1 = $this->shareManager->createShare($share1);
  464. // call getShare() with share ID
  465. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  466. $result = $ocs->getShare($share1->getId());
  467. $ocs->cleanup();
  468. // test should return one share created from testCreateShare()
  469. $this->assertEquals(1, count($result->getData()));
  470. $this->shareManager->deleteShare($share1);
  471. }
  472. /**
  473. * @medium
  474. */
  475. public function testGetShareFromFolder() {
  476. $node1 = $this->userFolder->get($this->filename);
  477. $share1 = $this->shareManager->newShare();
  478. $share1->setNode($node1)
  479. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  480. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  481. ->setShareType(IShare::TYPE_USER)
  482. ->setPermissions(19);
  483. $share1 = $this->shareManager->createShare($share1);
  484. $node2 = $this->userFolder->get($this->folder.'/'.$this->filename);
  485. $share2 = $this->shareManager->newShare();
  486. $share2->setNode($node2)
  487. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  488. ->setShareType(IShare::TYPE_LINK)
  489. ->setPermissions(1);
  490. $share2 = $this->shareManager->createShare($share2);
  491. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  492. $result = $ocs->getShares('false', 'false', 'true', $this->folder);
  493. $ocs->cleanup();
  494. // test should return one share within $this->folder
  495. $this->assertTrue(count($result->getData()) === 1);
  496. $this->shareManager->deleteShare($share1);
  497. $this->shareManager->deleteShare($share2);
  498. }
  499. public function testGetShareFromFolderWithFile() {
  500. $node1 = $this->userFolder->get($this->filename);
  501. $share1 = $this->shareManager->newShare();
  502. $share1->setNode($node1)
  503. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  504. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  505. ->setShareType(IShare::TYPE_USER)
  506. ->setPermissions(19);
  507. $share1 = $this->shareManager->createShare($share1);
  508. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  509. try {
  510. $ocs->getShares('false', 'false', 'true', $this->filename);
  511. $this->fail();
  512. } catch (OCSBadRequestException $e) {
  513. $this->assertEquals('Not a directory', $e->getMessage());
  514. }
  515. $ocs->cleanup();
  516. $this->shareManager->deleteShare($share1);
  517. }
  518. /**
  519. * share a folder, than reshare a file within the shared folder and check if we construct the correct path
  520. * @medium
  521. */
  522. public function testGetShareFromFolderReshares() {
  523. $node1 = $this->userFolder->get($this->folder);
  524. $share1 = $this->shareManager->newShare();
  525. $share1->setNode($node1)
  526. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  527. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  528. ->setShareType(IShare::TYPE_USER)
  529. ->setPermissions(31);
  530. $share1 = $this->shareManager->createShare($share1);
  531. $share1->setStatus(IShare::STATUS_ACCEPTED);
  532. $this->shareManager->updateShare($share1);
  533. $node2 = $this->userFolder->get($this->folder.'/'.$this->filename);
  534. $share2 = $this->shareManager->newShare();
  535. $share2->setNode($node2)
  536. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  537. ->setShareType(IShare::TYPE_LINK)
  538. ->setPermissions(1);
  539. $share2 = $this->shareManager->createShare($share2);
  540. $share2->setStatus(IShare::STATUS_ACCEPTED);
  541. $this->shareManager->updateShare($share2);
  542. $node3 = $this->userFolder->get($this->folder.'/'.$this->subfolder.'/'.$this->filename);
  543. $share3 = $this->shareManager->newShare();
  544. $share3->setNode($node3)
  545. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  546. ->setShareType(IShare::TYPE_LINK)
  547. ->setPermissions(1);
  548. $share3 = $this->shareManager->createShare($share3);
  549. $share3->setStatus(IShare::STATUS_ACCEPTED);
  550. $this->shareManager->updateShare($share3);
  551. $testValues = [
  552. ['query' => $this->folder,
  553. 'expectedResult' => $this->folder . $this->filename],
  554. ['query' => $this->folder . $this->subfolder,
  555. 'expectedResult' => $this->folder . $this->subfolder . $this->filename],
  556. ];
  557. foreach ($testValues as $value) {
  558. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  559. $result = $ocs->getShares('false', 'false', 'true', $value['query']);
  560. $ocs->cleanup();
  561. // test should return one share within $this->folder
  562. $data = $result->getData();
  563. $this->assertEquals($value['expectedResult'], $data[0]['path']);
  564. }
  565. // cleanup
  566. $this->shareManager->deleteShare($share1);
  567. $this->shareManager->deleteShare($share2);
  568. $this->shareManager->deleteShare($share3);
  569. }
  570. /**
  571. * reshare a sub folder and check if we get the correct path
  572. * @medium
  573. */
  574. public function testGetShareFromSubFolderReShares() {
  575. $node1 = $this->userFolder->get($this->folder . $this->subfolder);
  576. $share1 = $this->shareManager->newShare();
  577. $share1->setNode($node1)
  578. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  579. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  580. ->setShareType(IShare::TYPE_USER)
  581. ->setPermissions(31);
  582. $share1 = $this->shareManager->createShare($share1);
  583. $share1->setStatus(IShare::STATUS_ACCEPTED);
  584. $this->shareManager->updateShare($share1);
  585. $node2 = \OC::$server->getRootFolder()->getUserFolder(self::TEST_FILES_SHARING_API_USER2)->get($this->subfolder);
  586. $share2 = $this->shareManager->newShare();
  587. $share2->setNode($node2)
  588. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  589. ->setShareType(IShare::TYPE_LINK)
  590. ->setPermissions(1);
  591. $share2 = $this->shareManager->createShare($share2);
  592. $share2->setStatus(IShare::STATUS_ACCEPTED);
  593. $this->shareManager->updateShare($share2);
  594. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  595. $result = $ocs->getShares();
  596. $ocs->cleanup();
  597. // test should return one share within $this->folder
  598. $data = $result->getData();
  599. // we should get exactly one result
  600. $this->assertCount(1, $data);
  601. $this->assertEquals($this->subfolder, $data[0]['path']);
  602. $this->shareManager->deleteShare($share2);
  603. $this->shareManager->deleteShare($share1);
  604. }
  605. /**
  606. * test re-re-share of folder if the path gets constructed correctly
  607. * @medium
  608. */
  609. public function XtestGetShareFromFolderReReShares() {
  610. $node1 = $this->userFolder->get($this->folder . $this->subfolder);
  611. $share1 = $this->shareManager->newShare();
  612. $share1->setNode($node1)
  613. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  614. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  615. ->setShareType(IShare::TYPE_USER)
  616. ->setPermissions(31);
  617. $share1 = $this->shareManager->createShare($share1);
  618. $node2 = $this->userFolder->get($this->folder . $this->subfolder . $this->subsubfolder);
  619. $share2 = $this->shareManager->newShare();
  620. $share2->setNode($node2)
  621. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  622. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  623. ->setShareType(IShare::TYPE_USER)
  624. ->setPermissions(31);
  625. $share2 = $this->shareManager->createShare($share2);
  626. $share3 = $this->shareManager->newShare();
  627. $share3->setNode($node2)
  628. ->setSharedBy(self::TEST_FILES_SHARING_API_USER3)
  629. ->setShareType(IShare::TYPE_LINK)
  630. ->setPermissions(1);
  631. $share3 = $this->shareManager->createShare($share3);
  632. /*
  633. * Test as recipient
  634. */
  635. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER3);
  636. $result = $ocs->getShares();
  637. $ocs->cleanup();
  638. // test should return one share within $this->folder
  639. $data = $result->getData();
  640. // we should get exactly one result
  641. $this->assertCount(1, $data);
  642. $this->assertEquals($this->subsubfolder, $data[0]['path']);
  643. /*
  644. * Test for first owner/initiator
  645. */
  646. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  647. $result = $ocs->getShares();
  648. $ocs->cleanup();
  649. // test should return one share within $this->folder
  650. $data = $result->getData();
  651. // we should get exactly one result
  652. $this->assertCount(1, $data);
  653. $this->assertEquals($this->folder . $this->subfolder, $data[0]['path']);
  654. /*
  655. * Test for second initiator
  656. */
  657. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  658. $result = $ocs->getShares();
  659. $ocs->cleanup();
  660. // test should return one share within $this->folder
  661. $data = $result->getData();
  662. // we should get exactly one result
  663. $this->assertCount(1, $data);
  664. $this->assertEquals($this->subfolder . $this->subsubfolder, $data[0]['path']);
  665. $this->shareManager->deleteShare($share1);
  666. $this->shareManager->deleteShare($share2);
  667. $this->shareManager->deleteShare($share3);
  668. }
  669. /**
  670. * test multiple shared folder if the path gets constructed correctly
  671. * @medium
  672. */
  673. public function testGetShareMultipleSharedFolder() {
  674. $this->setUp();
  675. $node1 = $this->userFolder->get($this->folder . $this->subfolder);
  676. $share1 = $this->shareManager->newShare();
  677. $share1->setNode($node1)
  678. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  679. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  680. ->setShareType(IShare::TYPE_USER)
  681. ->setPermissions(31);
  682. $share1 = $this->shareManager->createShare($share1);
  683. $share1->setStatus(IShare::STATUS_ACCEPTED);
  684. $this->shareManager->updateShare($share1);
  685. $node2 = $this->userFolder->get($this->folder);
  686. $share2 = $this->shareManager->newShare();
  687. $share2->setNode($node2)
  688. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  689. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  690. ->setShareType(IShare::TYPE_USER)
  691. ->setPermissions(31);
  692. $share2 = $this->shareManager->createShare($share2);
  693. $share2->setStatus(IShare::STATUS_ACCEPTED);
  694. $this->shareManager->updateShare($share2);
  695. $share3 = $this->shareManager->newShare();
  696. $share3->setNode($node1)
  697. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  698. ->setShareType(IShare::TYPE_LINK)
  699. ->setPermissions(1);
  700. $share3 = $this->shareManager->createShare($share3);
  701. $share3->setStatus(IShare::STATUS_ACCEPTED);
  702. $this->shareManager->updateShare($share3);
  703. // $request = $this->createRequest(['path' => $this->subfolder]);
  704. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  705. $result1 = $ocs->getShares('false', 'false', 'false', $this->subfolder);
  706. $ocs->cleanup();
  707. // test should return one share within $this->folder
  708. $data1 = $result1->getData();
  709. $this->assertCount(1, $data1);
  710. $s1 = reset($data1);
  711. //$request = $this->createRequest(['path' => $this->folder.$this->subfolder]);
  712. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  713. $result2 = $ocs->getShares('false', 'false', 'false', $this->folder . $this->subfolder);
  714. $ocs->cleanup();
  715. // test should return one share within $this->folder
  716. $data2 = $result2->getData();
  717. $this->assertCount(1, $data2);
  718. $s2 = reset($data2);
  719. $this->assertEquals($this->subfolder, $s1['path']);
  720. $this->assertEquals($this->folder.$this->subfolder, $s2['path']);
  721. $this->shareManager->deleteShare($share1);
  722. $this->shareManager->deleteShare($share2);
  723. $this->shareManager->deleteShare($share3);
  724. }
  725. /**
  726. * test re-re-share of folder if the path gets constructed correctly
  727. * @medium
  728. */
  729. public function testGetShareFromFileReReShares() {
  730. $node1 = $this->userFolder->get($this->folder . $this->subfolder);
  731. $share1 = $this->shareManager->newShare();
  732. $share1->setNode($node1)
  733. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  734. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  735. ->setShareType(IShare::TYPE_USER)
  736. ->setPermissions(31);
  737. $share1 = $this->shareManager->createShare($share1);
  738. $share1->setStatus(IShare::STATUS_ACCEPTED);
  739. $this->shareManager->updateShare($share1);
  740. $user2Folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER2);
  741. $node2 = $user2Folder->get($this->subfolder . $this->filename);
  742. $share2 = $this->shareManager->newShare();
  743. $share2->setNode($node2)
  744. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  745. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  746. ->setShareType(IShare::TYPE_USER)
  747. ->setPermissions(19);
  748. $share2 = $this->shareManager->createShare($share2);
  749. $share2->setStatus(IShare::STATUS_ACCEPTED);
  750. $this->shareManager->updateShare($share2);
  751. $user3Folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER3);
  752. $node3 = $user3Folder->get($this->filename);
  753. $share3 = $this->shareManager->newShare();
  754. $share3->setNode($node3)
  755. ->setSharedBy(self::TEST_FILES_SHARING_API_USER3)
  756. ->setShareType(IShare::TYPE_LINK)
  757. ->setPermissions(1);
  758. $share3 = $this->shareManager->createShare($share3);
  759. $share3->setStatus(IShare::STATUS_ACCEPTED);
  760. $this->shareManager->updateShare($share3);
  761. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER3);
  762. $result = $ocs->getShares();
  763. $ocs->cleanup();
  764. // test should return one share within $this->folder
  765. $data = $result->getData();
  766. // we should get exactly one result
  767. $this->assertCount(1, $data);
  768. $this->assertEquals($this->filename, $data[0]['path']);
  769. $this->shareManager->deleteShare($share1);
  770. $this->shareManager->deleteShare($share2);
  771. $this->shareManager->deleteShare($share3);
  772. }
  773. /**
  774. * @medium
  775. */
  776. public function testGetShareFromUnknownId() {
  777. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER3);
  778. try {
  779. $ocs->getShare(0);
  780. $this->fail();
  781. } catch (OCSNotFoundException $e) {
  782. $this->assertEquals('Wrong share ID, share does not exist', $e->getMessage());
  783. }
  784. $ocs->cleanup();
  785. }
  786. /**
  787. * @medium
  788. * @depends testCreateShareUserFile
  789. * @depends testCreateShareLink
  790. */
  791. public function testUpdateShare() {
  792. $password = md5(time());
  793. $node1 = $this->userFolder->get($this->filename);
  794. $share1 = $this->shareManager->newShare();
  795. $share1->setNode($node1)
  796. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  797. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  798. ->setShareType(IShare::TYPE_USER)
  799. ->setPermissions(19)
  800. ->setAttributes($this->shareManager->newShare()->newAttributes());
  801. $this->assertNotNull($share1->getAttributes());
  802. $share1 = $this->shareManager->createShare($share1);
  803. $this->assertNull($share1->getAttributes());
  804. $this->assertEquals(19, $share1->getPermissions());
  805. // attributes get cleared when empty
  806. $this->assertNull($share1->getAttributes());
  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($share1);
  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, 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. }