ManagerTest.php 28 KB

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