MountProviderTest.php 12 KB

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