PrincipalTest.php 29 KB

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