ManagerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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\Http\Client\IClientService;
  34. use Test\Traits\UserTrait;
  35. /**
  36. * Class ManagerTest
  37. *
  38. * @group DB
  39. *
  40. * @package OCA\Files_Sharing\Tests\External
  41. */
  42. class ManagerTest extends TestCase {
  43. use UserTrait;
  44. /** @var Manager **/
  45. private $manager;
  46. /** @var \OC\Files\Mount\Manager */
  47. private $mountManager;
  48. /** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */
  49. private $clientService;
  50. private $uid;
  51. /**
  52. * @var \OCP\IUser
  53. */
  54. private $user;
  55. private $testMountProvider;
  56. protected function setUp() {
  57. parent::setUp();
  58. $this->uid = $this->getUniqueID('user');
  59. $this->createUser($this->uid, '');
  60. $this->user = \OC::$server->getUserManager()->get($this->uid);
  61. $this->mountManager = new \OC\Files\Mount\Manager();
  62. $this->clientService = $this->getMockBuilder(IClientService::class)
  63. ->disableOriginalConstructor()->getMock();
  64. $this->manager = new Manager(
  65. \OC::$server->getDatabaseConnection(),
  66. $this->mountManager,
  67. new StorageFactory(),
  68. $this->clientService,
  69. \OC::$server->getNotificationManager(),
  70. \OC::$server->query(\OCP\OCS\IDiscoveryService::class),
  71. $this->uid
  72. );
  73. $this->testMountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function() {
  74. return $this->manager;
  75. }, new CloudIdManager());
  76. }
  77. private function setupMounts() {
  78. $mounts = $this->testMountProvider->getMountsForUser($this->user, new StorageFactory());
  79. foreach ($mounts as $mount) {
  80. $this->mountManager->addMount($mount);
  81. }
  82. }
  83. public function testAddShare() {
  84. $shareData1 = [
  85. 'remote' => 'http://localhost',
  86. 'token' => 'token1',
  87. 'password' => '',
  88. 'name' => '/SharedFolder',
  89. 'owner' => 'foobar',
  90. 'accepted' => false,
  91. 'user' => $this->uid,
  92. ];
  93. $shareData2 = $shareData1;
  94. $shareData2['token'] = 'token2';
  95. $shareData3 = $shareData1;
  96. $shareData3['token'] = 'token3';
  97. // Add a share for "user"
  98. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData1));
  99. $openShares = $this->manager->getOpenShares();
  100. $this->assertCount(1, $openShares);
  101. $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  102. $this->setupMounts();
  103. $this->assertNotMount('SharedFolder');
  104. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  105. // Add a second share for "user" with the same name
  106. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData2));
  107. $openShares = $this->manager->getOpenShares();
  108. $this->assertCount(2, $openShares);
  109. $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  110. // New share falls back to "-1" appendix, because the name is already taken
  111. $this->assertExternalShareEntry($shareData2, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  112. $this->setupMounts();
  113. $this->assertNotMount('SharedFolder');
  114. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  115. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  116. $client = $this->getMockBuilder('OCP\Http\Client\IClient')
  117. ->disableOriginalConstructor()->getMock();
  118. $this->clientService->expects($this->at(0))
  119. ->method('newClient')
  120. ->willReturn($client);
  121. $response = $this->getMockBuilder('OCP\Http\Client\IResponse')
  122. ->disableOriginalConstructor()->getMock();
  123. $client->expects($this->once())
  124. ->method('post')
  125. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything())
  126. ->willReturn($response);
  127. // Accept the first share
  128. $this->manager->acceptShare($openShares[0]['id']);
  129. // Check remaining shares - Accepted
  130. $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
  131. $this->assertCount(1, $acceptedShares);
  132. $shareData1['accepted'] = true;
  133. $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']);
  134. // Check remaining shares - Open
  135. $openShares = $this->manager->getOpenShares();
  136. $this->assertCount(1, $openShares);
  137. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  138. $this->setupMounts();
  139. $this->assertMount($shareData1['name']);
  140. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  141. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  142. // Add another share for "user" with the same name
  143. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData3));
  144. $openShares = $this->manager->getOpenShares();
  145. $this->assertCount(2, $openShares);
  146. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  147. // New share falls back to the original name (no "-\d", because the name is not taken)
  148. $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}');
  149. $this->setupMounts();
  150. $this->assertMount($shareData1['name']);
  151. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  152. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  153. $client = $this->getMockBuilder('OCP\Http\Client\IClient')
  154. ->disableOriginalConstructor()->getMock();
  155. $this->clientService->expects($this->at(0))
  156. ->method('newClient')
  157. ->willReturn($client);
  158. $response = $this->getMockBuilder('OCP\Http\Client\IResponse')
  159. ->disableOriginalConstructor()->getMock();
  160. $client->expects($this->once())
  161. ->method('post')
  162. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything())
  163. ->willReturn($response);
  164. // Decline the third share
  165. $this->manager->declineShare($openShares[1]['id']);
  166. $this->setupMounts();
  167. $this->assertMount($shareData1['name']);
  168. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  169. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  170. // Check remaining shares - Accepted
  171. $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
  172. $this->assertCount(1, $acceptedShares);
  173. $shareData1['accepted'] = true;
  174. $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']);
  175. // Check remaining shares - Open
  176. $openShares = $this->manager->getOpenShares();
  177. $this->assertCount(1, $openShares);
  178. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  179. $this->setupMounts();
  180. $this->assertMount($shareData1['name']);
  181. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  182. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  183. $client1 = $this->getMockBuilder('OCP\Http\Client\IClient')
  184. ->disableOriginalConstructor()->getMock();
  185. $client2 = $this->getMockBuilder('OCP\Http\Client\IClient')
  186. ->disableOriginalConstructor()->getMock();
  187. $this->clientService->expects($this->at(0))
  188. ->method('newClient')
  189. ->willReturn($client1);
  190. $this->clientService->expects($this->at(1))
  191. ->method('newClient')
  192. ->willReturn($client2);
  193. $response = $this->getMockBuilder('OCP\Http\Client\IResponse')
  194. ->disableOriginalConstructor()->getMock();
  195. $client1->expects($this->once())
  196. ->method('post')
  197. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything())
  198. ->willReturn($response);
  199. $client2->expects($this->once())
  200. ->method('post')
  201. ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything())
  202. ->willReturn($response);
  203. $this->manager->removeUserShares($this->uid);
  204. $this->assertEmpty(self::invokePrivate($this->manager, 'getShares', [null]), 'Asserting all shares for the user have been deleted');
  205. $this->mountManager->clear();
  206. self::invokePrivate($this->manager, 'setupMounts');
  207. $this->assertNotMount($shareData1['name']);
  208. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  209. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  210. }
  211. /**
  212. * @param array $expected
  213. * @param array $actual
  214. * @param int $share
  215. * @param string $mountPoint
  216. */
  217. protected function assertExternalShareEntry($expected, $actual, $share, $mountPoint) {
  218. $this->assertEquals($expected['remote'], $actual['remote'], 'Asserting remote of a share #' . $share);
  219. $this->assertEquals($expected['token'], $actual['share_token'], 'Asserting token of a share #' . $share);
  220. $this->assertEquals($expected['name'], $actual['name'], 'Asserting name of a share #' . $share);
  221. $this->assertEquals($expected['owner'], $actual['owner'], 'Asserting owner of a share #' . $share);
  222. $this->assertEquals($expected['accepted'], (int) $actual['accepted'], 'Asserting accept of a share #' . $share);
  223. $this->assertEquals($expected['user'], $actual['user'], 'Asserting user of a share #' . $share);
  224. $this->assertEquals($mountPoint, $actual['mountpoint'], 'Asserting mountpoint of a share #' . $share);
  225. }
  226. private function assertMount($mountPoint) {
  227. $mountPoint = rtrim($mountPoint, '/');
  228. $mount = $this->mountManager->find($this->getFullPath($mountPoint));
  229. $this->assertInstanceOf('\OCA\Files_Sharing\External\Mount', $mount);
  230. $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
  231. $this->assertEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
  232. $storage = $mount->getStorage();
  233. $this->assertInstanceOf('\OCA\Files_Sharing\External\Storage', $storage);
  234. }
  235. private function assertNotMount($mountPoint) {
  236. $mountPoint = rtrim($mountPoint, '/');
  237. $mount = $this->mountManager->find($this->getFullPath($mountPoint));
  238. if ($mount) {
  239. $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
  240. $this->assertNotEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
  241. } else {
  242. $this->assertNull($mount);
  243. }
  244. }
  245. private function getFullPath($path) {
  246. return '/' . $this->uid . '/files' . $path;
  247. }
  248. }