RemotePluginTest.php 9.7 KB

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