ManagerTest.php 27 KB

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