1
0

ManagerTest.php 28 KB

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