1
0

MountProviderTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests;
  8. use OC\Memcache\NullCache;
  9. use OCA\Files_Sharing\MountProvider;
  10. use OCP\EventDispatcher\IEventDispatcher;
  11. use OCP\Files\IRootFolder;
  12. use OCP\Files\Storage\IStorageFactory;
  13. use OCP\ICacheFactory;
  14. use OCP\IConfig;
  15. use OCP\IUser;
  16. use OCP\IUserManager;
  17. use OCP\Share\IAttributes as IShareAttributes;
  18. use OCP\Share\IManager;
  19. use OCP\Share\IShare;
  20. use Psr\Log\LoggerInterface;
  21. /**
  22. * @group DB
  23. */
  24. class MountProviderTest extends \Test\TestCase {
  25. /** @var MountProvider */
  26. private $provider;
  27. /** @var IConfig|MockObject */
  28. private $config;
  29. /** @var IUser|MockObject */
  30. private $user;
  31. /** @var IStorageFactory|MockObject */
  32. private $loader;
  33. /** @var IManager|MockObject */
  34. private $shareManager;
  35. /** @var LoggerInterface|MockObject */
  36. private $logger;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  40. $this->user = $this->getMockBuilder(IUser::class)->getMock();
  41. $this->loader = $this->getMockBuilder('OCP\Files\Storage\IStorageFactory')->getMock();
  42. $this->shareManager = $this->getMockBuilder(IManager::class)->getMock();
  43. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  44. $eventDispatcher = $this->createMock(IEventDispatcher::class);
  45. $cacheFactory = $this->createMock(ICacheFactory::class);
  46. $cacheFactory->method('createLocal')
  47. ->willReturn(new NullCache());
  48. $this->provider = new MountProvider($this->config, $this->shareManager, $this->logger, $eventDispatcher, $cacheFactory);
  49. }
  50. private function makeMockShareAttributes($attrs) {
  51. if ($attrs === null) {
  52. return null;
  53. }
  54. $shareAttributes = $this->createMock(IShareAttributes::class);
  55. $shareAttributes->method('toArray')->willReturn($attrs);
  56. $shareAttributes->method('getAttribute')->will(
  57. $this->returnCallback(function ($scope, $key) use ($attrs) {
  58. $result = null;
  59. foreach ($attrs as $attr) {
  60. if ($attr['key'] === $key && $attr['scope'] === $scope) {
  61. $result = $attr['enabled'];
  62. }
  63. }
  64. return $result;
  65. })
  66. );
  67. return $shareAttributes;
  68. }
  69. private function makeMockShare($id, $nodeId, $owner = 'user2', $target = null, $permissions = 31, $attributes = null) {
  70. $share = $this->createMock(IShare::class);
  71. $share->expects($this->any())
  72. ->method('getPermissions')
  73. ->willReturn($permissions);
  74. $share->expects($this->any())
  75. ->method('getAttributes')
  76. ->will($this->returnValue($this->makeMockShareAttributes($attributes)));
  77. $share->expects($this->any())
  78. ->method('getShareOwner')
  79. ->willReturn($owner);
  80. $share->expects($this->any())
  81. ->method('getTarget')
  82. ->willReturn($target);
  83. $share->expects($this->any())
  84. ->method('getId')
  85. ->willReturn($id);
  86. $share->expects($this->any())
  87. ->method('getNodeId')
  88. ->willReturn($nodeId);
  89. $share->expects($this->any())
  90. ->method('getShareTime')
  91. ->willReturn(
  92. // compute share time based on id, simulating share order
  93. new \DateTime('@' . (1469193980 + 1000 * $id))
  94. );
  95. return $share;
  96. }
  97. /**
  98. * Tests excluding shares from the current view. This includes:
  99. * - shares that were opted out of (permissions === 0)
  100. * - shares with a group in which the owner is already in
  101. */
  102. public function testExcludeShares() {
  103. $rootFolder = $this->createMock(IRootFolder::class);
  104. $userManager = $this->createMock(IUserManager::class);
  105. $attr1 = [];
  106. $attr2 = [['scope' => 'permission', 'key' => 'download', 'enabled' => true]];
  107. $userShares = [
  108. $this->makeMockShare(1, 100, 'user2', '/share2', 0, $attr1),
  109. $this->makeMockShare(2, 100, 'user2', '/share2', 31, $attr2),
  110. ];
  111. $groupShares = [
  112. $this->makeMockShare(3, 100, 'user2', '/share2', 0, $attr1),
  113. $this->makeMockShare(4, 101, 'user2', '/share4', 31, $attr2),
  114. $this->makeMockShare(5, 100, 'user1', '/share4', 31, $attr2),
  115. ];
  116. $roomShares = [
  117. $this->makeMockShare(6, 102, 'user2', '/share6', 0),
  118. $this->makeMockShare(7, 102, 'user1', '/share6', 31),
  119. $this->makeMockShare(8, 102, 'user2', '/share6', 31),
  120. $this->makeMockShare(9, 102, 'user2', '/share6', 31),
  121. ];
  122. $deckShares = [
  123. $this->makeMockShare(10, 103, 'user2', '/share7', 0),
  124. $this->makeMockShare(11, 103, 'user1', '/share7', 31),
  125. $this->makeMockShare(12, 103, 'user2', '/share7', 31),
  126. $this->makeMockShare(13, 103, 'user2', '/share7', 31),
  127. ];
  128. // tests regarding circles and sciencemesh are made in the apps themselves.
  129. $circleShares = [];
  130. $sciencemeshShares = [];
  131. $this->user->expects($this->any())
  132. ->method('getUID')
  133. ->willReturn('user1');
  134. $this->shareManager->expects($this->exactly(6))
  135. ->method('getSharedWith')
  136. ->withConsecutive(
  137. ['user1', IShare::TYPE_USER],
  138. ['user1', IShare::TYPE_GROUP, null, -1],
  139. ['user1', IShare::TYPE_CIRCLE, null, -1],
  140. ['user1', IShare::TYPE_ROOM, null, -1],
  141. ['user1', IShare::TYPE_DECK, null, -1],
  142. ['user1', IShare::TYPE_SCIENCEMESH, null, -1],
  143. )->willReturnOnConsecutiveCalls(
  144. $userShares,
  145. $groupShares,
  146. $circleShares,
  147. $roomShares,
  148. $deckShares,
  149. $sciencemeshShares
  150. );
  151. $this->shareManager->expects($this->any())
  152. ->method('newShare')
  153. ->willReturnCallback(function () use ($rootFolder, $userManager) {
  154. return new \OC\Share20\Share($rootFolder, $userManager);
  155. });
  156. $mounts = $this->provider->getMountsForUser($this->user, $this->loader);
  157. $this->assertCount(4, $mounts);
  158. $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[0]);
  159. $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[1]);
  160. $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[2]);
  161. $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[3]);
  162. $mountedShare1 = $mounts[0]->getShare();
  163. $this->assertEquals('2', $mountedShare1->getId());
  164. $this->assertEquals('user2', $mountedShare1->getShareOwner());
  165. $this->assertEquals(100, $mountedShare1->getNodeId());
  166. $this->assertEquals('/share2', $mountedShare1->getTarget());
  167. $this->assertEquals(31, $mountedShare1->getPermissions());
  168. $this->assertEquals(true, $mountedShare1->getAttributes()->getAttribute('permission', 'download'));
  169. $mountedShare2 = $mounts[1]->getShare();
  170. $this->assertEquals('4', $mountedShare2->getId());
  171. $this->assertEquals('user2', $mountedShare2->getShareOwner());
  172. $this->assertEquals(101, $mountedShare2->getNodeId());
  173. $this->assertEquals('/share4', $mountedShare2->getTarget());
  174. $this->assertEquals(31, $mountedShare2->getPermissions());
  175. $this->assertEquals(true, $mountedShare2->getAttributes()->getAttribute('permission', 'download'));
  176. $mountedShare3 = $mounts[2]->getShare();
  177. $this->assertEquals('8', $mountedShare3->getId());
  178. $this->assertEquals('user2', $mountedShare3->getShareOwner());
  179. $this->assertEquals(102, $mountedShare3->getNodeId());
  180. $this->assertEquals('/share6', $mountedShare3->getTarget());
  181. $this->assertEquals(31, $mountedShare3->getPermissions());
  182. $mountedShare4 = $mounts[3]->getShare();
  183. $this->assertEquals('12', $mountedShare4->getId());
  184. $this->assertEquals('user2', $mountedShare4->getShareOwner());
  185. $this->assertEquals(103, $mountedShare4->getNodeId());
  186. $this->assertEquals('/share7', $mountedShare4->getTarget());
  187. $this->assertEquals(31, $mountedShare4->getPermissions());
  188. }
  189. public function mergeSharesDataProvider() {
  190. // note: the user in the specs here is the shareOwner not recipient
  191. // the recipient is always "user1"
  192. return [
  193. // #0: share as outsider with "group1" and "user1" with same permissions
  194. [
  195. [
  196. [1, 100, 'user2', '/share2', 31, null],
  197. ],
  198. [
  199. [2, 100, 'user2', '/share2', 31, null],
  200. ],
  201. [
  202. // combined, user share has higher priority
  203. ['1', 100, 'user2', '/share2', 31, []],
  204. ],
  205. ],
  206. // #1: share as outsider with "group1" and "user1" with different permissions
  207. [
  208. [
  209. [1, 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'enabled' => true], ['scope' => 'app', 'key' => 'attribute1', 'enabled' => true]]],
  210. ],
  211. [
  212. [2, 100, 'user2', '/share', 15, [['scope' => 'permission', 'key' => 'download', 'enabled' => false], ['scope' => 'app', 'key' => 'attribute2', 'enabled' => false]]],
  213. ],
  214. [
  215. // use highest permissions
  216. ['1', 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'enabled' => true], ['scope' => 'app', 'key' => 'attribute1', 'enabled' => true], ['scope' => 'app', 'key' => 'attribute2', 'enabled' => false]]],
  217. ],
  218. ],
  219. // #2: share as outsider with "group1" and "group2" with same permissions
  220. [
  221. [
  222. ],
  223. [
  224. [1, 100, 'user2', '/share', 31, null],
  225. [2, 100, 'user2', '/share', 31, []],
  226. ],
  227. [
  228. // combined, first group share has higher priority
  229. ['1', 100, 'user2', '/share', 31, []],
  230. ],
  231. ],
  232. // #3: share as outsider with "group1" and "group2" with different permissions
  233. [
  234. [
  235. ],
  236. [
  237. [1, 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'enabled' => false]]],
  238. [2, 100, 'user2', '/share', 15, [['scope' => 'permission', 'key' => 'download', 'enabled' => true]]],
  239. ],
  240. [
  241. // use higher permissions (most permissive)
  242. ['1', 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'enabled' => true]]],
  243. ],
  244. ],
  245. // #4: share as insider with "group1"
  246. [
  247. [
  248. ],
  249. [
  250. [1, 100, 'user1', '/share', 31, []],
  251. ],
  252. [
  253. // no received share since "user1" is the sharer/owner
  254. ],
  255. ],
  256. // #5: share as insider with "group1" and "group2" with different permissions
  257. [
  258. [
  259. ],
  260. [
  261. [1, 100, 'user1', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'enabled' => true]]],
  262. [2, 100, 'user1', '/share', 15, [['scope' => 'permission', 'key' => 'download', 'enabled' => false]]],
  263. ],
  264. [
  265. // no received share since "user1" is the sharer/owner
  266. ],
  267. ],
  268. // #6: share as outside with "group1", recipient opted out
  269. [
  270. [
  271. ],
  272. [
  273. [1, 100, 'user2', '/share', 0, []],
  274. ],
  275. [
  276. // no received share since "user1" opted out
  277. ],
  278. ],
  279. // #7: share as outsider with "group1" and "user1" where recipient renamed in between
  280. [
  281. [
  282. [1, 100, 'user2', '/share2-renamed', 31, []],
  283. ],
  284. [
  285. [2, 100, 'user2', '/share2', 31, []],
  286. ],
  287. [
  288. // use target of least recent share
  289. ['1', 100, 'user2', '/share2-renamed', 31, []],
  290. ],
  291. ],
  292. // #8: share as outsider with "group1" and "user1" where recipient renamed in between
  293. [
  294. [
  295. [2, 100, 'user2', '/share2', 31, []],
  296. ],
  297. [
  298. [1, 100, 'user2', '/share2-renamed', 31, []],
  299. ],
  300. [
  301. // use target of least recent share
  302. ['1', 100, 'user2', '/share2-renamed', 31, []],
  303. ],
  304. ],
  305. // #9: share as outsider with "nullgroup" and "user1" where recipient renamed in between
  306. [
  307. [
  308. [2, 100, 'user2', '/share2', 31, []],
  309. ],
  310. [
  311. [1, 100, 'nullgroup', '/share2-renamed', 31, []],
  312. ],
  313. [
  314. // use target of least recent share
  315. ['1', 100, 'nullgroup', '/share2-renamed', 31, []],
  316. ],
  317. true
  318. ],
  319. ];
  320. }
  321. /**
  322. * Tests merging shares.
  323. *
  324. * Happens when sharing the same entry to a user through multiple ways,
  325. * like several groups and also direct shares at the same time.
  326. *
  327. * @dataProvider mergeSharesDataProvider
  328. *
  329. * @param array $userShares array of user share specs
  330. * @param array $groupShares array of group share specs
  331. * @param array $expectedShares array of expected supershare specs
  332. */
  333. public function testMergeShares($userShares, $groupShares, $expectedShares, $moveFails = false) {
  334. $rootFolder = $this->createMock(IRootFolder::class);
  335. $userManager = $this->createMock(IUserManager::class);
  336. $userShares = array_map(function ($shareSpec) {
  337. return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4], $shareSpec[5]);
  338. }, $userShares);
  339. $groupShares = array_map(function ($shareSpec) {
  340. return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4], $shareSpec[5]);
  341. }, $groupShares);
  342. $this->user->expects($this->any())
  343. ->method('getUID')
  344. ->willReturn('user1');
  345. // tests regarding circles are made in the app itself.
  346. $circleShares = [];
  347. $roomShares = [];
  348. $deckShares = [];
  349. $sciencemeshShares = [];
  350. $this->shareManager->expects($this->exactly(6))
  351. ->method('getSharedWith')
  352. ->withConsecutive(
  353. ['user1', IShare::TYPE_USER],
  354. ['user1', IShare::TYPE_GROUP, null, -1],
  355. ['user1', IShare::TYPE_CIRCLE, null, -1],
  356. ['user1', IShare::TYPE_ROOM, null, -1],
  357. ['user1', IShare::TYPE_DECK, null, -1],
  358. ['user1', IShare::TYPE_SCIENCEMESH, null, -1],
  359. )->willReturnOnConsecutiveCalls(
  360. $userShares,
  361. $groupShares,
  362. $circleShares,
  363. $roomShares,
  364. $deckShares,
  365. $sciencemeshShares
  366. );
  367. $this->shareManager->expects($this->any())
  368. ->method('newShare')
  369. ->willReturnCallback(function () use ($rootFolder, $userManager) {
  370. return new \OC\Share20\Share($rootFolder, $userManager);
  371. });
  372. if ($moveFails) {
  373. $this->shareManager->expects($this->any())
  374. ->method('moveShare')
  375. ->will($this->throwException(new \InvalidArgumentException()));
  376. }
  377. $mounts = $this->provider->getMountsForUser($this->user, $this->loader);
  378. $this->assertCount(count($expectedShares), $mounts);
  379. foreach ($mounts as $index => $mount) {
  380. $expectedShare = $expectedShares[$index];
  381. $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mount);
  382. // supershare
  383. $share = $mount->getShare();
  384. $this->assertEquals($expectedShare[0], $share->getId());
  385. $this->assertEquals($expectedShare[1], $share->getNodeId());
  386. $this->assertEquals($expectedShare[2], $share->getShareOwner());
  387. $this->assertEquals($expectedShare[3], $share->getTarget());
  388. $this->assertEquals($expectedShare[4], $share->getPermissions());
  389. if ($expectedShare[5] === null) {
  390. $this->assertNull($share->getAttributes());
  391. } else {
  392. $this->assertEquals($expectedShare[5], $share->getAttributes()->toArray());
  393. }
  394. }
  395. }
  396. }