ManagerTest.php 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843
  1. <?php
  2. /**
  3. * @author Roeland Jago Douma <rullzer@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Share20;
  22. use OC\Files\Mount\MoveableMount;
  23. use OC\HintException;
  24. use OC\Share20\DefaultShareProvider;
  25. use OCP\Files\File;
  26. use OCP\Files\Folder;
  27. use OCP\Files\IRootFolder;
  28. use OCP\Files\Mount\IMountPoint;
  29. use OCP\Files\Node;
  30. use OCP\Files\Storage;
  31. use OCP\IGroup;
  32. use OCP\IServerContainer;
  33. use OCP\IUser;
  34. use OCP\IUserManager;
  35. use OCP\Share\Exceptions\ShareNotFound;
  36. use OCP\Share\IProviderFactory;
  37. use OCP\Share\IShare;
  38. use OC\Share20\Manager;
  39. use OC\Share20\Exception;
  40. use OC\Share20\Share;
  41. use OCP\IL10N;
  42. use OCP\ILogger;
  43. use OCP\IConfig;
  44. use OCP\Share\IShareProvider;
  45. use OCP\Security\ISecureRandom;
  46. use OCP\Security\IHasher;
  47. use OCP\Files\Mount\IMountManager;
  48. use OCP\IGroupManager;
  49. use Symfony\Component\EventDispatcher\EventDispatcher;
  50. use Symfony\Component\EventDispatcher\GenericEvent;
  51. /**
  52. * Class ManagerTest
  53. *
  54. * @package Test\Share20
  55. * @group DB
  56. */
  57. class ManagerTest extends \Test\TestCase {
  58. /** @var Manager */
  59. protected $manager;
  60. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  61. protected $logger;
  62. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  63. protected $config;
  64. /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
  65. protected $secureRandom;
  66. /** @var IHasher|\PHPUnit_Framework_MockObject_MockObject */
  67. protected $hasher;
  68. /** @var IShareProvider|\PHPUnit_Framework_MockObject_MockObject */
  69. protected $defaultProvider;
  70. /** @var IMountManager|\PHPUnit_Framework_MockObject_MockObject */
  71. protected $mountManager;
  72. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  73. protected $groupManager;
  74. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  75. protected $l;
  76. /** @var DummyFactory */
  77. protected $factory;
  78. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  79. protected $userManager;
  80. /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
  81. protected $rootFolder;
  82. /** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */
  83. protected $eventDispatcher;
  84. public function setUp() {
  85. $this->logger = $this->createMock(ILogger::class);
  86. $this->config = $this->createMock(IConfig::class);
  87. $this->secureRandom = $this->createMock(ISecureRandom::class);
  88. $this->hasher = $this->createMock(IHasher::class);
  89. $this->mountManager = $this->createMock(IMountManager::class);
  90. $this->groupManager = $this->createMock(IGroupManager::class);
  91. $this->userManager = $this->createMock(IUserManager::class);
  92. $this->rootFolder = $this->createMock(IRootFolder::class);
  93. $this->eventDispatcher = $this->createMock(EventDispatcher::class);
  94. $this->l = $this->createMock(IL10N::class);
  95. $this->l->method('t')
  96. ->will($this->returnCallback(function($text, $parameters = []) {
  97. return vsprintf($text, $parameters);
  98. }));
  99. $this->factory = new DummyFactory(\OC::$server);
  100. $this->manager = new Manager(
  101. $this->logger,
  102. $this->config,
  103. $this->secureRandom,
  104. $this->hasher,
  105. $this->mountManager,
  106. $this->groupManager,
  107. $this->l,
  108. $this->factory,
  109. $this->userManager,
  110. $this->rootFolder,
  111. $this->eventDispatcher
  112. );
  113. $this->defaultProvider = $this->createMock(DefaultShareProvider::class);
  114. $this->defaultProvider->method('identifier')->willReturn('default');
  115. $this->factory->setProvider($this->defaultProvider);
  116. }
  117. /**
  118. * @return \PHPUnit_Framework_MockObject_MockBuilder
  119. */
  120. private function createManagerMock() {
  121. return $this->getMockBuilder('\OC\Share20\Manager')
  122. ->setConstructorArgs([
  123. $this->logger,
  124. $this->config,
  125. $this->secureRandom,
  126. $this->hasher,
  127. $this->mountManager,
  128. $this->groupManager,
  129. $this->l,
  130. $this->factory,
  131. $this->userManager,
  132. $this->rootFolder,
  133. $this->eventDispatcher
  134. ]);
  135. }
  136. /**
  137. * @expectedException \InvalidArgumentException
  138. */
  139. public function testDeleteNoShareId() {
  140. $share = $this->manager->newShare();
  141. $this->manager->deleteShare($share);
  142. }
  143. public function dataTestDelete() {
  144. $user = $this->createMock(IUser::class);
  145. $user->method('getUID')->willReturn('sharedWithUser');
  146. $group = $this->createMock(IGroup::class);
  147. $group->method('getGID')->willReturn('sharedWithGroup');
  148. return [
  149. [\OCP\Share::SHARE_TYPE_USER, 'sharedWithUser'],
  150. [\OCP\Share::SHARE_TYPE_GROUP, 'sharedWithGroup'],
  151. [\OCP\Share::SHARE_TYPE_LINK, ''],
  152. [\OCP\Share::SHARE_TYPE_REMOTE, 'foo@bar.com'],
  153. ];
  154. }
  155. /**
  156. * @dataProvider dataTestDelete
  157. */
  158. public function testDelete($shareType, $sharedWith) {
  159. $manager = $this->createManagerMock()
  160. ->setMethods(['getShareById', 'deleteChildren'])
  161. ->getMock();
  162. $path = $this->createMock(File::class);
  163. $path->method('getId')->willReturn(1);
  164. $share = $this->manager->newShare();
  165. $share->setId(42)
  166. ->setProviderId('prov')
  167. ->setShareType($shareType)
  168. ->setSharedWith($sharedWith)
  169. ->setSharedBy('sharedBy')
  170. ->setNode($path)
  171. ->setTarget('myTarget');
  172. $manager->expects($this->once())->method('deleteChildren')->with($share);
  173. $this->defaultProvider
  174. ->expects($this->once())
  175. ->method('delete')
  176. ->with($share);
  177. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock();
  178. \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
  179. \OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');
  180. $hookListnerExpectsPre = [
  181. 'id' => 42,
  182. 'itemType' => 'file',
  183. 'itemSource' => 1,
  184. 'shareType' => $shareType,
  185. 'shareWith' => $sharedWith,
  186. 'itemparent' => null,
  187. 'uidOwner' => 'sharedBy',
  188. 'fileSource' => 1,
  189. 'fileTarget' => 'myTarget',
  190. ];
  191. $hookListnerExpectsPost = [
  192. 'id' => 42,
  193. 'itemType' => 'file',
  194. 'itemSource' => 1,
  195. 'shareType' => $shareType,
  196. 'shareWith' => $sharedWith,
  197. 'itemparent' => null,
  198. 'uidOwner' => 'sharedBy',
  199. 'fileSource' => 1,
  200. 'fileTarget' => 'myTarget',
  201. 'deletedShares' => [
  202. [
  203. 'id' => 42,
  204. 'itemType' => 'file',
  205. 'itemSource' => 1,
  206. 'shareType' => $shareType,
  207. 'shareWith' => $sharedWith,
  208. 'itemparent' => null,
  209. 'uidOwner' => 'sharedBy',
  210. 'fileSource' => 1,
  211. 'fileTarget' => 'myTarget',
  212. ],
  213. ],
  214. ];
  215. $hookListner
  216. ->expects($this->exactly(1))
  217. ->method('pre')
  218. ->with($hookListnerExpectsPre);
  219. $hookListner
  220. ->expects($this->exactly(1))
  221. ->method('post')
  222. ->with($hookListnerExpectsPost);
  223. $manager->deleteShare($share);
  224. }
  225. public function testDeleteLazyShare() {
  226. $manager = $this->createManagerMock()
  227. ->setMethods(['getShareById', 'deleteChildren'])
  228. ->getMock();
  229. $share = $this->manager->newShare();
  230. $share->setId(42)
  231. ->setProviderId('prov')
  232. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  233. ->setSharedWith('sharedWith')
  234. ->setSharedBy('sharedBy')
  235. ->setShareOwner('shareOwner')
  236. ->setTarget('myTarget')
  237. ->setNodeId(1)
  238. ->setNodeType('file');
  239. $this->rootFolder->expects($this->never())->method($this->anything());
  240. $manager->expects($this->once())->method('deleteChildren')->with($share);
  241. $this->defaultProvider
  242. ->expects($this->once())
  243. ->method('delete')
  244. ->with($share);
  245. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock();
  246. \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
  247. \OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');
  248. $hookListnerExpectsPre = [
  249. 'id' => 42,
  250. 'itemType' => 'file',
  251. 'itemSource' => 1,
  252. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  253. 'shareWith' => 'sharedWith',
  254. 'itemparent' => null,
  255. 'uidOwner' => 'sharedBy',
  256. 'fileSource' => 1,
  257. 'fileTarget' => 'myTarget',
  258. ];
  259. $hookListnerExpectsPost = [
  260. 'id' => 42,
  261. 'itemType' => 'file',
  262. 'itemSource' => 1,
  263. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  264. 'shareWith' => 'sharedWith',
  265. 'itemparent' => null,
  266. 'uidOwner' => 'sharedBy',
  267. 'fileSource' => 1,
  268. 'fileTarget' => 'myTarget',
  269. 'deletedShares' => [
  270. [
  271. 'id' => 42,
  272. 'itemType' => 'file',
  273. 'itemSource' => 1,
  274. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  275. 'shareWith' => 'sharedWith',
  276. 'itemparent' => null,
  277. 'uidOwner' => 'sharedBy',
  278. 'fileSource' => 1,
  279. 'fileTarget' => 'myTarget',
  280. ],
  281. ],
  282. ];
  283. $hookListner
  284. ->expects($this->exactly(1))
  285. ->method('pre')
  286. ->with($hookListnerExpectsPre);
  287. $hookListner
  288. ->expects($this->exactly(1))
  289. ->method('post')
  290. ->with($hookListnerExpectsPost);
  291. $manager->deleteShare($share);
  292. }
  293. public function testDeleteNested() {
  294. $manager = $this->createManagerMock()
  295. ->setMethods(['getShareById'])
  296. ->getMock();
  297. $path = $this->createMock(File::class);
  298. $path->method('getId')->willReturn(1);
  299. $share1 = $this->manager->newShare();
  300. $share1->setId(42)
  301. ->setProviderId('prov')
  302. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  303. ->setSharedWith('sharedWith1')
  304. ->setSharedBy('sharedBy1')
  305. ->setNode($path)
  306. ->setTarget('myTarget1');
  307. $share2 = $this->manager->newShare();
  308. $share2->setId(43)
  309. ->setProviderId('prov')
  310. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  311. ->setSharedWith('sharedWith2')
  312. ->setSharedBy('sharedBy2')
  313. ->setNode($path)
  314. ->setTarget('myTarget2')
  315. ->setParent(42);
  316. $share3 = $this->manager->newShare();
  317. $share3->setId(44)
  318. ->setProviderId('prov')
  319. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  320. ->setSharedBy('sharedBy3')
  321. ->setNode($path)
  322. ->setTarget('myTarget3')
  323. ->setParent(43);
  324. $this->defaultProvider
  325. ->method('getChildren')
  326. ->will($this->returnValueMap([
  327. [$share1, [$share2]],
  328. [$share2, [$share3]],
  329. [$share3, []],
  330. ]));
  331. $this->defaultProvider
  332. ->method('delete')
  333. ->withConsecutive($share3, $share2, $share1);
  334. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock();
  335. \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
  336. \OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');
  337. $hookListnerExpectsPre = [
  338. 'id' => 42,
  339. 'itemType' => 'file',
  340. 'itemSource' => 1,
  341. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  342. 'shareWith' => 'sharedWith1',
  343. 'itemparent' => null,
  344. 'uidOwner' => 'sharedBy1',
  345. 'fileSource' => 1,
  346. 'fileTarget' => 'myTarget1',
  347. ];
  348. $hookListnerExpectsPost = [
  349. 'id' => 42,
  350. 'itemType' => 'file',
  351. 'itemSource' => 1,
  352. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  353. 'shareWith' => 'sharedWith1',
  354. 'itemparent' => null,
  355. 'uidOwner' => 'sharedBy1',
  356. 'fileSource' => 1,
  357. 'fileTarget' => 'myTarget1',
  358. 'deletedShares' => [
  359. [
  360. 'id' => 44,
  361. 'itemType' => 'file',
  362. 'itemSource' => 1,
  363. 'shareType' => \OCP\Share::SHARE_TYPE_LINK,
  364. 'shareWith' => '',
  365. 'itemparent' => 43,
  366. 'uidOwner' => 'sharedBy3',
  367. 'fileSource' => 1,
  368. 'fileTarget' => 'myTarget3',
  369. ],
  370. [
  371. 'id' => 43,
  372. 'itemType' => 'file',
  373. 'itemSource' => 1,
  374. 'shareType' => \OCP\Share::SHARE_TYPE_GROUP,
  375. 'shareWith' => 'sharedWith2',
  376. 'itemparent' => 42,
  377. 'uidOwner' => 'sharedBy2',
  378. 'fileSource' => 1,
  379. 'fileTarget' => 'myTarget2',
  380. ],
  381. [
  382. 'id' => 42,
  383. 'itemType' => 'file',
  384. 'itemSource' => 1,
  385. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  386. 'shareWith' => 'sharedWith1',
  387. 'itemparent' => null,
  388. 'uidOwner' => 'sharedBy1',
  389. 'fileSource' => 1,
  390. 'fileTarget' => 'myTarget1',
  391. ],
  392. ],
  393. ];
  394. $hookListner
  395. ->expects($this->exactly(1))
  396. ->method('pre')
  397. ->with($hookListnerExpectsPre);
  398. $hookListner
  399. ->expects($this->exactly(1))
  400. ->method('post')
  401. ->with($hookListnerExpectsPost);
  402. $manager->deleteShare($share1);
  403. }
  404. public function testDeleteChildren() {
  405. $manager = $this->createManagerMock()
  406. ->setMethods(['deleteShare'])
  407. ->getMock();
  408. $share = $this->createMock(IShare::class);
  409. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  410. $child1 = $this->createMock(IShare::class);
  411. $child1->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  412. $child2 = $this->createMock(IShare::class);
  413. $child2->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  414. $child3 = $this->createMock(IShare::class);
  415. $child3->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  416. $shares = [
  417. $child1,
  418. $child2,
  419. $child3,
  420. ];
  421. $this->defaultProvider
  422. ->expects($this->exactly(4))
  423. ->method('getChildren')
  424. ->will($this->returnCallback(function($_share) use ($share, $shares) {
  425. if ($_share === $share) {
  426. return $shares;
  427. }
  428. return [];
  429. }));
  430. $this->defaultProvider
  431. ->expects($this->exactly(3))
  432. ->method('delete')
  433. ->withConsecutive($child1, $child2, $child3);
  434. $result = self::invokePrivate($manager, 'deleteChildren', [$share]);
  435. $this->assertSame($shares, $result);
  436. }
  437. public function testGetShareById() {
  438. $share = $this->createMock(IShare::class);
  439. $this->defaultProvider
  440. ->expects($this->once())
  441. ->method('getShareById')
  442. ->with(42)
  443. ->willReturn($share);
  444. $this->assertEquals($share, $this->manager->getShareById('default:42'));
  445. }
  446. /**
  447. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  448. */
  449. public function testGetExpiredShareById() {
  450. $manager = $this->createManagerMock()
  451. ->setMethods(['deleteShare'])
  452. ->getMock();
  453. $date = new \DateTime();
  454. $date->setTime(0,0,0);
  455. $share = $this->manager->newShare();
  456. $share->setExpirationDate($date)
  457. ->setShareType(\OCP\Share::SHARE_TYPE_LINK);
  458. $this->defaultProvider->expects($this->once())
  459. ->method('getShareById')
  460. ->with('42')
  461. ->willReturn($share);
  462. $manager->expects($this->once())
  463. ->method('deleteShare')
  464. ->with($share);
  465. $manager->getShareById('default:42');
  466. }
  467. /**
  468. * @expectedException \InvalidArgumentException
  469. * @expectedExceptionMessage Passwords are enforced for link shares
  470. */
  471. public function testVerifyPasswordNullButEnforced() {
  472. $this->config->method('getAppValue')->will($this->returnValueMap([
  473. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  474. ]));
  475. self::invokePrivate($this->manager, 'verifyPassword', [null]);
  476. }
  477. public function testVerifyPasswordNull() {
  478. $this->config->method('getAppValue')->will($this->returnValueMap([
  479. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  480. ]));
  481. $result = self::invokePrivate($this->manager, 'verifyPassword', [null]);
  482. $this->assertNull($result);
  483. }
  484. public function testVerifyPasswordHook() {
  485. $this->config->method('getAppValue')->will($this->returnValueMap([
  486. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  487. ]));
  488. $this->eventDispatcher->expects($this->once())->method('dispatch')
  489. ->willReturnCallback(function($eventName, GenericEvent $event) {
  490. $this->assertSame('OCP\PasswordPolicy::validate', $eventName);
  491. $this->assertSame('password', $event->getSubject());
  492. }
  493. );
  494. $result = self::invokePrivate($this->manager, 'verifyPassword', ['password']);
  495. $this->assertNull($result);
  496. }
  497. /**
  498. * @expectedException \Exception
  499. * @expectedExceptionMessage password not accepted
  500. */
  501. public function testVerifyPasswordHookFails() {
  502. $this->config->method('getAppValue')->will($this->returnValueMap([
  503. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  504. ]));
  505. $this->eventDispatcher->expects($this->once())->method('dispatch')
  506. ->willReturnCallback(function($eventName, GenericEvent $event) {
  507. $this->assertSame('OCP\PasswordPolicy::validate', $eventName);
  508. $this->assertSame('password', $event->getSubject());
  509. throw new HintException('message', 'password not accepted');
  510. }
  511. );
  512. self::invokePrivate($this->manager, 'verifyPassword', ['password']);
  513. }
  514. public function createShare($id, $type, $path, $sharedWith, $sharedBy, $shareOwner,
  515. $permissions, $expireDate = null, $password = null) {
  516. $share = $this->createMock(IShare::class);
  517. $share->method('getShareType')->willReturn($type);
  518. $share->method('getSharedWith')->willReturn($sharedWith);
  519. $share->method('getSharedBy')->willReturn($sharedBy);
  520. $share->method('getShareOwner')->willReturn($shareOwner);
  521. $share->method('getNode')->willReturn($path);
  522. $share->method('getPermissions')->willReturn($permissions);
  523. $share->method('getExpirationDate')->willReturn($expireDate);
  524. $share->method('getPassword')->willReturn($password);
  525. return $share;
  526. }
  527. public function dataGeneralChecks() {
  528. $user0 = 'user0';
  529. $user2 = 'user1';
  530. $group0 = 'group0';
  531. $file = $this->createMock(File::class);
  532. $node = $this->createMock(Node::class);
  533. $data = [
  534. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  535. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  536. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  537. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  538. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  539. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  540. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  541. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  542. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  543. [$this->createShare(null, -1, $file, null, $user0, $user0, 31, null, null), 'unkown share type', true],
  544. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $user2, null, $user0, 31, null, null), 'SharedBy should be set', true],
  545. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, $group0, null, $user0, 31, null, null), 'SharedBy should be set', true],
  546. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, null, null, $user0, 31, null, null), 'SharedBy should be set', true],
  547. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $user0, $user0, $user0, 31, null, null), 'Can\'t share with yourself', true],
  548. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, null, $user2, $user0, $user0, 31, null, null), 'Path should be set', true],
  549. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, null, $group0, $user0, $user0, 31, null, null), 'Path should be set', true],
  550. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, null, null, $user0, $user0, 31, null, null), 'Path should be set', true],
  551. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $node, $user2, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
  552. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $node, $group0, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
  553. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $node, null, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
  554. ];
  555. $nonShareAble = $this->createMock(Folder::class);
  556. $nonShareAble->method('isShareable')->willReturn(false);
  557. $nonShareAble->method('getPath')->willReturn('path');
  558. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $nonShareAble, $user2, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  559. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $nonShareAble, $group0, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  560. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $nonShareAble, null, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  561. $limitedPermssions = $this->createMock(File::class);
  562. $limitedPermssions->method('isShareable')->willReturn(true);
  563. $limitedPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  564. $limitedPermssions->method('getPath')->willReturn('path');
  565. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $limitedPermssions, $user2, $user0, $user0, null, null, null), 'A share requires permissions', true];
  566. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, null, null, null), 'A share requires permissions', true];
  567. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $limitedPermssions, null, $user0, $user0, null, null, null), 'A share requires permissions', true];
  568. $mount = $this->createMock(MoveableMount::class);
  569. $limitedPermssions->method('getMountPoint')->willReturn($mount);
  570. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $limitedPermssions, $user2, $user0, $user0, 31, null, null), 'Cannot increase permissions of path', true];
  571. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, 17, null, null), 'Cannot increase permissions of path', true];
  572. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $limitedPermssions, null, $user0, $user0, 3, null, null), 'Cannot increase permissions of path', true];
  573. $nonMoveableMountPermssions = $this->createMock(Folder::class);
  574. $nonMoveableMountPermssions->method('isShareable')->willReturn(true);
  575. $nonMoveableMountPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  576. $nonMoveableMountPermssions->method('getPath')->willReturn('path');
  577. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $nonMoveableMountPermssions, $user2, $user0, $user0, 11, null, null), 'Cannot increase permissions of path', false];
  578. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $nonMoveableMountPermssions, $group0, $user0, $user0, 11, null, null), 'Cannot increase permissions of path', false];
  579. $rootFolder = $this->createMock(Folder::class);
  580. $rootFolder->method('isShareable')->willReturn(true);
  581. $rootFolder->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  582. $rootFolder->method('getPath')->willReturn('myrootfolder');
  583. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $rootFolder, $user2, $user0, $user0, 30, null, null), 'You can\'t share your root folder', true];
  584. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $rootFolder, $group0, $user0, $user0, 2, null, null), 'You can\'t share your root folder', true];
  585. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $rootFolder, null, $user0, $user0, 16, null, null), 'You can\'t share your root folder', true];
  586. $allPermssions = $this->createMock(Folder::class);
  587. $allPermssions->method('isShareable')->willReturn(true);
  588. $allPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  589. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $allPermssions, $user2, $user0, $user0, 30, null, null), 'Shares need at least read permissions', true];
  590. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 2, null, null), 'Shares need at least read permissions', true];
  591. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $allPermssions, $user2, $user0, $user0, 31, null, null), null, false];
  592. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 3, null, null), null, false];
  593. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $allPermssions, null, $user0, $user0, 17, null, null), null, false];
  594. return $data;
  595. }
  596. /**
  597. * @dataProvider dataGeneralChecks
  598. *
  599. * @param $share
  600. * @param $exceptionMessage
  601. * @param $exception
  602. */
  603. public function testGeneralChecks($share, $exceptionMessage, $exception) {
  604. $thrown = null;
  605. $this->userManager->method('userExists')->will($this->returnValueMap([
  606. ['user0', true],
  607. ['user1', true],
  608. ]));
  609. $this->groupManager->method('groupExists')->will($this->returnValueMap([
  610. ['group0', true],
  611. ]));
  612. $userFolder = $this->createMock(Folder::class);
  613. $userFolder->method('getPath')->willReturn('myrootfolder');
  614. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  615. try {
  616. self::invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  617. $thrown = false;
  618. } catch (\OCP\Share\Exceptions\GenericShareException $e) {
  619. $this->assertEquals($exceptionMessage, $e->getHint());
  620. $thrown = true;
  621. } catch(\InvalidArgumentException $e) {
  622. $this->assertEquals($exceptionMessage, $e->getMessage());
  623. $thrown = true;
  624. }
  625. $this->assertSame($exception, $thrown);
  626. }
  627. /**
  628. * @expectedException \InvalidArgumentException
  629. * @expectedExceptionMessage You can't share your root folder
  630. */
  631. public function testGeneralCheckShareRoot() {
  632. $thrown = null;
  633. $this->userManager->method('userExists')->will($this->returnValueMap([
  634. ['user0', true],
  635. ['user1', true],
  636. ]));
  637. $userFolder = $this->createMock(Folder::class);
  638. $userFolder->method('isSubNode')->with($userFolder)->willReturn(false);
  639. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  640. $share = $this->manager->newShare();
  641. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  642. ->setSharedWith('user0')
  643. ->setSharedBy('user1')
  644. ->setNode($userFolder);
  645. self::invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  646. }
  647. /**
  648. * @expectedException \OCP\Share\Exceptions\GenericShareException
  649. * @expectedExceptionMessage Expiration date is in the past
  650. */
  651. public function testvalidateExpirationDateInPast() {
  652. // Expire date in the past
  653. $past = new \DateTime();
  654. $past->sub(new \DateInterval('P1D'));
  655. $share = $this->manager->newShare();
  656. $share->setExpirationDate($past);
  657. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  658. }
  659. /**
  660. * @expectedException \InvalidArgumentException
  661. * @expectedExceptionMessage Expiration date is enforced
  662. */
  663. public function testvalidateExpirationDateEnforceButNotSet() {
  664. $share = $this->manager->newShare();
  665. $share->setProviderId('foo')->setId('bar');
  666. $this->config->method('getAppValue')
  667. ->will($this->returnValueMap([
  668. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  669. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  670. ]));
  671. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  672. }
  673. public function testvalidateExpirationDateEnforceButNotEnabledAndNotSet() {
  674. $share = $this->manager->newShare();
  675. $share->setProviderId('foo')->setId('bar');
  676. $this->config->method('getAppValue')
  677. ->will($this->returnValueMap([
  678. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  679. ]));
  680. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  681. $this->assertNull($share->getExpirationDate());
  682. }
  683. public function testvalidateExpirationDateEnforceButNotSetNewShare() {
  684. $share = $this->manager->newShare();
  685. $this->config->method('getAppValue')
  686. ->will($this->returnValueMap([
  687. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  688. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  689. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  690. ]));
  691. $expected = new \DateTime();
  692. $expected->setTime(0,0,0);
  693. $expected->add(new \DateInterval('P3D'));
  694. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  695. $this->assertNotNull($share->getExpirationDate());
  696. $this->assertEquals($expected, $share->getExpirationDate());
  697. }
  698. public function testvalidateExpirationDateEnforceToFarIntoFuture() {
  699. // Expire date in the past
  700. $future = new \DateTime();
  701. $future->add(new \DateInterval('P7D'));
  702. $share = $this->manager->newShare();
  703. $share->setExpirationDate($future);
  704. $this->config->method('getAppValue')
  705. ->will($this->returnValueMap([
  706. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  707. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  708. ]));
  709. try {
  710. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  711. } catch (\OCP\Share\Exceptions\GenericShareException $e) {
  712. $this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getMessage());
  713. $this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getHint());
  714. $this->assertEquals(404, $e->getCode());
  715. }
  716. }
  717. public function testvalidateExpirationDateEnforceValid() {
  718. // Expire date in the past
  719. $future = new \DateTime();
  720. $future->add(new \DateInterval('P2D'));
  721. $future->setTime(0,0,0);
  722. $expected = clone $future;
  723. $future->setTime(1,2,3);
  724. $share = $this->manager->newShare();
  725. $share->setExpirationDate($future);
  726. $this->config->method('getAppValue')
  727. ->will($this->returnValueMap([
  728. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  729. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  730. ]));
  731. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  732. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  733. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($future) {
  734. return $data['expirationDate'] == $future;
  735. }));
  736. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  737. $this->assertEquals($expected, $share->getExpirationDate());
  738. }
  739. public function testvalidateExpirationDateNoDateNoDefaultNull() {
  740. $date = new \DateTime();
  741. $date->add(new \DateInterval('P5D'));
  742. $expected = clone $date;
  743. $expected->setTime(0,0,0);
  744. $share = $this->manager->newShare();
  745. $share->setExpirationDate($date);
  746. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  747. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  748. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  749. return $data['expirationDate'] == $expected && $data['passwordSet'] === false;
  750. }));
  751. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  752. $this->assertEquals($expected, $share->getExpirationDate());
  753. }
  754. public function testvalidateExpirationDateNoDateNoDefault() {
  755. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  756. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  757. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) {
  758. return $data['expirationDate'] === null && $data['passwordSet'] === true;
  759. }));
  760. $share = $this->manager->newShare();
  761. $share->setPassword('password');
  762. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  763. $this->assertNull($share->getExpirationDate());
  764. }
  765. public function testvalidateExpirationDateNoDateDefault() {
  766. $future = new \DateTime();
  767. $future->add(new \DateInterval('P3D'));
  768. $future->setTime(0,0,0);
  769. $expected = clone $future;
  770. $share = $this->manager->newShare();
  771. $share->setExpirationDate($future);
  772. $this->config->method('getAppValue')
  773. ->will($this->returnValueMap([
  774. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  775. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  776. ]));
  777. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  778. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  779. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  780. return $data['expirationDate'] == $expected;
  781. }));
  782. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  783. $this->assertEquals($expected, $share->getExpirationDate());
  784. }
  785. public function testValidateExpirationDateHookModification() {
  786. $nextWeek = new \DateTime();
  787. $nextWeek->add(new \DateInterval('P7D'));
  788. $nextWeek->setTime(0,0,0);
  789. $save = clone $nextWeek;
  790. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  791. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  792. $hookListner->expects($this->once())->method('listener')->will($this->returnCallback(function ($data) {
  793. $data['expirationDate']->sub(new \DateInterval('P2D'));
  794. }));
  795. $share = $this->manager->newShare();
  796. $share->setExpirationDate($nextWeek);
  797. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  798. $save->sub(new \DateInterval('P2D'));
  799. $this->assertEquals($save, $share->getExpirationDate());
  800. }
  801. /**
  802. * @expectedException \Exception
  803. * @expectedExceptionMessage Invalid date!
  804. */
  805. public function testValidateExpirationDateHookException() {
  806. $nextWeek = new \DateTime();
  807. $nextWeek->add(new \DateInterval('P7D'));
  808. $nextWeek->setTime(0,0,0);
  809. $share = $this->manager->newShare();
  810. $share->setExpirationDate($nextWeek);
  811. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  812. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  813. $hookListner->expects($this->once())->method('listener')->will($this->returnCallback(function ($data) {
  814. $data['accepted'] = false;
  815. $data['message'] = 'Invalid date!';
  816. }));
  817. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  818. }
  819. public function testValidateExpirationDateExistingShareNoDefault() {
  820. $share = $this->manager->newShare();
  821. $share->setId('42')->setProviderId('foo');
  822. $this->config->method('getAppValue')
  823. ->will($this->returnValueMap([
  824. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  825. ['core', 'shareapi_expire_after_n_days', '7', '6'],
  826. ]));
  827. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  828. $this->assertEquals(null, $share->getExpirationDate());
  829. }
  830. /**
  831. * @expectedException Exception
  832. * @expectedExceptionMessage Only sharing with group members is allowed
  833. */
  834. public function testUserCreateChecksShareWithGroupMembersOnlyDifferentGroups() {
  835. $share = $this->manager->newShare();
  836. $sharedBy = $this->createMock(IUser::class);
  837. $sharedWith = $this->createMock(IUser::class);
  838. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  839. $this->groupManager
  840. ->method('getUserGroupIds')
  841. ->will(
  842. $this->returnValueMap([
  843. [$sharedBy, ['group1']],
  844. [$sharedWith, ['group2']],
  845. ])
  846. );
  847. $this->userManager->method('get')->will($this->returnValueMap([
  848. ['sharedBy', $sharedBy],
  849. ['sharedWith', $sharedWith],
  850. ]));
  851. $this->config
  852. ->method('getAppValue')
  853. ->will($this->returnValueMap([
  854. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  855. ]));
  856. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  857. }
  858. public function testUserCreateChecksShareWithGroupMembersOnlySharedGroup() {
  859. $share = $this->manager->newShare();
  860. $sharedBy = $this->createMock(IUser::class);
  861. $sharedWith = $this->createMock(IUser::class);
  862. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  863. $path = $this->createMock(Node::class);
  864. $share->setNode($path);
  865. $this->groupManager
  866. ->method('getUserGroupIds')
  867. ->will(
  868. $this->returnValueMap([
  869. [$sharedBy, ['group1', 'group3']],
  870. [$sharedWith, ['group2', 'group3']],
  871. ])
  872. );
  873. $this->userManager->method('get')->will($this->returnValueMap([
  874. ['sharedBy', $sharedBy],
  875. ['sharedWith', $sharedWith],
  876. ]));
  877. $this->config
  878. ->method('getAppValue')
  879. ->will($this->returnValueMap([
  880. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  881. ]));
  882. $this->defaultProvider
  883. ->method('getSharesByPath')
  884. ->with($path)
  885. ->willReturn([]);
  886. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  887. }
  888. /**
  889. * @expectedException Exception
  890. * @expectedExceptionMessage Path already shared with this user
  891. */
  892. public function testUserCreateChecksIdenticalShareExists() {
  893. $share = $this->manager->newShare();
  894. $share2 = $this->manager->newShare();
  895. $sharedWith = $this->createMock(IUser::class);
  896. $path = $this->createMock(Node::class);
  897. $share->setSharedWith('sharedWith')->setNode($path)
  898. ->setProviderId('foo')->setId('bar');
  899. $share2->setSharedWith('sharedWith')->setNode($path)
  900. ->setProviderId('foo')->setId('baz');
  901. $this->defaultProvider
  902. ->method('getSharesByPath')
  903. ->with($path)
  904. ->willReturn([$share2]);
  905. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  906. }
  907. /**
  908. * @expectedException Exception
  909. * @expectedExceptionMessage Path already shared with this user
  910. */
  911. public function testUserCreateChecksIdenticalPathSharedViaGroup() {
  912. $share = $this->manager->newShare();
  913. $sharedWith = $this->createMock(IUser::class);
  914. $sharedWith->method('getUID')->willReturn('sharedWith');
  915. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  916. $path = $this->createMock(Node::class);
  917. $share->setSharedWith('sharedWith')
  918. ->setNode($path)
  919. ->setShareOwner('shareOwner')
  920. ->setProviderId('foo')
  921. ->setId('bar');
  922. $share2 = $this->manager->newShare();
  923. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  924. ->setShareOwner('shareOwner2')
  925. ->setProviderId('foo')
  926. ->setId('baz')
  927. ->setSharedWith('group');
  928. $group = $this->createMock(IGroup::class);
  929. $group->method('inGroup')
  930. ->with($sharedWith)
  931. ->willReturn(true);
  932. $this->groupManager->method('get')->with('group')->willReturn($group);
  933. $this->defaultProvider
  934. ->method('getSharesByPath')
  935. ->with($path)
  936. ->willReturn([$share2]);
  937. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  938. }
  939. public function testUserCreateChecksIdenticalPathNotSharedWithUser() {
  940. $share = $this->manager->newShare();
  941. $sharedWith = $this->createMock(IUser::class);
  942. $path = $this->createMock(Node::class);
  943. $share->setSharedWith('sharedWith')
  944. ->setNode($path)
  945. ->setShareOwner('shareOwner')
  946. ->setProviderId('foo')
  947. ->setId('bar');
  948. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  949. $share2 = $this->manager->newShare();
  950. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  951. ->setShareOwner('shareOwner2')
  952. ->setProviderId('foo')
  953. ->setId('baz');
  954. $group = $this->createMock(IGroup::class);
  955. $group->method('inGroup')
  956. ->with($sharedWith)
  957. ->willReturn(false);
  958. $this->groupManager->method('get')->with('group')->willReturn($group);
  959. $share2->setSharedWith('group');
  960. $this->defaultProvider
  961. ->method('getSharesByPath')
  962. ->with($path)
  963. ->willReturn([$share2]);
  964. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  965. }
  966. /**
  967. * @expectedException Exception
  968. * @expectedExceptionMessage Group sharing is now allowed
  969. */
  970. public function testGroupCreateChecksShareWithGroupMembersGroupSharingNotAllowed() {
  971. $share = $this->manager->newShare();
  972. $this->config
  973. ->method('getAppValue')
  974. ->will($this->returnValueMap([
  975. ['core', 'shareapi_allow_group_sharing', 'yes', 'no'],
  976. ]));
  977. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  978. }
  979. /**
  980. * @expectedException Exception
  981. * @expectedExceptionMessage Only sharing within your own groups is allowed
  982. */
  983. public function testGroupCreateChecksShareWithGroupMembersOnlyNotInGroup() {
  984. $share = $this->manager->newShare();
  985. $user = $this->createMock(IUser::class);
  986. $group = $this->createMock(IGroup::class);
  987. $share->setSharedBy('user')->setSharedWith('group');
  988. $group->method('inGroup')->with($user)->willReturn(false);
  989. $this->groupManager->method('get')->with('group')->willReturn($group);
  990. $this->userManager->method('get')->with('user')->willReturn($user);
  991. $this->config
  992. ->method('getAppValue')
  993. ->will($this->returnValueMap([
  994. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  995. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  996. ]));
  997. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  998. }
  999. public function testGroupCreateChecksShareWithGroupMembersOnlyInGroup() {
  1000. $share = $this->manager->newShare();
  1001. $user = $this->createMock(IUser::class);
  1002. $group = $this->createMock(IGroup::class);
  1003. $share->setSharedBy('user')->setSharedWith('group');
  1004. $this->userManager->method('get')->with('user')->willReturn($user);
  1005. $this->groupManager->method('get')->with('group')->willReturn($group);
  1006. $group->method('inGroup')->with($user)->willReturn(true);
  1007. $path = $this->createMock(Node::class);
  1008. $share->setNode($path);
  1009. $this->defaultProvider->method('getSharesByPath')
  1010. ->with($path)
  1011. ->willReturn([]);
  1012. $this->config
  1013. ->method('getAppValue')
  1014. ->will($this->returnValueMap([
  1015. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1016. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1017. ]));
  1018. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1019. }
  1020. /**
  1021. * @expectedException Exception
  1022. * @expectedExceptionMessage Path already shared with this group
  1023. */
  1024. public function testGroupCreateChecksPathAlreadySharedWithSameGroup() {
  1025. $share = $this->manager->newShare();
  1026. $path = $this->createMock(Node::class);
  1027. $share->setSharedWith('sharedWith')
  1028. ->setNode($path)
  1029. ->setProviderId('foo')
  1030. ->setId('bar');
  1031. $share2 = $this->manager->newShare();
  1032. $share2->setSharedWith('sharedWith')
  1033. ->setProviderId('foo')
  1034. ->setId('baz');
  1035. $this->defaultProvider->method('getSharesByPath')
  1036. ->with($path)
  1037. ->willReturn([$share2]);
  1038. $this->config
  1039. ->method('getAppValue')
  1040. ->will($this->returnValueMap([
  1041. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1042. ]));
  1043. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1044. }
  1045. public function testGroupCreateChecksPathAlreadySharedWithDifferentGroup() {
  1046. $share = $this->manager->newShare();
  1047. $share->setSharedWith('sharedWith');
  1048. $path = $this->createMock(Node::class);
  1049. $share->setNode($path);
  1050. $share2 = $this->manager->newShare();
  1051. $share2->setSharedWith('sharedWith2');
  1052. $this->defaultProvider->method('getSharesByPath')
  1053. ->with($path)
  1054. ->willReturn([$share2]);
  1055. $this->config
  1056. ->method('getAppValue')
  1057. ->will($this->returnValueMap([
  1058. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1059. ]));
  1060. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1061. }
  1062. /**
  1063. * @expectedException Exception
  1064. * @expectedExceptionMessage Link sharing not allowed
  1065. */
  1066. public function testLinkCreateChecksNoLinkSharesAllowed() {
  1067. $share = $this->manager->newShare();
  1068. $this->config
  1069. ->method('getAppValue')
  1070. ->will($this->returnValueMap([
  1071. ['core', 'shareapi_allow_links', 'yes', 'no'],
  1072. ]));
  1073. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1074. }
  1075. /**
  1076. * @expectedException Exception
  1077. * @expectedExceptionMessage Link shares can't have reshare permissions
  1078. */
  1079. public function testLinkCreateChecksSharePermissions() {
  1080. $share = $this->manager->newShare();
  1081. $share->setPermissions(\OCP\Constants::PERMISSION_SHARE);
  1082. $this->config
  1083. ->method('getAppValue')
  1084. ->will($this->returnValueMap([
  1085. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1086. ]));
  1087. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1088. }
  1089. /**
  1090. * @expectedException Exception
  1091. * @expectedExceptionMessage Public upload not allowed
  1092. */
  1093. public function testLinkCreateChecksNoPublicUpload() {
  1094. $share = $this->manager->newShare();
  1095. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1096. $this->config
  1097. ->method('getAppValue')
  1098. ->will($this->returnValueMap([
  1099. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1100. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1101. ]));
  1102. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1103. }
  1104. public function testLinkCreateChecksPublicUpload() {
  1105. $share = $this->manager->newShare();
  1106. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1107. $this->config
  1108. ->method('getAppValue')
  1109. ->will($this->returnValueMap([
  1110. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1111. ['core', 'shareapi_allow_public_upload', 'yes', 'yes']
  1112. ]));
  1113. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1114. }
  1115. public function testLinkCreateChecksReadOnly() {
  1116. $share = $this->manager->newShare();
  1117. $share->setPermissions(\OCP\Constants::PERMISSION_READ);
  1118. $this->config
  1119. ->method('getAppValue')
  1120. ->will($this->returnValueMap([
  1121. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1122. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1123. ]));
  1124. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1125. }
  1126. /**
  1127. * @expectedException \InvalidArgumentException
  1128. * @expectedExceptionMessage Path contains files shared with you
  1129. */
  1130. public function testPathCreateChecksContainsSharedMount() {
  1131. $path = $this->createMock(Folder::class);
  1132. $path->method('getPath')->willReturn('path');
  1133. $mount = $this->createMock(IMountPoint::class);
  1134. $storage = $this->createMock(Storage::class);
  1135. $mount->method('getStorage')->willReturn($storage);
  1136. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(true);
  1137. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1138. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1139. }
  1140. public function testPathCreateChecksContainsNoSharedMount() {
  1141. $path = $this->createMock(Folder::class);
  1142. $path->method('getPath')->willReturn('path');
  1143. $mount = $this->createMock(IMountPoint::class);
  1144. $storage = $this->createMock(Storage::class);
  1145. $mount->method('getStorage')->willReturn($storage);
  1146. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(false);
  1147. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1148. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1149. }
  1150. public function testPathCreateChecksContainsNoFolder() {
  1151. $path = $this->createMock(File::class);
  1152. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1153. }
  1154. public function dataIsSharingDisabledForUser() {
  1155. $data = [];
  1156. // No exclude groups
  1157. $data[] = ['no', null, null, null, false];
  1158. // empty exclude list, user no groups
  1159. $data[] = ['yes', '', json_encode(['']), [], false];
  1160. // empty exclude list, user groups
  1161. $data[] = ['yes', '', json_encode(['']), ['group1', 'group2'], false];
  1162. // Convert old list to json
  1163. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), [], false];
  1164. // Old list partly groups in common
  1165. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1', 'group3'], false];
  1166. // Old list only groups in common
  1167. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1'], true];
  1168. // New list partly in common
  1169. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group1', 'group3'], false];
  1170. // New list only groups in common
  1171. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group2'], true];
  1172. return $data;
  1173. }
  1174. /**
  1175. * @dataProvider dataIsSharingDisabledForUser
  1176. *
  1177. * @param string $excludeGroups
  1178. * @param string $groupList
  1179. * @param string $setList
  1180. * @param string[] $groupIds
  1181. * @param bool $expected
  1182. */
  1183. public function testIsSharingDisabledForUser($excludeGroups, $groupList, $setList, $groupIds, $expected) {
  1184. $user = $this->createMock(IUser::class);
  1185. $this->config->method('getAppValue')
  1186. ->will($this->returnValueMap([
  1187. ['core', 'shareapi_exclude_groups', 'no', $excludeGroups],
  1188. ['core', 'shareapi_exclude_groups_list', '', $groupList],
  1189. ]));
  1190. if ($setList !== null) {
  1191. $this->config->expects($this->once())
  1192. ->method('setAppValue')
  1193. ->with('core', 'shareapi_exclude_groups_list', $setList);
  1194. } else {
  1195. $this->config->expects($this->never())
  1196. ->method('setAppValue');
  1197. }
  1198. $this->groupManager->method('getUserGroupIds')
  1199. ->with($user)
  1200. ->willReturn($groupIds);
  1201. $this->userManager->method('get')->with('user')->willReturn($user);
  1202. $res = $this->manager->sharingDisabledForUser('user');
  1203. $this->assertEquals($expected, $res);
  1204. }
  1205. public function dataCanShare() {
  1206. $data = [];
  1207. /*
  1208. * [expected, sharing enabled, disabled for user]
  1209. */
  1210. $data[] = [false, 'no', false];
  1211. $data[] = [false, 'no', true];
  1212. $data[] = [true, 'yes', false];
  1213. $data[] = [false, 'yes', true];
  1214. return $data;
  1215. }
  1216. /**
  1217. * @dataProvider dataCanShare
  1218. *
  1219. * @param bool $expected
  1220. * @param string $sharingEnabled
  1221. * @param bool $disabledForUser
  1222. */
  1223. public function testCanShare($expected, $sharingEnabled, $disabledForUser) {
  1224. $this->config->method('getAppValue')
  1225. ->will($this->returnValueMap([
  1226. ['core', 'shareapi_enabled', 'yes', $sharingEnabled],
  1227. ]));
  1228. $manager = $this->createManagerMock()
  1229. ->setMethods(['sharingDisabledForUser'])
  1230. ->getMock();
  1231. $manager->method('sharingDisabledForUser')
  1232. ->with('user')
  1233. ->willReturn($disabledForUser);
  1234. $share = $this->manager->newShare();
  1235. $share->setSharedBy('user');
  1236. $exception = false;
  1237. try {
  1238. $res = self::invokePrivate($manager, 'canShare', [$share]);
  1239. } catch (\Exception $e) {
  1240. $exception = true;
  1241. }
  1242. $this->assertEquals($expected, !$exception);
  1243. }
  1244. public function testCreateShareUser() {
  1245. $manager = $this->createManagerMock()
  1246. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  1247. ->getMock();
  1248. $shareOwner = $this->createMock(IUser::class);
  1249. $shareOwner->method('getUID')->willReturn('shareOwner');
  1250. $storage = $this->createMock(Storage::class);
  1251. $path = $this->createMock(File::class);
  1252. $path->method('getOwner')->willReturn($shareOwner);
  1253. $path->method('getName')->willReturn('target');
  1254. $path->method('getStorage')->willReturn($storage);
  1255. $share = $this->createShare(
  1256. null,
  1257. \OCP\Share::SHARE_TYPE_USER,
  1258. $path,
  1259. 'sharedWith',
  1260. 'sharedBy',
  1261. null,
  1262. \OCP\Constants::PERMISSION_ALL);
  1263. $manager->expects($this->once())
  1264. ->method('canShare')
  1265. ->with($share)
  1266. ->willReturn(true);
  1267. $manager->expects($this->once())
  1268. ->method('generalCreateChecks')
  1269. ->with($share);;
  1270. $manager->expects($this->once())
  1271. ->method('userCreateChecks')
  1272. ->with($share);;
  1273. $manager->expects($this->once())
  1274. ->method('pathCreateChecks')
  1275. ->with($path);
  1276. $this->defaultProvider
  1277. ->expects($this->once())
  1278. ->method('create')
  1279. ->with($share)
  1280. ->will($this->returnArgument(0));
  1281. $share->expects($this->once())
  1282. ->method('setShareOwner')
  1283. ->with('shareOwner');
  1284. $share->expects($this->once())
  1285. ->method('setTarget')
  1286. ->with('/target');
  1287. $manager->createShare($share);
  1288. }
  1289. public function testCreateShareGroup() {
  1290. $manager = $this->createManagerMock()
  1291. ->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks'])
  1292. ->getMock();
  1293. $shareOwner = $this->createMock(IUser::class);
  1294. $shareOwner->method('getUID')->willReturn('shareOwner');
  1295. $storage = $this->createMock(Storage::class);
  1296. $path = $this->createMock(File::class);
  1297. $path->method('getOwner')->willReturn($shareOwner);
  1298. $path->method('getName')->willReturn('target');
  1299. $path->method('getStorage')->willReturn($storage);
  1300. $share = $this->createShare(
  1301. null,
  1302. \OCP\Share::SHARE_TYPE_GROUP,
  1303. $path,
  1304. 'sharedWith',
  1305. 'sharedBy',
  1306. null,
  1307. \OCP\Constants::PERMISSION_ALL);
  1308. $manager->expects($this->once())
  1309. ->method('canShare')
  1310. ->with($share)
  1311. ->willReturn(true);
  1312. $manager->expects($this->once())
  1313. ->method('generalCreateChecks')
  1314. ->with($share);;
  1315. $manager->expects($this->once())
  1316. ->method('groupCreateChecks')
  1317. ->with($share);;
  1318. $manager->expects($this->once())
  1319. ->method('pathCreateChecks')
  1320. ->with($path);
  1321. $this->defaultProvider
  1322. ->expects($this->once())
  1323. ->method('create')
  1324. ->with($share)
  1325. ->will($this->returnArgument(0));
  1326. $share->expects($this->once())
  1327. ->method('setShareOwner')
  1328. ->with('shareOwner');
  1329. $share->expects($this->once())
  1330. ->method('setTarget')
  1331. ->with('/target');
  1332. $manager->createShare($share);
  1333. }
  1334. public function testCreateShareLink() {
  1335. $manager = $this->createManagerMock()
  1336. ->setMethods([
  1337. 'canShare',
  1338. 'generalCreateChecks',
  1339. 'linkCreateChecks',
  1340. 'pathCreateChecks',
  1341. 'validateExpirationDate',
  1342. 'verifyPassword',
  1343. 'setLinkParent',
  1344. ])
  1345. ->getMock();
  1346. $shareOwner = $this->createMock(IUser::class);
  1347. $shareOwner->method('getUID')->willReturn('shareOwner');
  1348. $storage = $this->createMock(Storage::class);
  1349. $path = $this->createMock(File::class);
  1350. $path->method('getOwner')->willReturn($shareOwner);
  1351. $path->method('getName')->willReturn('target');
  1352. $path->method('getId')->willReturn(1);
  1353. $path->method('getStorage')->willReturn($storage);
  1354. $date = new \DateTime();
  1355. $share = $this->manager->newShare();
  1356. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1357. ->setNode($path)
  1358. ->setSharedBy('sharedBy')
  1359. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1360. ->setExpirationDate($date)
  1361. ->setPassword('password');
  1362. $manager->expects($this->once())
  1363. ->method('canShare')
  1364. ->with($share)
  1365. ->willReturn(true);
  1366. $manager->expects($this->once())
  1367. ->method('generalCreateChecks')
  1368. ->with($share);;
  1369. $manager->expects($this->once())
  1370. ->method('linkCreateChecks')
  1371. ->with($share);;
  1372. $manager->expects($this->once())
  1373. ->method('pathCreateChecks')
  1374. ->with($path);
  1375. $manager->expects($this->once())
  1376. ->method('validateExpirationDate')
  1377. ->with($share);
  1378. $manager->expects($this->once())
  1379. ->method('verifyPassword')
  1380. ->with('password');
  1381. $manager->expects($this->once())
  1382. ->method('setLinkParent')
  1383. ->with($share);
  1384. $this->hasher->expects($this->once())
  1385. ->method('hash')
  1386. ->with('password')
  1387. ->willReturn('hashed');
  1388. $this->secureRandom->method('getMediumStrengthGenerator')
  1389. ->will($this->returnSelf());
  1390. $this->secureRandom->method('generate')
  1391. ->willReturn('token');
  1392. $this->defaultProvider
  1393. ->expects($this->once())
  1394. ->method('create')
  1395. ->with($share)
  1396. ->will($this->returnCallback(function(Share $share) {
  1397. return $share->setId(42);
  1398. }));
  1399. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock();
  1400. \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'pre');
  1401. \OCP\Util::connectHook('OCP\Share', 'post_shared', $hookListner, 'post');
  1402. $hookListnerExpectsPre = [
  1403. 'itemType' => 'file',
  1404. 'itemSource' => 1,
  1405. 'shareType' => \OCP\Share::SHARE_TYPE_LINK,
  1406. 'uidOwner' => 'sharedBy',
  1407. 'permissions' => 31,
  1408. 'fileSource' => 1,
  1409. 'expiration' => $date,
  1410. 'token' => 'token',
  1411. 'run' => true,
  1412. 'error' => '',
  1413. 'itemTarget' => '/target',
  1414. 'shareWith' => null,
  1415. ];
  1416. $hookListnerExpectsPost = [
  1417. 'itemType' => 'file',
  1418. 'itemSource' => 1,
  1419. 'shareType' => \OCP\Share::SHARE_TYPE_LINK,
  1420. 'uidOwner' => 'sharedBy',
  1421. 'permissions' => 31,
  1422. 'fileSource' => 1,
  1423. 'expiration' => $date,
  1424. 'token' => 'token',
  1425. 'id' => 42,
  1426. 'itemTarget' => '/target',
  1427. 'fileTarget' => '/target',
  1428. 'shareWith' => null,
  1429. ];
  1430. $hookListner->expects($this->once())
  1431. ->method('pre')
  1432. ->with($this->equalTo($hookListnerExpectsPre));
  1433. $hookListner->expects($this->once())
  1434. ->method('post')
  1435. ->with($this->equalTo($hookListnerExpectsPost));
  1436. /** @var IShare $share */
  1437. $share = $manager->createShare($share);
  1438. $this->assertSame('shareOwner', $share->getShareOwner());
  1439. $this->assertEquals('/target', $share->getTarget());
  1440. $this->assertSame($date, $share->getExpirationDate());
  1441. $this->assertEquals('token', $share->getToken());
  1442. $this->assertEquals('hashed', $share->getPassword());
  1443. }
  1444. public function testCreateShareMail() {
  1445. $manager = $this->createManagerMock()
  1446. ->setMethods([
  1447. 'canShare',
  1448. 'generalCreateChecks',
  1449. 'linkCreateChecks',
  1450. 'pathCreateChecks',
  1451. 'validateExpirationDate',
  1452. 'verifyPassword',
  1453. 'setLinkParent',
  1454. ])
  1455. ->getMock();
  1456. $shareOwner = $this->createMock(IUser::class);
  1457. $shareOwner->method('getUID')->willReturn('shareOwner');
  1458. $storage = $this->createMock(Storage::class);
  1459. $path = $this->createMock(File::class);
  1460. $path->method('getOwner')->willReturn($shareOwner);
  1461. $path->method('getName')->willReturn('target');
  1462. $path->method('getId')->willReturn(1);
  1463. $path->method('getStorage')->willReturn($storage);
  1464. $share = $this->manager->newShare();
  1465. $share->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  1466. ->setNode($path)
  1467. ->setSharedBy('sharedBy')
  1468. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  1469. $manager->expects($this->once())
  1470. ->method('canShare')
  1471. ->with($share)
  1472. ->willReturn(true);
  1473. $manager->expects($this->once())
  1474. ->method('generalCreateChecks')
  1475. ->with($share);;
  1476. $manager->expects($this->never())
  1477. ->method('linkCreateChecks');
  1478. $manager->expects($this->once())
  1479. ->method('pathCreateChecks')
  1480. ->with($path);
  1481. $manager->expects($this->never())
  1482. ->method('validateExpirationDate');
  1483. $manager->expects($this->never())
  1484. ->method('verifyPassword');
  1485. $manager->expects($this->never())
  1486. ->method('setLinkParent');
  1487. $this->secureRandom->method('getMediumStrengthGenerator')
  1488. ->will($this->returnSelf());
  1489. $this->secureRandom->method('generate')
  1490. ->willReturn('token');
  1491. $this->defaultProvider
  1492. ->expects($this->once())
  1493. ->method('create')
  1494. ->with($share)
  1495. ->will($this->returnCallback(function(Share $share) {
  1496. return $share->setId(42);
  1497. }));
  1498. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock();
  1499. \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'pre');
  1500. \OCP\Util::connectHook('OCP\Share', 'post_shared', $hookListner, 'post');
  1501. $hookListnerExpectsPre = [
  1502. 'itemType' => 'file',
  1503. 'itemSource' => 1,
  1504. 'shareType' => \OCP\Share::SHARE_TYPE_EMAIL,
  1505. 'uidOwner' => 'sharedBy',
  1506. 'permissions' => 31,
  1507. 'fileSource' => 1,
  1508. 'expiration' => null,
  1509. 'token' => 'token',
  1510. 'run' => true,
  1511. 'error' => '',
  1512. 'itemTarget' => '/target',
  1513. 'shareWith' => null,
  1514. ];
  1515. $hookListnerExpectsPost = [
  1516. 'itemType' => 'file',
  1517. 'itemSource' => 1,
  1518. 'shareType' => \OCP\Share::SHARE_TYPE_EMAIL,
  1519. 'uidOwner' => 'sharedBy',
  1520. 'permissions' => 31,
  1521. 'fileSource' => 1,
  1522. 'expiration' => null,
  1523. 'token' => 'token',
  1524. 'id' => 42,
  1525. 'itemTarget' => '/target',
  1526. 'fileTarget' => '/target',
  1527. 'shareWith' => null,
  1528. ];
  1529. $hookListner->expects($this->once())
  1530. ->method('pre')
  1531. ->with($this->equalTo($hookListnerExpectsPre));
  1532. $hookListner->expects($this->once())
  1533. ->method('post')
  1534. ->with($this->equalTo($hookListnerExpectsPost));
  1535. /** @var IShare $share */
  1536. $share = $manager->createShare($share);
  1537. $this->assertSame('shareOwner', $share->getShareOwner());
  1538. $this->assertEquals('/target', $share->getTarget());
  1539. $this->assertEquals('token', $share->getToken());
  1540. }
  1541. /**
  1542. * @expectedException Exception
  1543. * @expectedExceptionMessage I won't let you share
  1544. */
  1545. public function testCreateShareHookError() {
  1546. $manager = $this->createManagerMock()
  1547. ->setMethods([
  1548. 'canShare',
  1549. 'generalCreateChecks',
  1550. 'userCreateChecks',
  1551. 'pathCreateChecks',
  1552. ])
  1553. ->getMock();
  1554. $shareOwner = $this->createMock(IUser::class);
  1555. $shareOwner->method('getUID')->willReturn('shareOwner');
  1556. $storage = $this->createMock(Storage::class);
  1557. $path = $this->createMock(File::class);
  1558. $path->method('getOwner')->willReturn($shareOwner);
  1559. $path->method('getName')->willReturn('target');
  1560. $path->method('getStorage')->willReturn($storage);
  1561. $share = $this->createShare(
  1562. null,
  1563. \OCP\Share::SHARE_TYPE_USER,
  1564. $path,
  1565. 'sharedWith',
  1566. 'sharedBy',
  1567. null,
  1568. \OCP\Constants::PERMISSION_ALL);
  1569. $manager->expects($this->once())
  1570. ->method('canShare')
  1571. ->with($share)
  1572. ->willReturn(true);
  1573. $manager->expects($this->once())
  1574. ->method('generalCreateChecks')
  1575. ->with($share);;
  1576. $manager->expects($this->once())
  1577. ->method('userCreateChecks')
  1578. ->with($share);;
  1579. $manager->expects($this->once())
  1580. ->method('pathCreateChecks')
  1581. ->with($path);
  1582. $share->expects($this->once())
  1583. ->method('setShareOwner')
  1584. ->with('shareOwner');
  1585. $share->expects($this->once())
  1586. ->method('setTarget')
  1587. ->with('/target');
  1588. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre'])->getMock();
  1589. \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'pre');
  1590. $hookListner->expects($this->once())
  1591. ->method('pre')
  1592. ->will($this->returnCallback(function (array $data) {
  1593. $data['run'] = false;
  1594. $data['error'] = 'I won\'t let you share!';
  1595. }));
  1596. $manager->createShare($share);
  1597. }
  1598. public function testCreateShareOfIncommingFederatedShare() {
  1599. $manager = $this->createManagerMock()
  1600. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  1601. ->getMock();
  1602. $shareOwner = $this->createMock(IUser::class);
  1603. $shareOwner->method('getUID')->willReturn('shareOwner');
  1604. $storage = $this->createMock(Storage::class);
  1605. $storage->method('instanceOfStorage')
  1606. ->with('OCA\Files_Sharing\External\Storage')
  1607. ->willReturn(true);
  1608. $storage2 = $this->createMock(Storage::class);
  1609. $storage2->method('instanceOfStorage')
  1610. ->with('OCA\Files_Sharing\External\Storage')
  1611. ->willReturn(false);
  1612. $path = $this->createMock(File::class);
  1613. $path->expects($this->never())->method('getOwner');
  1614. $path->method('getName')->willReturn('target');
  1615. $path->method('getStorage')->willReturn($storage);
  1616. $parent = $this->createMock(Folder::class);
  1617. $parent->method('getStorage')->willReturn($storage);
  1618. $parentParent = $this->createMock(Folder::class);
  1619. $parentParent->method('getStorage')->willReturn($storage2);
  1620. $parentParent->method('getOwner')->willReturn($shareOwner);
  1621. $path->method('getParent')->willReturn($parent);
  1622. $parent->method('getParent')->willReturn($parentParent);
  1623. $share = $this->createShare(
  1624. null,
  1625. \OCP\Share::SHARE_TYPE_USER,
  1626. $path,
  1627. 'sharedWith',
  1628. 'sharedBy',
  1629. null,
  1630. \OCP\Constants::PERMISSION_ALL);
  1631. $manager->expects($this->once())
  1632. ->method('canShare')
  1633. ->with($share)
  1634. ->willReturn(true);
  1635. $manager->expects($this->once())
  1636. ->method('generalCreateChecks')
  1637. ->with($share);;
  1638. $manager->expects($this->once())
  1639. ->method('userCreateChecks')
  1640. ->with($share);;
  1641. $manager->expects($this->once())
  1642. ->method('pathCreateChecks')
  1643. ->with($path);
  1644. $this->defaultProvider
  1645. ->expects($this->once())
  1646. ->method('create')
  1647. ->with($share)
  1648. ->will($this->returnArgument(0));
  1649. $share->expects($this->once())
  1650. ->method('setShareOwner')
  1651. ->with('shareOwner');
  1652. $share->expects($this->once())
  1653. ->method('setTarget')
  1654. ->with('/target');
  1655. $manager->createShare($share);
  1656. }
  1657. public function testGetSharesBy() {
  1658. $share = $this->manager->newShare();
  1659. $node = $this->createMock(Folder::class);
  1660. $this->defaultProvider->expects($this->once())
  1661. ->method('getSharesBy')
  1662. ->with(
  1663. $this->equalTo('user'),
  1664. $this->equalTo(\OCP\Share::SHARE_TYPE_USER),
  1665. $this->equalTo($node),
  1666. $this->equalTo(true),
  1667. $this->equalTo(1),
  1668. $this->equalTo(1)
  1669. )->willReturn([$share]);
  1670. $shares = $this->manager->getSharesBy('user', \OCP\Share::SHARE_TYPE_USER, $node, true, 1, 1);
  1671. $this->assertCount(1, $shares);
  1672. $this->assertSame($share, $shares[0]);
  1673. }
  1674. /**
  1675. * Test to ensure we correctly remove expired link shares
  1676. *
  1677. * We have 8 Shares and we want the 3 first valid shares.
  1678. * share 3-6 and 8 are expired. Thus at the end of this test we should
  1679. * have received share 1,2 and 7. And from the manager. Share 3-6 should be
  1680. * deleted (as they are evaluated). but share 8 should still be there.
  1681. */
  1682. public function testGetSharesByExpiredLinkShares() {
  1683. $manager = $this->createManagerMock()
  1684. ->setMethods(['deleteShare'])
  1685. ->getMock();
  1686. /** @var \OCP\Share\IShare[] $shares */
  1687. $shares = [];
  1688. /*
  1689. * This results in an array of 8 IShare elements
  1690. */
  1691. for ($i = 0; $i < 8; $i++) {
  1692. $share = $this->manager->newShare();
  1693. $share->setId($i);
  1694. $shares[] = $share;
  1695. }
  1696. $today = new \DateTime();
  1697. $today->setTime(0,0,0);
  1698. /*
  1699. * Set the expiration date to today for some shares
  1700. */
  1701. $shares[2]->setExpirationDate($today);
  1702. $shares[3]->setExpirationDate($today);
  1703. $shares[4]->setExpirationDate($today);
  1704. $shares[5]->setExpirationDate($today);
  1705. /** @var \OCP\Share\IShare[] $i */
  1706. $shares2 = [];
  1707. for ($i = 0; $i < 8; $i++) {
  1708. $shares2[] = clone $shares[$i];
  1709. }
  1710. $node = $this->createMock(File::class);
  1711. /*
  1712. * Simulate the getSharesBy call.
  1713. */
  1714. $this->defaultProvider
  1715. ->method('getSharesBy')
  1716. ->will($this->returnCallback(function($uid, $type, $node, $reshares, $limit, $offset) use (&$shares2) {
  1717. return array_slice($shares2, $offset, $limit);
  1718. }));
  1719. /*
  1720. * Simulate the deleteShare call.
  1721. */
  1722. $manager->method('deleteShare')
  1723. ->will($this->returnCallback(function($share) use (&$shares2) {
  1724. for($i = 0; $i < count($shares2); $i++) {
  1725. if ($shares2[$i]->getId() === $share->getId()) {
  1726. array_splice($shares2, $i, 1);
  1727. break;
  1728. }
  1729. }
  1730. }));
  1731. $res = $manager->getSharesBy('user', \OCP\Share::SHARE_TYPE_LINK, $node, true, 3, 0);
  1732. $this->assertCount(3, $res);
  1733. $this->assertEquals($shares[0]->getId(), $res[0]->getId());
  1734. $this->assertEquals($shares[1]->getId(), $res[1]->getId());
  1735. $this->assertEquals($shares[6]->getId(), $res[2]->getId());
  1736. $this->assertCount(4, $shares2);
  1737. $this->assertEquals(0, $shares2[0]->getId());
  1738. $this->assertEquals(1, $shares2[1]->getId());
  1739. $this->assertEquals(6, $shares2[2]->getId());
  1740. $this->assertEquals(7, $shares2[3]->getId());
  1741. $this->assertSame($today, $shares[3]->getExpirationDate());
  1742. }
  1743. public function testGetShareByToken() {
  1744. $this->config
  1745. ->expects($this->once())
  1746. ->method('getAppValue')
  1747. ->with('core', 'shareapi_allow_links', 'yes')
  1748. ->willReturn('yes');
  1749. $factory = $this->createMock(IProviderFactory::class);
  1750. $manager = new Manager(
  1751. $this->logger,
  1752. $this->config,
  1753. $this->secureRandom,
  1754. $this->hasher,
  1755. $this->mountManager,
  1756. $this->groupManager,
  1757. $this->l,
  1758. $factory,
  1759. $this->userManager,
  1760. $this->rootFolder,
  1761. $this->eventDispatcher
  1762. );
  1763. $share = $this->createMock(IShare::class);
  1764. $factory->expects($this->once())
  1765. ->method('getProviderForType')
  1766. ->with(\OCP\Share::SHARE_TYPE_LINK)
  1767. ->willReturn($this->defaultProvider);
  1768. $this->defaultProvider->expects($this->once())
  1769. ->method('getShareByToken')
  1770. ->with('token')
  1771. ->willReturn($share);
  1772. $ret = $manager->getShareByToken('token');
  1773. $this->assertSame($share, $ret);
  1774. }
  1775. public function testGetShareByTokenWithException() {
  1776. $this->config
  1777. ->expects($this->once())
  1778. ->method('getAppValue')
  1779. ->with('core', 'shareapi_allow_links', 'yes')
  1780. ->willReturn('yes');
  1781. $factory = $this->createMock(IProviderFactory::class);
  1782. $manager = new Manager(
  1783. $this->logger,
  1784. $this->config,
  1785. $this->secureRandom,
  1786. $this->hasher,
  1787. $this->mountManager,
  1788. $this->groupManager,
  1789. $this->l,
  1790. $factory,
  1791. $this->userManager,
  1792. $this->rootFolder,
  1793. $this->eventDispatcher
  1794. );
  1795. $share = $this->createMock(IShare::class);
  1796. $factory->expects($this->at(0))
  1797. ->method('getProviderForType')
  1798. ->with(\OCP\Share::SHARE_TYPE_LINK)
  1799. ->willReturn($this->defaultProvider);
  1800. $factory->expects($this->at(1))
  1801. ->method('getProviderForType')
  1802. ->with(\OCP\Share::SHARE_TYPE_REMOTE)
  1803. ->willReturn($this->defaultProvider);
  1804. $this->defaultProvider->expects($this->at(0))
  1805. ->method('getShareByToken')
  1806. ->with('token')
  1807. ->will($this->throwException(new ShareNotFound()));
  1808. $this->defaultProvider->expects($this->at(1))
  1809. ->method('getShareByToken')
  1810. ->with('token')
  1811. ->willReturn($share);
  1812. $ret = $manager->getShareByToken('token');
  1813. $this->assertSame($share, $ret);
  1814. }
  1815. /**
  1816. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  1817. */
  1818. public function testGetShareByTokenExpired() {
  1819. $this->config
  1820. ->expects($this->once())
  1821. ->method('getAppValue')
  1822. ->with('core', 'shareapi_allow_links', 'yes')
  1823. ->willReturn('yes');
  1824. $manager = $this->createManagerMock()
  1825. ->setMethods(['deleteShare'])
  1826. ->getMock();
  1827. $date = new \DateTime();
  1828. $date->setTime(0,0,0);
  1829. $share = $this->manager->newShare();
  1830. $share->setExpirationDate($date);
  1831. $this->defaultProvider->expects($this->once())
  1832. ->method('getShareByToken')
  1833. ->with('expiredToken')
  1834. ->willReturn($share);
  1835. $manager->expects($this->once())
  1836. ->method('deleteShare')
  1837. ->with($this->equalTo($share));
  1838. $manager->getShareByToken('expiredToken');
  1839. }
  1840. public function testGetShareByTokenNotExpired() {
  1841. $this->config
  1842. ->expects($this->once())
  1843. ->method('getAppValue')
  1844. ->with('core', 'shareapi_allow_links', 'yes')
  1845. ->willReturn('yes');
  1846. $date = new \DateTime();
  1847. $date->setTime(0,0,0);
  1848. $date->add(new \DateInterval('P2D'));
  1849. $share = $this->manager->newShare();
  1850. $share->setExpirationDate($date);
  1851. $this->defaultProvider->expects($this->once())
  1852. ->method('getShareByToken')
  1853. ->with('expiredToken')
  1854. ->willReturn($share);
  1855. $res = $this->manager->getShareByToken('expiredToken');
  1856. $this->assertSame($share, $res);
  1857. }
  1858. /**
  1859. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  1860. */
  1861. public function testGetShareByTokenWithPublicLinksDisabled() {
  1862. $this->config
  1863. ->expects($this->once())
  1864. ->method('getAppValue')
  1865. ->with('core', 'shareapi_allow_links', 'yes')
  1866. ->willReturn('no');
  1867. $this->manager->getShareByToken('validToken');
  1868. }
  1869. public function testGetShareByTokenPublicUploadDisabled() {
  1870. $this->config
  1871. ->expects($this->at(0))
  1872. ->method('getAppValue')
  1873. ->with('core', 'shareapi_allow_links', 'yes')
  1874. ->willReturn('yes');
  1875. $share = $this->manager->newShare();
  1876. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1877. ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1878. $this->config
  1879. ->expects($this->at(1))
  1880. ->method('getAppValue')
  1881. ->will($this->returnValueMap([
  1882. ['core', 'shareapi_allow_public_upload', 'yes', 'no'],
  1883. ]));
  1884. $this->defaultProvider->expects($this->once())
  1885. ->method('getShareByToken')
  1886. ->willReturn('validToken')
  1887. ->willReturn($share);
  1888. $res = $this->manager->getShareByToken('validToken');
  1889. $this->assertSame(\OCP\Constants::PERMISSION_READ, $res->getPermissions());
  1890. }
  1891. public function testCheckPasswordNoLinkShare() {
  1892. $share = $this->createMock(IShare::class);
  1893. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  1894. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  1895. }
  1896. public function testCheckPasswordNoPassword() {
  1897. $share = $this->createMock(IShare::class);
  1898. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1899. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  1900. $share->method('getPassword')->willReturn('password');
  1901. $this->assertFalse($this->manager->checkPassword($share, null));
  1902. }
  1903. public function testCheckPasswordInvalidPassword() {
  1904. $share = $this->createMock(IShare::class);
  1905. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1906. $share->method('getPassword')->willReturn('password');
  1907. $this->hasher->method('verify')->with('invalidpassword', 'password', '')->willReturn(false);
  1908. $this->assertFalse($this->manager->checkPassword($share, 'invalidpassword'));
  1909. }
  1910. public function testCheckPasswordValidPassword() {
  1911. $share = $this->createMock(IShare::class);
  1912. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1913. $share->method('getPassword')->willReturn('passwordHash');
  1914. $this->hasher->method('verify')->with('password', 'passwordHash', '')->willReturn(true);
  1915. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  1916. }
  1917. public function testCheckPasswordUpdateShare() {
  1918. $share = $this->manager->newShare();
  1919. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1920. ->setPassword('passwordHash');
  1921. $this->hasher->method('verify')->with('password', 'passwordHash', '')
  1922. ->will($this->returnCallback(function($pass, $hash, &$newHash) {
  1923. $newHash = 'newHash';
  1924. return true;
  1925. }));
  1926. $this->defaultProvider->expects($this->once())
  1927. ->method('update')
  1928. ->with($this->callback(function (\OCP\Share\IShare $share) {
  1929. return $share->getPassword() === 'newHash';
  1930. }));
  1931. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  1932. }
  1933. /**
  1934. * @expectedException Exception
  1935. * @expectedExceptionMessage Can't change share type
  1936. */
  1937. public function testUpdateShareCantChangeShareType() {
  1938. $manager = $this->createManagerMock()
  1939. ->setMethods([
  1940. 'canShare',
  1941. 'getShareById'
  1942. ])
  1943. ->getMock();
  1944. $originalShare = $this->manager->newShare();
  1945. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  1946. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1947. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1948. $share = $this->manager->newShare();
  1949. $share->setProviderId('foo')
  1950. ->setId('42')
  1951. ->setShareType(\OCP\Share::SHARE_TYPE_USER);
  1952. $manager->updateShare($share);
  1953. }
  1954. /**
  1955. * @expectedException Exception
  1956. * @expectedExceptionMessage Can only update recipient on user shares
  1957. */
  1958. public function testUpdateShareCantChangeRecipientForGroupShare() {
  1959. $manager = $this->createManagerMock()
  1960. ->setMethods([
  1961. 'canShare',
  1962. 'getShareById'
  1963. ])
  1964. ->getMock();
  1965. $originalShare = $this->manager->newShare();
  1966. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1967. ->setSharedWith('origGroup');
  1968. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1969. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1970. $share = $this->manager->newShare();
  1971. $share->setProviderId('foo')
  1972. ->setId('42')
  1973. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1974. ->setSharedWith('newGroup');
  1975. $manager->updateShare($share);
  1976. }
  1977. /**
  1978. * @expectedException Exception
  1979. * @expectedExceptionMessage Can't share with the share owner
  1980. */
  1981. public function testUpdateShareCantShareWithOwner() {
  1982. $manager = $this->createManagerMock()
  1983. ->setMethods([
  1984. 'canShare',
  1985. 'getShareById'
  1986. ])
  1987. ->getMock();
  1988. $originalShare = $this->manager->newShare();
  1989. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1990. ->setSharedWith('sharedWith');
  1991. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1992. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1993. $share = $this->manager->newShare();
  1994. $share->setProviderId('foo')
  1995. ->setId('42')
  1996. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1997. ->setSharedWith('newUser')
  1998. ->setShareOwner('newUser');
  1999. $manager->updateShare($share);
  2000. }
  2001. public function testUpdateShareUser() {
  2002. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  2003. $manager = $this->createManagerMock()
  2004. ->setMethods([
  2005. 'canShare',
  2006. 'getShareById',
  2007. 'generalCreateChecks',
  2008. 'userCreateChecks',
  2009. 'pathCreateChecks',
  2010. ])
  2011. ->getMock();
  2012. $originalShare = $this->manager->newShare();
  2013. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2014. ->setSharedWith('origUser')
  2015. ->setPermissions(1);
  2016. $node = $this->createMock(File::class);
  2017. $node->method('getId')->willReturn(100);
  2018. $node->method('getPath')->willReturn('/newUser/files/myPath');
  2019. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2020. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2021. $share = $this->manager->newShare();
  2022. $share->setProviderId('foo')
  2023. ->setId('42')
  2024. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2025. ->setSharedWith('origUser')
  2026. ->setShareOwner('newUser')
  2027. ->setSharedBy('sharer')
  2028. ->setPermissions(31)
  2029. ->setNode($node);
  2030. $this->defaultProvider->expects($this->once())
  2031. ->method('update')
  2032. ->with($share)
  2033. ->willReturn($share);
  2034. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2035. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2036. $hookListner->expects($this->never())->method('post');
  2037. $this->rootFolder->method('getUserFolder')->with('newUser')->will($this->returnSelf());
  2038. $this->rootFolder->method('getRelativePath')->with('/newUser/files/myPath')->willReturn('/myPath');
  2039. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2040. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner2, 'post');
  2041. $hookListner2->expects($this->once())->method('post')->with([
  2042. 'itemType' => 'file',
  2043. 'itemSource' => 100,
  2044. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  2045. 'shareWith' => 'origUser',
  2046. 'uidOwner' => 'sharer',
  2047. 'permissions' => 31,
  2048. 'path' => '/myPath',
  2049. ]);
  2050. $manager->updateShare($share);
  2051. }
  2052. public function testUpdateShareGroup() {
  2053. $manager = $this->createManagerMock()
  2054. ->setMethods([
  2055. 'canShare',
  2056. 'getShareById',
  2057. 'generalCreateChecks',
  2058. 'groupCreateChecks',
  2059. 'pathCreateChecks',
  2060. ])
  2061. ->getMock();
  2062. $originalShare = $this->manager->newShare();
  2063. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2064. ->setSharedWith('origUser')
  2065. ->setPermissions(31);
  2066. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2067. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2068. $node = $this->createMock(File::class);
  2069. $share = $this->manager->newShare();
  2070. $share->setProviderId('foo')
  2071. ->setId('42')
  2072. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2073. ->setSharedWith('origUser')
  2074. ->setShareOwner('owner')
  2075. ->setNode($node)
  2076. ->setPermissions(31);
  2077. $this->defaultProvider->expects($this->once())
  2078. ->method('update')
  2079. ->with($share)
  2080. ->willReturn($share);
  2081. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2082. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2083. $hookListner->expects($this->never())->method('post');
  2084. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2085. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner2, 'post');
  2086. $hookListner2->expects($this->never())->method('post');
  2087. $manager->updateShare($share);
  2088. }
  2089. public function testUpdateShareLink() {
  2090. $manager = $this->createManagerMock()
  2091. ->setMethods([
  2092. 'canShare',
  2093. 'getShareById',
  2094. 'generalCreateChecks',
  2095. 'linkCreateChecks',
  2096. 'pathCreateChecks',
  2097. 'verifyPassword',
  2098. 'validateExpirationDate',
  2099. ])
  2100. ->getMock();
  2101. $originalShare = $this->manager->newShare();
  2102. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2103. ->setPermissions(15);
  2104. $tomorrow = new \DateTime();
  2105. $tomorrow->setTime(0,0,0);
  2106. $tomorrow->add(new \DateInterval('P1D'));
  2107. $file = $this->createMock(File::class);
  2108. $file->method('getId')->willReturn(100);
  2109. $share = $this->manager->newShare();
  2110. $share->setProviderId('foo')
  2111. ->setId('42')
  2112. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2113. ->setSharedBy('owner')
  2114. ->setShareOwner('owner')
  2115. ->setPassword('password')
  2116. ->setExpirationDate($tomorrow)
  2117. ->setNode($file)
  2118. ->setPermissions(15);
  2119. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2120. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2121. $manager->expects($this->once())->method('validateExpirationDate')->with($share);
  2122. $this->defaultProvider->expects($this->once())
  2123. ->method('update')
  2124. ->with($share)
  2125. ->willReturn($share);
  2126. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2127. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2128. $hookListner->expects($this->once())->method('post')->with([
  2129. 'itemType' => 'file',
  2130. 'itemSource' => 100,
  2131. 'date' => $tomorrow,
  2132. 'uidOwner' => 'owner',
  2133. ]);
  2134. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2135. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner2, 'post');
  2136. $hookListner2->expects($this->never())->method('post');
  2137. $manager->updateShare($share);
  2138. }
  2139. /**
  2140. * @expectedException \InvalidArgumentException
  2141. * @expectedExceptionMessage Can't change target of link share
  2142. */
  2143. public function testMoveShareLink() {
  2144. $share = $this->manager->newShare();
  2145. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK);
  2146. $recipient = $this->createMock(IUser::class);
  2147. $this->manager->moveShare($share, $recipient);
  2148. }
  2149. /**
  2150. * @expectedException \InvalidArgumentException
  2151. * @expectedExceptionMessage Invalid recipient
  2152. */
  2153. public function testMoveShareUserNotRecipient() {
  2154. $share = $this->manager->newShare();
  2155. $share->setShareType(\OCP\Share::SHARE_TYPE_USER);
  2156. $share->setSharedWith('sharedWith');
  2157. $this->manager->moveShare($share, 'recipient');
  2158. }
  2159. public function testMoveShareUser() {
  2160. $share = $this->manager->newShare();
  2161. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2162. ->setId('42')
  2163. ->setProviderId('foo');
  2164. $share->setSharedWith('recipient');
  2165. $this->defaultProvider->method('move')->with($share, 'recipient')->will($this->returnArgument(0));
  2166. $this->manager->moveShare($share, 'recipient');
  2167. }
  2168. /**
  2169. * @expectedException \InvalidArgumentException
  2170. * @expectedExceptionMessage Invalid recipient
  2171. */
  2172. public function testMoveShareGroupNotRecipient() {
  2173. $share = $this->manager->newShare();
  2174. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  2175. $sharedWith = $this->createMock(IGroup::class);
  2176. $share->setSharedWith('shareWith');
  2177. $recipient = $this->createMock(IUser::class);
  2178. $sharedWith->method('inGroup')->with($recipient)->willReturn(false);
  2179. $this->groupManager->method('get')->with('shareWith')->willReturn($sharedWith);
  2180. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2181. $this->manager->moveShare($share, 'recipient');
  2182. }
  2183. public function testMoveShareGroup() {
  2184. $share = $this->manager->newShare();
  2185. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2186. ->setId('42')
  2187. ->setProviderId('foo');
  2188. $group = $this->createMock(IGroup::class);
  2189. $share->setSharedWith('group');
  2190. $recipient = $this->createMock(IUser::class);
  2191. $group->method('inGroup')->with($recipient)->willReturn(true);
  2192. $this->groupManager->method('get')->with('group')->willReturn($group);
  2193. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2194. $this->defaultProvider->method('move')->with($share, 'recipient')->will($this->returnArgument(0));
  2195. $this->manager->moveShare($share, 'recipient');
  2196. }
  2197. /**
  2198. * @dataProvider dataTestShareProviderExists
  2199. */
  2200. public function testShareProviderExists($shareType, $expected) {
  2201. $factory = $this->getMockBuilder('OCP\Share\IProviderFactory')->getMock();
  2202. $factory->expects($this->any())->method('getProviderForType')
  2203. ->willReturnCallback(function ($id) {
  2204. if ($id === \OCP\Share::SHARE_TYPE_USER) {
  2205. return true;
  2206. }
  2207. throw new Exception\ProviderException();
  2208. });
  2209. $manager = new Manager(
  2210. $this->logger,
  2211. $this->config,
  2212. $this->secureRandom,
  2213. $this->hasher,
  2214. $this->mountManager,
  2215. $this->groupManager,
  2216. $this->l,
  2217. $factory,
  2218. $this->userManager,
  2219. $this->rootFolder,
  2220. $this->eventDispatcher
  2221. );
  2222. $this->assertSame($expected,
  2223. $manager->shareProviderExists($shareType)
  2224. );
  2225. }
  2226. public function dataTestShareProviderExists() {
  2227. return [
  2228. [\OCP\Share::SHARE_TYPE_USER, true],
  2229. [42, false],
  2230. ];
  2231. }
  2232. public function testGetSharesInFolder() {
  2233. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  2234. $manager = new Manager(
  2235. $this->logger,
  2236. $this->config,
  2237. $this->secureRandom,
  2238. $this->hasher,
  2239. $this->mountManager,
  2240. $this->groupManager,
  2241. $this->l,
  2242. $factory,
  2243. $this->userManager,
  2244. $this->rootFolder,
  2245. $this->eventDispatcher
  2246. );
  2247. $factory->setProvider($this->defaultProvider);
  2248. $extraProvider = $this->createMock(IShareProvider::class);
  2249. $factory->setSecondProvider($extraProvider);
  2250. $share1 = $this->createMock(IShare::class);
  2251. $share2 = $this->createMock(IShare::class);
  2252. $share3 = $this->createMock(IShare::class);
  2253. $share4 = $this->createMock(IShare::class);
  2254. $folder = $this->createMock(Folder::class);
  2255. $this->defaultProvider->method('getSharesInFolder')
  2256. ->with(
  2257. $this->equalTo('user'),
  2258. $this->equalTo($folder),
  2259. $this->equalTo(false)
  2260. )->willReturn([
  2261. 1 => [$share1],
  2262. 2 => [$share2],
  2263. ]);
  2264. $extraProvider->method('getSharesInFolder')
  2265. ->with(
  2266. $this->equalTo('user'),
  2267. $this->equalTo($folder),
  2268. $this->equalTo(false)
  2269. )->willReturn([
  2270. 2 => [$share3],
  2271. 3 => [$share4],
  2272. ]);
  2273. $result = $manager->getSharesInFolder('user', $folder, false);
  2274. $expects = [
  2275. 1 => [$share1],
  2276. 2 => [$share2, $share3],
  2277. 3 => [$share4],
  2278. ];
  2279. $this->assertSame($expects, $result);
  2280. }
  2281. }
  2282. class DummyFactory implements IProviderFactory {
  2283. /** @var IShareProvider */
  2284. protected $provider;
  2285. public function __construct(\OCP\IServerContainer $serverContainer) {
  2286. }
  2287. /**
  2288. * @param IShareProvider $provider
  2289. */
  2290. public function setProvider($provider) {
  2291. $this->provider = $provider;
  2292. }
  2293. /**
  2294. * @param string $id
  2295. * @return IShareProvider
  2296. */
  2297. public function getProvider($id) {
  2298. return $this->provider;
  2299. }
  2300. /**
  2301. * @param int $shareType
  2302. * @return IShareProvider
  2303. */
  2304. public function getProviderForType($shareType) {
  2305. return $this->provider;
  2306. }
  2307. /**
  2308. * @return IShareProvider[]
  2309. */
  2310. public function getAllProviders() {
  2311. return [$this->provider];
  2312. }
  2313. }
  2314. class DummyFactory2 extends DummyFactory {
  2315. /** @var IShareProvider */
  2316. private $provider2;
  2317. /**
  2318. * @param IShareProvider $provider
  2319. */
  2320. public function setSecondProvider($provider) {
  2321. $this->provider2 = $provider;
  2322. }
  2323. public function getAllProviders() {
  2324. return [$this->provider, $this->provider2];
  2325. }
  2326. }