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.

PrincipalTest.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  31. use OC\User\User;
  32. use OCA\DAV\CalDAV\Proxy\Proxy;
  33. use OCA\DAV\CalDAV\Proxy\ProxyMapper;
  34. use OCP\App\IAppManager;
  35. use OCP\IConfig;
  36. use OCP\IGroup;
  37. use OCP\IGroupManager;
  38. use OCP\IUser;
  39. use OCP\IUserManager;
  40. use OCP\IUserSession;
  41. use OCP\Share\IManager;
  42. use Sabre\DAV\PropPatch;
  43. use Test\TestCase;
  44. class PrincipalTest extends TestCase {
  45. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
  46. private $userManager;
  47. /** @var \OCA\DAV\Connector\Sabre\Principal */
  48. private $connector;
  49. /** @var IGroupManager | \PHPUnit\Framework\MockObject\MockObject */
  50. private $groupManager;
  51. /** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
  52. private $shareManager;
  53. /** @var IUserSession | \PHPUnit\Framework\MockObject\MockObject */
  54. private $userSession;
  55. /** @var IAppManager | \PHPUnit\Framework\MockObject\MockObject */
  56. private $appManager;
  57. /** @var ProxyMapper | \PHPUnit\Framework\MockObject\MockObject */
  58. private $proxyMapper;
  59. /** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */
  60. private $config;
  61. protected function setUp(): void {
  62. $this->userManager = $this->createMock(IUserManager::class);
  63. $this->groupManager = $this->createMock(IGroupManager::class);
  64. $this->shareManager = $this->createMock(IManager::class);
  65. $this->userSession = $this->createMock(IUserSession::class);
  66. $this->appManager = $this->createMock(IAppManager::class);
  67. $this->proxyMapper = $this->createMock(ProxyMapper::class);
  68. $this->config = $this->createMock(IConfig::class);
  69. $this->connector = new \OCA\DAV\Connector\Sabre\Principal(
  70. $this->userManager,
  71. $this->groupManager,
  72. $this->shareManager,
  73. $this->userSession,
  74. $this->appManager,
  75. $this->proxyMapper,
  76. $this->config
  77. );
  78. parent::setUp();
  79. }
  80. public function testGetPrincipalsByPrefixWithoutPrefix() {
  81. $response = $this->connector->getPrincipalsByPrefix('');
  82. $this->assertSame([], $response);
  83. }
  84. public function testGetPrincipalsByPrefixWithUsers() {
  85. $fooUser = $this->createMock(User::class);
  86. $fooUser
  87. ->expects($this->exactly(1))
  88. ->method('getUID')
  89. ->willReturn('foo');
  90. $fooUser
  91. ->expects($this->exactly(1))
  92. ->method('getDisplayName')
  93. ->willReturn('Dr. Foo-Bar');
  94. $fooUser
  95. ->expects($this->exactly(1))
  96. ->method('getEMailAddress')
  97. ->willReturn('');
  98. $barUser = $this->createMock(User::class);
  99. $barUser
  100. ->expects($this->exactly(1))
  101. ->method('getUID')
  102. ->willReturn('bar');
  103. $barUser
  104. ->expects($this->exactly(1))
  105. ->method('getEMailAddress')
  106. ->willReturn('bar@nextcloud.com');
  107. $this->userManager
  108. ->expects($this->once())
  109. ->method('search')
  110. ->with('')
  111. ->willReturn([$fooUser, $barUser]);
  112. $expectedResponse = [
  113. 0 => [
  114. 'uri' => 'principals/users/foo',
  115. '{DAV:}displayname' => 'Dr. Foo-Bar',
  116. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
  117. ],
  118. 1 => [
  119. 'uri' => 'principals/users/bar',
  120. '{DAV:}displayname' => 'bar',
  121. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
  122. '{http://sabredav.org/ns}email-address' => 'bar@nextcloud.com',
  123. ]
  124. ];
  125. $response = $this->connector->getPrincipalsByPrefix('principals/users');
  126. $this->assertSame($expectedResponse, $response);
  127. }
  128. public function testGetPrincipalsByPrefixEmpty() {
  129. $this->userManager
  130. ->expects($this->once())
  131. ->method('search')
  132. ->with('')
  133. ->willReturn([]);
  134. $response = $this->connector->getPrincipalsByPrefix('principals/users');
  135. $this->assertSame([], $response);
  136. }
  137. public function testGetPrincipalsByPathWithoutMail() {
  138. $fooUser = $this->createMock(User::class);
  139. $fooUser
  140. ->expects($this->exactly(1))
  141. ->method('getUID')
  142. ->willReturn('foo');
  143. $this->userManager
  144. ->expects($this->once())
  145. ->method('get')
  146. ->with('foo')
  147. ->willReturn($fooUser);
  148. $expectedResponse = [
  149. 'uri' => 'principals/users/foo',
  150. '{DAV:}displayname' => 'foo',
  151. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
  152. ];
  153. $response = $this->connector->getPrincipalByPath('principals/users/foo');
  154. $this->assertSame($expectedResponse, $response);
  155. }
  156. public function testGetPrincipalsByPathWithMail() {
  157. $fooUser = $this->createMock(User::class);
  158. $fooUser
  159. ->expects($this->exactly(1))
  160. ->method('getEMailAddress')
  161. ->willReturn('foo@nextcloud.com');
  162. $fooUser
  163. ->expects($this->exactly(1))
  164. ->method('getUID')
  165. ->willReturn('foo');
  166. $this->userManager
  167. ->expects($this->once())
  168. ->method('get')
  169. ->with('foo')
  170. ->willReturn($fooUser);
  171. $expectedResponse = [
  172. 'uri' => 'principals/users/foo',
  173. '{DAV:}displayname' => 'foo',
  174. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
  175. '{http://sabredav.org/ns}email-address' => 'foo@nextcloud.com',
  176. ];
  177. $response = $this->connector->getPrincipalByPath('principals/users/foo');
  178. $this->assertSame($expectedResponse, $response);
  179. }
  180. public function testGetPrincipalsByPathEmpty() {
  181. $this->userManager
  182. ->expects($this->once())
  183. ->method('get')
  184. ->with('foo')
  185. ->willReturn(null);
  186. $response = $this->connector->getPrincipalByPath('principals/users/foo');
  187. $this->assertSame(null, $response);
  188. }
  189. public function testGetGroupMemberSet() {
  190. $response = $this->connector->getGroupMemberSet('principals/users/foo');
  191. $this->assertSame([], $response);
  192. }
  193. public function testGetGroupMemberSetEmpty() {
  194. $this->expectException(\Sabre\DAV\Exception::class);
  195. $this->expectExceptionMessage('Principal not found');
  196. $this->userManager
  197. ->expects($this->once())
  198. ->method('get')
  199. ->with('foo')
  200. ->willReturn(null);
  201. $this->connector->getGroupMemberSet('principals/users/foo/calendar-proxy-read');
  202. }
  203. public function testGetGroupMemberSetProxyRead() {
  204. $fooUser = $this->createMock(User::class);
  205. $fooUser
  206. ->expects($this->exactly(1))
  207. ->method('getUID')
  208. ->willReturn('foo');
  209. $this->userManager
  210. ->expects($this->once())
  211. ->method('get')
  212. ->with('foo')
  213. ->willReturn($fooUser);
  214. $proxy1 = new Proxy();
  215. $proxy1->setProxyId('proxyId1');
  216. $proxy1->setPermissions(1);
  217. $proxy2 = new Proxy();
  218. $proxy2->setProxyId('proxyId2');
  219. $proxy2->setPermissions(3);
  220. $proxy3 = new Proxy();
  221. $proxy3->setProxyId('proxyId3');
  222. $proxy3->setPermissions(3);
  223. $this->proxyMapper->expects($this->once())
  224. ->method('getProxiesOf')
  225. ->with('principals/users/foo')
  226. ->willReturn([$proxy1, $proxy2, $proxy3]);
  227. $this->assertEquals(['proxyId1'], $this->connector->getGroupMemberSet('principals/users/foo/calendar-proxy-read'));
  228. }
  229. public function testGetGroupMemberSetProxyWrite() {
  230. $fooUser = $this->createMock(User::class);
  231. $fooUser
  232. ->expects($this->exactly(1))
  233. ->method('getUID')
  234. ->willReturn('foo');
  235. $this->userManager
  236. ->expects($this->once())
  237. ->method('get')
  238. ->with('foo')
  239. ->willReturn($fooUser);
  240. $proxy1 = new Proxy();
  241. $proxy1->setProxyId('proxyId1');
  242. $proxy1->setPermissions(1);
  243. $proxy2 = new Proxy();
  244. $proxy2->setProxyId('proxyId2');
  245. $proxy2->setPermissions(3);
  246. $proxy3 = new Proxy();
  247. $proxy3->setProxyId('proxyId3');
  248. $proxy3->setPermissions(3);
  249. $this->proxyMapper->expects($this->once())
  250. ->method('getProxiesOf')
  251. ->with('principals/users/foo')
  252. ->willReturn([$proxy1, $proxy2, $proxy3]);
  253. $this->assertEquals(['proxyId2', 'proxyId3'], $this->connector->getGroupMemberSet('principals/users/foo/calendar-proxy-write'));
  254. }
  255. public function testGetGroupMembership() {
  256. $fooUser = $this->createMock(User::class);
  257. $group1 = $this->createMock(IGroup::class);
  258. $group1->expects($this->once())
  259. ->method('getGID')
  260. ->willReturn('group1');
  261. $group2 = $this->createMock(IGroup::class);
  262. $group2->expects($this->once())
  263. ->method('getGID')
  264. ->willReturn('foo/bar');
  265. $this->userManager
  266. ->expects($this->exactly(2))
  267. ->method('get')
  268. ->with('foo')
  269. ->willReturn($fooUser);
  270. $this->groupManager
  271. ->expects($this->once())
  272. ->method('getUserGroups')
  273. ->with($fooUser)
  274. ->willReturn([
  275. $group1,
  276. $group2,
  277. ]);
  278. $proxy1 = new Proxy();
  279. $proxy1->setOwnerId('proxyId1');
  280. $proxy1->setPermissions(1);
  281. $proxy2 = new Proxy();
  282. $proxy2->setOwnerId('proxyId2');
  283. $proxy2->setPermissions(3);
  284. $this->proxyMapper->expects($this->once())
  285. ->method('getProxiesFor')
  286. ->with('principals/users/foo')
  287. ->willReturn([$proxy1, $proxy2]);
  288. $expectedResponse = [
  289. 'principals/groups/group1',
  290. 'principals/groups/foo%2Fbar',
  291. 'proxyId1/calendar-proxy-read',
  292. 'proxyId2/calendar-proxy-write',
  293. ];
  294. $response = $this->connector->getGroupMembership('principals/users/foo');
  295. $this->assertSame($expectedResponse, $response);
  296. }
  297. public function testGetGroupMembershipEmpty() {
  298. $this->expectException(\Sabre\DAV\Exception::class);
  299. $this->expectExceptionMessage('Principal not found');
  300. $this->userManager
  301. ->expects($this->once())
  302. ->method('get')
  303. ->with('foo')
  304. ->willReturn(null);
  305. $this->connector->getGroupMembership('principals/users/foo');
  306. }
  307. public function testSetGroupMembership() {
  308. $this->expectException(\Sabre\DAV\Exception::class);
  309. $this->expectExceptionMessage('Setting members of the group is not supported yet');
  310. $this->connector->setGroupMemberSet('principals/users/foo', ['foo']);
  311. }
  312. public function testSetGroupMembershipProxy() {
  313. $fooUser = $this->createMock(User::class);
  314. $fooUser
  315. ->expects($this->exactly(1))
  316. ->method('getUID')
  317. ->willReturn('foo');
  318. $barUser = $this->createMock(User::class);
  319. $barUser
  320. ->expects($this->exactly(1))
  321. ->method('getUID')
  322. ->willReturn('bar');
  323. $this->userManager
  324. ->expects($this->at(0))
  325. ->method('get')
  326. ->with('foo')
  327. ->willReturn($fooUser);
  328. $this->userManager
  329. ->expects($this->at(1))
  330. ->method('get')
  331. ->with('bar')
  332. ->willReturn($barUser);
  333. $this->proxyMapper->expects($this->at(0))
  334. ->method('getProxiesOf')
  335. ->with('principals/users/foo')
  336. ->willReturn([]);
  337. $this->proxyMapper->expects($this->at(1))
  338. ->method('insert')
  339. ->with($this->callback(function ($proxy) {
  340. /** @var Proxy $proxy */
  341. if ($proxy->getOwnerId() !== 'principals/users/foo') {
  342. return false;
  343. }
  344. if ($proxy->getProxyId() !== 'principals/users/bar') {
  345. return false;
  346. }
  347. if ($proxy->getPermissions() !== 3) {
  348. return false;
  349. }
  350. return true;
  351. }));
  352. $this->connector->setGroupMemberSet('principals/users/foo/calendar-proxy-write', ['principals/users/bar']);
  353. }
  354. public function testUpdatePrincipal() {
  355. $this->assertSame(0, $this->connector->updatePrincipal('foo', new PropPatch([])));
  356. }
  357. public function testSearchPrincipalsWithEmptySearchProperties() {
  358. $this->assertSame([], $this->connector->searchPrincipals('principals/users', []));
  359. }
  360. public function testSearchPrincipalsWithWrongPrefixPath() {
  361. $this->assertSame([], $this->connector->searchPrincipals('principals/groups',
  362. ['{http://sabredav.org/ns}email-address' => 'foo']));
  363. }
  364. /**
  365. * @dataProvider searchPrincipalsDataProvider
  366. */
  367. public function testSearchPrincipals($sharingEnabled, $groupsOnly, $test, $result) {
  368. $this->shareManager->expects($this->once())
  369. ->method('shareAPIEnabled')
  370. ->willReturn($sharingEnabled);
  371. if ($sharingEnabled) {
  372. $this->shareManager->expects($this->once())
  373. ->method('allowEnumeration')
  374. ->willReturn(true);
  375. $this->shareManager->expects($this->once())
  376. ->method('shareWithGroupMembersOnly')
  377. ->willReturn($groupsOnly);
  378. if ($groupsOnly) {
  379. $user = $this->createMock(IUser::class);
  380. $this->userSession->expects($this->once())
  381. ->method('getUser')
  382. ->willReturn($user);
  383. $this->groupManager->expects($this->at(0))
  384. ->method('getUserGroupIds')
  385. ->with($user)
  386. ->willReturn(['group1', 'group2', 'group5']);
  387. }
  388. } else {
  389. $this->config->expects($this->never())
  390. ->method('getAppValue');
  391. $this->shareManager->expects($this->never())
  392. ->method('shareWithGroupMembersOnly');
  393. $this->groupManager->expects($this->never())
  394. ->method($this->anything());
  395. }
  396. $user2 = $this->createMock(IUser::class);
  397. $user2->method('getUID')->willReturn('user2');
  398. $user3 = $this->createMock(IUser::class);
  399. $user3->method('getUID')->willReturn('user3');
  400. $user4 = $this->createMock(IUser::class);
  401. $user4->method('getUID')->willReturn('user4');
  402. if ($sharingEnabled) {
  403. $this->userManager->expects($this->at(0))
  404. ->method('getByEmail')
  405. ->with('user@example.com')
  406. ->willReturn([$user2, $user3]);
  407. $this->userManager->expects($this->at(1))
  408. ->method('searchDisplayName')
  409. ->with('User 12')
  410. ->willReturn([$user3, $user4]);
  411. } else {
  412. $this->userManager->expects($this->never())
  413. ->method('getByEmail');
  414. $this->userManager->expects($this->never())
  415. ->method('searchDisplayName');
  416. }
  417. if ($sharingEnabled && $groupsOnly) {
  418. $this->groupManager->expects($this->at(1))
  419. ->method('getUserGroupIds')
  420. ->with($user2)
  421. ->willReturn(['group1', 'group3']);
  422. $this->groupManager->expects($this->at(2))
  423. ->method('getUserGroupIds')
  424. ->with($user3)
  425. ->willReturn(['group3', 'group4']);
  426. $this->groupManager->expects($this->at(3))
  427. ->method('getUserGroupIds')
  428. ->with($user3)
  429. ->willReturn(['group3', 'group4']);
  430. $this->groupManager->expects($this->at(4))
  431. ->method('getUserGroupIds')
  432. ->with($user4)
  433. ->willReturn(['group4', 'group5']);
  434. }
  435. $this->assertEquals($result, $this->connector->searchPrincipals('principals/users',
  436. ['{http://sabredav.org/ns}email-address' => 'user@example.com',
  437. '{DAV:}displayname' => 'User 12'], $test));
  438. }
  439. public function searchPrincipalsDataProvider() {
  440. return [
  441. [true, false, 'allof', ['principals/users/user3']],
  442. [true, false, 'anyof', ['principals/users/user2', 'principals/users/user3', 'principals/users/user4']],
  443. [true, true, 'allof', []],
  444. [true, true, 'anyof', ['principals/users/user2', 'principals/users/user4']],
  445. [false, false, 'allof', []],
  446. [false, false, 'anyof', []],
  447. ];
  448. }
  449. public function testSearchPrincipalByCalendarUserAddressSet() {
  450. $this->shareManager->expects($this->exactly(2))
  451. ->method('shareAPIEnabled')
  452. ->willReturn(true);
  453. $this->shareManager->expects($this->exactly(2))
  454. ->method('allowEnumeration')
  455. ->willReturn(true);
  456. $this->shareManager->expects($this->exactly(2))
  457. ->method('shareWithGroupMembersOnly')
  458. ->willReturn(false);
  459. $user2 = $this->createMock(IUser::class);
  460. $user2->method('getUID')->willReturn('user2');
  461. $user3 = $this->createMock(IUser::class);
  462. $user3->method('getUID')->willReturn('user3');
  463. $this->userManager->expects($this->at(0))
  464. ->method('getByEmail')
  465. ->with('user@example.com')
  466. ->willReturn([$user2, $user3]);
  467. $this->assertEquals([
  468. 'principals/users/user2',
  469. 'principals/users/user3',
  470. ], $this->connector->searchPrincipals('principals/users',
  471. ['{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => 'user@example.com']));
  472. }
  473. public function testSearchPrincipalWithEnumerationDisabledDisplayname() {
  474. $this->shareManager->expects($this->once())
  475. ->method('shareAPIEnabled')
  476. ->willReturn(true);
  477. $this->shareManager->expects($this->once())
  478. ->method('allowEnumeration')
  479. ->willReturn(false);
  480. $this->shareManager->expects($this->once())
  481. ->method('shareWithGroupMembersOnly')
  482. ->willReturn(false);
  483. $user2 = $this->createMock(IUser::class);
  484. $user2->method('getUID')->willReturn('user2');
  485. $user2->method('getDisplayName')->willReturn('User 2');
  486. $user2->method('getEMailAddress')->willReturn('user2@foo.bar');
  487. $user3 = $this->createMock(IUser::class);
  488. $user3->method('getUID')->willReturn('user3');
  489. $user2->method('getDisplayName')->willReturn('User 22');
  490. $user2->method('getEMailAddress')->willReturn('user2@foo.bar123');
  491. $user4 = $this->createMock(IUser::class);
  492. $user4->method('getUID')->willReturn('user4');
  493. $user2->method('getDisplayName')->willReturn('User 222');
  494. $user2->method('getEMailAddress')->willReturn('user2@foo.bar456');
  495. $this->userManager->expects($this->at(0))
  496. ->method('searchDisplayName')
  497. ->with('User 2')
  498. ->willReturn([$user2, $user3, $user4]);
  499. $this->assertEquals(['principals/users/user2'], $this->connector->searchPrincipals('principals/users',
  500. ['{DAV:}displayname' => 'User 2']));
  501. }
  502. public function testSearchPrincipalWithEnumerationDisabledEmail() {
  503. $this->shareManager->expects($this->once())
  504. ->method('shareAPIEnabled')
  505. ->willReturn(true);
  506. $this->shareManager->expects($this->once())
  507. ->method('allowEnumeration')
  508. ->willReturn(false);
  509. $this->shareManager->expects($this->once())
  510. ->method('shareWithGroupMembersOnly')
  511. ->willReturn(false);
  512. $user2 = $this->createMock(IUser::class);
  513. $user2->method('getUID')->willReturn('user2');
  514. $user2->method('getDisplayName')->willReturn('User 2');
  515. $user2->method('getEMailAddress')->willReturn('user2@foo.bar');
  516. $user3 = $this->createMock(IUser::class);
  517. $user3->method('getUID')->willReturn('user3');
  518. $user2->method('getDisplayName')->willReturn('User 22');
  519. $user2->method('getEMailAddress')->willReturn('user2@foo.bar123');
  520. $user4 = $this->createMock(IUser::class);
  521. $user4->method('getUID')->willReturn('user4');
  522. $user2->method('getDisplayName')->willReturn('User 222');
  523. $user2->method('getEMailAddress')->willReturn('user2@foo.bar456');
  524. $this->userManager->expects($this->at(0))
  525. ->method('getByEmail')
  526. ->with('user2@foo.bar')
  527. ->willReturn([$user2, $user3, $user4]);
  528. $this->assertEquals(['principals/users/user2'], $this->connector->searchPrincipals('principals/users',
  529. ['{http://sabredav.org/ns}email-address' => 'user2@foo.bar']));
  530. }
  531. public function testSearchPrincipalWithEnumerationLimitedDisplayname() {
  532. $this->shareManager->expects($this->at(0))
  533. ->method('shareAPIEnabled')
  534. ->willReturn(true);
  535. $this->shareManager->expects($this->at(1))
  536. ->method('allowEnumeration')
  537. ->willReturn(true);
  538. $this->shareManager->expects($this->at(2))
  539. ->method('limitEnumerationToGroups')
  540. ->willReturn(true);
  541. $this->shareManager->expects($this->once())
  542. ->method('shareWithGroupMembersOnly')
  543. ->willReturn(false);
  544. $user2 = $this->createMock(IUser::class);
  545. $user2->method('getUID')->willReturn('user2');
  546. $user2->method('getDisplayName')->willReturn('User 2');
  547. $user2->method('getEMailAddress')->willReturn('user2@foo.bar');
  548. $user3 = $this->createMock(IUser::class);
  549. $user3->method('getUID')->willReturn('user3');
  550. $user3->method('getDisplayName')->willReturn('User 22');
  551. $user3->method('getEMailAddress')->willReturn('user2@foo.bar123');
  552. $user4 = $this->createMock(IUser::class);
  553. $user4->method('getUID')->willReturn('user4');
  554. $user4->method('getDisplayName')->willReturn('User 222');
  555. $user4->method('getEMailAddress')->willReturn('user2@foo.bar456');
  556. $this->userSession->expects($this->at(0))
  557. ->method('getUser')
  558. ->willReturn($user2);
  559. $this->groupManager->expects($this->at(0))
  560. ->method('getUserGroupIds')
  561. ->willReturn(['group1']);
  562. $this->groupManager->expects($this->at(1))
  563. ->method('getUserGroupIds')
  564. ->willReturn(['group1']);
  565. $this->groupManager->expects($this->at(2))
  566. ->method('getUserGroupIds')
  567. ->willReturn(['group1']);
  568. $this->groupManager->expects($this->at(3))
  569. ->method('getUserGroupIds')
  570. ->willReturn(['group2']);
  571. $this->userManager->expects($this->at(0))
  572. ->method('searchDisplayName')
  573. ->with('User')
  574. ->willReturn([$user2, $user3, $user4]);
  575. $this->assertEquals([
  576. 'principals/users/user2',
  577. 'principals/users/user3',
  578. ], $this->connector->searchPrincipals('principals/users',
  579. ['{DAV:}displayname' => 'User']));
  580. }
  581. public function testSearchPrincipalWithEnumerationLimitedMail() {
  582. $this->shareManager->expects($this->at(0))
  583. ->method('shareAPIEnabled')
  584. ->willReturn(true);
  585. $this->shareManager->expects($this->at(1))
  586. ->method('allowEnumeration')
  587. ->willReturn(true);
  588. $this->shareManager->expects($this->at(2))
  589. ->method('limitEnumerationToGroups')
  590. ->willReturn(true);
  591. $this->shareManager->expects($this->once())
  592. ->method('shareWithGroupMembersOnly')
  593. ->willReturn(false);
  594. $user2 = $this->createMock(IUser::class);
  595. $user2->method('getUID')->willReturn('user2');
  596. $user2->method('getDisplayName')->willReturn('User 2');
  597. $user2->method('getEMailAddress')->willReturn('user2@foo.bar');
  598. $user3 = $this->createMock(IUser::class);
  599. $user3->method('getUID')->willReturn('user3');
  600. $user3->method('getDisplayName')->willReturn('User 22');
  601. $user3->method('getEMailAddress')->willReturn('user2@foo.bar123');
  602. $user4 = $this->createMock(IUser::class);
  603. $user4->method('getUID')->willReturn('user4');
  604. $user4->method('getDisplayName')->willReturn('User 222');
  605. $user4->method('getEMailAddress')->willReturn('user2@foo.bar456');
  606. $this->userSession->expects($this->at(0))
  607. ->method('getUser')
  608. ->willReturn($user2);
  609. $this->groupManager->expects($this->at(0))
  610. ->method('getUserGroupIds')
  611. ->willReturn(['group1']);
  612. $this->groupManager->expects($this->at(1))
  613. ->method('getUserGroupIds')
  614. ->willReturn(['group1']);
  615. $this->groupManager->expects($this->at(2))
  616. ->method('getUserGroupIds')
  617. ->willReturn(['group1']);
  618. $this->groupManager->expects($this->at(3))
  619. ->method('getUserGroupIds')
  620. ->willReturn(['group2']);
  621. $this->userManager->expects($this->at(0))
  622. ->method('getByEmail')
  623. ->with('user')
  624. ->willReturn([$user2, $user3, $user4]);
  625. $this->assertEquals([
  626. 'principals/users/user2',
  627. 'principals/users/user3'
  628. ], $this->connector->searchPrincipals('principals/users',
  629. ['{http://sabredav.org/ns}email-address' => 'user']));
  630. }
  631. public function testFindByUriSharingApiDisabled() {
  632. $this->shareManager->expects($this->once())
  633. ->method('shareApiEnabled')
  634. ->willReturn(false);
  635. $this->assertEquals(null, $this->connector->findByUri('mailto:user@foo.com', 'principals/users'));
  636. }
  637. /**
  638. * @dataProvider findByUriWithGroupRestrictionDataProvider
  639. */
  640. public function testFindByUriWithGroupRestriction($uri, $email, $expects) {
  641. $this->shareManager->expects($this->once())
  642. ->method('shareApiEnabled')
  643. ->willReturn(true);
  644. $this->shareManager->expects($this->once())
  645. ->method('shareWithGroupMembersOnly')
  646. ->willReturn(true);
  647. $user = $this->createMock(IUser::class);
  648. $this->userSession->expects($this->once())
  649. ->method('getUser')
  650. ->willReturn($user);
  651. $this->groupManager->expects($this->at(0))
  652. ->method('getUserGroupIds')
  653. ->with($user)
  654. ->willReturn(['group1', 'group2']);
  655. $user2 = $this->createMock(IUser::class);
  656. $user2->method('getUID')->willReturn('user2');
  657. $user3 = $this->createMock(IUser::class);
  658. $user3->method('getUID')->willReturn('user3');
  659. $this->userManager->expects($this->once())
  660. ->method('getByEmail')
  661. ->with($email)
  662. ->willReturn([$email === 'user2@foo.bar' ? $user2 : $user3]);
  663. if ($email === 'user2@foo.bar') {
  664. $this->groupManager->expects($this->at(1))
  665. ->method('getUserGroupIds')
  666. ->with($user2)
  667. ->willReturn(['group1', 'group3']);
  668. } else {
  669. $this->groupManager->expects($this->at(1))
  670. ->method('getUserGroupIds')
  671. ->with($user3)
  672. ->willReturn(['group3', 'group3']);
  673. }
  674. $this->assertEquals($expects, $this->connector->findByUri($uri, 'principals/users'));
  675. }
  676. public function findByUriWithGroupRestrictionDataProvider() {
  677. return [
  678. ['mailto:user2@foo.bar', 'user2@foo.bar', 'principals/users/user2'],
  679. ['mailto:user3@foo.bar', 'user3@foo.bar', null],
  680. ];
  681. }
  682. /**
  683. * @dataProvider findByUriWithoutGroupRestrictionDataProvider
  684. */
  685. public function testFindByUriWithoutGroupRestriction($uri, $email, $expects) {
  686. $this->shareManager->expects($this->once())
  687. ->method('shareApiEnabled')
  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. $user3 = $this->createMock(IUser::class);
  695. $user3->method('getUID')->willReturn('user3');
  696. $this->userManager->expects($this->once())
  697. ->method('getByEmail')
  698. ->with($email)
  699. ->willReturn([$email === 'user2@foo.bar' ? $user2 : $user3]);
  700. $this->assertEquals($expects, $this->connector->findByUri($uri, 'principals/users'));
  701. }
  702. public function findByUriWithoutGroupRestrictionDataProvider() {
  703. return [
  704. ['mailto:user2@foo.bar', 'user2@foo.bar', 'principals/users/user2'],
  705. ['mailto:user3@foo.bar', 'user3@foo.bar', 'principals/users/user3'],
  706. ];
  707. }
  708. }