MountProviderTest.php 12 KB

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