1
0

PrincipalTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2018, Georg Ehrke
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Richard Steinmetz <richard@steinmetz.cloud>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  32. use OC\KnownUser\KnownUserService;
  33. use OC\User\User;
  34. use OCA\DAV\CalDAV\Proxy\Proxy;
  35. use OCA\DAV\CalDAV\Proxy\ProxyMapper;
  36. use OCA\DAV\Connector\Sabre\Principal;
  37. use OCP\Accounts\IAccount;
  38. use OCP\Accounts\IAccountManager;
  39. use OCP\Accounts\IAccountProperty;
  40. use OCP\Accounts\IAccountPropertyCollection;
  41. use OCP\App\IAppManager;
  42. use OCP\IConfig;
  43. use OCP\IGroup;
  44. use OCP\IGroupManager;
  45. use OCP\IUser;
  46. use OCP\IUserManager;
  47. use OCP\IUserSession;
  48. use OCP\L10N\IFactory;
  49. use OCP\Share\IManager;
  50. use PHPUnit\Framework\MockObject\MockObject;
  51. use Sabre\DAV\Exception;
  52. use Sabre\DAV\PropPatch;
  53. use Test\TestCase;
  54. class PrincipalTest extends TestCase {
  55. /** @var IUserManager | MockObject */
  56. private $userManager;
  57. /** @var Principal */
  58. private $connector;
  59. /** @var IGroupManager | MockObject */
  60. private $groupManager;
  61. /** @var IAccountManager|MockObject */
  62. private $accountManager;
  63. /** @var IManager | MockObject */
  64. private $shareManager;
  65. /** @var IUserSession | MockObject */
  66. private $userSession;
  67. /** @var IAppManager | MockObject */
  68. private $appManager;
  69. /** @var ProxyMapper | MockObject */
  70. private $proxyMapper;
  71. /** @var KnownUserService|MockObject */
  72. private $knownUserService;
  73. /** @var IConfig | MockObject */
  74. private $config;
  75. /** @var IFactory|MockObject */
  76. private $languageFactory;
  77. protected function setUp(): void {
  78. $this->userManager = $this->createMock(IUserManager::class);
  79. $this->groupManager = $this->createMock(IGroupManager::class);
  80. $this->accountManager = $this->createMock(IAccountManager::class);
  81. $this->shareManager = $this->createMock(IManager::class);
  82. $this->userSession = $this->createMock(IUserSession::class);
  83. $this->appManager = $this->createMock(IAppManager::class);
  84. $this->proxyMapper = $this->createMock(ProxyMapper::class);
  85. $this->knownUserService = $this->createMock(KnownUserService::class);
  86. $this->config = $this->createMock(IConfig::class);
  87. $this->languageFactory = $this->createMock(IFactory::class);
  88. $this->connector = new Principal(
  89. $this->userManager,
  90. $this->groupManager,
  91. $this->accountManager,
  92. $this->shareManager,
  93. $this->userSession,
  94. $this->appManager,
  95. $this->proxyMapper,
  96. $this->knownUserService,
  97. $this->config,
  98. $this->languageFactory
  99. );
  100. parent::setUp();
  101. }
  102. public function testGetPrincipalsByPrefixWithoutPrefix(): void {
  103. $response = $this->connector->getPrincipalsByPrefix('');
  104. $this->assertSame([], $response);
  105. }
  106. public function testGetPrincipalsByPrefixWithUsers(): void {
  107. $fooUser = $this->createMock(User::class);
  108. $fooUser
  109. ->expects($this->once())
  110. ->method('getUID')
  111. ->willReturn('foo');
  112. $fooUser
  113. ->expects($this->once())
  114. ->method('getDisplayName')
  115. ->willReturn('Dr. Foo-Bar');
  116. $fooUser
  117. ->expects($this->once())
  118. ->method('getSystemEMailAddress')
  119. ->willReturn('');
  120. $barUser = $this->createMock(User::class);
  121. $barUser
  122. ->expects($this->once())
  123. ->method('getUID')
  124. ->willReturn('bar');
  125. $barUser
  126. ->expects($this->once())
  127. ->method('getSystemEMailAddress')
  128. ->willReturn('bar@nextcloud.com');
  129. $this->userManager
  130. ->expects($this->once())
  131. ->method('search')
  132. ->with('')
  133. ->willReturn([$fooUser, $barUser]);
  134. $this->languageFactory
  135. ->expects($this->exactly(2))
  136. ->method('getUserLanguage')
  137. ->withConsecutive([$fooUser], [$barUser])
  138. ->willReturnOnConsecutiveCalls('de', 'en');
  139. $fooAccountPropertyCollection = $this->createMock(IAccountPropertyCollection::class);
  140. $fooAccountPropertyCollection->expects($this->once())
  141. ->method('getProperties')
  142. ->with()
  143. ->willReturn([]);
  144. $fooAccount = $this->createMock(IAccount::class);
  145. $fooAccount->expects($this->once())
  146. ->method('getPropertyCollection')
  147. ->with(IAccountManager::COLLECTION_EMAIL)
  148. ->willReturn($fooAccountPropertyCollection);
  149. $emailPropertyOne = $this->createMock(IAccountProperty::class);
  150. $emailPropertyOne->expects($this->once())
  151. ->method('getValue')
  152. ->with()
  153. ->willReturn('alias@nextcloud.com');
  154. $emailPropertyTwo = $this->createMock(IAccountProperty::class);
  155. $emailPropertyTwo->expects($this->once())
  156. ->method('getValue')
  157. ->with()
  158. ->willReturn('alias2@nextcloud.com');
  159. $barAccountPropertyCollection = $this->createMock(IAccountPropertyCollection::class);
  160. $barAccountPropertyCollection->expects($this->once())
  161. ->method('getProperties')
  162. ->with()
  163. ->willReturn([$emailPropertyOne, $emailPropertyTwo]);
  164. $barAccount = $this->createMock(IAccount::class);
  165. $barAccount->expects($this->once())
  166. ->method('getPropertyCollection')
  167. ->with(IAccountManager::COLLECTION_EMAIL)
  168. ->willReturn($barAccountPropertyCollection);
  169. $this->accountManager
  170. ->expects($this->exactly(2))
  171. ->method('getAccount')
  172. ->withConsecutive([$fooUser], [$barUser])
  173. ->willReturnOnConsecutiveCalls($fooAccount, $barAccount);
  174. $expectedResponse = [
  175. 0 => [
  176. 'uri' => 'principals/users/foo',
  177. '{DAV:}displayname' => 'Dr. Foo-Bar',
  178. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
  179. '{http://nextcloud.com/ns}language' => 'de',
  180. ],
  181. 1 => [
  182. 'uri' => 'principals/users/bar',
  183. '{DAV:}displayname' => 'bar',
  184. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
  185. '{http://nextcloud.com/ns}language' => 'en',
  186. '{http://sabredav.org/ns}email-address' => 'bar@nextcloud.com',
  187. '{DAV:}alternate-URI-set' => ['mailto:alias@nextcloud.com', 'mailto:alias2@nextcloud.com']
  188. ]
  189. ];
  190. $response = $this->connector->getPrincipalsByPrefix('principals/users');
  191. $this->assertSame($expectedResponse, $response);
  192. }
  193. public function testGetPrincipalsByPrefixEmpty(): void {
  194. $this->userManager
  195. ->expects($this->once())
  196. ->method('search')
  197. ->with('')
  198. ->willReturn([]);
  199. $response = $this->connector->getPrincipalsByPrefix('principals/users');
  200. $this->assertSame([], $response);
  201. }
  202. public function testGetPrincipalsByPathWithoutMail(): void {
  203. $fooUser = $this->createMock(User::class);
  204. $fooUser
  205. ->expects($this->once())
  206. ->method('getUID')
  207. ->willReturn('foo');
  208. $this->userManager
  209. ->expects($this->once())
  210. ->method('get')
  211. ->with('foo')
  212. ->willReturn($fooUser);
  213. $this->languageFactory
  214. ->expects($this->once())
  215. ->method('getUserLanguage')
  216. ->with($fooUser)
  217. ->willReturn('de');
  218. $expectedResponse = [
  219. 'uri' => 'principals/users/foo',
  220. '{DAV:}displayname' => 'foo',
  221. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
  222. '{http://nextcloud.com/ns}language' => 'de'
  223. ];
  224. $response = $this->connector->getPrincipalByPath('principals/users/foo');
  225. $this->assertSame($expectedResponse, $response);
  226. }
  227. public function testGetPrincipalsByPathWithMail(): void {
  228. $fooUser = $this->createMock(User::class);
  229. $fooUser
  230. ->expects($this->once())
  231. ->method('getSystemEMailAddress')
  232. ->willReturn('foo@nextcloud.com');
  233. $fooUser
  234. ->expects($this->once())
  235. ->method('getUID')
  236. ->willReturn('foo');
  237. $this->userManager
  238. ->expects($this->once())
  239. ->method('get')
  240. ->with('foo')
  241. ->willReturn($fooUser);
  242. $this->languageFactory
  243. ->expects($this->once())
  244. ->method('getUserLanguage')
  245. ->with($fooUser)
  246. ->willReturn('de');
  247. $expectedResponse = [
  248. 'uri' => 'principals/users/foo',
  249. '{DAV:}displayname' => 'foo',
  250. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
  251. '{http://nextcloud.com/ns}language' => 'de',
  252. '{http://sabredav.org/ns}email-address' => 'foo@nextcloud.com',
  253. ];
  254. $response = $this->connector->getPrincipalByPath('principals/users/foo');
  255. $this->assertSame($expectedResponse, $response);
  256. }
  257. public function testGetPrincipalsByPathEmpty(): void {
  258. $this->userManager
  259. ->expects($this->once())
  260. ->method('get')
  261. ->with('foo')
  262. ->willReturn(null);
  263. $response = $this->connector->getPrincipalByPath('principals/users/foo');
  264. $this->assertNull($response);
  265. }
  266. public function testGetGroupMemberSet(): void {
  267. $response = $this->connector->getGroupMemberSet('principals/users/foo');
  268. $this->assertSame([], $response);
  269. }
  270. public function testGetGroupMemberSetEmpty(): void {
  271. $this->expectException(Exception::class);
  272. $this->expectExceptionMessage('Principal not found');
  273. $this->userManager
  274. ->expects($this->once())
  275. ->method('get')
  276. ->with('foo')
  277. ->willReturn(null);
  278. $this->connector->getGroupMemberSet('principals/users/foo/calendar-proxy-read');
  279. }
  280. public function testGetGroupMemberSetProxyRead(): void {
  281. $fooUser = $this->createMock(User::class);
  282. $fooUser
  283. ->expects($this->once())
  284. ->method('getUID')
  285. ->willReturn('foo');
  286. $this->userManager
  287. ->expects($this->once())
  288. ->method('get')
  289. ->with('foo')
  290. ->willReturn($fooUser);
  291. $proxy1 = new Proxy();
  292. $proxy1->setProxyId('proxyId1');
  293. $proxy1->setPermissions(1);
  294. $proxy2 = new Proxy();
  295. $proxy2->setProxyId('proxyId2');
  296. $proxy2->setPermissions(3);
  297. $proxy3 = new Proxy();
  298. $proxy3->setProxyId('proxyId3');
  299. $proxy3->setPermissions(3);
  300. $this->proxyMapper->expects($this->once())
  301. ->method('getProxiesOf')
  302. ->with('principals/users/foo')
  303. ->willReturn([$proxy1, $proxy2, $proxy3]);
  304. $this->assertEquals(['proxyId1'], $this->connector->getGroupMemberSet('principals/users/foo/calendar-proxy-read'));
  305. }
  306. public function testGetGroupMemberSetProxyWrite(): void {
  307. $fooUser = $this->createMock(User::class);
  308. $fooUser
  309. ->expects($this->once())
  310. ->method('getUID')
  311. ->willReturn('foo');
  312. $this->userManager
  313. ->expects($this->once())
  314. ->method('get')
  315. ->with('foo')
  316. ->willReturn($fooUser);
  317. $proxy1 = new Proxy();
  318. $proxy1->setProxyId('proxyId1');
  319. $proxy1->setPermissions(1);
  320. $proxy2 = new Proxy();
  321. $proxy2->setProxyId('proxyId2');
  322. $proxy2->setPermissions(3);
  323. $proxy3 = new Proxy();
  324. $proxy3->setProxyId('proxyId3');
  325. $proxy3->setPermissions(3);
  326. $this->proxyMapper->expects($this->once())
  327. ->method('getProxiesOf')
  328. ->with('principals/users/foo')
  329. ->willReturn([$proxy1, $proxy2, $proxy3]);
  330. $this->assertEquals(['proxyId2', 'proxyId3'], $this->connector->getGroupMemberSet('principals/users/foo/calendar-proxy-write'));
  331. }
  332. public function testGetGroupMembership(): void {
  333. $fooUser = $this->createMock(User::class);
  334. $group1 = $this->createMock(IGroup::class);
  335. $group1->expects($this->once())
  336. ->method('getGID')
  337. ->willReturn('group1');
  338. $group2 = $this->createMock(IGroup::class);
  339. $group2->expects($this->once())
  340. ->method('getGID')
  341. ->willReturn('foo/bar');
  342. $this->userManager
  343. ->expects($this->exactly(2))
  344. ->method('get')
  345. ->with('foo')
  346. ->willReturn($fooUser);
  347. $this->groupManager
  348. ->expects($this->once())
  349. ->method('getUserGroups')
  350. ->with($fooUser)
  351. ->willReturn([
  352. $group1,
  353. $group2,
  354. ]);
  355. $proxy1 = new Proxy();
  356. $proxy1->setOwnerId('proxyId1');
  357. $proxy1->setPermissions(1);
  358. $proxy2 = new Proxy();
  359. $proxy2->setOwnerId('proxyId2');
  360. $proxy2->setPermissions(3);
  361. $this->proxyMapper->expects($this->once())
  362. ->method('getProxiesFor')
  363. ->with('principals/users/foo')
  364. ->willReturn([$proxy1, $proxy2]);
  365. $expectedResponse = [
  366. 'principals/groups/group1',
  367. 'principals/groups/foo%2Fbar',
  368. 'proxyId1/calendar-proxy-read',
  369. 'proxyId2/calendar-proxy-write',
  370. ];
  371. $response = $this->connector->getGroupMembership('principals/users/foo');
  372. $this->assertSame($expectedResponse, $response);
  373. }
  374. public function testGetGroupMembershipEmpty(): void {
  375. $this->expectException(Exception::class);
  376. $this->expectExceptionMessage('Principal not found');
  377. $this->userManager
  378. ->expects($this->once())
  379. ->method('get')
  380. ->with('foo')
  381. ->willReturn(null);
  382. $this->connector->getGroupMembership('principals/users/foo');
  383. }
  384. public function testSetGroupMembership(): void {
  385. $this->expectException(Exception::class);
  386. $this->expectExceptionMessage('Setting members of the group is not supported yet');
  387. $this->connector->setGroupMemberSet('principals/users/foo', ['foo']);
  388. }
  389. public function testSetGroupMembershipProxy(): void {
  390. $fooUser = $this->createMock(User::class);
  391. $fooUser
  392. ->expects($this->once())
  393. ->method('getUID')
  394. ->willReturn('foo');
  395. $barUser = $this->createMock(User::class);
  396. $barUser
  397. ->expects($this->once())
  398. ->method('getUID')
  399. ->willReturn('bar');
  400. $this->userManager
  401. ->expects($this->exactly(2))
  402. ->method('get')
  403. ->willReturnMap([
  404. ['foo', $fooUser],
  405. ['bar', $barUser],
  406. ]);
  407. $this->proxyMapper->expects($this->once())
  408. ->method('getProxiesOf')
  409. ->with('principals/users/foo')
  410. ->willReturn([]);
  411. $this->proxyMapper->expects($this->once())
  412. ->method('insert')
  413. ->with($this->callback(function ($proxy) {
  414. /** @var Proxy $proxy */
  415. if ($proxy->getOwnerId() !== 'principals/users/foo') {
  416. return false;
  417. }
  418. if ($proxy->getProxyId() !== 'principals/users/bar') {
  419. return false;
  420. }
  421. if ($proxy->getPermissions() !== 3) {
  422. return false;
  423. }
  424. return true;
  425. }));
  426. $this->connector->setGroupMemberSet('principals/users/foo/calendar-proxy-write', ['principals/users/bar']);
  427. }
  428. public function testUpdatePrincipal(): void {
  429. $this->assertSame(0, $this->connector->updatePrincipal('foo', new PropPatch([])));
  430. }
  431. public function testSearchPrincipalsWithEmptySearchProperties(): void {
  432. $this->assertSame([], $this->connector->searchPrincipals('principals/users', []));
  433. }
  434. public function testSearchPrincipalsWithWrongPrefixPath(): void {
  435. $this->assertSame([], $this->connector->searchPrincipals('principals/groups',
  436. ['{http://sabredav.org/ns}email-address' => 'foo']));
  437. }
  438. /**
  439. * @dataProvider searchPrincipalsDataProvider
  440. */
  441. public function testSearchPrincipals($sharingEnabled, $groupsOnly, $test, $result): void {
  442. $this->shareManager->expects($this->once())
  443. ->method('shareAPIEnabled')
  444. ->willReturn($sharingEnabled);
  445. $getUserGroupIdsReturnMap = [];
  446. if ($sharingEnabled) {
  447. $this->shareManager->expects($this->once())
  448. ->method('allowEnumeration')
  449. ->willReturn(true);
  450. $this->shareManager->expects($this->once())
  451. ->method('shareWithGroupMembersOnly')
  452. ->willReturn($groupsOnly);
  453. if ($groupsOnly) {
  454. $user = $this->createMock(IUser::class);
  455. $this->userSession->expects($this->atLeastOnce())
  456. ->method('getUser')
  457. ->willReturn($user);
  458. $getUserGroupIdsReturnMap[] = [$user, ['group1', 'group2', 'group5']];
  459. }
  460. } else {
  461. $this->config->expects($this->never())
  462. ->method('getAppValue');
  463. $this->shareManager->expects($this->never())
  464. ->method('shareWithGroupMembersOnly');
  465. $this->groupManager->expects($this->never())
  466. ->method($this->anything());
  467. }
  468. $user2 = $this->createMock(IUser::class);
  469. $user2->method('getUID')->willReturn('user2');
  470. $user3 = $this->createMock(IUser::class);
  471. $user3->method('getUID')->willReturn('user3');
  472. $user4 = $this->createMock(IUser::class);
  473. $user4->method('getUID')->willReturn('user4');
  474. if ($sharingEnabled) {
  475. $this->userManager->expects($this->once())
  476. ->method('getByEmail')
  477. ->with('user@example.com')
  478. ->willReturn([$user2, $user3]);
  479. $this->userManager->expects($this->once())
  480. ->method('searchDisplayName')
  481. ->with('User 12')
  482. ->willReturn([$user3, $user4]);
  483. } else {
  484. $this->userManager->expects($this->never())
  485. ->method('getByEmail');
  486. $this->userManager->expects($this->never())
  487. ->method('searchDisplayName');
  488. }
  489. if ($sharingEnabled && $groupsOnly) {
  490. $getUserGroupIdsReturnMap[] = [$user2, ['group1', 'group3']];
  491. $getUserGroupIdsReturnMap[] = [$user3, ['group3', 'group4']];
  492. $getUserGroupIdsReturnMap[] = [$user4, ['group4', 'group5']];
  493. }
  494. $this->groupManager->expects($this->any())
  495. ->method('getUserGroupIds')
  496. ->willReturnMap($getUserGroupIdsReturnMap);
  497. $this->assertEquals($result, $this->connector->searchPrincipals('principals/users',
  498. ['{http://sabredav.org/ns}email-address' => 'user@example.com',
  499. '{DAV:}displayname' => 'User 12'], $test));
  500. }
  501. public function searchPrincipalsDataProvider(): array {
  502. return [
  503. [true, false, 'allof', ['principals/users/user3']],
  504. [true, false, 'anyof', ['principals/users/user2', 'principals/users/user3', 'principals/users/user4']],
  505. [true, true, 'allof', []],
  506. [true, true, 'anyof', ['principals/users/user2', 'principals/users/user4']],
  507. [false, false, 'allof', []],
  508. [false, false, 'anyof', []],
  509. ];
  510. }
  511. public function testSearchPrincipalByCalendarUserAddressSet(): void {
  512. $this->shareManager->expects($this->exactly(2))
  513. ->method('shareAPIEnabled')
  514. ->willReturn(true);
  515. $this->shareManager->expects($this->exactly(2))
  516. ->method('allowEnumeration')
  517. ->willReturn(true);
  518. $this->shareManager->expects($this->exactly(2))
  519. ->method('shareWithGroupMembersOnly')
  520. ->willReturn(false);
  521. $user2 = $this->createMock(IUser::class);
  522. $user2->method('getUID')->willReturn('user2');
  523. $user3 = $this->createMock(IUser::class);
  524. $user3->method('getUID')->willReturn('user3');
  525. $this->userManager->expects($this->once())
  526. ->method('getByEmail')
  527. ->with('user@example.com')
  528. ->willReturn([$user2, $user3]);
  529. $this->assertEquals([
  530. 'principals/users/user2',
  531. 'principals/users/user3',
  532. ], $this->connector->searchPrincipals('principals/users',
  533. ['{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => 'user@example.com']));
  534. }
  535. public function testSearchPrincipalWithEnumerationDisabledDisplayname(): void {
  536. $this->shareManager->expects($this->once())
  537. ->method('shareAPIEnabled')
  538. ->willReturn(true);
  539. $this->shareManager->expects($this->once())
  540. ->method('allowEnumeration')
  541. ->willReturn(false);
  542. $this->shareManager->expects($this->once())
  543. ->method('shareWithGroupMembersOnly')
  544. ->willReturn(false);
  545. $this->shareManager->expects($this->once())
  546. ->method('allowEnumerationFullMatch')
  547. ->willReturn(true);
  548. $user2 = $this->createMock(IUser::class);
  549. $user2->method('getUID')->willReturn('user2');
  550. $user2->method('getDisplayName')->willReturn('User 2');
  551. $user2->method('getSystemEMailAddress')->willReturn('user2@foo.bar');
  552. $user3 = $this->createMock(IUser::class);
  553. $user3->method('getUID')->willReturn('user3');
  554. $user3->method('getDisplayName')->willReturn('User 22');
  555. $user3->method('getSystemEMailAddress')->willReturn('user2@foo.bar123');
  556. $user4 = $this->createMock(IUser::class);
  557. $user4->method('getUID')->willReturn('user4');
  558. $user4->method('getDisplayName')->willReturn('User 222');
  559. $user4->method('getSystemEMailAddress')->willReturn('user2@foo.bar456');
  560. $this->userManager->expects($this->once())
  561. ->method('searchDisplayName')
  562. ->with('User 2')
  563. ->willReturn([$user2, $user3, $user4]);
  564. $this->assertEquals(['principals/users/user2'], $this->connector->searchPrincipals('principals/users',
  565. ['{DAV:}displayname' => 'User 2']));
  566. }
  567. public function testSearchPrincipalWithEnumerationDisabledDisplaynameOnFullMatch(): void {
  568. $this->shareManager->expects($this->once())
  569. ->method('shareAPIEnabled')
  570. ->willReturn(true);
  571. $this->shareManager->expects($this->once())
  572. ->method('allowEnumeration')
  573. ->willReturn(false);
  574. $this->shareManager->expects($this->once())
  575. ->method('shareWithGroupMembersOnly')
  576. ->willReturn(false);
  577. $this->shareManager->expects($this->once())
  578. ->method('allowEnumerationFullMatch')
  579. ->willReturn(false);
  580. $this->assertEquals([], $this->connector->searchPrincipals('principals/users',
  581. ['{DAV:}displayname' => 'User 2']));
  582. }
  583. public function testSearchPrincipalWithEnumerationDisabledEmail(): void {
  584. $this->shareManager->expects($this->once())
  585. ->method('shareAPIEnabled')
  586. ->willReturn(true);
  587. $this->shareManager->expects($this->once())
  588. ->method('allowEnumeration')
  589. ->willReturn(false);
  590. $this->shareManager->expects($this->once())
  591. ->method('shareWithGroupMembersOnly')
  592. ->willReturn(false);
  593. $this->shareManager->expects($this->once())
  594. ->method('allowEnumerationFullMatch')
  595. ->willReturn(true);
  596. $this->shareManager->expects($this->once())
  597. ->method('matchEmail')
  598. ->willReturn(true);
  599. $user2 = $this->createMock(IUser::class);
  600. $user2->method('getUID')->willReturn('user2');
  601. $user2->method('getDisplayName')->willReturn('User 2');
  602. $user2->method('getSystemEMailAddress')->willReturn('user2@foo.bar');
  603. $user3 = $this->createMock(IUser::class);
  604. $user3->method('getUID')->willReturn('user3');
  605. $user2->method('getDisplayName')->willReturn('User 22');
  606. $user2->method('getSystemEMailAddress')->willReturn('user2@foo.bar123');
  607. $user4 = $this->createMock(IUser::class);
  608. $user4->method('getUID')->willReturn('user4');
  609. $user2->method('getDisplayName')->willReturn('User 222');
  610. $user2->method('getSystemEMailAddress')->willReturn('user2@foo.bar456');
  611. $this->userManager->expects($this->once())
  612. ->method('getByEmail')
  613. ->with('user2@foo.bar')
  614. ->willReturn([$user2]);
  615. $this->assertEquals(['principals/users/user2'], $this->connector->searchPrincipals('principals/users',
  616. ['{http://sabredav.org/ns}email-address' => 'user2@foo.bar']));
  617. }
  618. public function testSearchPrincipalWithEnumerationDisabledEmailOnFullMatch(): void {
  619. $this->shareManager->expects($this->once())
  620. ->method('shareAPIEnabled')
  621. ->willReturn(true);
  622. $this->shareManager->expects($this->once())
  623. ->method('allowEnumeration')
  624. ->willReturn(false);
  625. $this->shareManager->expects($this->once())
  626. ->method('shareWithGroupMembersOnly')
  627. ->willReturn(false);
  628. $this->shareManager->expects($this->once())
  629. ->method('allowEnumerationFullMatch')
  630. ->willReturn(false);
  631. $this->assertEquals([], $this->connector->searchPrincipals('principals/users',
  632. ['{http://sabredav.org/ns}email-address' => 'user2@foo.bar']));
  633. }
  634. public function testSearchPrincipalWithEnumerationLimitedDisplayname(): void {
  635. $this->shareManager->expects($this->once())
  636. ->method('shareAPIEnabled')
  637. ->willReturn(true);
  638. $this->shareManager->expects($this->once())
  639. ->method('allowEnumeration')
  640. ->willReturn(true);
  641. $this->shareManager->expects($this->once())
  642. ->method('limitEnumerationToGroups')
  643. ->willReturn(true);
  644. $this->shareManager->expects($this->once())
  645. ->method('shareWithGroupMembersOnly')
  646. ->willReturn(false);
  647. $user2 = $this->createMock(IUser::class);
  648. $user2->method('getUID')->willReturn('user2');
  649. $user2->method('getDisplayName')->willReturn('User 2');
  650. $user2->method('getSystemEMailAddress')->willReturn('user2@foo.bar');
  651. $user3 = $this->createMock(IUser::class);
  652. $user3->method('getUID')->willReturn('user3');
  653. $user3->method('getDisplayName')->willReturn('User 22');
  654. $user3->method('getSystemEMailAddress')->willReturn('user2@foo.bar123');
  655. $user4 = $this->createMock(IUser::class);
  656. $user4->method('getUID')->willReturn('user4');
  657. $user4->method('getDisplayName')->willReturn('User 222');
  658. $user4->method('getSystemEMailAddress')->willReturn('user2@foo.bar456');
  659. $this->userSession->expects($this->once())
  660. ->method('getUser')
  661. ->willReturn($user2);
  662. $this->groupManager->expects($this->exactly(4))
  663. ->method('getUserGroupIds')
  664. ->willReturnMap([
  665. [$user2, ['group1']],
  666. [$user3, ['group1']],
  667. [$user4, ['group2']],
  668. ]);
  669. $this->userManager->expects($this->once())
  670. ->method('searchDisplayName')
  671. ->with('User')
  672. ->willReturn([$user2, $user3, $user4]);
  673. $this->assertEquals([
  674. 'principals/users/user2',
  675. 'principals/users/user3',
  676. ], $this->connector->searchPrincipals('principals/users',
  677. ['{DAV:}displayname' => 'User']));
  678. }
  679. public function testSearchPrincipalWithEnumerationLimitedMail(): void {
  680. $this->shareManager->expects($this->once())
  681. ->method('shareAPIEnabled')
  682. ->willReturn(true);
  683. $this->shareManager->expects($this->once())
  684. ->method('allowEnumeration')
  685. ->willReturn(true);
  686. $this->shareManager->expects($this->once())
  687. ->method('limitEnumerationToGroups')
  688. ->willReturn(true);
  689. $this->shareManager->expects($this->once())
  690. ->method('shareWithGroupMembersOnly')
  691. ->willReturn(false);
  692. $user2 = $this->createMock(IUser::class);
  693. $user2->method('getUID')->willReturn('user2');
  694. $user2->method('getDisplayName')->willReturn('User 2');
  695. $user2->method('getSystemEMailAddress')->willReturn('user2@foo.bar');
  696. $user3 = $this->createMock(IUser::class);
  697. $user3->method('getUID')->willReturn('user3');
  698. $user3->method('getDisplayName')->willReturn('User 22');
  699. $user3->method('getSystemEMailAddress')->willReturn('user2@foo.bar123');
  700. $user4 = $this->createMock(IUser::class);
  701. $user4->method('getUID')->willReturn('user4');
  702. $user4->method('getDisplayName')->willReturn('User 222');
  703. $user4->method('getSystemEMailAddress')->willReturn('user2@foo.bar456');
  704. $this->userSession->expects($this->once())
  705. ->method('getUser')
  706. ->willReturn($user2);
  707. $this->groupManager->expects($this->exactly(4))
  708. ->method('getUserGroupIds')
  709. ->willReturnMap([
  710. [$user2, ['group1']],
  711. [$user3, ['group1']],
  712. [$user4, ['group2']],
  713. ]);
  714. $this->userManager->expects($this->once())
  715. ->method('getByEmail')
  716. ->with('user')
  717. ->willReturn([$user2, $user3, $user4]);
  718. $this->assertEquals([
  719. 'principals/users/user2',
  720. 'principals/users/user3'
  721. ], $this->connector->searchPrincipals('principals/users',
  722. ['{http://sabredav.org/ns}email-address' => 'user']));
  723. }
  724. public function testFindByUriSharingApiDisabled(): void {
  725. $this->shareManager->expects($this->once())
  726. ->method('shareApiEnabled')
  727. ->willReturn(false);
  728. $this->assertEquals(null, $this->connector->findByUri('mailto:user@foo.com', 'principals/users'));
  729. }
  730. /**
  731. * @dataProvider findByUriWithGroupRestrictionDataProvider
  732. */
  733. public function testFindByUriWithGroupRestriction($uri, $email, $expects): void {
  734. $this->shareManager->expects($this->once())
  735. ->method('shareApiEnabled')
  736. ->willReturn(true);
  737. $this->shareManager->expects($this->once())
  738. ->method('shareWithGroupMembersOnly')
  739. ->willReturn(true);
  740. $user = $this->createMock(IUser::class);
  741. $this->userSession->expects($this->once())
  742. ->method('getUser')
  743. ->willReturn($user);
  744. $user2 = $this->createMock(IUser::class);
  745. $user2->method('getUID')->willReturn('user2');
  746. $user3 = $this->createMock(IUser::class);
  747. $user3->method('getUID')->willReturn('user3');
  748. $this->userManager->expects($this->once())
  749. ->method('getByEmail')
  750. ->with($email)
  751. ->willReturn([$email === 'user2@foo.bar' ? $user2 : $user3]);
  752. if ($email === 'user2@foo.bar') {
  753. $this->groupManager->expects($this->exactly(2))
  754. ->method('getUserGroupIds')
  755. ->withConsecutive(
  756. [$user],
  757. [$user2],
  758. )
  759. ->willReturnOnConsecutiveCalls(
  760. ['group1', 'group2'],
  761. ['group1', 'group3'],
  762. );
  763. } else {
  764. $this->groupManager->expects($this->exactly(2))
  765. ->method('getUserGroupIds')
  766. ->withConsecutive(
  767. [$user],
  768. [$user3],
  769. )
  770. ->willReturnOnConsecutiveCalls(
  771. ['group1', 'group2'],
  772. ['group3', 'group3'],
  773. );
  774. }
  775. $this->assertEquals($expects, $this->connector->findByUri($uri, 'principals/users'));
  776. }
  777. public function findByUriWithGroupRestrictionDataProvider(): array {
  778. return [
  779. ['mailto:user2@foo.bar', 'user2@foo.bar', 'principals/users/user2'],
  780. ['mailto:user3@foo.bar', 'user3@foo.bar', null],
  781. ];
  782. }
  783. /**
  784. * @dataProvider findByUriWithoutGroupRestrictionDataProvider
  785. */
  786. public function testFindByUriWithoutGroupRestriction($uri, $email, $expects): void {
  787. $this->shareManager->expects($this->once())
  788. ->method('shareApiEnabled')
  789. ->willReturn(true);
  790. $this->shareManager->expects($this->once())
  791. ->method('shareWithGroupMembersOnly')
  792. ->willReturn(false);
  793. $user2 = $this->createMock(IUser::class);
  794. $user2->method('getUID')->willReturn('user2');
  795. $user3 = $this->createMock(IUser::class);
  796. $user3->method('getUID')->willReturn('user3');
  797. $this->userManager->expects($this->once())
  798. ->method('getByEmail')
  799. ->with($email)
  800. ->willReturn([$email === 'user2@foo.bar' ? $user2 : $user3]);
  801. $this->assertEquals($expects, $this->connector->findByUri($uri, 'principals/users'));
  802. }
  803. public function findByUriWithoutGroupRestrictionDataProvider(): array {
  804. return [
  805. ['mailto:user2@foo.bar', 'user2@foo.bar', 'principals/users/user2'],
  806. ['mailto:user3@foo.bar', 'user3@foo.bar', 'principals/users/user3'],
  807. ];
  808. }
  809. public function testGetEmailAddressesOfPrincipal(): void {
  810. $principal = [
  811. '{http://sabredav.org/ns}email-address' => 'bar@company.org',
  812. '{DAV:}alternate-URI-set' => [
  813. '/some/url',
  814. 'mailto:foo@bar.com',
  815. 'mailto:duplicate@example.com',
  816. ],
  817. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => [
  818. 'mailto:bernard@example.com',
  819. 'mailto:bernard.desruisseaux@example.com',
  820. ],
  821. '{http://calendarserver.org/ns/}email-address-set' => [
  822. 'mailto:duplicate@example.com',
  823. 'mailto:user@some.org',
  824. ],
  825. ];
  826. $expected = [
  827. 'bar@company.org',
  828. 'foo@bar.com',
  829. 'duplicate@example.com',
  830. 'bernard@example.com',
  831. 'bernard.desruisseaux@example.com',
  832. 'user@some.org',
  833. ];
  834. $actual = $this->connector->getEmailAddressesOfPrincipal($principal);
  835. $this->assertEquals($expected, $actual);
  836. }
  837. }