ManagerTest.php 13 KB

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