RemotePluginTest.php 11 KB

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