1
0

MountProviderTest.php 15 KB

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