ManagerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_Sharing\Tests\External;
  29. use OC\Federation\CloudIdManager;
  30. use OC\Files\Storage\StorageFactory;
  31. use OCA\Files_Sharing\External\Manager;
  32. use OCA\Files_Sharing\External\MountProvider;
  33. use OCA\Files_Sharing\Tests\TestCase;
  34. use OCP\Contacts\IManager;
  35. use OCP\EventDispatcher\IEventDispatcher;
  36. use OCP\Federation\ICloudFederationFactory;
  37. use OCP\Federation\ICloudFederationProviderManager;
  38. use OCP\Http\Client\IClientService;
  39. use OCP\Http\Client\IResponse;
  40. use OCP\IGroupManager;
  41. use OCP\IUserManager;
  42. use OCP\Share\IShare;
  43. use Test\Traits\UserTrait;
  44. /**
  45. * Class ManagerTest
  46. *
  47. * @group DB
  48. *
  49. * @package OCA\Files_Sharing\Tests\External
  50. */
  51. class ManagerTest extends TestCase {
  52. use UserTrait;
  53. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  54. protected $contactsManager;
  55. /** @var Manager|\PHPUnit\Framework\MockObject\MockObject **/
  56. private $manager;
  57. /** @var \OC\Files\Mount\Manager */
  58. private $mountManager;
  59. /** @var IClientService|\PHPUnit\Framework\MockObject\MockObject */
  60. private $clientService;
  61. /** @var ICloudFederationProviderManager|\PHPUnit\Framework\MockObject\MockObject */
  62. private $cloudFederationProviderManager;
  63. /** @var ICloudFederationFactory|\PHPUnit\Framework\MockObject\MockObject */
  64. private $cloudFederationFactory;
  65. /** @var \PHPUnit\Framework\MockObject\MockObject|IGroupManager */
  66. private $groupManager;
  67. /** @var \PHPUnit\Framework\MockObject\MockObject|IUserManager */
  68. private $userManager;
  69. private $uid;
  70. /**
  71. * @var \OCP\IUser
  72. */
  73. private $user;
  74. private $testMountProvider;
  75. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  76. private $eventDispatcher;
  77. protected function setUp(): void {
  78. parent::setUp();
  79. $this->uid = $this->getUniqueID('user');
  80. $this->createUser($this->uid, '');
  81. $this->user = \OC::$server->getUserManager()->get($this->uid);
  82. $this->mountManager = new \OC\Files\Mount\Manager();
  83. $this->clientService = $this->getMockBuilder(IClientService::class)
  84. ->disableOriginalConstructor()->getMock();
  85. $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
  86. $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
  87. $this->groupManager = $this->createMock(IGroupManager::class);
  88. $this->userManager = $this->createMock(IUserManager::class);
  89. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  90. $this->contactsManager = $this->createMock(IManager::class);
  91. // needed for MountProvider() initialization
  92. $this->contactsManager->expects($this->any())
  93. ->method('search')
  94. ->willReturn([]);
  95. $this->manager = $this->getMockBuilder(Manager::class)
  96. ->setConstructorArgs(
  97. [
  98. \OC::$server->getDatabaseConnection(),
  99. $this->mountManager,
  100. new StorageFactory(),
  101. $this->clientService,
  102. \OC::$server->getNotificationManager(),
  103. \OC::$server->query(\OCP\OCS\IDiscoveryService::class),
  104. $this->cloudFederationProviderManager,
  105. $this->cloudFederationFactory,
  106. $this->groupManager,
  107. $this->userManager,
  108. $this->uid,
  109. $this->eventDispatcher,
  110. ]
  111. )->setMethods(['tryOCMEndPoint'])->getMock();
  112. $this->testMountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function () {
  113. return $this->manager;
  114. }, new CloudIdManager($this->contactsManager));
  115. }
  116. private function setupMounts() {
  117. $mounts = $this->testMountProvider->getMountsForUser($this->user, new StorageFactory());
  118. foreach ($mounts as $mount) {
  119. $this->mountManager->addMount($mount);
  120. }
  121. }
  122. public function testAddShare() {
  123. $shareData1 = [
  124. 'remote' => 'http://localhost',
  125. 'token' => 'token1',
  126. 'password' => '',
  127. 'name' => '/SharedFolder',
  128. 'owner' => 'foobar',
  129. 'shareType' => IShare::TYPE_USER,
  130. 'accepted' => false,
  131. 'user' => $this->uid,
  132. 'remote_id' => '2342'
  133. ];
  134. $shareData2 = $shareData1;
  135. $shareData2['token'] = 'token2';
  136. $shareData3 = $shareData1;
  137. $shareData3['token'] = 'token3';
  138. $this->userManager->expects($this->any())->method('get')->willReturn($this->user);
  139. $this->groupManager->expects($this->any())->method(('getUserGroups'))->willReturn([]);
  140. $this->manager->expects($this->at(0))->method('tryOCMEndPoint')->with('http://localhost', 'token1', '2342', 'accept')->willReturn(false);
  141. $this->manager->expects($this->at(1))->method('tryOCMEndPoint')->with('http://localhost', 'token3', '2342', 'decline')->willReturn(false);
  142. // Add a share for "user"
  143. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData1));
  144. $openShares = $this->manager->getOpenShares();
  145. $this->assertCount(1, $openShares);
  146. $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  147. $this->setupMounts();
  148. $this->assertNotMount('SharedFolder');
  149. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  150. // Add a second share for "user" with the same name
  151. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData2));
  152. $openShares = $this->manager->getOpenShares();
  153. $this->assertCount(2, $openShares);
  154. $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  155. // New share falls back to "-1" appendix, because the name is already taken
  156. $this->assertExternalShareEntry($shareData2, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  157. $this->setupMounts();
  158. $this->assertNotMount('SharedFolder');
  159. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  160. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  161. $client = $this->getMockBuilder('OCP\Http\Client\IClient')
  162. ->disableOriginalConstructor()->getMock();
  163. $this->clientService->expects($this->at(0))
  164. ->method('newClient')
  165. ->willReturn($client);
  166. $response = $this->createMock(IResponse::class);
  167. $response->method('getBody')
  168. ->willReturn(json_encode([
  169. 'ocs' => [
  170. 'meta' => [
  171. 'statuscode' => 200,
  172. ]
  173. ]
  174. ]));
  175. $client->expects($this->once())
  176. ->method('post')
  177. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything())
  178. ->willReturn($response);
  179. // Accept the first share
  180. $this->manager->acceptShare($openShares[0]['id']);
  181. // Check remaining shares - Accepted
  182. $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
  183. $this->assertCount(1, $acceptedShares);
  184. $shareData1['accepted'] = true;
  185. $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']);
  186. // Check remaining shares - Open
  187. $openShares = $this->manager->getOpenShares();
  188. $this->assertCount(1, $openShares);
  189. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  190. $this->setupMounts();
  191. $this->assertMount($shareData1['name']);
  192. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  193. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  194. // Add another share for "user" with the same name
  195. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData3));
  196. $openShares = $this->manager->getOpenShares();
  197. $this->assertCount(2, $openShares);
  198. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  199. // New share falls back to the original name (no "-\d", because the name is not taken)
  200. $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}');
  201. $this->setupMounts();
  202. $this->assertMount($shareData1['name']);
  203. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  204. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  205. $client = $this->getMockBuilder('OCP\Http\Client\IClient')
  206. ->disableOriginalConstructor()->getMock();
  207. $this->clientService->expects($this->at(0))
  208. ->method('newClient')
  209. ->willReturn($client);
  210. $response = $this->createMock(IResponse::class);
  211. $response->method('getBody')
  212. ->willReturn(json_encode([
  213. 'ocs' => [
  214. 'meta' => [
  215. 'statuscode' => 200,
  216. ]
  217. ]
  218. ]));
  219. $client->expects($this->once())
  220. ->method('post')
  221. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything())
  222. ->willReturn($response);
  223. // Decline the third share
  224. $this->manager->declineShare($openShares[1]['id']);
  225. $this->setupMounts();
  226. $this->assertMount($shareData1['name']);
  227. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  228. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  229. // Check remaining shares - Accepted
  230. $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
  231. $this->assertCount(1, $acceptedShares);
  232. $shareData1['accepted'] = true;
  233. $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']);
  234. // Check remaining shares - Open
  235. $openShares = $this->manager->getOpenShares();
  236. $this->assertCount(1, $openShares);
  237. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  238. $this->setupMounts();
  239. $this->assertMount($shareData1['name']);
  240. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  241. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  242. $client1 = $this->getMockBuilder('OCP\Http\Client\IClient')
  243. ->disableOriginalConstructor()->getMock();
  244. $client2 = $this->getMockBuilder('OCP\Http\Client\IClient')
  245. ->disableOriginalConstructor()->getMock();
  246. $this->clientService->expects($this->at(0))
  247. ->method('newClient')
  248. ->willReturn($client1);
  249. $this->clientService->expects($this->at(1))
  250. ->method('newClient')
  251. ->willReturn($client2);
  252. $response = $this->createMock(IResponse::class);
  253. $response->method('getBody')
  254. ->willReturn(json_encode([
  255. 'ocs' => [
  256. 'meta' => [
  257. 'statuscode' => 200,
  258. ]
  259. ]
  260. ]));
  261. $client1->expects($this->once())
  262. ->method('post')
  263. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything())
  264. ->willReturn($response);
  265. $client2->expects($this->once())
  266. ->method('post')
  267. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything())
  268. ->willReturn($response);
  269. $this->manager->removeUserShares($this->uid);
  270. $this->assertEmpty(self::invokePrivate($this->manager, 'getShares', [null]), 'Asserting all shares for the user have been deleted');
  271. $this->mountManager->clear();
  272. self::invokePrivate($this->manager, 'setupMounts');
  273. $this->assertNotMount($shareData1['name']);
  274. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  275. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  276. }
  277. /**
  278. * @param array $expected
  279. * @param array $actual
  280. * @param int $share
  281. * @param string $mountPoint
  282. */
  283. protected function assertExternalShareEntry($expected, $actual, $share, $mountPoint) {
  284. $this->assertEquals($expected['remote'], $actual['remote'], 'Asserting remote of a share #' . $share);
  285. $this->assertEquals($expected['token'], $actual['share_token'], 'Asserting token of a share #' . $share);
  286. $this->assertEquals($expected['name'], $actual['name'], 'Asserting name of a share #' . $share);
  287. $this->assertEquals($expected['owner'], $actual['owner'], 'Asserting owner of a share #' . $share);
  288. $this->assertEquals($expected['accepted'], (int) $actual['accepted'], 'Asserting accept of a share #' . $share);
  289. $this->assertEquals($expected['user'], $actual['user'], 'Asserting user of a share #' . $share);
  290. $this->assertEquals($mountPoint, $actual['mountpoint'], 'Asserting mountpoint of a share #' . $share);
  291. }
  292. private function assertMount($mountPoint) {
  293. $mountPoint = rtrim($mountPoint, '/');
  294. $mount = $this->mountManager->find($this->getFullPath($mountPoint));
  295. $this->assertInstanceOf('\OCA\Files_Sharing\External\Mount', $mount);
  296. $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
  297. $this->assertEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
  298. $storage = $mount->getStorage();
  299. $this->assertInstanceOf('\OCA\Files_Sharing\External\Storage', $storage);
  300. }
  301. private function assertNotMount($mountPoint) {
  302. $mountPoint = rtrim($mountPoint, '/');
  303. $mount = $this->mountManager->find($this->getFullPath($mountPoint));
  304. if ($mount) {
  305. $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
  306. $this->assertNotEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
  307. } else {
  308. $this->assertNull($mount);
  309. }
  310. }
  311. private function getFullPath($path) {
  312. return '/' . $this->uid . '/files' . $path;
  313. }
  314. }