MountProviderTest.php 14 KB

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