1
0

MountProviderTest.php 13 KB

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