RemotePluginTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Collaboration\Collaborators;
  24. use OC\Collaboration\Collaborators\RemotePlugin;
  25. use OC\Collaboration\Collaborators\SearchResult;
  26. use OC\Federation\CloudIdManager;
  27. use OCP\Collaboration\Collaborators\SearchResultType;
  28. use OCP\Contacts\IManager;
  29. use OCP\Federation\ICloudIdManager;
  30. use OCP\IConfig;
  31. use OCP\IURLGenerator;
  32. use OCP\IUser;
  33. use OCP\IUserManager;
  34. use OCP\IUserSession;
  35. use OCP\Share\IShare;
  36. use Test\TestCase;
  37. class RemotePluginTest extends TestCase {
  38. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  39. protected $userManager;
  40. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  41. protected $config;
  42. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  43. protected $contactsManager;
  44. /** @var ICloudIdManager|\PHPUnit\Framework\MockObject\MockObject */
  45. protected $cloudIdManager;
  46. /** @var RemotePlugin */
  47. protected $plugin;
  48. /** @var SearchResult */
  49. protected $searchResult;
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->userManager = $this->createMock(IUserManager::class);
  53. $this->config = $this->createMock(IConfig::class);
  54. $this->contactsManager = $this->createMock(IManager::class);
  55. $this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->createMock(IUserManager::class));
  56. $this->searchResult = new SearchResult();
  57. }
  58. public function instantiatePlugin() {
  59. $user = $this->createMock(IUser::class);
  60. $user->expects($this->any())
  61. ->method('getUID')
  62. ->willReturn('admin');
  63. $userSession = $this->createMock(IUserSession::class);
  64. $userSession->expects($this->any())
  65. ->method('getUser')
  66. ->willReturn($user);
  67. $this->plugin = new RemotePlugin($this->contactsManager, $this->cloudIdManager, $this->config, $this->userManager, $userSession);
  68. }
  69. /**
  70. * @dataProvider dataGetRemote
  71. *
  72. * @param string $searchTerm
  73. * @param array $contacts
  74. * @param bool $shareeEnumeration
  75. * @param array $expected
  76. * @param bool $exactIdMatch
  77. * @param bool $reachedEnd
  78. */
  79. public function testSearch($searchTerm, array $contacts, $shareeEnumeration, array $expected, $exactIdMatch, $reachedEnd) {
  80. $this->config->expects($this->any())
  81. ->method('getAppValue')
  82. ->willReturnCallback(
  83. function ($appName, $key, $default) use ($shareeEnumeration) {
  84. if ($appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration') {
  85. return $shareeEnumeration ? 'yes' : 'no';
  86. }
  87. return $default;
  88. }
  89. );
  90. $this->instantiatePlugin();
  91. $this->contactsManager->expects($this->any())
  92. ->method('search')
  93. ->willReturnCallback(function ($search, $searchAttributes) use ($searchTerm, $contacts) {
  94. if ($search === $searchTerm) {
  95. return $contacts;
  96. }
  97. return [];
  98. });
  99. $moreResults = $this->plugin->search($searchTerm, 2, 0, $this->searchResult);
  100. $result = $this->searchResult->asArray();
  101. $this->assertSame($exactIdMatch, $this->searchResult->hasExactIdMatch(new SearchResultType('remotes')));
  102. $this->assertEquals($expected, $result);
  103. $this->assertSame($reachedEnd, $moreResults);
  104. }
  105. /**
  106. * @dataProvider dataTestSplitUserRemote
  107. *
  108. * @param string $remote
  109. * @param string $expectedUser
  110. * @param string $expectedUrl
  111. */
  112. public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) {
  113. $this->instantiatePlugin();
  114. $this->contactsManager->expects($this->any())
  115. ->method('search')
  116. ->willReturn([]);
  117. [$remoteUser, $remoteUrl] = $this->plugin->splitUserRemote($remote);
  118. $this->assertSame($expectedUser, $remoteUser);
  119. $this->assertSame($expectedUrl, $remoteUrl);
  120. }
  121. /**
  122. * @dataProvider dataTestSplitUserRemoteError
  123. *
  124. * @param string $id
  125. */
  126. public function testSplitUserRemoteError($id) {
  127. $this->expectException(\Exception::class);
  128. $this->instantiatePlugin();
  129. $this->plugin->splitUserRemote($id);
  130. }
  131. public function dataGetRemote() {
  132. return [
  133. ['test', [], true, ['remotes' => [], 'exact' => ['remotes' => []]], false, true],
  134. ['test', [], false, ['remotes' => [], 'exact' => ['remotes' => []]], false, true],
  135. [
  136. 'test@remote',
  137. [],
  138. true,
  139. ['remotes' => [], 'exact' => ['remotes' => [['label' => 'test (remote)', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'test@remote', 'server' => 'remote'], 'uuid' => 'test', 'name' => 'test']]]],
  140. false,
  141. true,
  142. ],
  143. [
  144. 'test@remote',
  145. [],
  146. false,
  147. ['remotes' => [], 'exact' => ['remotes' => [['label' => 'test (remote)', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'test@remote', 'server' => 'remote'], 'uuid' => 'test', 'name' => 'test']]]],
  148. false,
  149. true,
  150. ],
  151. [
  152. 'test',
  153. [
  154. [
  155. 'UID' => 'uid',
  156. 'FN' => 'User3 @ Localhost',
  157. ],
  158. [
  159. 'UID' => 'uid',
  160. 'FN' => 'User2 @ Localhost',
  161. 'CLOUD' => [
  162. ],
  163. ],
  164. [
  165. 'UID' => 'uid1',
  166. 'FN' => 'User @ Localhost',
  167. 'CLOUD' => [
  168. 'username@localhost',
  169. ],
  170. ],
  171. ],
  172. true,
  173. ['remotes' => [['name' => 'User @ Localhost', 'label' => 'User @ Localhost (username@localhost)', 'uuid' => 'uid1', 'type' => '', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]], 'exact' => ['remotes' => []]],
  174. false,
  175. true,
  176. ],
  177. [
  178. 'test',
  179. [
  180. [
  181. 'UID' => 'uid',
  182. 'FN' => 'User3 @ Localhost',
  183. ],
  184. [
  185. 'UID' => 'uid',
  186. 'FN' => 'User2 @ Localhost',
  187. 'CLOUD' => [
  188. ],
  189. ],
  190. [
  191. 'UID' => 'uid',
  192. 'FN' => 'User @ Localhost',
  193. 'CLOUD' => [
  194. 'username@localhost',
  195. ],
  196. ],
  197. ],
  198. false,
  199. ['remotes' => [], 'exact' => ['remotes' => []]],
  200. false,
  201. true,
  202. ],
  203. [
  204. 'test@remote',
  205. [
  206. [
  207. 'UID' => 'uid',
  208. 'FN' => 'User3 @ Localhost',
  209. ],
  210. [
  211. 'UID' => 'uid',
  212. 'FN' => 'User2 @ Localhost',
  213. 'CLOUD' => [
  214. ],
  215. ],
  216. [
  217. 'UID' => 'uid',
  218. 'FN' => 'User @ Localhost',
  219. 'CLOUD' => [
  220. 'username@localhost',
  221. ],
  222. ],
  223. ],
  224. true,
  225. ['remotes' => [['name' => 'User @ Localhost', 'label' => 'User @ Localhost (username@localhost)', 'uuid' => 'uid', 'type' => '', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]], 'exact' => ['remotes' => [['label' => 'test (remote)', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'test@remote', 'server' => 'remote'], 'uuid' => 'test', 'name' => 'test']]]],
  226. false,
  227. true,
  228. ],
  229. [
  230. 'test@remote',
  231. [
  232. [
  233. 'UID' => 'uid',
  234. 'FN' => 'User3 @ Localhost',
  235. ],
  236. [
  237. 'UID' => 'uid',
  238. 'FN' => 'User2 @ Localhost',
  239. 'CLOUD' => [
  240. ],
  241. ],
  242. [
  243. 'UID' => 'uid',
  244. 'FN' => 'User @ Localhost',
  245. 'CLOUD' => [
  246. 'username@localhost',
  247. ],
  248. ],
  249. ],
  250. false,
  251. ['remotes' => [], 'exact' => ['remotes' => [['label' => 'test (remote)', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'test@remote', 'server' => 'remote'], 'uuid' => 'test', 'name' => 'test']]]],
  252. false,
  253. true,
  254. ],
  255. [
  256. 'username@localhost',
  257. [
  258. [
  259. 'UID' => 'uid3',
  260. 'FN' => 'User3 @ Localhost',
  261. ],
  262. [
  263. 'UID' => '2',
  264. 'FN' => 'User2 @ Localhost',
  265. 'CLOUD' => [
  266. ],
  267. ],
  268. [
  269. 'UID' => 'uid1',
  270. 'FN' => 'User @ Localhost',
  271. 'CLOUD' => [
  272. 'username@localhost',
  273. ],
  274. ],
  275. ],
  276. true,
  277. ['remotes' => [], 'exact' => ['remotes' => [['name' => 'User @ Localhost', 'label' => 'User @ Localhost (username@localhost)', 'uuid' => 'uid1', 'type' => '', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]]]],
  278. true,
  279. true,
  280. ],
  281. [
  282. 'username@localhost',
  283. [
  284. [
  285. 'UID' => 'uid3',
  286. 'FN' => 'User3 @ Localhost',
  287. ],
  288. [
  289. 'UID' => 'uid2',
  290. 'FN' => 'User2 @ Localhost',
  291. 'CLOUD' => [
  292. ],
  293. ],
  294. [
  295. 'UID' => 'uid1',
  296. 'FN' => 'User @ Localhost',
  297. 'CLOUD' => [
  298. 'username@localhost',
  299. ],
  300. ],
  301. ],
  302. false,
  303. ['remotes' => [], 'exact' => ['remotes' => [['name' => 'User @ Localhost', 'label' => 'User @ Localhost (username@localhost)', 'uuid' => 'uid1', 'type' => '', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]]]],
  304. true,
  305. true,
  306. ],
  307. // contact with space
  308. [
  309. 'user name@localhost',
  310. [
  311. [
  312. 'UID' => 'uid1',
  313. 'FN' => 'User3 @ Localhost',
  314. ],
  315. [
  316. 'UID' => 'uid2',
  317. 'FN' => 'User2 @ Localhost',
  318. 'CLOUD' => [
  319. ],
  320. ],
  321. [
  322. 'UID' => 'uid3',
  323. 'FN' => 'User Name @ Localhost',
  324. 'CLOUD' => [
  325. 'user name@localhost',
  326. ],
  327. ],
  328. ],
  329. false,
  330. ['remotes' => [], 'exact' => ['remotes' => [['name' => 'User Name @ Localhost', 'label' => 'User Name @ Localhost (user name@localhost)', 'uuid' => 'uid3', 'type' => '', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'user name@localhost', 'server' => 'localhost']]]]],
  331. true,
  332. true,
  333. ],
  334. // remote with space, no contact
  335. [
  336. 'user space@remote',
  337. [
  338. [
  339. 'UID' => 'uid3',
  340. 'FN' => 'User3 @ Localhost',
  341. ],
  342. [
  343. 'UID' => 'uid2',
  344. 'FN' => 'User2 @ Localhost',
  345. 'CLOUD' => [
  346. ],
  347. ],
  348. [
  349. 'UID' => 'uid1',
  350. 'FN' => 'User @ Localhost',
  351. 'CLOUD' => [
  352. 'username@localhost',
  353. ],
  354. ],
  355. ],
  356. false,
  357. ['remotes' => [], 'exact' => ['remotes' => [['label' => 'user space (remote)', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'user space@remote', 'server' => 'remote'], 'uuid' => 'user space', 'name' => 'user space']]]],
  358. false,
  359. true,
  360. ],
  361. ];
  362. }
  363. public function dataTestSplitUserRemote() {
  364. $userPrefix = ['user@name', 'username'];
  365. $protocols = ['', 'http://', 'https://'];
  366. $remotes = [
  367. 'localhost',
  368. 'local.host',
  369. 'dev.local.host',
  370. 'dev.local.host/path',
  371. 'dev.local.host/at@inpath',
  372. '127.0.0.1',
  373. '::1',
  374. '::192.0.2.128',
  375. '::192.0.2.128/at@inpath',
  376. ];
  377. $testCases = [];
  378. foreach ($userPrefix as $user) {
  379. foreach ($remotes as $remote) {
  380. foreach ($protocols as $protocol) {
  381. $baseUrl = $user . '@' . $protocol . $remote;
  382. $testCases[] = [$baseUrl, $user, $protocol . $remote];
  383. $testCases[] = [$baseUrl . '/', $user, $protocol . $remote];
  384. $testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote];
  385. $testCases[] = [$baseUrl . '/index.php/s/token', $user, $protocol . $remote];
  386. }
  387. }
  388. }
  389. return $testCases;
  390. }
  391. public function dataTestSplitUserRemoteError() {
  392. return [
  393. // Invalid path
  394. ['user@'],
  395. // Invalid user
  396. ['@server'],
  397. ['us/er@server'],
  398. ['us:er@server'],
  399. // Invalid splitting
  400. ['user'],
  401. [''],
  402. ['us/erserver'],
  403. ['us:erserver'],
  404. ];
  405. }
  406. }