MountProviderTest.php 12 KB

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