ManagerTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests\External;
  8. use OC\Federation\CloudIdManager;
  9. use OC\Files\Mount\MountPoint;
  10. use OC\Files\SetupManagerFactory;
  11. use OC\Files\Storage\StorageFactory;
  12. use OC\Files\Storage\Temporary;
  13. use OCA\Files_Sharing\External\Manager;
  14. use OCA\Files_Sharing\External\MountProvider;
  15. use OCA\Files_Sharing\Tests\TestCase;
  16. use OCP\Contacts\IManager;
  17. use OCP\EventDispatcher\IEventDispatcher;
  18. use OCP\Federation\ICloudFederationFactory;
  19. use OCP\Federation\ICloudFederationProviderManager;
  20. use OCP\Files\NotFoundException;
  21. use OCP\Http\Client\IClientService;
  22. use OCP\Http\Client\IResponse;
  23. use OCP\ICacheFactory;
  24. use OCP\IGroup;
  25. use OCP\IGroupManager;
  26. use OCP\IURLGenerator;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  29. use OCP\IUserSession;
  30. use OCP\OCS\IDiscoveryService;
  31. use OCP\Share\IShare;
  32. use Psr\Log\LoggerInterface;
  33. use Test\Traits\UserTrait;
  34. /**
  35. * Class ManagerTest
  36. *
  37. * @group DB
  38. *
  39. * @package OCA\Files_Sharing\Tests\External
  40. */
  41. class ManagerTest extends TestCase {
  42. use UserTrait;
  43. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  44. protected $contactsManager;
  45. /** @var Manager|\PHPUnit\Framework\MockObject\MockObject * */
  46. private $manager;
  47. /** @var \OC\Files\Mount\Manager */
  48. private $mountManager;
  49. /** @var IClientService|\PHPUnit\Framework\MockObject\MockObject */
  50. private $clientService;
  51. /** @var ICloudFederationProviderManager|\PHPUnit\Framework\MockObject\MockObject */
  52. private $cloudFederationProviderManager;
  53. /** @var ICloudFederationFactory|\PHPUnit\Framework\MockObject\MockObject */
  54. private $cloudFederationFactory;
  55. /** @var \PHPUnit\Framework\MockObject\MockObject|IGroupManager */
  56. private $groupManager;
  57. /** @var \PHPUnit\Framework\MockObject\MockObject|IUserManager */
  58. private $userManager;
  59. /** @var LoggerInterface */
  60. private $logger;
  61. private $uid;
  62. /**
  63. * @var IUser
  64. */
  65. private $user;
  66. private $testMountProvider;
  67. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  68. private $eventDispatcher;
  69. protected function setUp(): void {
  70. parent::setUp();
  71. $this->uid = $this->getUniqueID('user');
  72. $this->user = $this->createUser($this->uid, '');
  73. $this->mountManager = new \OC\Files\Mount\Manager($this->createMock(SetupManagerFactory::class));
  74. $this->clientService = $this->getMockBuilder(IClientService::class)
  75. ->disableOriginalConstructor()->getMock();
  76. $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
  77. $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
  78. $this->groupManager = $this->createMock(IGroupManager::class);
  79. $this->userManager = $this->createMock(IUserManager::class);
  80. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  81. $this->contactsManager = $this->createMock(IManager::class);
  82. // needed for MountProvider() initialization
  83. $this->contactsManager->expects($this->any())
  84. ->method('search')
  85. ->willReturn([]);
  86. $this->logger = $this->createMock(LoggerInterface::class);
  87. $this->logger->expects($this->never())->method('emergency');
  88. $this->manager = $this->createManagerForUser($this->uid);
  89. $this->testMountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function () {
  90. return $this->manager;
  91. }, new CloudIdManager(
  92. $this->contactsManager,
  93. $this->createMock(IURLGenerator::class),
  94. $this->userManager,
  95. $this->createMock(ICacheFactory::class),
  96. $this->createMock(IEventDispatcher::class)
  97. ));
  98. $group1 = $this->createMock(IGroup::class);
  99. $group1->expects($this->any())->method('getGID')->willReturn('group1');
  100. $group1->expects($this->any())->method('inGroup')->with($this->user)->willReturn(true);
  101. $group2 = $this->createMock(IGroup::class);
  102. $group2->expects($this->any())->method('getGID')->willReturn('group2');
  103. $group2->expects($this->any())->method('inGroup')->with($this->user)->willReturn(true);
  104. $this->userManager->expects($this->any())->method('get')->willReturn($this->user);
  105. $this->groupManager->expects($this->any())->method(('getUserGroups'))->willReturn([$group1, $group2]);
  106. $this->groupManager->expects($this->any())->method(('get'))
  107. ->will($this->returnValueMap([
  108. ['group1', $group1],
  109. ['group2', $group2],
  110. ]));
  111. }
  112. protected function tearDown(): void {
  113. // clear the share external table to avoid side effects
  114. $query = \OC::$server->getDatabaseConnection()->prepare('DELETE FROM `*PREFIX*share_external`');
  115. $result = $query->execute();
  116. $result->closeCursor();
  117. parent::tearDown();
  118. }
  119. private function createManagerForUser($userId) {
  120. $user = $this->createMock(IUser::class);
  121. $user->method('getUID')
  122. ->willReturn($userId);
  123. $userSession = $this->createMock(IUserSession::class);
  124. $userSession->method('getUser')
  125. ->willReturn($user);
  126. return $this->getMockBuilder(Manager::class)
  127. ->setConstructorArgs(
  128. [
  129. \OC::$server->getDatabaseConnection(),
  130. $this->mountManager,
  131. new StorageFactory(),
  132. $this->clientService,
  133. \OC::$server->getNotificationManager(),
  134. \OC::$server->query(IDiscoveryService::class),
  135. $this->cloudFederationProviderManager,
  136. $this->cloudFederationFactory,
  137. $this->groupManager,
  138. $this->userManager,
  139. $userSession,
  140. $this->eventDispatcher,
  141. $this->logger,
  142. ]
  143. )->setMethods(['tryOCMEndPoint'])->getMock();
  144. }
  145. private function setupMounts() {
  146. $this->clearMounts();
  147. $mounts = $this->testMountProvider->getMountsForUser($this->user, new StorageFactory());
  148. foreach ($mounts as $mount) {
  149. $this->mountManager->addMount($mount);
  150. }
  151. }
  152. private function clearMounts() {
  153. $this->mountManager->clear();
  154. $this->mountManager->addMount(new MountPoint(Temporary::class, '', []));
  155. }
  156. public function testAddUserShare(): void {
  157. $this->doTestAddShare([
  158. 'remote' => 'http://localhost',
  159. 'token' => 'token1',
  160. 'password' => '',
  161. 'name' => '/SharedFolder',
  162. 'owner' => 'foobar',
  163. 'shareType' => IShare::TYPE_USER,
  164. 'accepted' => false,
  165. 'user' => $this->uid,
  166. 'remoteId' => '2342'
  167. ], false);
  168. }
  169. public function testAddGroupShare(): void {
  170. $this->doTestAddShare([
  171. 'remote' => 'http://localhost',
  172. 'token' => 'token1',
  173. 'password' => '',
  174. 'name' => '/SharedFolder',
  175. 'owner' => 'foobar',
  176. 'shareType' => IShare::TYPE_GROUP,
  177. 'accepted' => false,
  178. 'user' => 'group1',
  179. 'remoteId' => '2342'
  180. ], true);
  181. }
  182. public function doTestAddShare($shareData1, $isGroup = false) {
  183. $shareData2 = $shareData1;
  184. $shareData2['token'] = 'token2';
  185. $shareData3 = $shareData1;
  186. $shareData3['token'] = 'token3';
  187. if ($isGroup) {
  188. $this->manager->expects($this->never())->method('tryOCMEndPoint');
  189. } else {
  190. $this->manager->method('tryOCMEndPoint')
  191. ->withConsecutive(
  192. ['http://localhost', 'token1', '2342', 'accept'],
  193. ['http://localhost', 'token3', '2342', 'decline'],
  194. )->willReturnOnConsecutiveCalls(
  195. false,
  196. false,
  197. );
  198. }
  199. // Add a share for "user"
  200. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData1));
  201. $openShares = $this->manager->getOpenShares();
  202. $this->assertCount(1, $openShares);
  203. $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}', $shareData1['user']);
  204. $this->setupMounts();
  205. $this->assertNotMount('SharedFolder');
  206. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  207. // Add a second share for "user" with the same name
  208. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData2));
  209. $openShares = $this->manager->getOpenShares();
  210. $this->assertCount(2, $openShares);
  211. $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}', $shareData1['user']);
  212. // New share falls back to "-1" appendix, because the name is already taken
  213. $this->assertExternalShareEntry($shareData2, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']);
  214. $this->setupMounts();
  215. $this->assertNotMount('SharedFolder');
  216. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  217. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  218. if (!$isGroup) {
  219. $client = $this->getMockBuilder('OCP\Http\Client\IClient')
  220. ->disableOriginalConstructor()->getMock();
  221. $this->clientService->expects($this->at(0))
  222. ->method('newClient')
  223. ->willReturn($client);
  224. $response = $this->createMock(IResponse::class);
  225. $response->method('getBody')
  226. ->willReturn(json_encode([
  227. 'ocs' => [
  228. 'meta' => [
  229. 'statuscode' => 200,
  230. ]
  231. ]
  232. ]));
  233. $client->expects($this->once())
  234. ->method('post')
  235. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything())
  236. ->willReturn($response);
  237. }
  238. // Accept the first share
  239. $this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
  240. // Check remaining shares - Accepted
  241. $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
  242. $this->assertCount(1, $acceptedShares);
  243. $shareData1['accepted'] = true;
  244. $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name'], $this->uid);
  245. // Check remaining shares - Open
  246. $openShares = $this->manager->getOpenShares();
  247. $this->assertCount(1, $openShares);
  248. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']);
  249. $this->setupMounts();
  250. $this->assertMount($shareData1['name']);
  251. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  252. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  253. // Add another share for "user" with the same name
  254. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData3));
  255. $openShares = $this->manager->getOpenShares();
  256. $this->assertCount(2, $openShares);
  257. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']);
  258. if (!$isGroup) {
  259. // New share falls back to the original name (no "-\d", because the name is not taken)
  260. $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}', $shareData3['user']);
  261. } else {
  262. $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}-2', $shareData3['user']);
  263. }
  264. $this->setupMounts();
  265. $this->assertMount($shareData1['name']);
  266. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  267. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  268. if (!$isGroup) {
  269. $client = $this->getMockBuilder('OCP\Http\Client\IClient')
  270. ->disableOriginalConstructor()->getMock();
  271. $this->clientService->expects($this->at(0))
  272. ->method('newClient')
  273. ->willReturn($client);
  274. $response = $this->createMock(IResponse::class);
  275. $response->method('getBody')
  276. ->willReturn(json_encode([
  277. 'ocs' => [
  278. 'meta' => [
  279. 'statuscode' => 200,
  280. ]
  281. ]
  282. ]));
  283. $client->expects($this->once())
  284. ->method('post')
  285. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything())
  286. ->willReturn($response);
  287. }
  288. // Decline the third share
  289. $this->assertTrue($this->manager->declineShare($openShares[1]['id']));
  290. $this->setupMounts();
  291. $this->assertMount($shareData1['name']);
  292. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  293. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  294. // Check remaining shares - Accepted
  295. $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
  296. $this->assertCount(1, $acceptedShares);
  297. $shareData1['accepted'] = true;
  298. $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name'], $this->uid);
  299. // Check remaining shares - Open
  300. $openShares = $this->manager->getOpenShares();
  301. if ($isGroup) {
  302. // declining a group share adds it back to pending instead of deleting it
  303. $this->assertCount(2, $openShares);
  304. // this is a group share that is still open
  305. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']);
  306. // this is the user share sub-entry matching the group share which got declined
  307. $this->assertExternalShareEntry($shareData3, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}-2', $this->uid);
  308. } else {
  309. $this->assertCount(1, $openShares);
  310. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $this->uid);
  311. }
  312. $this->setupMounts();
  313. $this->assertMount($shareData1['name']);
  314. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  315. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  316. if ($isGroup) {
  317. // no http requests here
  318. $this->manager->removeGroupShares('group1');
  319. } else {
  320. $client1 = $this->getMockBuilder('OCP\Http\Client\IClient')
  321. ->disableOriginalConstructor()->getMock();
  322. $client2 = $this->getMockBuilder('OCP\Http\Client\IClient')
  323. ->disableOriginalConstructor()->getMock();
  324. $this->clientService->expects($this->exactly(2))
  325. ->method('newClient')
  326. ->willReturnOnConsecutiveCalls(
  327. $client1,
  328. $client2,
  329. );
  330. $response = $this->createMock(IResponse::class);
  331. $response->method('getBody')
  332. ->willReturn(json_encode([
  333. 'ocs' => [
  334. 'meta' => [
  335. 'statuscode' => 200,
  336. ]
  337. ]
  338. ]));
  339. $client1->expects($this->once())
  340. ->method('post')
  341. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything())
  342. ->willReturn($response);
  343. $client2->expects($this->once())
  344. ->method('post')
  345. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything())
  346. ->willReturn($response);
  347. $this->manager->removeUserShares($this->uid);
  348. }
  349. $this->assertEmpty(self::invokePrivate($this->manager, 'getShares', [null]), 'Asserting all shares for the user have been deleted');
  350. $this->clearMounts();
  351. self::invokePrivate($this->manager, 'setupMounts');
  352. $this->assertNotMount($shareData1['name']);
  353. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  354. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  355. }
  356. private function verifyAcceptedGroupShare($shareData) {
  357. $openShares = $this->manager->getOpenShares();
  358. $this->assertCount(0, $openShares);
  359. $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
  360. $this->assertCount(1, $acceptedShares);
  361. $shareData['accepted'] = true;
  362. $this->assertExternalShareEntry($shareData, $acceptedShares[0], 0, $shareData['name'], $this->uid);
  363. $this->setupMounts();
  364. $this->assertMount($shareData['name']);
  365. }
  366. private function verifyDeclinedGroupShare($shareData, $tempMount = null) {
  367. if ($tempMount === null) {
  368. $tempMount = '{{TemporaryMountPointName#/SharedFolder}}';
  369. }
  370. $openShares = $this->manager->getOpenShares();
  371. $this->assertCount(1, $openShares);
  372. $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
  373. $this->assertCount(0, $acceptedShares);
  374. $this->assertExternalShareEntry($shareData, $openShares[0], 0, $tempMount, $this->uid);
  375. $this->setupMounts();
  376. $this->assertNotMount($shareData['name']);
  377. $this->assertNotMount($tempMount);
  378. }
  379. private function createTestUserShare($userId = 'user1') {
  380. $shareData = [
  381. 'remote' => 'http://localhost',
  382. 'token' => 'token1',
  383. 'password' => '',
  384. 'name' => '/SharedFolder',
  385. 'owner' => 'foobar',
  386. 'shareType' => IShare::TYPE_USER,
  387. 'accepted' => false,
  388. 'user' => $userId,
  389. 'remoteId' => '2342'
  390. ];
  391. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData));
  392. return $shareData;
  393. }
  394. private function createTestGroupShare($groupId = 'group1') {
  395. $shareData = [
  396. 'remote' => 'http://localhost',
  397. 'token' => 'token1',
  398. 'password' => '',
  399. 'name' => '/SharedFolder',
  400. 'owner' => 'foobar',
  401. 'shareType' => IShare::TYPE_GROUP,
  402. 'accepted' => false,
  403. 'user' => $groupId,
  404. 'remoteId' => '2342'
  405. ];
  406. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData));
  407. $allShares = self::invokePrivate($this->manager, 'getShares', [null]);
  408. foreach ($allShares as $share) {
  409. if ($share['user'] === $groupId) {
  410. // this will hold the main group entry
  411. $groupShare = $share;
  412. break;
  413. }
  414. }
  415. return [$shareData, $groupShare];
  416. }
  417. public function testAcceptOriginalGroupShare(): void {
  418. [$shareData, $groupShare] = $this->createTestGroupShare();
  419. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  420. $this->verifyAcceptedGroupShare($shareData);
  421. // a second time
  422. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  423. $this->verifyAcceptedGroupShare($shareData);
  424. }
  425. public function testAcceptGroupShareAgainThroughGroupShare(): void {
  426. [$shareData, $groupShare] = $this->createTestGroupShare();
  427. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  428. $this->verifyAcceptedGroupShare($shareData);
  429. // decline again, this keeps the sub-share
  430. $this->assertTrue($this->manager->declineShare($groupShare['id']));
  431. $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
  432. // this will return sub-entries
  433. $openShares = $this->manager->getOpenShares();
  434. $this->assertCount(1, $openShares);
  435. // accept through group share
  436. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  437. $this->verifyAcceptedGroupShare($shareData, '/SharedFolder');
  438. // accept a second time
  439. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  440. $this->verifyAcceptedGroupShare($shareData, '/SharedFolder');
  441. }
  442. public function testAcceptGroupShareAgainThroughSubShare(): void {
  443. [$shareData, $groupShare] = $this->createTestGroupShare();
  444. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  445. $this->verifyAcceptedGroupShare($shareData);
  446. // decline again, this keeps the sub-share
  447. $this->assertTrue($this->manager->declineShare($groupShare['id']));
  448. $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
  449. // this will return sub-entries
  450. $openShares = $this->manager->getOpenShares();
  451. $this->assertCount(1, $openShares);
  452. // accept through sub-share
  453. $this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
  454. $this->verifyAcceptedGroupShare($shareData);
  455. // accept a second time
  456. $this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
  457. $this->verifyAcceptedGroupShare($shareData);
  458. }
  459. public function testDeclineOriginalGroupShare(): void {
  460. [$shareData, $groupShare] = $this->createTestGroupShare();
  461. $this->assertTrue($this->manager->declineShare($groupShare['id']));
  462. $this->verifyDeclinedGroupShare($shareData);
  463. // a second time
  464. $this->assertTrue($this->manager->declineShare($groupShare['id']));
  465. $this->verifyDeclinedGroupShare($shareData);
  466. }
  467. public function testDeclineGroupShareAgainThroughGroupShare(): void {
  468. [$shareData, $groupShare] = $this->createTestGroupShare();
  469. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  470. $this->verifyAcceptedGroupShare($shareData);
  471. // decline again, this keeps the sub-share
  472. $this->assertTrue($this->manager->declineShare($groupShare['id']));
  473. $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
  474. // a second time
  475. $this->assertTrue($this->manager->declineShare($groupShare['id']));
  476. $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
  477. }
  478. public function testDeclineGroupShareAgainThroughSubshare(): void {
  479. [$shareData, $groupShare] = $this->createTestGroupShare();
  480. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  481. $this->verifyAcceptedGroupShare($shareData);
  482. // this will return sub-entries
  483. $allShares = self::invokePrivate($this->manager, 'getShares', [null]);
  484. $this->assertCount(1, $allShares);
  485. // decline again through sub-share
  486. $this->assertTrue($this->manager->declineShare($allShares[0]['id']));
  487. $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
  488. // a second time
  489. $this->assertTrue($this->manager->declineShare($allShares[0]['id']));
  490. $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
  491. }
  492. public function testDeclineGroupShareAgainThroughMountPoint(): void {
  493. [$shareData, $groupShare] = $this->createTestGroupShare();
  494. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  495. $this->verifyAcceptedGroupShare($shareData);
  496. // decline through mount point name
  497. $this->assertTrue($this->manager->removeShare($this->uid . '/files/' . $shareData['name']));
  498. $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
  499. // second time must fail as the mount point is gone
  500. $this->assertFalse($this->manager->removeShare($this->uid . '/files/' . $shareData['name']));
  501. }
  502. public function testDeclineThenAcceptGroupShareAgainThroughGroupShare(): void {
  503. [$shareData, $groupShare] = $this->createTestGroupShare();
  504. // decline, this creates a declined sub-share
  505. $this->assertTrue($this->manager->declineShare($groupShare['id']));
  506. $this->verifyDeclinedGroupShare($shareData);
  507. // this will return sub-entries
  508. $openShares = $this->manager->getOpenShares();
  509. // accept through sub-share
  510. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  511. $this->verifyAcceptedGroupShare($shareData, '/SharedFolder');
  512. // accept a second time
  513. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  514. $this->verifyAcceptedGroupShare($shareData, '/SharedFolder');
  515. }
  516. public function testDeclineThenAcceptGroupShareAgainThroughSubShare(): void {
  517. [$shareData, $groupShare] = $this->createTestGroupShare();
  518. // decline, this creates a declined sub-share
  519. $this->assertTrue($this->manager->declineShare($groupShare['id']));
  520. $this->verifyDeclinedGroupShare($shareData);
  521. // this will return sub-entries
  522. $openShares = $this->manager->getOpenShares();
  523. // accept through sub-share
  524. $this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
  525. $this->verifyAcceptedGroupShare($shareData);
  526. // accept a second time
  527. $this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
  528. $this->verifyAcceptedGroupShare($shareData);
  529. }
  530. public function testDeleteUserShares(): void {
  531. // user 1 shares
  532. $shareData = $this->createTestUserShare($this->uid);
  533. [$shareData, $groupShare] = $this->createTestGroupShare();
  534. $shares = $this->manager->getOpenShares();
  535. $this->assertCount(2, $shares);
  536. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  537. // user 2 shares
  538. $manager2 = $this->createManagerForUser('user2');
  539. $shareData2 = [
  540. 'remote' => 'http://localhost',
  541. 'token' => 'token1',
  542. 'password' => '',
  543. 'name' => '/SharedFolder',
  544. 'owner' => 'foobar',
  545. 'shareType' => IShare::TYPE_USER,
  546. 'accepted' => false,
  547. 'user' => 'user2',
  548. 'remoteId' => '2342'
  549. ];
  550. $this->assertSame(null, call_user_func_array([$manager2, 'addShare'], $shareData2));
  551. $user2Shares = $manager2->getOpenShares();
  552. $this->assertCount(2, $user2Shares);
  553. $this->manager->expects($this->once())->method('tryOCMEndPoint')->with('http://localhost', 'token1', '2342', 'decline')->willReturn([]);
  554. $this->manager->removeUserShares($this->uid);
  555. $user1Shares = $this->manager->getOpenShares();
  556. // user share is gone, group is still there
  557. $this->assertCount(1, $user1Shares);
  558. $this->assertEquals($user1Shares[0]['share_type'], IShare::TYPE_GROUP);
  559. // user 2 shares untouched
  560. $user2Shares = $manager2->getOpenShares();
  561. $this->assertCount(2, $user2Shares);
  562. $this->assertEquals($user2Shares[0]['share_type'], IShare::TYPE_GROUP);
  563. $this->assertEquals($user2Shares[0]['user'], 'group1');
  564. $this->assertEquals($user2Shares[1]['share_type'], IShare::TYPE_USER);
  565. $this->assertEquals($user2Shares[1]['user'], 'user2');
  566. }
  567. public function testDeleteGroupShares(): void {
  568. $shareData = $this->createTestUserShare($this->uid);
  569. [$shareData, $groupShare] = $this->createTestGroupShare();
  570. $shares = $this->manager->getOpenShares();
  571. $this->assertCount(2, $shares);
  572. $this->assertTrue($this->manager->acceptShare($groupShare['id']));
  573. // user 2 shares
  574. $manager2 = $this->createManagerForUser('user2');
  575. $shareData2 = [
  576. 'remote' => 'http://localhost',
  577. 'token' => 'token1',
  578. 'password' => '',
  579. 'name' => '/SharedFolder',
  580. 'owner' => 'foobar',
  581. 'shareType' => IShare::TYPE_USER,
  582. 'accepted' => false,
  583. 'user' => 'user2',
  584. 'remoteId' => '2342'
  585. ];
  586. $this->assertSame(null, call_user_func_array([$manager2, 'addShare'], $shareData2));
  587. $user2Shares = $manager2->getOpenShares();
  588. $this->assertCount(2, $user2Shares);
  589. $this->manager->expects($this->never())->method('tryOCMEndPoint');
  590. $this->manager->removeGroupShares('group1');
  591. $user1Shares = $this->manager->getOpenShares();
  592. // user share is gone, group is still there
  593. $this->assertCount(1, $user1Shares);
  594. $this->assertEquals($user1Shares[0]['share_type'], IShare::TYPE_USER);
  595. // user 2 shares untouched
  596. $user2Shares = $manager2->getOpenShares();
  597. $this->assertCount(1, $user2Shares);
  598. $this->assertEquals($user2Shares[0]['share_type'], IShare::TYPE_USER);
  599. $this->assertEquals($user2Shares[0]['user'], 'user2');
  600. }
  601. /**
  602. * @param array $expected
  603. * @param array $actual
  604. * @param int $share
  605. * @param string $mountPoint
  606. */
  607. protected function assertExternalShareEntry($expected, $actual, $share, $mountPoint, $targetEntity) {
  608. $this->assertEquals($expected['remote'], $actual['remote'], 'Asserting remote of a share #' . $share);
  609. $this->assertEquals($expected['token'], $actual['share_token'], 'Asserting token of a share #' . $share);
  610. $this->assertEquals($expected['name'], $actual['name'], 'Asserting name of a share #' . $share);
  611. $this->assertEquals($expected['owner'], $actual['owner'], 'Asserting owner of a share #' . $share);
  612. $this->assertEquals($expected['accepted'], (int)$actual['accepted'], 'Asserting accept of a share #' . $share);
  613. $this->assertEquals($targetEntity, $actual['user'], 'Asserting user of a share #' . $share);
  614. $this->assertEquals($mountPoint, $actual['mountpoint'], 'Asserting mountpoint of a share #' . $share);
  615. }
  616. private function assertMount($mountPoint) {
  617. $mountPoint = rtrim($mountPoint, '/');
  618. $mount = $this->mountManager->find($this->getFullPath($mountPoint));
  619. $this->assertInstanceOf('\OCA\Files_Sharing\External\Mount', $mount);
  620. $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
  621. $this->assertEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
  622. $storage = $mount->getStorage();
  623. $this->assertInstanceOf('\OCA\Files_Sharing\External\Storage', $storage);
  624. }
  625. private function assertNotMount($mountPoint) {
  626. $mountPoint = rtrim($mountPoint, '/');
  627. try {
  628. $mount = $this->mountManager->find($this->getFullPath($mountPoint));
  629. $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
  630. $this->assertNotEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
  631. } catch (NotFoundException $e) {
  632. }
  633. }
  634. private function getFullPath($path) {
  635. return '/' . $this->uid . '/files' . $path;
  636. }
  637. }