You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
12 KiB

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