MountProviderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. // tests regarding circles are made in the app itself.
  104. $circleShares = [];
  105. $this->user->expects($this->any())
  106. ->method('getUID')
  107. ->will($this->returnValue('user1'));
  108. $this->shareManager->expects($this->at(0))
  109. ->method('getSharedWith')
  110. ->with('user1', \OCP\Share::SHARE_TYPE_USER)
  111. ->will($this->returnValue($userShares));
  112. $this->shareManager->expects($this->at(1))
  113. ->method('getSharedWith')
  114. ->with('user1', \OCP\Share::SHARE_TYPE_GROUP, null, -1)
  115. ->will($this->returnValue($groupShares));
  116. $this->shareManager->expects($this->at(2))
  117. ->method('getSharedWith')
  118. ->with('user1', \OCP\Share::SHARE_TYPE_CIRCLE, null, -1)
  119. ->will($this->returnValue($circleShares));
  120. $this->shareManager->expects($this->any())
  121. ->method('newShare')
  122. ->will($this->returnCallback(function() use ($rootFolder, $userManager) {
  123. return new \OC\Share20\Share($rootFolder, $userManager);
  124. }));
  125. $mounts = $this->provider->getMountsForUser($this->user, $this->loader);
  126. $this->assertCount(2, $mounts);
  127. $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[0]);
  128. $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[1]);
  129. $mountedShare1 = $mounts[0]->getShare();
  130. $this->assertEquals('2', $mountedShare1->getId());
  131. $this->assertEquals('user2', $mountedShare1->getShareOwner());
  132. $this->assertEquals(100, $mountedShare1->getNodeId());
  133. $this->assertEquals('/share2', $mountedShare1->getTarget());
  134. $this->assertEquals(31, $mountedShare1->getPermissions());
  135. $mountedShare2 = $mounts[1]->getShare();
  136. $this->assertEquals('4', $mountedShare2->getId());
  137. $this->assertEquals('user2', $mountedShare2->getShareOwner());
  138. $this->assertEquals(101, $mountedShare2->getNodeId());
  139. $this->assertEquals('/share4', $mountedShare2->getTarget());
  140. $this->assertEquals(31, $mountedShare2->getPermissions());
  141. }
  142. public function mergeSharesDataProvider() {
  143. // note: the user in the specs here is the shareOwner not recipient
  144. // the recipient is always "user1"
  145. return [
  146. // #0: share as outsider with "group1" and "user1" with same permissions
  147. [
  148. [
  149. [1, 100, 'user2', '/share2', 31],
  150. ],
  151. [
  152. [2, 100, 'user2', '/share2', 31],
  153. ],
  154. [
  155. // combined, user share has higher priority
  156. ['1', 100, 'user2', '/share2', 31],
  157. ],
  158. ],
  159. // #1: share as outsider with "group1" and "user1" with different permissions
  160. [
  161. [
  162. [1, 100, 'user2', '/share', 31],
  163. ],
  164. [
  165. [2, 100, 'user2', '/share', 15],
  166. ],
  167. [
  168. // use highest permissions
  169. ['1', 100, 'user2', '/share', 31],
  170. ],
  171. ],
  172. // #2: share as outsider with "group1" and "group2" with same permissions
  173. [
  174. [
  175. ],
  176. [
  177. [1, 100, 'user2', '/share', 31],
  178. [2, 100, 'user2', '/share', 31],
  179. ],
  180. [
  181. // combined, first group share has higher priority
  182. ['1', 100, 'user2', '/share', 31],
  183. ],
  184. ],
  185. // #3: share as outsider with "group1" and "group2" with different permissions
  186. [
  187. [
  188. ],
  189. [
  190. [1, 100, 'user2', '/share', 31],
  191. [2, 100, 'user2', '/share', 15],
  192. ],
  193. [
  194. // use higher permissions
  195. ['1', 100, 'user2', '/share', 31],
  196. ],
  197. ],
  198. // #4: share as insider with "group1"
  199. [
  200. [
  201. ],
  202. [
  203. [1, 100, 'user1', '/share', 31],
  204. ],
  205. [
  206. // no received share since "user1" is the sharer/owner
  207. ],
  208. ],
  209. // #5: share as insider with "group1" and "group2" with different permissions
  210. [
  211. [
  212. ],
  213. [
  214. [1, 100, 'user1', '/share', 31],
  215. [2, 100, 'user1', '/share', 15],
  216. ],
  217. [
  218. // no received share since "user1" is the sharer/owner
  219. ],
  220. ],
  221. // #6: share as outside with "group1", recipient opted out
  222. [
  223. [
  224. ],
  225. [
  226. [1, 100, 'user2', '/share', 0],
  227. ],
  228. [
  229. // no received share since "user1" opted out
  230. ],
  231. ],
  232. // #7: share as outsider with "group1" and "user1" where recipient renamed in between
  233. [
  234. [
  235. [1, 100, 'user2', '/share2-renamed', 31],
  236. ],
  237. [
  238. [2, 100, 'user2', '/share2', 31],
  239. ],
  240. [
  241. // use target of least recent share
  242. ['1', 100, 'user2', '/share2-renamed', 31],
  243. ],
  244. ],
  245. // #8: share as outsider with "group1" and "user1" where recipient renamed in between
  246. [
  247. [
  248. [2, 100, 'user2', '/share2', 31],
  249. ],
  250. [
  251. [1, 100, 'user2', '/share2-renamed', 31],
  252. ],
  253. [
  254. // use target of least recent share
  255. ['1', 100, 'user2', '/share2-renamed', 31],
  256. ],
  257. ],
  258. // #9: share as outsider with "nullgroup" and "user1" where recipient renamed in between
  259. [
  260. [
  261. [2, 100, 'user2', '/share2', 31],
  262. ],
  263. [
  264. [1, 100, 'nullgroup', '/share2-renamed', 31],
  265. ],
  266. [
  267. // use target of least recent share
  268. ['1', 100, 'nullgroup', '/share2-renamed', 31],
  269. ],
  270. true
  271. ],
  272. ];
  273. }
  274. /**
  275. * Tests merging shares.
  276. *
  277. * Happens when sharing the same entry to a user through multiple ways,
  278. * like several groups and also direct shares at the same time.
  279. *
  280. * @dataProvider mergeSharesDataProvider
  281. *
  282. * @param array $userShares array of user share specs
  283. * @param array $groupShares array of group share specs
  284. * @param array $expectedShares array of expected supershare specs
  285. */
  286. public function testMergeShares($userShares, $groupShares, $expectedShares, $moveFails = false) {
  287. $rootFolder = $this->createMock(IRootFolder::class);
  288. $userManager = $this->createMock(IUserManager::class);
  289. $userShares = array_map(function($shareSpec) {
  290. return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4]);
  291. }, $userShares);
  292. $groupShares = array_map(function($shareSpec) {
  293. return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4]);
  294. }, $groupShares);
  295. $this->user->expects($this->any())
  296. ->method('getUID')
  297. ->will($this->returnValue('user1'));
  298. // tests regarding circles are made in the app itself.
  299. $circleShares = [];
  300. $this->shareManager->expects($this->at(0))
  301. ->method('getSharedWith')
  302. ->with('user1', \OCP\Share::SHARE_TYPE_USER)
  303. ->will($this->returnValue($userShares));
  304. $this->shareManager->expects($this->at(1))
  305. ->method('getSharedWith')
  306. ->with('user1', \OCP\Share::SHARE_TYPE_GROUP, null, -1)
  307. ->will($this->returnValue($groupShares));
  308. $this->shareManager->expects($this->at(2))
  309. ->method('getSharedWith')
  310. ->with('user1', \OCP\Share::SHARE_TYPE_CIRCLE, null, -1)
  311. ->will($this->returnValue($circleShares));
  312. $this->shareManager->expects($this->any())
  313. ->method('newShare')
  314. ->will($this->returnCallback(function() use ($rootFolder, $userManager) {
  315. return new \OC\Share20\Share($rootFolder, $userManager);
  316. }));
  317. if ($moveFails) {
  318. $this->shareManager->expects($this->any())
  319. ->method('moveShare')
  320. ->will($this->throwException(new \InvalidArgumentException()));
  321. }
  322. $mounts = $this->provider->getMountsForUser($this->user, $this->loader);
  323. $this->assertCount(count($expectedShares), $mounts);
  324. foreach ($mounts as $index => $mount) {
  325. $expectedShare = $expectedShares[$index];
  326. $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mount);
  327. // supershare
  328. $share = $mount->getShare();
  329. $this->assertEquals($expectedShare[0], $share->getId());
  330. $this->assertEquals($expectedShare[1], $share->getNodeId());
  331. $this->assertEquals($expectedShare[2], $share->getShareOwner());
  332. $this->assertEquals($expectedShare[3], $share->getTarget());
  333. $this->assertEquals($expectedShare[4], $share->getPermissions());
  334. }
  335. }
  336. }