You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ManagerTest.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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\Storage\StorageFactory;
  34. use OCA\Files_Sharing\External\Manager;
  35. use OCA\Files_Sharing\External\MountProvider;
  36. use OCA\Files_Sharing\Tests\TestCase;
  37. use OCP\Contacts\IManager;
  38. use OCP\EventDispatcher\IEventDispatcher;
  39. use OCP\Federation\ICloudFederationFactory;
  40. use OCP\Federation\ICloudFederationProviderManager;
  41. use OCP\Http\Client\IClientService;
  42. use OCP\Http\Client\IResponse;
  43. use OCP\IGroupManager;
  44. use OCP\IURLGenerator;
  45. use OCP\IUserManager;
  46. use OCP\Share\IShare;
  47. use Test\Traits\UserTrait;
  48. /**
  49. * Class ManagerTest
  50. *
  51. * @group DB
  52. *
  53. * @package OCA\Files_Sharing\Tests\External
  54. */
  55. class ManagerTest extends TestCase {
  56. use UserTrait;
  57. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  58. protected $contactsManager;
  59. /** @var Manager|\PHPUnit\Framework\MockObject\MockObject **/
  60. private $manager;
  61. /** @var \OC\Files\Mount\Manager */
  62. private $mountManager;
  63. /** @var IClientService|\PHPUnit\Framework\MockObject\MockObject */
  64. private $clientService;
  65. /** @var ICloudFederationProviderManager|\PHPUnit\Framework\MockObject\MockObject */
  66. private $cloudFederationProviderManager;
  67. /** @var ICloudFederationFactory|\PHPUnit\Framework\MockObject\MockObject */
  68. private $cloudFederationFactory;
  69. /** @var \PHPUnit\Framework\MockObject\MockObject|IGroupManager */
  70. private $groupManager;
  71. /** @var \PHPUnit\Framework\MockObject\MockObject|IUserManager */
  72. private $userManager;
  73. private $uid;
  74. /**
  75. * @var \OCP\IUser
  76. */
  77. private $user;
  78. private $testMountProvider;
  79. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  80. private $eventDispatcher;
  81. protected function setUp(): void {
  82. parent::setUp();
  83. $this->uid = $this->getUniqueID('user');
  84. $this->createUser($this->uid, '');
  85. $this->user = \OC::$server->getUserManager()->get($this->uid);
  86. $this->mountManager = new \OC\Files\Mount\Manager();
  87. $this->clientService = $this->getMockBuilder(IClientService::class)
  88. ->disableOriginalConstructor()->getMock();
  89. $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
  90. $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
  91. $this->groupManager = $this->createMock(IGroupManager::class);
  92. $this->userManager = $this->createMock(IUserManager::class);
  93. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  94. $this->contactsManager = $this->createMock(IManager::class);
  95. // needed for MountProvider() initialization
  96. $this->contactsManager->expects($this->any())
  97. ->method('search')
  98. ->willReturn([]);
  99. $this->manager = $this->getMockBuilder(Manager::class)
  100. ->setConstructorArgs(
  101. [
  102. \OC::$server->getDatabaseConnection(),
  103. $this->mountManager,
  104. new StorageFactory(),
  105. $this->clientService,
  106. \OC::$server->getNotificationManager(),
  107. \OC::$server->query(\OCP\OCS\IDiscoveryService::class),
  108. $this->cloudFederationProviderManager,
  109. $this->cloudFederationFactory,
  110. $this->groupManager,
  111. $this->userManager,
  112. $this->uid,
  113. $this->eventDispatcher,
  114. ]
  115. )->setMethods(['tryOCMEndPoint'])->getMock();
  116. $this->testMountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function () {
  117. return $this->manager;
  118. }, new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->userManager));
  119. }
  120. private function setupMounts() {
  121. $mounts = $this->testMountProvider->getMountsForUser($this->user, new StorageFactory());
  122. foreach ($mounts as $mount) {
  123. $this->mountManager->addMount($mount);
  124. }
  125. }
  126. public function testAddShare() {
  127. $shareData1 = [
  128. 'remote' => 'http://localhost',
  129. 'token' => 'token1',
  130. 'password' => '',
  131. 'name' => '/SharedFolder',
  132. 'owner' => 'foobar',
  133. 'shareType' => IShare::TYPE_USER,
  134. 'accepted' => false,
  135. 'user' => $this->uid,
  136. 'remoteId' => '2342'
  137. ];
  138. $shareData2 = $shareData1;
  139. $shareData2['token'] = 'token2';
  140. $shareData3 = $shareData1;
  141. $shareData3['token'] = 'token3';
  142. $this->userManager->expects($this->any())->method('get')->willReturn($this->user);
  143. $this->groupManager->expects($this->any())->method(('getUserGroups'))->willReturn([]);
  144. $this->manager->expects($this->at(0))->method('tryOCMEndPoint')->with('http://localhost', 'token1', '2342', 'accept')->willReturn(false);
  145. $this->manager->expects($this->at(1))->method('tryOCMEndPoint')->with('http://localhost', 'token3', '2342', 'decline')->willReturn(false);
  146. // Add a share for "user"
  147. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData1));
  148. $openShares = $this->manager->getOpenShares();
  149. $this->assertCount(1, $openShares);
  150. $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  151. $this->setupMounts();
  152. $this->assertNotMount('SharedFolder');
  153. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  154. // Add a second share for "user" with the same name
  155. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData2));
  156. $openShares = $this->manager->getOpenShares();
  157. $this->assertCount(2, $openShares);
  158. $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  159. // New share falls back to "-1" appendix, because the name is already taken
  160. $this->assertExternalShareEntry($shareData2, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  161. $this->setupMounts();
  162. $this->assertNotMount('SharedFolder');
  163. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  164. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  165. $client = $this->getMockBuilder('OCP\Http\Client\IClient')
  166. ->disableOriginalConstructor()->getMock();
  167. $this->clientService->expects($this->at(0))
  168. ->method('newClient')
  169. ->willReturn($client);
  170. $response = $this->createMock(IResponse::class);
  171. $response->method('getBody')
  172. ->willReturn(json_encode([
  173. 'ocs' => [
  174. 'meta' => [
  175. 'statuscode' => 200,
  176. ]
  177. ]
  178. ]));
  179. $client->expects($this->once())
  180. ->method('post')
  181. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything())
  182. ->willReturn($response);
  183. // Accept the first share
  184. $this->manager->acceptShare($openShares[0]['id']);
  185. // Check remaining shares - Accepted
  186. $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
  187. $this->assertCount(1, $acceptedShares);
  188. $shareData1['accepted'] = true;
  189. $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']);
  190. // Check remaining shares - Open
  191. $openShares = $this->manager->getOpenShares();
  192. $this->assertCount(1, $openShares);
  193. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  194. $this->setupMounts();
  195. $this->assertMount($shareData1['name']);
  196. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  197. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  198. // Add another share for "user" with the same name
  199. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData3));
  200. $openShares = $this->manager->getOpenShares();
  201. $this->assertCount(2, $openShares);
  202. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  203. // New share falls back to the original name (no "-\d", because the name is not taken)
  204. $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}');
  205. $this->setupMounts();
  206. $this->assertMount($shareData1['name']);
  207. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  208. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  209. $client = $this->getMockBuilder('OCP\Http\Client\IClient')
  210. ->disableOriginalConstructor()->getMock();
  211. $this->clientService->expects($this->at(0))
  212. ->method('newClient')
  213. ->willReturn($client);
  214. $response = $this->createMock(IResponse::class);
  215. $response->method('getBody')
  216. ->willReturn(json_encode([
  217. 'ocs' => [
  218. 'meta' => [
  219. 'statuscode' => 200,
  220. ]
  221. ]
  222. ]));
  223. $client->expects($this->once())
  224. ->method('post')
  225. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything())
  226. ->willReturn($response);
  227. // Decline the third share
  228. $this->manager->declineShare($openShares[1]['id']);
  229. $this->setupMounts();
  230. $this->assertMount($shareData1['name']);
  231. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  232. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  233. // Check remaining shares - Accepted
  234. $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
  235. $this->assertCount(1, $acceptedShares);
  236. $shareData1['accepted'] = true;
  237. $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']);
  238. // Check remaining shares - Open
  239. $openShares = $this->manager->getOpenShares();
  240. $this->assertCount(1, $openShares);
  241. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  242. $this->setupMounts();
  243. $this->assertMount($shareData1['name']);
  244. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  245. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  246. $client1 = $this->getMockBuilder('OCP\Http\Client\IClient')
  247. ->disableOriginalConstructor()->getMock();
  248. $client2 = $this->getMockBuilder('OCP\Http\Client\IClient')
  249. ->disableOriginalConstructor()->getMock();
  250. $this->clientService->expects($this->at(0))
  251. ->method('newClient')
  252. ->willReturn($client1);
  253. $this->clientService->expects($this->at(1))
  254. ->method('newClient')
  255. ->willReturn($client2);
  256. $response = $this->createMock(IResponse::class);
  257. $response->method('getBody')
  258. ->willReturn(json_encode([
  259. 'ocs' => [
  260. 'meta' => [
  261. 'statuscode' => 200,
  262. ]
  263. ]
  264. ]));
  265. $client1->expects($this->once())
  266. ->method('post')
  267. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything())
  268. ->willReturn($response);
  269. $client2->expects($this->once())
  270. ->method('post')
  271. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything())
  272. ->willReturn($response);
  273. $this->manager->removeUserShares($this->uid);
  274. $this->assertEmpty(self::invokePrivate($this->manager, 'getShares', [null]), 'Asserting all shares for the user have been deleted');
  275. $this->mountManager->clear();
  276. self::invokePrivate($this->manager, 'setupMounts');
  277. $this->assertNotMount($shareData1['name']);
  278. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  279. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  280. }
  281. /**
  282. * @param array $expected
  283. * @param array $actual
  284. * @param int $share
  285. * @param string $mountPoint
  286. */
  287. protected function assertExternalShareEntry($expected, $actual, $share, $mountPoint) {
  288. $this->assertEquals($expected['remote'], $actual['remote'], 'Asserting remote of a share #' . $share);
  289. $this->assertEquals($expected['token'], $actual['share_token'], 'Asserting token of a share #' . $share);
  290. $this->assertEquals($expected['name'], $actual['name'], 'Asserting name of a share #' . $share);
  291. $this->assertEquals($expected['owner'], $actual['owner'], 'Asserting owner of a share #' . $share);
  292. $this->assertEquals($expected['accepted'], (int) $actual['accepted'], 'Asserting accept of a share #' . $share);
  293. $this->assertEquals($expected['user'], $actual['user'], 'Asserting user of a share #' . $share);
  294. $this->assertEquals($mountPoint, $actual['mountpoint'], 'Asserting mountpoint of a share #' . $share);
  295. }
  296. private function assertMount($mountPoint) {
  297. $mountPoint = rtrim($mountPoint, '/');
  298. $mount = $this->mountManager->find($this->getFullPath($mountPoint));
  299. $this->assertInstanceOf('\OCA\Files_Sharing\External\Mount', $mount);
  300. $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
  301. $this->assertEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
  302. $storage = $mount->getStorage();
  303. $this->assertInstanceOf('\OCA\Files_Sharing\External\Storage', $storage);
  304. }
  305. private function assertNotMount($mountPoint) {
  306. $mountPoint = rtrim($mountPoint, '/');
  307. $mount = $this->mountManager->find($this->getFullPath($mountPoint));
  308. if ($mount) {
  309. $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
  310. $this->assertNotEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
  311. } else {
  312. $this->assertNull($mount);
  313. }
  314. }
  315. private function getFullPath($path) {
  316. return '/' . $this->uid . '/files' . $path;
  317. }
  318. }