ShareAPIControllerTest.php 68 KB

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