ShareAPIControllerTest.php 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Vincent Petry <pvince81@owncloud.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_Sharing\Tests\Controller;
  25. use OCP\AppFramework\Http\DataResponse;
  26. use OCP\AppFramework\OCS\OCSNotFoundException;
  27. use OCP\Files\Folder;
  28. use OCP\IL10N;
  29. use OCA\Files_Sharing\Controller\ShareAPIController;
  30. use OCP\Files\NotFoundException;
  31. use OCP\IGroupManager;
  32. use OCP\IUserManager;
  33. use OCP\IRequest;
  34. use OCP\IURLGenerator;
  35. use OCP\IUser;
  36. use OCP\Files\IRootFolder;
  37. use OCP\Lock\LockedException;
  38. use OCP\Share\IManager;
  39. use OCP\Share;
  40. /**
  41. * Class ShareAPIControllerTest
  42. *
  43. * @package OCA\Files_Sharing\Tests\Controller
  44. * @group DB
  45. */
  46. class ShareAPIControllerTest extends \Test\TestCase {
  47. /** @var string */
  48. private $appName = 'files_sharing';
  49. /** @var \OC\Share20\Manager|\PHPUnit_Framework_MockObject_MockObject */
  50. private $shareManager;
  51. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  52. private $groupManager;
  53. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  54. private $userManager;
  55. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  56. private $request;
  57. /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
  58. private $rootFolder;
  59. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  60. private $urlGenerator;
  61. /** @var string|\PHPUnit_Framework_MockObject_MockObject */
  62. private $currentUser;
  63. /** @var ShareAPIController */
  64. private $ocs;
  65. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  66. private $l;
  67. protected function setUp() {
  68. $this->shareManager = $this->createMock(IManager::class);
  69. $this->shareManager
  70. ->expects($this->any())
  71. ->method('shareApiEnabled')
  72. ->willReturn(true);
  73. $this->groupManager = $this->createMock(IGroupManager::class);
  74. $this->userManager = $this->createMock(IUserManager::class);
  75. $this->request = $this->createMock(IRequest::class);
  76. $this->rootFolder = $this->createMock(IRootFolder::class);
  77. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  78. $this->currentUser = 'currentUser';
  79. $this->l = $this->createMock(IL10N::class);
  80. $this->l->method('t')
  81. ->will($this->returnCallback(function($text, $parameters = []) {
  82. return vsprintf($text, $parameters);
  83. }));
  84. $this->ocs = new ShareAPIController(
  85. $this->appName,
  86. $this->request,
  87. $this->shareManager,
  88. $this->groupManager,
  89. $this->userManager,
  90. $this->rootFolder,
  91. $this->urlGenerator,
  92. $this->currentUser,
  93. $this->l
  94. );
  95. }
  96. /**
  97. * @return ShareAPIController|\PHPUnit_Framework_MockObject_MockObject
  98. */
  99. private function mockFormatShare() {
  100. return $this->getMockBuilder(ShareAPIController::class)
  101. ->setConstructorArgs([
  102. $this->appName,
  103. $this->request,
  104. $this->shareManager,
  105. $this->groupManager,
  106. $this->userManager,
  107. $this->rootFolder,
  108. $this->urlGenerator,
  109. $this->currentUser,
  110. $this->l,
  111. ])->setMethods(['formatShare'])
  112. ->getMock();
  113. }
  114. private function newShare() {
  115. return \OC::$server->getShareManager()->newShare();
  116. }
  117. /**
  118. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  119. * @expectedExceptionMessage Wrong share ID, share doesn't exist
  120. */
  121. public function testDeleteShareShareNotFound() {
  122. $this->shareManager
  123. ->expects($this->exactly(2))
  124. ->method('getShareById')
  125. ->will($this->returnCallback(function($id) {
  126. if ($id === 'ocinternal:42' || $id === 'ocFederatedSharing:42') {
  127. throw new \OCP\Share\Exceptions\ShareNotFound();
  128. } else {
  129. throw new \Exception();
  130. }
  131. }));
  132. $this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true);
  133. $this->ocs->deleteShare(42);
  134. }
  135. public function testDeleteShare() {
  136. $node = $this->getMockBuilder('\OCP\Files\File')->getMock();
  137. $share = $this->newShare();
  138. $share->setSharedBy($this->currentUser)
  139. ->setNode($node);
  140. $this->shareManager
  141. ->expects($this->once())
  142. ->method('getShareById')
  143. ->with('ocinternal:42')
  144. ->willReturn($share);
  145. $this->shareManager
  146. ->expects($this->once())
  147. ->method('deleteShare')
  148. ->with($share);
  149. $node->expects($this->once())
  150. ->method('lock')
  151. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  152. $expected = new DataResponse();
  153. $result = $this->ocs->deleteShare(42);
  154. $this->assertInstanceOf(get_class($expected), $result);
  155. $this->assertEquals($expected->getData(), $result->getData());
  156. }
  157. /**
  158. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  159. * @expectedExceptionMessage could not delete share
  160. */
  161. public function testDeleteShareLocked() {
  162. $node = $this->getMockBuilder('\OCP\Files\File')->getMock();
  163. $share = $this->newShare();
  164. $share->setSharedBy($this->currentUser)
  165. ->setNode($node);
  166. $this->shareManager
  167. ->expects($this->once())
  168. ->method('getShareById')
  169. ->with('ocinternal:42')
  170. ->willReturn($share);
  171. $this->shareManager
  172. ->expects($this->never())
  173. ->method('deleteShare')
  174. ->with($share);
  175. $node->expects($this->once())
  176. ->method('lock')
  177. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED)
  178. ->will($this->throwException(new LockedException('mypath')));
  179. $this->ocs->deleteShare(42);
  180. }
  181. /*
  182. * FIXME: Enable once we have a federated Share Provider
  183. public function testGetGetShareNotExists() {
  184. $this->shareManager
  185. ->expects($this->once())
  186. ->method('getShareById')
  187. ->with('ocinternal:42')
  188. ->will($this->throwException(new \OC\Share20\Exception\ShareNotFound()));
  189. $expected = new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.');
  190. $this->assertEquals($expected, $this->ocs->getShare(42));
  191. }
  192. */
  193. public function createShare($id, $shareType, $sharedWith, $sharedBy, $shareOwner, $path, $permissions,
  194. $shareTime, $expiration, $parent, $target, $mail_send, $token=null,
  195. $password=null) {
  196. $share = $this->getMockBuilder('\OCP\Share\IShare')->getMock();
  197. $share->method('getId')->willReturn($id);
  198. $share->method('getShareType')->willReturn($shareType);
  199. $share->method('getSharedWith')->willReturn($sharedWith);
  200. $share->method('getSharedBy')->willReturn($sharedBy);
  201. $share->method('getShareOwner')->willReturn($shareOwner);
  202. $share->method('getNode')->willReturn($path);
  203. $share->method('getPermissions')->willReturn($permissions);
  204. $time = new \DateTime();
  205. $time->setTimestamp($shareTime);
  206. $share->method('getShareTime')->willReturn($time);
  207. $share->method('getExpirationDate')->willReturn($expiration);
  208. $share->method('getTarget')->willReturn($target);
  209. $share->method('getMailSend')->willReturn($mail_send);
  210. $share->method('getToken')->willReturn($token);
  211. $share->method('getPassword')->willReturn($password);
  212. if ($shareType === \OCP\Share::SHARE_TYPE_USER ||
  213. $shareType === \OCP\Share::SHARE_TYPE_GROUP ||
  214. $shareType === \OCP\Share::SHARE_TYPE_LINK) {
  215. $share->method('getFullId')->willReturn('ocinternal:'.$id);
  216. }
  217. return $share;
  218. }
  219. public function dataGetShare() {
  220. $data = [];
  221. $cache = $this->getMockBuilder('OC\Files\Cache\Cache')
  222. ->disableOriginalConstructor()
  223. ->getMock();
  224. $cache->method('getNumericStorageId')->willReturn(101);
  225. $storage = $this->getMockBuilder('OC\Files\Storage\Storage')
  226. ->disableOriginalConstructor()
  227. ->getMock();
  228. $storage->method('getId')->willReturn('STORAGE');
  229. $storage->method('getCache')->willReturn($cache);
  230. $parentFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  231. $parentFolder->method('getId')->willReturn(3);
  232. $file = $this->getMockBuilder('OCP\Files\File')->getMock();
  233. $file->method('getId')->willReturn(1);
  234. $file->method('getPath')->willReturn('file');
  235. $file->method('getStorage')->willReturn($storage);
  236. $file->method('getParent')->willReturn($parentFolder);
  237. $file->method('getMimeType')->willReturn('myMimeType');
  238. $folder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  239. $folder->method('getId')->willReturn(2);
  240. $folder->method('getPath')->willReturn('folder');
  241. $folder->method('getStorage')->willReturn($storage);
  242. $folder->method('getParent')->willReturn($parentFolder);
  243. $folder->method('getMimeType')->willReturn('myFolderMimeType');
  244. // File shared with user
  245. $share = $this->createShare(
  246. 100,
  247. \OCP\Share::SHARE_TYPE_USER,
  248. 'userId',
  249. 'initiatorId',
  250. 'ownerId',
  251. $file,
  252. 4,
  253. 5,
  254. null,
  255. 6,
  256. 'target',
  257. 0
  258. );
  259. $expected = [
  260. 'id' => 100,
  261. 'share_type' => \OCP\Share::SHARE_TYPE_USER,
  262. 'share_with' => 'userId',
  263. 'share_with_displayname' => 'userDisplay',
  264. 'uid_owner' => 'initiatorId',
  265. 'displayname_owner' => 'initiatorDisplay',
  266. 'item_type' => 'file',
  267. 'item_source' => 1,
  268. 'file_source' => 1,
  269. 'file_target' => 'target',
  270. 'file_parent' => 3,
  271. 'token' => null,
  272. 'expiration' => null,
  273. 'permissions' => 4,
  274. 'stime' => 5,
  275. 'parent' => null,
  276. 'storage_id' => 'STORAGE',
  277. 'path' => 'file',
  278. 'storage' => 101,
  279. 'mail_send' => 0,
  280. 'uid_file_owner' => 'ownerId',
  281. 'displayname_file_owner' => 'ownerDisplay',
  282. 'mimetype' => 'myMimeType',
  283. ];
  284. $data[] = [$share, $expected];
  285. // Folder shared with group
  286. $share = $this->createShare(
  287. 101,
  288. \OCP\Share::SHARE_TYPE_GROUP,
  289. 'groupId',
  290. 'initiatorId',
  291. 'ownerId',
  292. $folder,
  293. 4,
  294. 5,
  295. null,
  296. 6,
  297. 'target',
  298. 0
  299. );
  300. $expected = [
  301. 'id' => 101,
  302. 'share_type' => \OCP\Share::SHARE_TYPE_GROUP,
  303. 'share_with' => 'groupId',
  304. 'share_with_displayname' => 'groupId',
  305. 'uid_owner' => 'initiatorId',
  306. 'displayname_owner' => 'initiatorDisplay',
  307. 'item_type' => 'folder',
  308. 'item_source' => 2,
  309. 'file_source' => 2,
  310. 'file_target' => 'target',
  311. 'file_parent' => 3,
  312. 'token' => null,
  313. 'expiration' => null,
  314. 'permissions' => 4,
  315. 'stime' => 5,
  316. 'parent' => null,
  317. 'storage_id' => 'STORAGE',
  318. 'path' => 'folder',
  319. 'storage' => 101,
  320. 'mail_send' => 0,
  321. 'uid_file_owner' => 'ownerId',
  322. 'displayname_file_owner' => 'ownerDisplay',
  323. 'mimetype' => 'myFolderMimeType',
  324. ];
  325. $data[] = [$share, $expected];
  326. // File shared by link with Expire
  327. $expire = \DateTime::createFromFormat('Y-m-d h:i:s', '2000-01-02 01:02:03');
  328. $share = $this->createShare(
  329. 101,
  330. \OCP\Share::SHARE_TYPE_LINK,
  331. null,
  332. 'initiatorId',
  333. 'ownerId',
  334. $folder,
  335. 4,
  336. 5,
  337. $expire,
  338. 6,
  339. 'target',
  340. 0,
  341. 'token',
  342. 'password'
  343. );
  344. $expected = [
  345. 'id' => 101,
  346. 'share_type' => \OCP\Share::SHARE_TYPE_LINK,
  347. 'share_with' => 'password',
  348. 'share_with_displayname' => 'password',
  349. 'uid_owner' => 'initiatorId',
  350. 'displayname_owner' => 'initiatorDisplay',
  351. 'item_type' => 'folder',
  352. 'item_source' => 2,
  353. 'file_source' => 2,
  354. 'file_target' => 'target',
  355. 'file_parent' => 3,
  356. 'token' => 'token',
  357. 'expiration' => '2000-01-02 00:00:00',
  358. 'permissions' => 4,
  359. 'stime' => 5,
  360. 'parent' => null,
  361. 'storage_id' => 'STORAGE',
  362. 'path' => 'folder',
  363. 'storage' => 101,
  364. 'mail_send' => 0,
  365. 'url' => 'url',
  366. 'uid_file_owner' => 'ownerId',
  367. 'displayname_file_owner' => 'ownerDisplay',
  368. 'mimetype' => 'myFolderMimeType',
  369. ];
  370. $data[] = [$share, $expected];
  371. return $data;
  372. }
  373. /**
  374. * @dataProvider dataGetShare
  375. */
  376. public function testGetShare(\OCP\Share\IShare $share, array $result) {
  377. /** @var ShareAPIController|\PHPUnit_Framework_MockObject_MockObject $ocs */
  378. $ocs = $this->getMockBuilder(ShareAPIController::class)
  379. ->setConstructorArgs([
  380. $this->appName,
  381. $this->request,
  382. $this->shareManager,
  383. $this->groupManager,
  384. $this->userManager,
  385. $this->rootFolder,
  386. $this->urlGenerator,
  387. $this->currentUser,
  388. $this->l,
  389. ])->setMethods(['canAccessShare'])
  390. ->getMock();
  391. $ocs->expects($this->any())
  392. ->method('canAccessShare')
  393. ->willReturn(true);
  394. $this->shareManager
  395. ->expects($this->once())
  396. ->method('getShareById')
  397. ->with($share->getFullId())
  398. ->willReturn($share);
  399. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  400. $userFolder
  401. ->method('getRelativePath')
  402. ->will($this->returnArgument(0));
  403. $userFolder->method('getById')
  404. ->with($share->getNodeId())
  405. ->willReturn([$share->getNode()]);
  406. $this->rootFolder->method('getUserFolder')
  407. ->with($this->currentUser)
  408. ->willReturn($userFolder);
  409. $this->urlGenerator
  410. ->method('linkToRouteAbsolute')
  411. ->willReturn('url');
  412. $initiator = $this->getMockBuilder('OCP\IUser')->getMock();
  413. $initiator->method('getUID')->willReturn('initiatorId');
  414. $initiator->method('getDisplayName')->willReturn('initiatorDisplay');
  415. $owner = $this->getMockBuilder('OCP\IUser')->getMock();
  416. $owner->method('getUID')->willReturn('ownerId');
  417. $owner->method('getDisplayName')->willReturn('ownerDisplay');
  418. $user = $this->getMockBuilder('OCP\IUser')->getMock();
  419. $user->method('getUID')->willReturn('userId');
  420. $user->method('getDisplayName')->willReturn('userDisplay');
  421. $group = $this->getMockBuilder('OCP\IGroup')->getMock();
  422. $group->method('getGID')->willReturn('groupId');
  423. $this->userManager->method('get')->will($this->returnValueMap([
  424. ['userId', $user],
  425. ['initiatorId', $initiator],
  426. ['ownerId', $owner],
  427. ]));
  428. $this->groupManager->method('get')->will($this->returnValueMap([
  429. ['group', $group],
  430. ]));
  431. $this->assertEquals($result, $ocs->getShare($share->getId())->getData()[0]);
  432. }
  433. /**
  434. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  435. * @expectedExceptionMessage Wrong share ID, share doesn't exist
  436. */
  437. public function testGetShareInvalidNode() {
  438. $share = \OC::$server->getShareManager()->newShare();
  439. $share->setSharedBy('initiator')
  440. ->setSharedWith('recipient')
  441. ->setShareOwner('owner');
  442. $this->shareManager
  443. ->expects($this->once())
  444. ->method('getShareById')
  445. ->with('ocinternal:42')
  446. ->willReturn($share);
  447. $this->ocs->getShare(42);
  448. }
  449. public function testCanAccessShare() {
  450. $share = $this->getMockBuilder('OCP\Share\IShare')->getMock();
  451. $share->method('getShareOwner')->willReturn($this->currentUser);
  452. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  453. $share = $this->getMockBuilder('OCP\Share\IShare')->getMock();
  454. $share->method('getSharedBy')->willReturn($this->currentUser);
  455. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  456. $share = $this->getMockBuilder('OCP\Share\IShare')->getMock();
  457. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  458. $share->method('getSharedWith')->willReturn($this->currentUser);
  459. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  460. $share = $this->getMockBuilder('OCP\Share\IShare')->getMock();
  461. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  462. $share->method('getSharedWith')->willReturn($this->getMockBuilder('OCP\IUser')->getMock());
  463. $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  464. $share = $this->getMockBuilder('OCP\Share\IShare')->getMock();
  465. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_GROUP);
  466. $share->method('getSharedWith')->willReturn('group');
  467. $user = $this->createMock(IUser::class);
  468. $this->userManager->method('get')
  469. ->with($this->currentUser)
  470. ->willReturn($user);
  471. $group = $this->getMockBuilder('OCP\IGroup')->getMock();
  472. $group->method('inGroup')->with($user)->willReturn(true);
  473. $group2 = $this->getMockBuilder('OCP\IGroup')->getMock();
  474. $group2->method('inGroup')->with($user)->willReturn(false);
  475. $this->groupManager->method('get')->will($this->returnValueMap([
  476. ['group', $group],
  477. ['group2', $group2],
  478. ]));
  479. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  480. $share = $this->getMockBuilder('OCP\Share\IShare')->getMock();
  481. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_GROUP);
  482. $share->method('getSharedWith')->willReturn('group2');
  483. $this->groupManager->method('get')->with('group2')->willReturn($group);
  484. $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  485. $share = $this->getMockBuilder('OCP\Share\IShare')->getMock();
  486. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  487. $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  488. }
  489. /**
  490. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  491. * @expectedExceptionMessage Please specify a file or folder path
  492. */
  493. public function testCreateShareNoPath() {
  494. $this->ocs->createShare();
  495. }
  496. /**
  497. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  498. * @expectedExceptionMessage Wrong path, file/folder doesn't exist
  499. */
  500. public function testCreateShareInvalidPath() {
  501. $userFolder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  502. $this->rootFolder->expects($this->once())
  503. ->method('getUserFolder')
  504. ->with('currentUser')
  505. ->willReturn($userFolder);
  506. $userFolder->expects($this->once())
  507. ->method('get')
  508. ->with('invalid-path')
  509. ->will($this->throwException(new \OCP\Files\NotFoundException()));
  510. $this->ocs->createShare('invalid-path');
  511. }
  512. /**
  513. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  514. * @expectedExceptionMessage invalid permissions
  515. */
  516. public function testCreateShareInvalidPermissions() {
  517. $share = $this->newShare();
  518. $this->shareManager->method('newShare')->willReturn($share);
  519. $userFolder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  520. $this->rootFolder->expects($this->once())
  521. ->method('getUserFolder')
  522. ->with('currentUser')
  523. ->willReturn($userFolder);
  524. $path = $this->getMockBuilder('\OCP\Files\File')->getMock();
  525. $userFolder->expects($this->once())
  526. ->method('get')
  527. ->with('valid-path')
  528. ->willReturn($path);
  529. $path->expects($this->once())
  530. ->method('lock')
  531. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  532. $this->ocs->createShare('valid-path', 32);
  533. }
  534. /**
  535. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  536. * @expectedExceptionMessage Please specify a valid user
  537. */
  538. public function testCreateShareUserNoShareWith() {
  539. $share = $this->newShare();
  540. $this->shareManager->method('newShare')->willReturn($share);
  541. $userFolder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  542. $this->rootFolder->expects($this->once())
  543. ->method('getUserFolder')
  544. ->with('currentUser')
  545. ->willReturn($userFolder);
  546. $path = $this->getMockBuilder('\OCP\Files\File')->getMock();
  547. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  548. $storage->method('instanceOfStorage')
  549. ->with('OCA\Files_Sharing\External\Storage')
  550. ->willReturn(false);
  551. $path->method('getStorage')->willReturn($storage);
  552. $userFolder->expects($this->once())
  553. ->method('get')
  554. ->with('valid-path')
  555. ->willReturn($path);
  556. $path->expects($this->once())
  557. ->method('lock')
  558. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  559. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER);
  560. }
  561. /**
  562. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  563. * @expectedExceptionMessage Please specify a valid user
  564. */
  565. public function testCreateShareUserNoValidShareWith() {
  566. $share = $this->newShare();
  567. $this->shareManager->method('newShare')->willReturn($share);
  568. $userFolder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  569. $this->rootFolder->expects($this->once())
  570. ->method('getUserFolder')
  571. ->with('currentUser')
  572. ->willReturn($userFolder);
  573. $path = $this->getMockBuilder('\OCP\Files\File')->getMock();
  574. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  575. $storage->method('instanceOfStorage')
  576. ->with('OCA\Files_Sharing\External\Storage')
  577. ->willReturn(false);
  578. $path->method('getStorage')->willReturn($storage);
  579. $userFolder->expects($this->once())
  580. ->method('get')
  581. ->with('valid-path')
  582. ->willReturn($path);
  583. $path->expects($this->once())
  584. ->method('lock')
  585. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  586. $this->userManager->method('userExists')
  587. ->with('invalidUser')
  588. ->willReturn(false);
  589. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, 'invalidUser');
  590. }
  591. public function testCreateShareUser() {
  592. $share = $this->newShare();
  593. $this->shareManager->method('newShare')->willReturn($share);
  594. /** @var \OCA\Files_Sharing\Controller\ShareAPIController $ocs */
  595. $ocs = $this->getMockBuilder(ShareAPIController::class)
  596. ->setConstructorArgs([
  597. $this->appName,
  598. $this->request,
  599. $this->shareManager,
  600. $this->groupManager,
  601. $this->userManager,
  602. $this->rootFolder,
  603. $this->urlGenerator,
  604. $this->currentUser,
  605. $this->l,
  606. ])->setMethods(['formatShare'])
  607. ->getMock();
  608. $userFolder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  609. $this->rootFolder->expects($this->once())
  610. ->method('getUserFolder')
  611. ->with('currentUser')
  612. ->willReturn($userFolder);
  613. $path = $this->getMockBuilder('\OCP\Files\File')->getMock();
  614. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  615. $storage->method('instanceOfStorage')
  616. ->with('OCA\Files_Sharing\External\Storage')
  617. ->willReturn(false);
  618. $path->method('getStorage')->willReturn($storage);
  619. $userFolder->expects($this->once())
  620. ->method('get')
  621. ->with('valid-path')
  622. ->willReturn($path);
  623. $this->userManager->method('userExists')->with('validUser')->willReturn(true);
  624. $path->expects($this->once())
  625. ->method('lock')
  626. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  627. $this->shareManager->method('createShare')
  628. ->with($this->callback(function (\OCP\Share\IShare $share) use ($path) {
  629. return $share->getNode() === $path &&
  630. $share->getPermissions() === (
  631. \OCP\Constants::PERMISSION_ALL &
  632. ~\OCP\Constants::PERMISSION_DELETE &
  633. ~\OCP\Constants::PERMISSION_CREATE
  634. ) &&
  635. $share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
  636. $share->getSharedWith() === 'validUser' &&
  637. $share->getSharedBy() === 'currentUser';
  638. }))
  639. ->will($this->returnArgument(0));
  640. $expected = new DataResponse(null);
  641. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, 'validUser');
  642. $this->assertInstanceOf(get_class($expected), $result);
  643. $this->assertEquals($expected->getData(), $result->getData());
  644. }
  645. /**
  646. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  647. * @expectedExceptionMessage Please specify a valid group
  648. */
  649. public function testCreateShareGroupNoValidShareWith() {
  650. $share = $this->newShare();
  651. $this->shareManager->method('newShare')->willReturn($share);
  652. $this->shareManager->method('createShare')->will($this->returnArgument(0));
  653. $this->shareManager->method('allowGroupSharing')->willReturn(true);
  654. $userFolder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  655. $this->rootFolder->expects($this->once())
  656. ->method('getUserFolder')
  657. ->with('currentUser')
  658. ->willReturn($userFolder);
  659. $path = $this->getMockBuilder('\OCP\Files\File')->getMock();
  660. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  661. $storage->method('instanceOfStorage')
  662. ->with('OCA\Files_Sharing\External\Storage')
  663. ->willReturn(false);
  664. $path->method('getStorage')->willReturn($storage);
  665. $userFolder->expects($this->once())
  666. ->method('get')
  667. ->with('valid-path')
  668. ->willReturn($path);
  669. $path->expects($this->once())
  670. ->method('lock')
  671. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  672. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_GROUP, 'invalidGroup');
  673. }
  674. public function testCreateShareGroup() {
  675. $share = $this->newShare();
  676. $this->shareManager->method('newShare')->willReturn($share);
  677. /** @var ShareAPIController|\PHPUnit_Framework_MockObject_MockObject $ocs */
  678. $ocs = $this->getMockBuilder(ShareAPIController::class)
  679. ->setConstructorArgs([
  680. $this->appName,
  681. $this->request,
  682. $this->shareManager,
  683. $this->groupManager,
  684. $this->userManager,
  685. $this->rootFolder,
  686. $this->urlGenerator,
  687. $this->currentUser,
  688. $this->l,
  689. ])->setMethods(['formatShare'])
  690. ->getMock();
  691. $this->request
  692. ->method('getParam')
  693. ->will($this->returnValueMap([
  694. ['path', null, 'valid-path'],
  695. ['permissions', null, \OCP\Constants::PERMISSION_ALL],
  696. ['shareType', '-1', \OCP\Share::SHARE_TYPE_GROUP],
  697. ['shareWith', null, 'validGroup'],
  698. ]));
  699. $userFolder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  700. $this->rootFolder->expects($this->once())
  701. ->method('getUserFolder')
  702. ->with('currentUser')
  703. ->willReturn($userFolder);
  704. $path = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  705. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  706. $storage->method('instanceOfStorage')
  707. ->with('OCA\Files_Sharing\External\Storage')
  708. ->willReturn(false);
  709. $path->method('getStorage')->willReturn($storage);
  710. $userFolder->expects($this->once())
  711. ->method('get')
  712. ->with('valid-path')
  713. ->willReturn($path);
  714. $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
  715. $this->shareManager->expects($this->once())
  716. ->method('allowGroupSharing')
  717. ->willReturn(true);
  718. $path->expects($this->once())
  719. ->method('lock')
  720. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  721. $this->shareManager->method('createShare')
  722. ->with($this->callback(function (\OCP\Share\IShare $share) use ($path) {
  723. return $share->getNode() === $path &&
  724. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  725. $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP &&
  726. $share->getSharedWith() === 'validGroup' &&
  727. $share->getSharedBy() === 'currentUser';
  728. }))
  729. ->will($this->returnArgument(0));
  730. $expected = new DataResponse(null);
  731. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_GROUP, 'validGroup');
  732. $this->assertInstanceOf(get_class($expected), $result);
  733. $this->assertEquals($expected->getData(), $result->getData());
  734. }
  735. /**
  736. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  737. * @expectedExceptionMessage Group sharing is disabled by the administrator
  738. */
  739. public function testCreateShareGroupNotAllowed() {
  740. $share = $this->newShare();
  741. $this->shareManager->method('newShare')->willReturn($share);
  742. $userFolder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  743. $this->rootFolder->expects($this->once())
  744. ->method('getUserFolder')
  745. ->with('currentUser')
  746. ->willReturn($userFolder);
  747. $path = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  748. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  749. $storage->method('instanceOfStorage')
  750. ->with('OCA\Files_Sharing\External\Storage')
  751. ->willReturn(false);
  752. $path->method('getStorage')->willReturn($storage);
  753. $userFolder->expects($this->once())
  754. ->method('get')
  755. ->with('valid-path')
  756. ->willReturn($path);
  757. $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
  758. $this->shareManager->expects($this->once())
  759. ->method('allowGroupSharing')
  760. ->willReturn(false);
  761. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_GROUP, 'invalidGroup');
  762. }
  763. /**
  764. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  765. * @expectedExceptionMessage Public link sharing is disabled by the administrator
  766. */
  767. public function testCreateShareLinkNoLinksAllowed() {
  768. $this->request
  769. ->method('getParam')
  770. ->will($this->returnValueMap([
  771. ['path', null, 'valid-path'],
  772. ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK],
  773. ]));
  774. $path = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  775. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  776. $storage->method('instanceOfStorage')
  777. ->with('OCA\Files_Sharing\External\Storage')
  778. ->willReturn(false);
  779. $path->method('getStorage')->willReturn($storage);
  780. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  781. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  782. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  783. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK);
  784. }
  785. /**
  786. * @expectedException \OCP\AppFramework\OCS\OCSForbiddenException
  787. * @expectedExceptionMessage Public upload disabled by the administrator
  788. */
  789. public function testCreateShareLinkNoPublicUpload() {
  790. $path = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  791. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  792. $storage->method('instanceOfStorage')
  793. ->with('OCA\Files_Sharing\External\Storage')
  794. ->willReturn(false);
  795. $path->method('getStorage')->willReturn($storage);
  796. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  797. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  798. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  799. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  800. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'true');
  801. }
  802. /**
  803. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  804. * @expectedExceptionMessage Public upload is only possible for publicly shared folders
  805. */
  806. public function testCreateShareLinkPublicUploadFile() {
  807. $path = $this->getMockBuilder('\OCP\Files\File')->getMock();
  808. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  809. $storage->method('instanceOfStorage')
  810. ->with('OCA\Files_Sharing\External\Storage')
  811. ->willReturn(false);
  812. $path->method('getStorage')->willReturn($storage);
  813. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  814. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  815. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  816. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  817. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  818. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'true');
  819. }
  820. public function testCreateShareLinkPublicUploadFolder() {
  821. $ocs = $this->mockFormatShare();
  822. $path = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  823. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  824. $storage->method('instanceOfStorage')
  825. ->with('OCA\Files_Sharing\External\Storage')
  826. ->willReturn(false);
  827. $path->method('getStorage')->willReturn($storage);
  828. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  829. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  830. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  831. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  832. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  833. $this->shareManager->expects($this->once())->method('createShare')->with(
  834. $this->callback(function (\OCP\Share\IShare $share) use ($path) {
  835. return $share->getNode() === $path &&
  836. $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  837. $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  838. $share->getSharedBy() === 'currentUser' &&
  839. $share->getPassword() === null &&
  840. $share->getExpirationDate() === null;
  841. })
  842. )->will($this->returnArgument(0));
  843. $expected = new DataResponse(null);
  844. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'true', '', '');
  845. $this->assertInstanceOf(get_class($expected), $result);
  846. $this->assertEquals($expected->getData(), $result->getData());
  847. }
  848. public function testCreateShareLinkPassword() {
  849. $ocs = $this->mockFormatShare();
  850. $path = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  851. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  852. $storage->method('instanceOfStorage')
  853. ->with('OCA\Files_Sharing\External\Storage')
  854. ->willReturn(false);
  855. $path->method('getStorage')->willReturn($storage);
  856. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  857. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  858. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  859. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  860. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  861. $this->shareManager->expects($this->once())->method('createShare')->with(
  862. $this->callback(function (\OCP\Share\IShare $share) use ($path) {
  863. return $share->getNode() === $path &&
  864. $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  865. $share->getPermissions() === \OCP\Constants::PERMISSION_READ &&
  866. $share->getSharedBy() === 'currentUser' &&
  867. $share->getPassword() === 'password' &&
  868. $share->getExpirationDate() === null;
  869. })
  870. )->will($this->returnArgument(0));
  871. $expected = new DataResponse(null);
  872. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', 'password', '');
  873. $this->assertInstanceOf(get_class($expected), $result);
  874. $this->assertEquals($expected->getData(), $result->getData());
  875. }
  876. public function testCreateShareValidExpireDate() {
  877. $ocs = $this->mockFormatShare();
  878. $this->request
  879. ->method('getParam')
  880. ->will($this->returnValueMap([
  881. ['path', null, 'valid-path'],
  882. ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK],
  883. ['publicUpload', null, 'false'],
  884. ['expireDate', '', '2000-01-01'],
  885. ['password', '', ''],
  886. ]));
  887. $path = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  888. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  889. $storage->method('instanceOfStorage')
  890. ->with('OCA\Files_Sharing\External\Storage')
  891. ->willReturn(false);
  892. $path->method('getStorage')->willReturn($storage);
  893. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  894. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  895. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  896. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  897. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  898. $this->shareManager->expects($this->once())->method('createShare')->with(
  899. $this->callback(function (\OCP\Share\IShare $share) use ($path) {
  900. $date = new \DateTime('2000-01-01');
  901. $date->setTime(0,0,0);
  902. return $share->getNode() === $path &&
  903. $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  904. $share->getPermissions() === \OCP\Constants::PERMISSION_READ &&
  905. $share->getSharedBy() === 'currentUser' &&
  906. $share->getPassword() === null &&
  907. $share->getExpirationDate() == $date;
  908. })
  909. )->will($this->returnArgument(0));
  910. $expected = new DataResponse(null);
  911. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', '2000-01-01');
  912. $this->assertInstanceOf(get_class($expected), $result);
  913. $this->assertEquals($expected->getData(), $result->getData());
  914. }
  915. /**
  916. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  917. * @expectedExceptionMessage Invalid date, date format must be YYYY-MM-DD
  918. */
  919. public function testCreateShareInvalidExpireDate() {
  920. $ocs = $this->mockFormatShare();
  921. $path = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  922. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  923. $storage->method('instanceOfStorage')
  924. ->with('OCA\Files_Sharing\External\Storage')
  925. ->willReturn(false);
  926. $path->method('getStorage')->willReturn($storage);
  927. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  928. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  929. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  930. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  931. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  932. $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', 'a1b2d3');
  933. }
  934. /**
  935. * Test for https://github.com/owncloud/core/issues/22587
  936. * TODO: Remove once proper solution is in place
  937. */
  938. public function testCreateReshareOfFederatedMountNoDeletePermissions() {
  939. $share = \OC::$server->getShareManager()->newShare();
  940. $this->shareManager->method('newShare')->willReturn($share);
  941. /** @var ShareAPIController|\PHPUnit_Framework_MockObject_MockObject $ocs */
  942. $ocs = $this->getMockBuilder(ShareAPIController::class)
  943. ->setConstructorArgs([
  944. $this->appName,
  945. $this->request,
  946. $this->shareManager,
  947. $this->groupManager,
  948. $this->userManager,
  949. $this->rootFolder,
  950. $this->urlGenerator,
  951. $this->currentUser,
  952. $this->l,
  953. ])->setMethods(['formatShare'])
  954. ->getMock();
  955. $userFolder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  956. $this->rootFolder->expects($this->once())
  957. ->method('getUserFolder')
  958. ->with('currentUser')
  959. ->willReturn($userFolder);
  960. $path = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  961. $storage = $this->getMockBuilder('OCP\Files\Storage')->getMock();
  962. $storage->method('instanceOfStorage')
  963. ->with('OCA\Files_Sharing\External\Storage')
  964. ->willReturn(true);
  965. $path->method('getStorage')->willReturn($storage);
  966. $path->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  967. $userFolder->expects($this->once())
  968. ->method('get')
  969. ->with('valid-path')
  970. ->willReturn($path);
  971. $this->userManager->method('userExists')->with('validUser')->willReturn(true);
  972. $this->shareManager
  973. ->expects($this->once())
  974. ->method('createShare')
  975. ->with($this->callback(function (\OCP\Share\IShare $share) {
  976. return $share->getPermissions() === \OCP\Constants::PERMISSION_READ;
  977. }))
  978. ->will($this->returnArgument(0));
  979. $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, 'validUser');
  980. }
  981. /**
  982. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  983. * @expectedExceptionMessage Wrong share ID, share doesn't exist
  984. */
  985. public function testUpdateShareCantAccess() {
  986. $node = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  987. $share = $this->newShare();
  988. $share->setNode($node);
  989. $node->expects($this->once())
  990. ->method('lock')
  991. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  992. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  993. $this->ocs->updateShare(42);
  994. }
  995. /**
  996. * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException
  997. * @expectedExceptionMessage Wrong or no update parameter given
  998. */
  999. public function testUpdateNoParametersLink() {
  1000. $node = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  1001. $share = $this->newShare();
  1002. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1003. ->setSharedBy($this->currentUser)
  1004. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1005. ->setNode($node);
  1006. $node->expects($this->once())
  1007. ->method('lock')
  1008. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1009. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1010. $this->ocs->updateShare(42);
  1011. }
  1012. /**
  1013. * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException
  1014. * @expectedExceptionMessage Wrong or no update parameter given
  1015. */
  1016. public function testUpdateNoParametersOther() {
  1017. $node = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  1018. $share = $this->newShare();
  1019. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1020. ->setSharedBy($this->currentUser)
  1021. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1022. ->setNode($node);
  1023. $node->expects($this->once())
  1024. ->method('lock')
  1025. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1026. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1027. $this->ocs->updateShare(42);
  1028. }
  1029. public function testUpdateLinkShareClear() {
  1030. $ocs = $this->mockFormatShare();
  1031. $node = $this->getMockBuilder(Folder::class)->getMock();
  1032. $share = $this->newShare();
  1033. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1034. ->setSharedBy($this->currentUser)
  1035. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1036. ->setPassword('password')
  1037. ->setExpirationDate(new \DateTime())
  1038. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1039. ->setNode($node);
  1040. $node->expects($this->once())
  1041. ->method('lock')
  1042. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1043. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1044. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1045. $this->callback(function (\OCP\Share\IShare $share) {
  1046. return $share->getPermissions() === \OCP\Constants::PERMISSION_READ &&
  1047. $share->getPassword() === null &&
  1048. $share->getExpirationDate() === null;
  1049. })
  1050. )->will($this->returnArgument(0));
  1051. $this->shareManager->method('getSharedWith')
  1052. ->willReturn([]);
  1053. $expected = new DataResponse(null);
  1054. $result = $ocs->updateShare(42, null, '', 'false', '');
  1055. $this->assertInstanceOf(get_class($expected), $result);
  1056. $this->assertEquals($expected->getData(), $result->getData());
  1057. }
  1058. public function testUpdateLinkShareSet() {
  1059. $ocs = $this->mockFormatShare();
  1060. $folder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  1061. $share = \OC::$server->getShareManager()->newShare();
  1062. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1063. ->setSharedBy($this->currentUser)
  1064. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1065. ->setNode($folder);
  1066. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1067. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1068. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1069. $this->callback(function (\OCP\Share\IShare $share) {
  1070. $date = new \DateTime('2000-01-01');
  1071. $date->setTime(0,0,0);
  1072. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  1073. $share->getPassword() === 'password' &&
  1074. $share->getExpirationDate() == $date;
  1075. })
  1076. )->will($this->returnArgument(0));
  1077. $this->shareManager->method('getSharedWith')
  1078. ->willReturn([]);
  1079. $expected = new DataResponse(null);
  1080. $result = $ocs->updateShare(42, null, 'password', 'true', '2000-01-01');
  1081. $this->assertInstanceOf(get_class($expected), $result);
  1082. $this->assertEquals($expected->getData(), $result->getData());
  1083. }
  1084. /**
  1085. * @dataProvider publicUploadParamsProvider
  1086. */
  1087. public function testUpdateLinkShareEnablePublicUpload($permissions, $publicUpload, $expireDate, $password) {
  1088. $ocs = $this->mockFormatShare();
  1089. $folder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  1090. $share = \OC::$server->getShareManager()->newShare();
  1091. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1092. ->setSharedBy($this->currentUser)
  1093. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1094. ->setPassword('password')
  1095. ->setNode($folder);
  1096. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1097. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1098. $this->shareManager->method('getSharedWith')->willReturn([]);
  1099. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1100. $this->callback(function (\OCP\Share\IShare $share) {
  1101. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  1102. $share->getPassword() === 'password' &&
  1103. $share->getExpirationDate() === null;
  1104. })
  1105. )->will($this->returnArgument(0));
  1106. $expected = new DataResponse(null);
  1107. $result = $ocs->updateShare(42, $permissions, $password, $publicUpload, $expireDate);
  1108. $this->assertInstanceOf(get_class($expected), $result);
  1109. $this->assertEquals($expected->getData(), $result->getData());
  1110. }
  1111. /**
  1112. * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException
  1113. * @expectedExceptionMessage Invalid date. Format must be YYYY-MM-DD
  1114. */
  1115. public function testUpdateLinkShareInvalidDate() {
  1116. $ocs = $this->mockFormatShare();
  1117. $folder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  1118. $share = \OC::$server->getShareManager()->newShare();
  1119. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1120. ->setSharedBy($this->currentUser)
  1121. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1122. ->setNode($folder);
  1123. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1124. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1125. $ocs->updateShare(42, null, 'password', 'true', '2000-01-a');
  1126. }
  1127. public function publicUploadParamsProvider() {
  1128. return [
  1129. [null, 'true', null, 'password'],
  1130. // legacy had no delete
  1131. [
  1132. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE,
  1133. null, null, 'password'
  1134. ],
  1135. // correct
  1136. [
  1137. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE,
  1138. null, null, 'password'
  1139. ],
  1140. ];
  1141. }
  1142. /**
  1143. * @dataProvider publicUploadParamsProvider
  1144. * @expectedException \OCP\AppFramework\OCS\OCSForbiddenException
  1145. * @expectedExceptionMessage Public upload disabled by the administrator
  1146. */
  1147. public function testUpdateLinkSharePublicUploadNotAllowed($permissions, $publicUpload, $expireDate, $password) {
  1148. $ocs = $this->mockFormatShare();
  1149. $folder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  1150. $share = \OC::$server->getShareManager()->newShare();
  1151. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1152. ->setSharedBy($this->currentUser)
  1153. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1154. ->setNode($folder);
  1155. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1156. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(false);
  1157. $ocs->updateShare(42, $permissions, $password, $publicUpload, $expireDate);
  1158. }
  1159. /**
  1160. * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException
  1161. * @expectedExceptionMessage Public upload is only possible for publicly shared folders
  1162. */
  1163. public function testUpdateLinkSharePublicUploadOnFile() {
  1164. $ocs = $this->mockFormatShare();
  1165. $file = $this->getMockBuilder('\OCP\Files\File')->getMock();
  1166. $share = \OC::$server->getShareManager()->newShare();
  1167. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1168. ->setSharedBy($this->currentUser)
  1169. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1170. ->setNode($file);
  1171. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1172. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1173. $ocs->updateShare(42, null, 'password', 'true', '');
  1174. }
  1175. public function testUpdateLinkSharePasswordDoesNotChangeOther() {
  1176. $ocs = $this->mockFormatShare();
  1177. $date = new \DateTime('2000-01-01');
  1178. $date->setTime(0,0,0);
  1179. $node = $this->getMockBuilder('\OCP\Files\File')->getMock();
  1180. $share = $this->newShare();
  1181. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1182. ->setSharedBy($this->currentUser)
  1183. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1184. ->setPassword('password')
  1185. ->setExpirationDate($date)
  1186. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1187. ->setNode($node);
  1188. $node->expects($this->once())
  1189. ->method('lock')
  1190. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1191. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1192. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1193. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  1194. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1195. $share->getPassword() === 'newpassword' &&
  1196. $share->getExpirationDate() === $date;
  1197. })
  1198. )->will($this->returnArgument(0));
  1199. $expected = new DataResponse(null);
  1200. $result = $ocs->updateShare(42, null, 'newpassword', null, null);
  1201. $this->assertInstanceOf(get_class($expected), $result);
  1202. $this->assertEquals($expected->getData(), $result->getData());
  1203. }
  1204. public function testUpdateLinkShareExpireDateDoesNotChangeOther() {
  1205. $ocs = $this->mockFormatShare();
  1206. $node = $this->getMockBuilder('\OCP\Files\File')->getMock();
  1207. $share = $this->newShare();
  1208. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1209. ->setSharedBy($this->currentUser)
  1210. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1211. ->setPassword('password')
  1212. ->setExpirationDate(new \DateTime())
  1213. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1214. ->setNode($node);
  1215. $node->expects($this->once())
  1216. ->method('lock')
  1217. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1218. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1219. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1220. $this->callback(function (\OCP\Share\IShare $share) {
  1221. $date = new \DateTime('2010-12-23');
  1222. $date->setTime(0,0,0);
  1223. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1224. $share->getPassword() === 'password' &&
  1225. $share->getExpirationDate() == $date;
  1226. })
  1227. )->will($this->returnArgument(0));
  1228. $expected = new DataResponse(null);
  1229. $result = $ocs->updateShare(42, null, null, null, '2010-12-23');
  1230. $this->assertInstanceOf(get_class($expected), $result);
  1231. $this->assertEquals($expected->getData(), $result->getData());
  1232. }
  1233. public function testUpdateLinkSharePublicUploadDoesNotChangeOther() {
  1234. $ocs = $this->mockFormatShare();
  1235. $date = new \DateTime('2000-01-01');
  1236. $folder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  1237. $share = \OC::$server->getShareManager()->newShare();
  1238. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1239. ->setSharedBy($this->currentUser)
  1240. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1241. ->setPassword('password')
  1242. ->setExpirationDate($date)
  1243. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1244. ->setNode($folder);
  1245. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1246. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1247. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1248. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  1249. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  1250. $share->getPassword() === 'password' &&
  1251. $share->getExpirationDate() === $date;
  1252. })
  1253. )->will($this->returnArgument(0));
  1254. $this->shareManager->method('getSharedWith')
  1255. ->willReturn([]);
  1256. $expected = new DataResponse(null);
  1257. $result = $ocs->updateShare(42, null, null, 'true', null);
  1258. $this->assertInstanceOf(get_class($expected), $result);
  1259. $this->assertEquals($expected->getData(), $result->getData());
  1260. }
  1261. public function testUpdateLinkSharePermissions() {
  1262. $ocs = $this->mockFormatShare();
  1263. $date = new \DateTime('2000-01-01');
  1264. $folder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  1265. $share = \OC::$server->getShareManager()->newShare();
  1266. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1267. ->setSharedBy($this->currentUser)
  1268. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1269. ->setPassword('password')
  1270. ->setExpirationDate($date)
  1271. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1272. ->setNode($folder);
  1273. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1274. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1275. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1276. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  1277. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  1278. $share->getPassword() === 'password' &&
  1279. $share->getExpirationDate() === $date;
  1280. })
  1281. )->will($this->returnArgument(0));
  1282. $this->shareManager->method('getSharedWith')->willReturn([]);
  1283. $expected = new DataResponse(null);
  1284. $result = $ocs->updateShare(42, 7, null, null, null);
  1285. $this->assertInstanceOf(get_class($expected), $result);
  1286. $this->assertEquals($expected->getData(), $result->getData());
  1287. }
  1288. /**
  1289. * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException
  1290. * @expectedExceptionMessage Can't change permissions for public share links
  1291. */
  1292. public function testUpdateLinkShareInvalidPermissions() {
  1293. $ocs = $this->mockFormatShare();
  1294. $date = new \DateTime('2000-01-01');
  1295. $folder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  1296. $share = \OC::$server->getShareManager()->newShare();
  1297. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1298. ->setSharedBy($this->currentUser)
  1299. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1300. ->setPassword('password')
  1301. ->setExpirationDate($date)
  1302. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1303. ->setNode($folder);
  1304. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1305. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1306. $ocs->updateShare(42, 31);
  1307. }
  1308. public function testUpdateOtherPermissions() {
  1309. $ocs = $this->mockFormatShare();
  1310. $file = $this->getMockBuilder('\OCP\Files\File')->getMock();
  1311. $share = \OC::$server->getShareManager()->newShare();
  1312. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1313. ->setSharedBy($this->currentUser)
  1314. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1315. ->setNode($file);
  1316. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1317. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1318. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1319. $this->callback(function (\OCP\Share\IShare $share) {
  1320. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL;
  1321. })
  1322. )->will($this->returnArgument(0));
  1323. $this->shareManager->method('getSharedWith')->willReturn([]);
  1324. $expected = new DataResponse(null);
  1325. $result = $ocs->updateShare(42, 31, null, null, null);
  1326. $this->assertInstanceOf(get_class($expected), $result);
  1327. $this->assertEquals($expected->getData(), $result->getData());
  1328. }
  1329. public function testUpdateShareCannotIncreasePermissions() {
  1330. $ocs = $this->mockFormatShare();
  1331. $folder = $this->createMock(Folder::class);
  1332. $share = \OC::$server->getShareManager()->newShare();
  1333. $share
  1334. ->setId(42)
  1335. ->setSharedBy($this->currentUser)
  1336. ->setShareOwner('anotheruser')
  1337. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1338. ->setSharedWith('group1')
  1339. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1340. ->setNode($folder);
  1341. // note: updateShare will modify the received instance but getSharedWith will reread from the database,
  1342. // so their values will be different
  1343. $incomingShare = \OC::$server->getShareManager()->newShare();
  1344. $incomingShare
  1345. ->setId(42)
  1346. ->setSharedBy($this->currentUser)
  1347. ->setShareOwner('anotheruser')
  1348. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1349. ->setSharedWith('group1')
  1350. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1351. ->setNode($folder);
  1352. $this->request
  1353. ->method('getParam')
  1354. ->will($this->returnValueMap([
  1355. ['permissions', null, '31'],
  1356. ]));
  1357. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1358. $this->shareManager->expects($this->any())
  1359. ->method('getSharedWith')
  1360. ->will($this->returnValueMap([
  1361. ['currentUser', \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0, []],
  1362. ['currentUser', \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]]
  1363. ]));
  1364. $this->shareManager->expects($this->never())->method('updateShare');
  1365. try {
  1366. $ocs->updateShare(42, 31);
  1367. $this->fail();
  1368. } catch (OCSNotFoundException $e) {
  1369. $this->assertEquals('Cannot increase permissions', $e->getMessage());
  1370. }
  1371. }
  1372. public function testUpdateShareCannotIncreasePermissionsLinkShare() {
  1373. $ocs = $this->mockFormatShare();
  1374. $folder = $this->createMock(Folder::class);
  1375. $share = \OC::$server->getShareManager()->newShare();
  1376. $share
  1377. ->setId(42)
  1378. ->setSharedBy($this->currentUser)
  1379. ->setShareOwner('anotheruser')
  1380. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1381. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1382. ->setNode($folder);
  1383. // note: updateShare will modify the received instance but getSharedWith will reread from the database,
  1384. // so their values will be different
  1385. $incomingShare = \OC::$server->getShareManager()->newShare();
  1386. $incomingShare
  1387. ->setId(42)
  1388. ->setSharedBy($this->currentUser)
  1389. ->setShareOwner('anotheruser')
  1390. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1391. ->setSharedWith('currentUser')
  1392. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1393. ->setNode($folder);
  1394. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1395. $this->shareManager->expects($this->any())
  1396. ->method('getSharedWith')
  1397. ->will($this->returnValueMap([
  1398. ['currentUser', \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0, [$incomingShare]],
  1399. ['currentUser', \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0, []]
  1400. ]));
  1401. $this->shareManager->expects($this->never())->method('updateShare');
  1402. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1403. try {
  1404. $ocs->updateShare(42, null, null, 'true');
  1405. $this->fail();
  1406. } catch (OCSNotFoundException $e) {
  1407. $this->assertEquals('Cannot increase permissions', $e->getMessage());
  1408. }
  1409. }
  1410. public function testUpdateShareCanIncreasePermissionsIfOwner() {
  1411. $ocs = $this->mockFormatShare();
  1412. $folder = $this->createMock(Folder::class);
  1413. $share = \OC::$server->getShareManager()->newShare();
  1414. $share
  1415. ->setId(42)
  1416. ->setSharedBy($this->currentUser)
  1417. ->setShareOwner($this->currentUser)
  1418. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1419. ->setSharedWith('group1')
  1420. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1421. ->setNode($folder);
  1422. // note: updateShare will modify the received instance but getSharedWith will reread from the database,
  1423. // so their values will be different
  1424. $incomingShare = \OC::$server->getShareManager()->newShare();
  1425. $incomingShare
  1426. ->setId(42)
  1427. ->setSharedBy($this->currentUser)
  1428. ->setShareOwner($this->currentUser)
  1429. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1430. ->setSharedWith('group1')
  1431. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1432. ->setNode($folder);
  1433. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1434. $this->shareManager->expects($this->any())
  1435. ->method('getSharedWith')
  1436. ->will($this->returnValueMap([
  1437. ['currentUser', \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0, []],
  1438. ['currentUser', \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]]
  1439. ]));
  1440. $this->shareManager->expects($this->once())
  1441. ->method('updateShare')
  1442. ->with($share)
  1443. ->willReturn($share);
  1444. $result = $ocs->updateShare(42, 31);
  1445. $this->assertInstanceOf(DataResponse::class, $result);
  1446. }
  1447. public function dataFormatShare() {
  1448. $file = $this->getMockBuilder('\OCP\Files\File')->getMock();
  1449. $folder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  1450. $parent = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  1451. $file->method('getMimeType')->willReturn('myMimeType');
  1452. $folder->method('getMimeType')->willReturn('myFolderMimeType');
  1453. $file->method('getPath')->willReturn('file');
  1454. $folder->method('getPath')->willReturn('folder');
  1455. $parent->method('getId')->willReturn(1);
  1456. $folder->method('getId')->willReturn(2);
  1457. $file->method('getId')->willReturn(3);
  1458. $file->method('getParent')->willReturn($parent);
  1459. $folder->method('getParent')->willReturn($parent);
  1460. $cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
  1461. $cache->method('getNumericStorageId')->willReturn(100);
  1462. $storage = $this->getMockBuilder('\OCP\Files\Storage')->getMock();
  1463. $storage->method('getId')->willReturn('storageId');
  1464. $storage->method('getCache')->willReturn($cache);
  1465. $file->method('getStorage')->willReturn($storage);
  1466. $folder->method('getStorage')->willReturn($storage);
  1467. $owner = $this->getMockBuilder('\OCP\IUser')->getMock();
  1468. $owner->method('getDisplayName')->willReturn('ownerDN');
  1469. $initiator = $this->getMockBuilder('\OCP\IUser')->getMock();
  1470. $initiator->method('getDisplayName')->willReturn('initiatorDN');
  1471. $recipient = $this->getMockBuilder('\OCP\IUser')->getMock();
  1472. $recipient->method('getDisplayName')->willReturn('recipientDN');
  1473. $result = [];
  1474. $share = \OC::$server->getShareManager()->newShare();
  1475. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1476. ->setSharedWith('recipient')
  1477. ->setSharedBy('initiator')
  1478. ->setShareOwner('owner')
  1479. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1480. ->setNode($file)
  1481. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  1482. ->setTarget('myTarget')
  1483. ->setId(42);
  1484. /* User backend down */
  1485. $result[] = [
  1486. [
  1487. 'id' => 42,
  1488. 'share_type' => \OCP\Share::SHARE_TYPE_USER,
  1489. 'uid_owner' => 'initiator',
  1490. 'displayname_owner' => 'initiator',
  1491. 'permissions' => 1,
  1492. 'stime' => 946684862,
  1493. 'parent' => null,
  1494. 'expiration' => null,
  1495. 'token' => null,
  1496. 'uid_file_owner' => 'owner',
  1497. 'displayname_file_owner' => 'owner',
  1498. 'path' => 'file',
  1499. 'item_type' => 'file',
  1500. 'storage_id' => 'storageId',
  1501. 'storage' => 100,
  1502. 'item_source' => 3,
  1503. 'file_source' => 3,
  1504. 'file_parent' => 1,
  1505. 'file_target' => 'myTarget',
  1506. 'share_with' => 'recipient',
  1507. 'share_with_displayname' => 'recipient',
  1508. 'mail_send' => 0,
  1509. 'mimetype' => 'myMimeType',
  1510. ], $share, [], false
  1511. ];
  1512. /* User backend up */
  1513. $result[] = [
  1514. [
  1515. 'id' => 42,
  1516. 'share_type' => \OCP\Share::SHARE_TYPE_USER,
  1517. 'uid_owner' => 'initiator',
  1518. 'displayname_owner' => 'initiatorDN',
  1519. 'permissions' => 1,
  1520. 'stime' => 946684862,
  1521. 'parent' => null,
  1522. 'expiration' => null,
  1523. 'token' => null,
  1524. 'uid_file_owner' => 'owner',
  1525. 'displayname_file_owner' => 'ownerDN',
  1526. 'path' => 'file',
  1527. 'item_type' => 'file',
  1528. 'storage_id' => 'storageId',
  1529. 'storage' => 100,
  1530. 'item_source' => 3,
  1531. 'file_source' => 3,
  1532. 'file_parent' => 1,
  1533. 'file_target' => 'myTarget',
  1534. 'share_with' => 'recipient',
  1535. 'share_with_displayname' => 'recipientDN',
  1536. 'mail_send' => 0,
  1537. 'mimetype' => 'myMimeType',
  1538. ], $share, [
  1539. ['owner', $owner],
  1540. ['initiator', $initiator],
  1541. ['recipient', $recipient],
  1542. ], false
  1543. ];
  1544. $share = \OC::$server->getShareManager()->newShare();
  1545. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1546. ->setSharedWith('recipient')
  1547. ->setSharedBy('initiator')
  1548. ->setShareOwner('owner')
  1549. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1550. ->setNode($file)
  1551. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  1552. ->setTarget('myTarget')
  1553. ->setId(42);
  1554. /* User backend down */
  1555. $result[] = [
  1556. [
  1557. 'id' => 42,
  1558. 'share_type' => \OCP\Share::SHARE_TYPE_USER,
  1559. 'uid_owner' => 'initiator',
  1560. 'displayname_owner' => 'initiator',
  1561. 'permissions' => 1,
  1562. 'stime' => 946684862,
  1563. 'parent' => null,
  1564. 'expiration' => null,
  1565. 'token' => null,
  1566. 'uid_file_owner' => 'owner',
  1567. 'displayname_file_owner' => 'owner',
  1568. 'path' => 'file',
  1569. 'item_type' => 'file',
  1570. 'storage_id' => 'storageId',
  1571. 'storage' => 100,
  1572. 'item_source' => 3,
  1573. 'file_source' => 3,
  1574. 'file_parent' => 1,
  1575. 'file_target' => 'myTarget',
  1576. 'share_with' => 'recipient',
  1577. 'share_with_displayname' => 'recipient',
  1578. 'mail_send' => 0,
  1579. 'mimetype' => 'myMimeType',
  1580. ], $share, [], false
  1581. ];
  1582. // with existing group
  1583. $share = \OC::$server->getShareManager()->newShare();
  1584. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1585. ->setSharedWith('recipientGroup')
  1586. ->setSharedBy('initiator')
  1587. ->setShareOwner('owner')
  1588. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1589. ->setNode($file)
  1590. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  1591. ->setTarget('myTarget')
  1592. ->setId(42);
  1593. $result[] = [
  1594. [
  1595. 'id' => 42,
  1596. 'share_type' => \OCP\Share::SHARE_TYPE_GROUP,
  1597. 'uid_owner' => 'initiator',
  1598. 'displayname_owner' => 'initiator',
  1599. 'permissions' => 1,
  1600. 'stime' => 946684862,
  1601. 'parent' => null,
  1602. 'expiration' => null,
  1603. 'token' => null,
  1604. 'uid_file_owner' => 'owner',
  1605. 'displayname_file_owner' => 'owner',
  1606. 'path' => 'file',
  1607. 'item_type' => 'file',
  1608. 'storage_id' => 'storageId',
  1609. 'storage' => 100,
  1610. 'item_source' => 3,
  1611. 'file_source' => 3,
  1612. 'file_parent' => 1,
  1613. 'file_target' => 'myTarget',
  1614. 'share_with' => 'recipientGroup',
  1615. 'share_with_displayname' => 'recipientGroupDisplayName',
  1616. 'mail_send' => 0,
  1617. 'mimetype' => 'myMimeType',
  1618. ], $share, [], false
  1619. ];
  1620. // with unknown group / no group backend
  1621. $share = \OC::$server->getShareManager()->newShare();
  1622. $share->setShareType(Share::SHARE_TYPE_GROUP)
  1623. ->setSharedWith('recipientGroup2')
  1624. ->setSharedBy('initiator')
  1625. ->setShareOwner('owner')
  1626. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1627. ->setNode($file)
  1628. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  1629. ->setTarget('myTarget')
  1630. ->setId(42);
  1631. $result[] = [
  1632. [
  1633. 'id' => 42,
  1634. 'share_type' => Share::SHARE_TYPE_GROUP,
  1635. 'uid_owner' => 'initiator',
  1636. 'displayname_owner' => 'initiator',
  1637. 'permissions' => 1,
  1638. 'stime' => 946684862,
  1639. 'parent' => null,
  1640. 'expiration' => null,
  1641. 'token' => null,
  1642. 'uid_file_owner' => 'owner',
  1643. 'displayname_file_owner' => 'owner',
  1644. 'path' => 'file',
  1645. 'item_type' => 'file',
  1646. 'storage_id' => 'storageId',
  1647. 'storage' => 100,
  1648. 'item_source' => 3,
  1649. 'file_source' => 3,
  1650. 'file_parent' => 1,
  1651. 'file_target' => 'myTarget',
  1652. 'share_with' => 'recipientGroup2',
  1653. 'share_with_displayname' => 'recipientGroup2',
  1654. 'mail_send' => 0,
  1655. 'mimetype' => 'myMimeType',
  1656. ], $share, [], false
  1657. ];
  1658. $share = \OC::$server->getShareManager()->newShare();
  1659. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1660. ->setSharedBy('initiator')
  1661. ->setShareOwner('owner')
  1662. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1663. ->setNode($file)
  1664. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  1665. ->setTarget('myTarget')
  1666. ->setPassword('mypassword')
  1667. ->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
  1668. ->setToken('myToken')
  1669. ->setId(42);
  1670. $result[] = [
  1671. [
  1672. 'id' => 42,
  1673. 'share_type' => \OCP\Share::SHARE_TYPE_LINK,
  1674. 'uid_owner' => 'initiator',
  1675. 'displayname_owner' => 'initiator',
  1676. 'permissions' => 1,
  1677. 'stime' => 946684862,
  1678. 'parent' => null,
  1679. 'expiration' => '2001-01-02 00:00:00',
  1680. 'token' => 'myToken',
  1681. 'uid_file_owner' => 'owner',
  1682. 'displayname_file_owner' => 'owner',
  1683. 'path' => 'file',
  1684. 'item_type' => 'file',
  1685. 'storage_id' => 'storageId',
  1686. 'storage' => 100,
  1687. 'item_source' => 3,
  1688. 'file_source' => 3,
  1689. 'file_parent' => 1,
  1690. 'file_target' => 'myTarget',
  1691. 'share_with' => 'mypassword',
  1692. 'share_with_displayname' => 'mypassword',
  1693. 'mail_send' => 0,
  1694. 'url' => 'myLink',
  1695. 'mimetype' => 'myMimeType',
  1696. ], $share, [], false
  1697. ];
  1698. $share = \OC::$server->getShareManager()->newShare();
  1699. $share->setShareType(\OCP\Share::SHARE_TYPE_REMOTE)
  1700. ->setSharedBy('initiator')
  1701. ->setSharedWith('user@server.com')
  1702. ->setShareOwner('owner')
  1703. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1704. ->setNode($folder)
  1705. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  1706. ->setTarget('myTarget')
  1707. ->setId(42);
  1708. $result[] = [
  1709. [
  1710. 'id' => 42,
  1711. 'share_type' => \OCP\Share::SHARE_TYPE_REMOTE,
  1712. 'uid_owner' => 'initiator',
  1713. 'displayname_owner' => 'initiator',
  1714. 'permissions' => 1,
  1715. 'stime' => 946684862,
  1716. 'parent' => null,
  1717. 'expiration' => null,
  1718. 'token' => null,
  1719. 'uid_file_owner' => 'owner',
  1720. 'displayname_file_owner' => 'owner',
  1721. 'path' => 'folder',
  1722. 'item_type' => 'folder',
  1723. 'storage_id' => 'storageId',
  1724. 'storage' => 100,
  1725. 'item_source' => 2,
  1726. 'file_source' => 2,
  1727. 'file_parent' => 1,
  1728. 'file_target' => 'myTarget',
  1729. 'share_with' => 'user@server.com',
  1730. 'share_with_displayname' => 'user@server.com',
  1731. 'mail_send' => 0,
  1732. 'mimetype' => 'myFolderMimeType',
  1733. ], $share, [], false
  1734. ];
  1735. $share = \OC::$server->getShareManager()->newShare();
  1736. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1737. ->setSharedBy('initiator')
  1738. ->setSharedWith('recipient')
  1739. ->setShareOwner('owner')
  1740. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1741. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  1742. ->setTarget('myTarget')
  1743. ->setId(42);
  1744. $result[] = [
  1745. [], $share, [], true
  1746. ];
  1747. return $result;
  1748. }
  1749. /**
  1750. * @dataProvider dataFormatShare
  1751. *
  1752. * @param array $expects
  1753. * @param \OCP\Share\IShare $share
  1754. * @param array $users
  1755. * @param $exception
  1756. */
  1757. public function testFormatShare(array $expects, \OCP\Share\IShare $share, array $users, $exception) {
  1758. $this->userManager->method('get')->will($this->returnValueMap($users));
  1759. $recipientGroup = $this->createMock('\OCP\IGroup');
  1760. $recipientGroup->method('getDisplayName')->willReturn('recipientGroupDisplayName');
  1761. $this->groupManager->method('get')->will($this->returnValueMap([
  1762. ['recipientGroup', $recipientGroup],
  1763. ]));
  1764. $this->urlGenerator->method('linkToRouteAbsolute')
  1765. ->with('files_sharing.sharecontroller.showShare', ['token' => 'myToken'])
  1766. ->willReturn('myLink');
  1767. $this->rootFolder->method('getUserFolder')
  1768. ->with($this->currentUser)
  1769. ->will($this->returnSelf());
  1770. if (!$exception) {
  1771. $this->rootFolder->method('getById')
  1772. ->with($share->getNodeId())
  1773. ->willReturn([$share->getNode()]);
  1774. $this->rootFolder->method('getRelativePath')
  1775. ->with($share->getNode()->getPath())
  1776. ->will($this->returnArgument(0));
  1777. }
  1778. try {
  1779. $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
  1780. $this->assertFalse($exception);
  1781. $this->assertEquals($expects, $result);
  1782. } catch (NotFoundException $e) {
  1783. $this->assertTrue($exception);
  1784. }
  1785. }
  1786. }