You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

473 lines
12 KiB

  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\MailPlugin;
  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\IGroupManager;
  32. use OCP\IUserSession;
  33. use OCP\Share;
  34. use Test\TestCase;
  35. class MailPluginTest extends TestCase {
  36. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  37. protected $config;
  38. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  39. protected $contactsManager;
  40. /** @var ICloudIdManager|\PHPUnit_Framework_MockObject_MockObject */
  41. protected $cloudIdManager;
  42. /** @var MailPlugin */
  43. protected $plugin;
  44. /** @var SearchResult */
  45. protected $searchResult;
  46. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  47. protected $groupManager;
  48. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  49. protected $userSession;
  50. public function setUp() {
  51. parent::setUp();
  52. $this->config = $this->createMock(IConfig::class);
  53. $this->contactsManager = $this->createMock(IManager::class);
  54. $this->groupManager = $this->createMock(IGroupManager::class);
  55. $this->userSession = $this->createMock(IUserSession::class);
  56. $this->cloudIdManager = new CloudIdManager();
  57. $this->searchResult = new SearchResult();
  58. }
  59. public function instantiatePlugin() {
  60. $this->plugin = new MailPlugin($this->contactsManager, $this->cloudIdManager, $this->config, $this->groupManager, $this->userSession);
  61. }
  62. /**
  63. * @dataProvider dataGetEmail
  64. *
  65. * @param string $searchTerm
  66. * @param array $contacts
  67. * @param bool $shareeEnumeration
  68. * @param array $expected
  69. * @param bool $reachedEnd
  70. */
  71. public function testSearch($searchTerm, $contacts, $shareeEnumeration, $expected, $exactIdMatch, $reachedEnd) {
  72. $this->config->expects($this->any())
  73. ->method('getAppValue')
  74. ->willReturnCallback(
  75. function($appName, $key, $default)
  76. use ($shareeEnumeration)
  77. {
  78. if ($appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration') {
  79. return $shareeEnumeration ? 'yes' : 'no';
  80. }
  81. return $default;
  82. }
  83. );
  84. $this->instantiatePlugin();
  85. $this->contactsManager->expects($this->any())
  86. ->method('search')
  87. ->with($searchTerm, ['EMAIL', 'FN'])
  88. ->willReturn($contacts);
  89. $moreResults = $this->plugin->search($searchTerm, 0, 0, $this->searchResult);
  90. $result = $this->searchResult->asArray();
  91. $this->assertSame($exactIdMatch, $this->searchResult->hasExactIdMatch(new SearchResultType('emails')));
  92. $this->assertEquals($expected, $result);
  93. $this->assertSame($reachedEnd, $moreResults);
  94. }
  95. public function dataGetEmail() {
  96. return [
  97. ['test', [], true, ['emails' => [], 'exact' => ['emails' => []]], false, true],
  98. ['test', [], false, ['emails' => [], 'exact' => ['emails' => []]], false, true],
  99. [
  100. 'test@remote.com',
  101. [],
  102. true,
  103. ['emails' => [], 'exact' => ['emails' => [['label' => 'test@remote.com', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'test@remote.com']]]]],
  104. false,
  105. true,
  106. ],
  107. [ // no valid email address
  108. 'test@remote',
  109. [],
  110. true,
  111. ['emails' => [], 'exact' => ['emails' => []]],
  112. false,
  113. true,
  114. ],
  115. [
  116. 'test@remote.com',
  117. [],
  118. false,
  119. ['emails' => [], 'exact' => ['emails' => [['label' => 'test@remote.com', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'test@remote.com']]]]],
  120. false,
  121. true,
  122. ],
  123. [
  124. 'test',
  125. [
  126. [
  127. 'FN' => 'User3 @ Localhost',
  128. ],
  129. [
  130. 'FN' => 'User2 @ Localhost',
  131. 'EMAIL' => [
  132. ],
  133. ],
  134. [
  135. 'FN' => 'User @ Localhost',
  136. 'EMAIL' => [
  137. 'username@localhost',
  138. ],
  139. ],
  140. ],
  141. true,
  142. ['emails' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'username@localhost']]], 'exact' => ['emails' => []]],
  143. false,
  144. true,
  145. ],
  146. [
  147. 'test',
  148. [
  149. [
  150. 'FN' => 'User3 @ Localhost',
  151. ],
  152. [
  153. 'FN' => 'User2 @ Localhost',
  154. 'EMAIL' => [
  155. ],
  156. ],
  157. [
  158. 'FN' => 'User @ Localhost',
  159. 'EMAIL' => [
  160. 'username@localhost',
  161. ],
  162. ],
  163. ],
  164. false,
  165. ['emails' => [], 'exact' => ['emails' => []]],
  166. false,
  167. true,
  168. ],
  169. [
  170. 'test@remote.com',
  171. [
  172. [
  173. 'FN' => 'User3 @ Localhost',
  174. ],
  175. [
  176. 'FN' => 'User2 @ Localhost',
  177. 'EMAIL' => [
  178. ],
  179. ],
  180. [
  181. 'FN' => 'User @ Localhost',
  182. 'EMAIL' => [
  183. 'username@localhost',
  184. ],
  185. ],
  186. ],
  187. true,
  188. ['emails' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'username@localhost']]], 'exact' => ['emails' => [['label' => 'test@remote.com', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'test@remote.com']]]]],
  189. false,
  190. true,
  191. ],
  192. [
  193. 'test@remote.com',
  194. [
  195. [
  196. 'FN' => 'User3 @ Localhost',
  197. ],
  198. [
  199. 'FN' => 'User2 @ Localhost',
  200. 'EMAIL' => [
  201. ],
  202. ],
  203. [
  204. 'FN' => 'User @ Localhost',
  205. 'EMAIL' => [
  206. 'username@localhost',
  207. ],
  208. ],
  209. ],
  210. false,
  211. ['emails' => [], 'exact' => ['emails' => [['label' => 'test@remote.com', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'test@remote.com']]]]],
  212. false,
  213. true,
  214. ],
  215. [
  216. 'username@localhost',
  217. [
  218. [
  219. 'FN' => 'User3 @ Localhost',
  220. ],
  221. [
  222. 'FN' => 'User2 @ Localhost',
  223. 'EMAIL' => [
  224. ],
  225. ],
  226. [
  227. 'FN' => 'User @ Localhost',
  228. 'EMAIL' => [
  229. 'username@localhost',
  230. ],
  231. ],
  232. ],
  233. true,
  234. ['emails' => [], 'exact' => ['emails' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'username@localhost']]]]],
  235. true,
  236. true,
  237. ],
  238. [
  239. 'username@localhost',
  240. [
  241. [
  242. 'FN' => 'User3 @ Localhost',
  243. ],
  244. [
  245. 'FN' => 'User2 @ Localhost',
  246. 'EMAIL' => [
  247. ],
  248. ],
  249. [
  250. 'FN' => 'User @ Localhost',
  251. 'EMAIL' => [
  252. 'username@localhost',
  253. ],
  254. ],
  255. ],
  256. false,
  257. ['emails' => [], 'exact' => ['emails' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'username@localhost']]]]],
  258. true,
  259. true,
  260. ],
  261. // contact with space
  262. [
  263. 'user name@localhost',
  264. [
  265. [
  266. 'FN' => 'User3 @ Localhost',
  267. ],
  268. [
  269. 'FN' => 'User2 @ Localhost',
  270. 'EMAIL' => [
  271. ],
  272. ],
  273. [
  274. 'FN' => 'User Name @ Localhost',
  275. 'EMAIL' => [
  276. 'user name@localhost',
  277. ],
  278. ],
  279. ],
  280. false,
  281. ['emails' => [], 'exact' => ['emails' => [['label' => 'User Name @ Localhost (user name@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'user name@localhost']]]]],
  282. true,
  283. true,
  284. ],
  285. // remote with space, no contact
  286. [
  287. 'user space@remote.com',
  288. [
  289. [
  290. 'FN' => 'User3 @ Localhost',
  291. ],
  292. [
  293. 'FN' => 'User2 @ Localhost',
  294. 'EMAIL' => [
  295. ],
  296. ],
  297. [
  298. 'FN' => 'User @ Localhost',
  299. 'EMAIL' => [
  300. 'username@localhost',
  301. ],
  302. ],
  303. ],
  304. false,
  305. ['emails' => [], 'exact' => ['emails' => []]],
  306. false,
  307. true,
  308. ],
  309. // Local user found by email
  310. [
  311. 'test@example.com',
  312. [
  313. [
  314. 'FN' => 'User',
  315. 'EMAIL' => ['test@example.com'],
  316. 'CLOUD' => ['test@localhost'],
  317. 'isLocalSystemBook' => true,
  318. ]
  319. ],
  320. false,
  321. ['users' => [], 'exact' => ['users' => [['label' => 'User (test@example.com)','value' => ['shareType' => 0, 'shareWith' => 'test'],]]]],
  322. true,
  323. false,
  324. ]
  325. ];
  326. }
  327. /**
  328. * @dataProvider dataGetEmailGroupsOnly
  329. *
  330. * @param string $searchTerm
  331. * @param array $contacts
  332. * @param array $expected
  333. * @param bool $exactIdMatch
  334. * @param bool $reachedEnd
  335. * @param array groups
  336. */
  337. public function testSearchGroupsOnly($searchTerm, $contacts, $expected, $exactIdMatch, $reachedEnd, $userToGroupMapping) {
  338. $this->config->expects($this->any())
  339. ->method('getAppValue')
  340. ->willReturnCallback(
  341. function($appName, $key, $default) {
  342. if ($appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration') {
  343. return 'yes';
  344. } else if ($appName === 'core' && $key === 'shareapi_only_share_with_group_members') {
  345. return 'yes';
  346. }
  347. return $default;
  348. }
  349. );
  350. $this->instantiatePlugin();
  351. /** @var \OCP\IUser | \PHPUnit_Framework_MockObject_MockObject */
  352. $currentUser = $this->createMock('\OCP\IUser');
  353. $currentUser->expects($this->any())
  354. ->method('getUID')
  355. ->willReturn('currentUser');
  356. $this->contactsManager->expects($this->any())
  357. ->method('search')
  358. ->with($searchTerm, ['EMAIL', 'FN'])
  359. ->willReturn($contacts);
  360. $this->userSession->expects($this->any())
  361. ->method('getUser')
  362. ->willReturn($currentUser);
  363. $this->groupManager->expects($this->any())
  364. ->method('getUserGroupIds')
  365. ->willReturnCallback(function(\OCP\IUser $user) use ($userToGroupMapping) {
  366. return $userToGroupMapping[$user->getUID()];
  367. });
  368. $this->groupManager->expects($this->any())
  369. ->method('isInGroup')
  370. ->willReturnCallback(function($userId, $group) use ($userToGroupMapping) {
  371. return in_array($group, $userToGroupMapping[$userId]);
  372. });
  373. $moreResults = $this->plugin->search($searchTerm, 0, 0, $this->searchResult);
  374. $result = $this->searchResult->asArray();
  375. $this->assertSame($exactIdMatch, $this->searchResult->hasExactIdMatch(new SearchResultType('emails')));
  376. $this->assertEquals($expected, $result);
  377. $this->assertSame($reachedEnd, $moreResults);
  378. }
  379. public function dataGetEmailGroupsOnly() {
  380. return [
  381. // The user `User` can share with the current user
  382. [
  383. 'test',
  384. [
  385. [
  386. 'FN' => 'User',
  387. 'EMAIL' => ['test@example.com'],
  388. 'CLOUD' => ['test@localhost'],
  389. 'isLocalSystemBook' => true,
  390. 'UID' => 'User'
  391. ]
  392. ],
  393. ['users' => [['label' => 'User (test@example.com)','value' => ['shareType' => 0, 'shareWith' => 'test'],]], 'emails' => [], 'exact' => ['emails' => [], 'users' => []]],
  394. false,
  395. true,
  396. [
  397. "currentUser" => ["group1"],
  398. "User" => ["group1"]
  399. ]
  400. ],
  401. // The user `User` cannot share with the current user
  402. [
  403. 'test',
  404. [
  405. [
  406. 'FN' => 'User',
  407. 'EMAIL' => ['test@example.com'],
  408. 'CLOUD' => ['test@localhost'],
  409. 'isLocalSystemBook' => true,
  410. 'UID' => 'User'
  411. ]
  412. ],
  413. ['emails'=> [], 'exact' => ['emails' => []]],
  414. false,
  415. true,
  416. [
  417. "currentUser" => ["group1"],
  418. "User" => ["group2"]
  419. ]
  420. ],
  421. // The user `User` cannot share with the current user, but there is an exact match on the e-mail address -> share by e-mail
  422. [
  423. 'test@example.com',
  424. [
  425. [
  426. 'FN' => 'User',
  427. 'EMAIL' => ['test@example.com'],
  428. 'CLOUD' => ['test@localhost'],
  429. 'isLocalSystemBook' => true,
  430. 'UID' => 'User'
  431. ]
  432. ],
  433. ['emails' => [], 'exact' => ['emails' => [['label' => 'test@example.com', 'value' => ['shareType' => 4,'shareWith' => 'test@example.com']]]]],
  434. false,
  435. true,
  436. [
  437. "currentUser" => ["group1"],
  438. "User" => ["group2"]
  439. ]
  440. ]
  441. ];
  442. }
  443. }