1
0

ApiTest.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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->assertEquals(19, $share1->getPermissions());
  804. $share2 = $this->shareManager->newShare();
  805. $share2->setNode($node1)
  806. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  807. ->setShareType(IShare::TYPE_LINK)
  808. ->setPermissions(1);
  809. $share2 = $this->shareManager->createShare($share2);
  810. $this->assertEquals(1, $share2->getPermissions());
  811. // update permissions
  812. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  813. $ocs->updateShare(
  814. $share1->getId(), 1, null, null, null, null, null, null, null,
  815. '[{"scope": "app1", "key": "attr1", "enabled": true}]'
  816. );
  817. $ocs->cleanup();
  818. $share1 = $this->shareManager->getShareById('ocinternal:' . $share1->getId());
  819. $this->assertEquals(1, $share1->getPermissions());
  820. $this->assertEquals(true, $share1->getAttributes()->getAttribute('app1', 'attr1'));
  821. // update password for link share
  822. $this->assertNull($share2->getPassword());
  823. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  824. $ocs->updateShare($share2->getId(), null, $password);
  825. $ocs->cleanup();
  826. $share2 = $this->shareManager->getShareById('ocinternal:' . $share2->getId());
  827. $this->assertNotNull($share2->getPassword());
  828. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  829. $ocs->updateShare($share2->getId(), null, '');
  830. $ocs->cleanup();
  831. $share2 = $this->shareManager->getShareById('ocinternal:' . $share2->getId());
  832. $this->assertNull($share2->getPassword());
  833. $this->shareManager->deleteShare($share1);
  834. $this->shareManager->deleteShare($share2);
  835. }
  836. /**
  837. * @medium
  838. */
  839. public function testUpdateShareUpload() {
  840. $node1 = $this->userFolder->get($this->folder);
  841. $share1 = $this->shareManager->newShare();
  842. $share1->setNode($node1)
  843. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  844. ->setShareType(IShare::TYPE_LINK)
  845. ->setPermissions(1);
  846. $share1 = $this->shareManager->createShare($share1);
  847. // update public upload
  848. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  849. $ocs->updateShare($share1->getId(), null, null, null, 'true');
  850. $ocs->cleanup();
  851. $share1 = $this->shareManager->getShareById($share1->getFullId());
  852. $this->assertEquals(
  853. \OCP\Constants::PERMISSION_READ |
  854. \OCP\Constants::PERMISSION_CREATE |
  855. \OCP\Constants::PERMISSION_UPDATE |
  856. \OCP\Constants::PERMISSION_DELETE |
  857. \OCP\Constants::PERMISSION_SHARE,
  858. $share1->getPermissions()
  859. );
  860. // cleanup
  861. $this->shareManager->deleteShare($share1);
  862. }
  863. /**
  864. * @medium
  865. */
  866. public function testUpdateShareExpireDate() {
  867. $node1 = $this->userFolder->get($this->folder);
  868. $share1 = $this->shareManager->newShare();
  869. $share1->setNode($node1)
  870. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  871. ->setShareType(IShare::TYPE_LINK)
  872. ->setPermissions(1);
  873. $share1 = $this->shareManager->createShare($share1);
  874. $share1->setStatus(IShare::STATUS_ACCEPTED);
  875. $this->shareManager->updateShare($share1);
  876. $config = \OC::$server->getConfig();
  877. // enforce expire date, by default 7 days after the file was shared
  878. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  879. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  880. $dateWithinRange = new \DateTime();
  881. $dateWithinRange->add(new \DateInterval('P6D'));
  882. $dateOutOfRange = new \DateTime();
  883. $dateOutOfRange->add(new \DateInterval('P8D'));
  884. // update expire date to a valid value
  885. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  886. $ocs->updateShare($share1->getId(), null, null, null, null, $dateWithinRange->format('Y-m-d'));
  887. $ocs->cleanup();
  888. $share1 = $this->shareManager->getShareById($share1->getFullId());
  889. // date should be changed
  890. $dateWithinRange->setTime(0, 0, 0);
  891. $dateWithinRange->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  892. $this->assertEquals($dateWithinRange, $share1->getExpirationDate());
  893. // update expire date to a value out of range
  894. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  895. try {
  896. $ocs->updateShare($share1->getId());
  897. $this->fail();
  898. } catch (OCSBadRequestException $e) {
  899. }
  900. $ocs->cleanup();
  901. $share1 = $this->shareManager->getShareById($share1->getFullId());
  902. // date shouldn't be changed
  903. $this->assertEquals($dateWithinRange, $share1->getExpirationDate());
  904. // Try to remove expire date
  905. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  906. try {
  907. $ocs->updateShare($share1->getId());
  908. $this->fail();
  909. } catch (OCSBadRequestException $e) {
  910. }
  911. $ocs->cleanup();
  912. $share1 = $this->shareManager->getShareById($share1->getFullId());
  913. // date shouldn't be changed
  914. $this->assertEquals($dateWithinRange, $share1->getExpirationDate());
  915. // cleanup
  916. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  917. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  918. $this->shareManager->deleteShare($share1);
  919. }
  920. /**
  921. * @medium
  922. * @depends testCreateShareUserFile
  923. */
  924. public function testDeleteShare() {
  925. $node1 = $this->userFolder->get($this->filename);
  926. $share1 = $this->shareManager->newShare();
  927. $share1->setNode($node1)
  928. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  929. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  930. ->setShareType(IShare::TYPE_USER)
  931. ->setPermissions(19);
  932. $share1 = $this->shareManager->createShare($share1);
  933. $share2 = $this->shareManager->newShare();
  934. $share2->setNode($node1)
  935. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  936. ->setShareType(IShare::TYPE_LINK)
  937. ->setPermissions(1);
  938. $share2 = $this->shareManager->createShare($share2);
  939. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  940. $ocs->deleteShare($share1->getId());
  941. $ocs->cleanup();
  942. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  943. $ocs->deleteShare($share2->getId());
  944. $ocs->cleanup();
  945. $this->assertEmpty($this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_USER));
  946. $this->assertEmpty($this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_LINK));
  947. }
  948. /**
  949. * test unshare of a reshared file
  950. */
  951. public function testDeleteReshare() {
  952. $node1 = $this->userFolder->get($this->folder);
  953. $share1 = $this->shareManager->newShare();
  954. $share1->setNode($node1)
  955. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  956. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  957. ->setShareType(IShare::TYPE_USER)
  958. ->setPermissions(31);
  959. $share1 = $this->shareManager->createShare($share1);
  960. $share1->setStatus(IShare::STATUS_ACCEPTED);
  961. $this->shareManager->updateShare($share1);
  962. $user2folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER2);
  963. $node2 = $user2folder->get($this->folder.'/'.$this->filename);
  964. $share2 = $this->shareManager->newShare();
  965. $share2->setNode($node2)
  966. ->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
  967. ->setShareType(IShare::TYPE_LINK)
  968. ->setPermissions(1);
  969. $share2 = $this->shareManager->createShare($share2);
  970. $share2->setStatus(IShare::STATUS_ACCEPTED);
  971. $this->shareManager->updateShare($share2);
  972. // test if we can unshare the link again
  973. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  974. $ocs->deleteShare($share2->getId());
  975. $ocs->cleanup();
  976. $this->shareManager->deleteShare($share1);
  977. $this->addToAssertionCount(1);
  978. }
  979. /**
  980. * share a folder which contains a share mount point, should be forbidden
  981. */
  982. public function testShareFolderWithAMountPoint() {
  983. // user 1 shares a folder with user2
  984. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  985. $share = $this->share(
  986. IShare::TYPE_USER,
  987. $this->folder,
  988. self::TEST_FILES_SHARING_API_USER1,
  989. self::TEST_FILES_SHARING_API_USER2,
  990. \OCP\Constants::PERMISSION_ALL
  991. );
  992. $share->setStatus(IShare::STATUS_ACCEPTED);
  993. $this->shareManager->updateShare($share);
  994. // user2 shares a file from the folder as link
  995. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  996. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  997. $view->mkdir("localDir");
  998. // move mount point to the folder "localDir"
  999. $result = $view->rename($this->folder, 'localDir/'.$this->folder);
  1000. $this->assertTrue($result !== false);
  1001. // try to share "localDir"
  1002. $fileInfo2 = $view->getFileInfo('localDir');
  1003. $this->assertTrue($fileInfo2 instanceof \OC\Files\FileInfo);
  1004. $pass = true;
  1005. try {
  1006. $this->share(
  1007. IShare::TYPE_USER,
  1008. 'localDir',
  1009. self::TEST_FILES_SHARING_API_USER2,
  1010. self::TEST_FILES_SHARING_API_USER3,
  1011. \OCP\Constants::PERMISSION_ALL
  1012. );
  1013. } catch (\Exception $e) {
  1014. $pass = false;
  1015. }
  1016. $this->assertFalse($pass);
  1017. //cleanup
  1018. $result = $view->rename('localDir/' . $this->folder, $this->folder);
  1019. $this->assertTrue($result !== false);
  1020. $view->unlink('localDir');
  1021. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  1022. $this->shareManager->deleteShare($share);
  1023. }
  1024. /**
  1025. * Post init mount points hook for mounting simulated ext storage
  1026. */
  1027. public static function initTestMountPointsHook($data) {
  1028. if ($data['user'] === self::TEST_FILES_SHARING_API_USER1) {
  1029. \OC\Files\Filesystem::mount(self::$tempStorage, [], '/' . self::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME);
  1030. }
  1031. }
  1032. /**
  1033. * Tests mounting a folder that is an external storage mount point.
  1034. */
  1035. public function testShareStorageMountPoint() {
  1036. $tempStorage = new \OC\Files\Storage\Temporary([]);
  1037. $tempStorage->file_put_contents('test.txt', 'abcdef');
  1038. $tempStorage->getScanner()->scan('');
  1039. $this->registerMount(self::TEST_FILES_SHARING_API_USER1, $tempStorage, self::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME);
  1040. // logging in will auto-mount the temp storage for user1 as well
  1041. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  1042. // user 1 shares the mount point folder with user2
  1043. $share = $this->share(
  1044. IShare::TYPE_USER,
  1045. $this->folder,
  1046. self::TEST_FILES_SHARING_API_USER1,
  1047. self::TEST_FILES_SHARING_API_USER2,
  1048. \OCP\Constants::PERMISSION_ALL
  1049. );
  1050. $share->setStatus(IShare::STATUS_ACCEPTED);
  1051. $this->shareManager->updateShare($share);
  1052. // user2: check that mount point name appears correctly
  1053. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  1054. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  1055. $this->assertTrue($view->file_exists($this->folder));
  1056. $this->assertTrue($view->file_exists($this->folder . '/test.txt'));
  1057. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  1058. $this->shareManager->deleteShare($share);
  1059. \OC_Hook::clear('OC_Filesystem', 'post_initMountPoints');
  1060. \OC_Hook::clear('\OCA\Files_Sharing\Tests\ApiTest', 'initTestMountPointsHook');
  1061. }
  1062. public function datesProvider() {
  1063. $date = new \DateTime();
  1064. $date->setTime(0, 0);
  1065. $date->add(new \DateInterval('P5D'));
  1066. $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  1067. return [
  1068. [$date->format('Y-m-d H:i:s'), true],
  1069. ['abc', false],
  1070. [$date->format('Y-m-d H:i:s') . 'xyz', false],
  1071. ];
  1072. }
  1073. /**
  1074. * Make sure only ISO 8601 dates are accepted
  1075. *
  1076. * @dataProvider datesProvider
  1077. * @group RoutingWeirdness
  1078. */
  1079. public function testPublicLinkExpireDate($date, $valid) {
  1080. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1081. try {
  1082. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date);
  1083. $this->assertTrue($valid);
  1084. } catch (OCSNotFoundException $e) {
  1085. $this->assertFalse($valid);
  1086. $this->assertEquals('Invalid date, date format must be YYYY-MM-DD', $e->getMessage());
  1087. $ocs->cleanup();
  1088. return;
  1089. }
  1090. $ocs->cleanup();
  1091. $data = $result->getData();
  1092. $this->assertTrue(is_string($data['token']));
  1093. $this->assertEquals(substr($date, 0, 10), substr($data['expiration'], 0, 10));
  1094. // check for correct link
  1095. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  1096. $this->assertEquals($url, $data['url']);
  1097. $share = $this->shareManager->getShareById('ocinternal:'.$data['id']);
  1098. $this->assertEquals($date, $share->getExpirationDate()->format('Y-m-d H:i:s'));
  1099. $this->shareManager->deleteShare($share);
  1100. }
  1101. /**
  1102. * @group RoutingWeirdness
  1103. */
  1104. public function testCreatePublicLinkExpireDateValid() {
  1105. $config = \OC::$server->getConfig();
  1106. // enforce expire date, by default 7 days after the file was shared
  1107. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  1108. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  1109. $date = new \DateTime();
  1110. $date->add(new \DateInterval('P5D'));
  1111. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1112. $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d'));
  1113. $ocs->cleanup();
  1114. $data = $result->getData();
  1115. $this->assertTrue(is_string($data['token']));
  1116. $this->assertEquals($date->format('Y-m-d 00:00:00'), $data['expiration']);
  1117. // check for correct link
  1118. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  1119. $this->assertEquals($url, $data['url']);
  1120. $share = $this->shareManager->getShareById('ocinternal:'.$data['id']);
  1121. $date->setTime(0, 0, 0);
  1122. $this->assertEquals($date, $share->getExpirationDate());
  1123. $this->shareManager->deleteShare($share);
  1124. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1125. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1126. }
  1127. public function testCreatePublicLinkExpireDateInvalidFuture() {
  1128. $config = \OC::$server->getConfig();
  1129. // enforce expire date, by default 7 days after the file was shared
  1130. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  1131. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  1132. $date = new \DateTime();
  1133. $date->add(new \DateInterval('P8D'));
  1134. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1135. try {
  1136. $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d'));
  1137. $this->fail();
  1138. } catch (OCSException $e) {
  1139. $this->assertEquals(404, $e->getCode());
  1140. $this->assertEquals('Cannot set expiration date more than 7 days in the future', $e->getMessage());
  1141. }
  1142. $ocs->cleanup();
  1143. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1144. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1145. }
  1146. public function XtestCreatePublicLinkExpireDateInvalidPast() {
  1147. $config = \OC::$server->getConfig();
  1148. $date = new \DateTime();
  1149. $date->sub(new \DateInterval('P8D'));
  1150. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1151. try {
  1152. $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d'));
  1153. $this->fail();
  1154. } catch (OCSException $e) {
  1155. $this->assertEquals(404, $e->getCode());
  1156. $this->assertEquals('Expiration date is in the past', $e->getMessage());
  1157. }
  1158. $ocs->cleanup();
  1159. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1160. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1161. }
  1162. /**
  1163. * test for no invisible shares
  1164. * See: https://github.com/owncloud/core/issues/22295
  1165. */
  1166. public function testInvisibleSharesUser() {
  1167. // simulate a post request
  1168. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1169. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  1170. $ocs->cleanup();
  1171. $data = $result->getData();
  1172. $topId = $data['id'];
  1173. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  1174. $ocs->acceptShare($topId);
  1175. $ocs->cleanup();
  1176. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  1177. $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  1178. $ocs->cleanup();
  1179. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1180. $ocs->deleteShare($topId);
  1181. $ocs->cleanup();
  1182. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1183. $result = $ocs->getShares();
  1184. $ocs->cleanup();
  1185. $this->assertEmpty($result->getData());
  1186. }
  1187. /**
  1188. * test for no invisible shares
  1189. * See: https://github.com/owncloud/core/issues/22295
  1190. */
  1191. public function testInvisibleSharesGroup() {
  1192. // simulate a post request
  1193. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1194. $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1);
  1195. $ocs->cleanup();
  1196. $data = $result->getData();
  1197. $topId = $data['id'];
  1198. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  1199. $ocs->acceptShare($topId);
  1200. $ocs->cleanup();
  1201. \OC_Util::tearDownFS();
  1202. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
  1203. $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  1204. $ocs->cleanup();
  1205. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1206. $ocs->deleteShare($topId);
  1207. $ocs->cleanup();
  1208. $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
  1209. $result = $ocs->getShares();
  1210. $ocs->cleanup();
  1211. $this->assertEmpty($result->getData());
  1212. }
  1213. }