Group_LDAPTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Frédéric Fortier <frederic.fortier@oronospolytechnique.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  15. * @author Xuanwo <xuanwo@yunify.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OCA\User_LDAP\Tests;
  33. use OCA\User_LDAP\GroupPluginManager;
  34. use OCP\GroupInterface;
  35. use OCA\User_LDAP\Access;
  36. use OCA\User_LDAP\Connection;
  37. use OCA\User_LDAP\Group_LDAP as GroupLDAP;
  38. use OCA\User_LDAP\ILDAPWrapper;
  39. use OCA\User_LDAP\User\Manager;
  40. /**
  41. * Class GroupLDAPTest
  42. *
  43. * @group DB
  44. *
  45. * @package OCA\User_LDAP\Tests
  46. */
  47. class Group_LDAPTest extends \Test\TestCase {
  48. /**
  49. * @return \PHPUnit_Framework_MockObject_MockObject|Access
  50. */
  51. private function getAccessMock() {
  52. static $conMethods;
  53. static $accMethods;
  54. if(is_null($conMethods) || is_null($accMethods)) {
  55. $conMethods = get_class_methods('\OCA\User_LDAP\Connection');
  56. $accMethods = get_class_methods('\OCA\User_LDAP\Access');
  57. }
  58. $lw = $this->createMock(ILDAPWrapper::class);
  59. $connector = $this->getMockBuilder('\OCA\User_LDAP\Connection')
  60. ->setMethods($conMethods)
  61. ->setConstructorArgs([$lw, null, null])
  62. ->getMock();
  63. $access = $this->createMock(Access::class);
  64. $access->expects($this->any())
  65. ->method('getConnection')
  66. ->will($this->returnValue($connector));
  67. return $access;
  68. }
  69. private function getPluginManagerMock() {
  70. return $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')->getMock();
  71. }
  72. /**
  73. * @param Access|\PHPUnit_Framework_MockObject_MockObject $access
  74. */
  75. private function enableGroups($access) {
  76. $access->connection = $this->createMock(Connection::class);
  77. $access->connection->expects($this->any())
  78. ->method('__get')
  79. ->will($this->returnCallback(function($name) {
  80. if($name === 'ldapDynamicGroupMemberURL') {
  81. return '';
  82. }
  83. return 1;
  84. }));
  85. }
  86. public function testCountEmptySearchString() {
  87. $access = $this->getAccessMock();
  88. $pluginManager = $this->getPluginManagerMock();
  89. $this->enableGroups($access);
  90. $access->expects($this->any())
  91. ->method('groupname2dn')
  92. ->will($this->returnValue('cn=group,dc=foo,dc=bar'));
  93. $access->expects($this->any())
  94. ->method('readAttribute')
  95. ->will($this->returnValue(array('u11', 'u22', 'u33', 'u34')));
  96. // for primary groups
  97. $access->expects($this->once())
  98. ->method('countUsers')
  99. ->will($this->returnValue(2));
  100. $groupBackend = new GroupLDAP($access, $pluginManager);
  101. $users = $groupBackend->countUsersInGroup('group');
  102. $this->assertSame(6, $users);
  103. }
  104. public function testCountWithSearchString() {
  105. $access = $this->getAccessMock();
  106. $pluginManager = $this->getPluginManagerMock();
  107. $this->enableGroups($access);
  108. $access->expects($this->any())
  109. ->method('groupname2dn')
  110. ->will($this->returnValue('cn=group,dc=foo,dc=bar'));
  111. $access->expects($this->any())
  112. ->method('fetchListOfUsers')
  113. ->will($this->returnValue(array()));
  114. $access->expects($this->any())
  115. ->method('readAttribute')
  116. ->will($this->returnCallback(function($name) {
  117. //the search operation will call readAttribute, thus we need
  118. //to anaylze the "dn". All other times we just need to return
  119. //something that is neither null or false, but once an array
  120. //with the users in the group – so we do so all other times for
  121. //simplicicity.
  122. if(strpos($name, 'u') === 0) {
  123. return strpos($name, '3');
  124. }
  125. return array('u11', 'u22', 'u33', 'u34');
  126. }));
  127. $access->expects($this->any())
  128. ->method('dn2username')
  129. ->will($this->returnCallback(function() {
  130. return 'foobar' . \OC::$server->getSecureRandom()->generate(7);
  131. }));
  132. $groupBackend = new GroupLDAP($access,$pluginManager);
  133. $users = $groupBackend->countUsersInGroup('group', '3');
  134. $this->assertSame(2, $users);
  135. }
  136. public function testCountUsersWithPlugin() {
  137. /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  138. $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
  139. ->setMethods(['implementsActions','countUsersInGroup'])
  140. ->getMock();
  141. $pluginManager->expects($this->once())
  142. ->method('implementsActions')
  143. ->with(GroupInterface::COUNT_USERS)
  144. ->willReturn(true);
  145. $pluginManager->expects($this->once())
  146. ->method('countUsersInGroup')
  147. ->with('gid', 'search')
  148. ->willReturn(42);
  149. $access = $this->getAccessMock();
  150. $access->connection = $this->createMock(Connection::class);
  151. $ldap = new GroupLDAP($access, $pluginManager);
  152. $this->assertEquals($ldap->countUsersInGroup('gid', 'search'),42);
  153. }
  154. public function testGidNumber2NameSuccess() {
  155. $access = $this->getAccessMock();
  156. $pluginManager = $this->getPluginManagerMock();
  157. $this->enableGroups($access);
  158. $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar';
  159. $access->expects($this->once())
  160. ->method('searchGroups')
  161. ->will($this->returnValue([['dn' => ['cn=foo,dc=barfoo,dc=bar']]]));
  162. $access->expects($this->once())
  163. ->method('dn2groupname')
  164. ->with('cn=foo,dc=barfoo,dc=bar')
  165. ->will($this->returnValue('MyGroup'));
  166. $groupBackend = new GroupLDAP($access, $pluginManager);
  167. $group = $groupBackend->gidNumber2Name('3117', $userDN);
  168. $this->assertSame('MyGroup', $group);
  169. }
  170. public function testGidNumberID2NameNoGroup() {
  171. $access = $this->getAccessMock();
  172. $pluginManager = $this->getPluginManagerMock();
  173. $this->enableGroups($access);
  174. $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar';
  175. $access->expects($this->once())
  176. ->method('searchGroups')
  177. ->will($this->returnValue(array()));
  178. $access->expects($this->never())
  179. ->method('dn2groupname');
  180. $groupBackend = new GroupLDAP($access, $pluginManager);
  181. $group = $groupBackend->gidNumber2Name('3117', $userDN);
  182. $this->assertSame(false, $group);
  183. }
  184. public function testGidNumberID2NameNoName() {
  185. $access = $this->getAccessMock();
  186. $pluginManager = $this->getPluginManagerMock();
  187. $this->enableGroups($access);
  188. $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar';
  189. $access->expects($this->once())
  190. ->method('searchGroups')
  191. ->will($this->returnValue([['dn' => ['cn=foo,dc=barfoo,dc=bar']]]));
  192. $access->expects($this->once())
  193. ->method('dn2groupname')
  194. ->will($this->returnValue(false));
  195. $groupBackend = new GroupLDAP($access, $pluginManager);
  196. $group = $groupBackend->gidNumber2Name('3117', $userDN);
  197. $this->assertSame(false, $group);
  198. }
  199. public function testGetEntryGidNumberValue() {
  200. $access = $this->getAccessMock();
  201. $pluginManager = $this->getPluginManagerMock();
  202. $this->enableGroups($access);
  203. $dn = 'cn=foobar,cn=foo,dc=barfoo,dc=bar';
  204. $attr = 'gidNumber';
  205. $access->expects($this->once())
  206. ->method('readAttribute')
  207. ->with($dn, $attr)
  208. ->will($this->returnValue(array('3117')));
  209. $groupBackend = new GroupLDAP($access, $pluginManager);
  210. $gid = $groupBackend->getGroupGidNumber($dn);
  211. $this->assertSame('3117', $gid);
  212. }
  213. public function testGetEntryGidNumberNoValue() {
  214. $access = $this->getAccessMock();
  215. $pluginManager = $this->getPluginManagerMock();
  216. $this->enableGroups($access);
  217. $dn = 'cn=foobar,cn=foo,dc=barfoo,dc=bar';
  218. $attr = 'gidNumber';
  219. $access->expects($this->once())
  220. ->method('readAttribute')
  221. ->with($dn, $attr)
  222. ->will($this->returnValue(false));
  223. $groupBackend = new GroupLDAP($access, $pluginManager);
  224. $gid = $groupBackend->getGroupGidNumber($dn);
  225. $this->assertSame(false, $gid);
  226. }
  227. public function testPrimaryGroupID2NameSuccess() {
  228. $access = $this->getAccessMock();
  229. $pluginManager = $this->getPluginManagerMock();
  230. $this->enableGroups($access);
  231. $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar';
  232. $access->expects($this->once())
  233. ->method('getSID')
  234. ->with($userDN)
  235. ->will($this->returnValue('S-1-5-21-249921958-728525901-1594176202'));
  236. $access->expects($this->once())
  237. ->method('searchGroups')
  238. ->will($this->returnValue([['dn' => ['cn=foo,dc=barfoo,dc=bar']]]));
  239. $access->expects($this->once())
  240. ->method('dn2groupname')
  241. ->with('cn=foo,dc=barfoo,dc=bar')
  242. ->will($this->returnValue('MyGroup'));
  243. $groupBackend = new GroupLDAP($access, $pluginManager);
  244. $group = $groupBackend->primaryGroupID2Name('3117', $userDN);
  245. $this->assertSame('MyGroup', $group);
  246. }
  247. public function testPrimaryGroupID2NameNoSID() {
  248. $access = $this->getAccessMock();
  249. $pluginManager = $this->getPluginManagerMock();
  250. $this->enableGroups($access);
  251. $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar';
  252. $access->expects($this->once())
  253. ->method('getSID')
  254. ->with($userDN)
  255. ->will($this->returnValue(false));
  256. $access->expects($this->never())
  257. ->method('searchGroups');
  258. $access->expects($this->never())
  259. ->method('dn2groupname');
  260. $groupBackend = new GroupLDAP($access, $pluginManager);
  261. $group = $groupBackend->primaryGroupID2Name('3117', $userDN);
  262. $this->assertSame(false, $group);
  263. }
  264. public function testPrimaryGroupID2NameNoGroup() {
  265. $access = $this->getAccessMock();
  266. $pluginManager = $this->getPluginManagerMock();
  267. $this->enableGroups($access);
  268. $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar';
  269. $access->expects($this->once())
  270. ->method('getSID')
  271. ->with($userDN)
  272. ->will($this->returnValue('S-1-5-21-249921958-728525901-1594176202'));
  273. $access->expects($this->once())
  274. ->method('searchGroups')
  275. ->will($this->returnValue(array()));
  276. $access->expects($this->never())
  277. ->method('dn2groupname');
  278. $groupBackend = new GroupLDAP($access, $pluginManager);
  279. $group = $groupBackend->primaryGroupID2Name('3117', $userDN);
  280. $this->assertSame(false, $group);
  281. }
  282. public function testPrimaryGroupID2NameNoName() {
  283. $access = $this->getAccessMock();
  284. $pluginManager = $this->getPluginManagerMock();
  285. $this->enableGroups($access);
  286. $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar';
  287. $access->expects($this->once())
  288. ->method('getSID')
  289. ->with($userDN)
  290. ->will($this->returnValue('S-1-5-21-249921958-728525901-1594176202'));
  291. $access->expects($this->once())
  292. ->method('searchGroups')
  293. ->will($this->returnValue([['dn' => ['cn=foo,dc=barfoo,dc=bar']]]));
  294. $access->expects($this->once())
  295. ->method('dn2groupname')
  296. ->will($this->returnValue(false));
  297. $groupBackend = new GroupLDAP($access, $pluginManager);
  298. $group = $groupBackend->primaryGroupID2Name('3117', $userDN);
  299. $this->assertSame(false, $group);
  300. }
  301. public function testGetEntryGroupIDValue() {
  302. //tests getEntryGroupID via getGroupPrimaryGroupID
  303. //which is basically identical to getUserPrimaryGroupIDs
  304. $access = $this->getAccessMock();
  305. $pluginManager = $this->getPluginManagerMock();
  306. $this->enableGroups($access);
  307. $dn = 'cn=foobar,cn=foo,dc=barfoo,dc=bar';
  308. $attr = 'primaryGroupToken';
  309. $access->expects($this->once())
  310. ->method('readAttribute')
  311. ->with($dn, $attr)
  312. ->will($this->returnValue(array('3117')));
  313. $groupBackend = new GroupLDAP($access, $pluginManager);
  314. $gid = $groupBackend->getGroupPrimaryGroupID($dn);
  315. $this->assertSame('3117', $gid);
  316. }
  317. public function testGetEntryGroupIDNoValue() {
  318. //tests getEntryGroupID via getGroupPrimaryGroupID
  319. //which is basically identical to getUserPrimaryGroupIDs
  320. $access = $this->getAccessMock();
  321. $pluginManager = $this->getPluginManagerMock();
  322. $this->enableGroups($access);
  323. $dn = 'cn=foobar,cn=foo,dc=barfoo,dc=bar';
  324. $attr = 'primaryGroupToken';
  325. $access->expects($this->once())
  326. ->method('readAttribute')
  327. ->with($dn, $attr)
  328. ->will($this->returnValue(false));
  329. $groupBackend = new GroupLDAP($access, $pluginManager);
  330. $gid = $groupBackend->getGroupPrimaryGroupID($dn);
  331. $this->assertSame(false, $gid);
  332. }
  333. /**
  334. * tests whether Group Backend behaves correctly when cache with uid and gid
  335. * is hit
  336. */
  337. public function testInGroupHitsUidGidCache() {
  338. $access = $this->getAccessMock();
  339. $pluginManager = $this->getPluginManagerMock();
  340. $this->enableGroups($access);
  341. $uid = 'someUser';
  342. $gid = 'someGroup';
  343. $cacheKey = 'inGroup'.$uid.':'.$gid;
  344. $access->connection->expects($this->once())
  345. ->method('getFromCache')
  346. ->with($cacheKey)
  347. ->will($this->returnValue(true));
  348. $access->expects($this->never())
  349. ->method('username2dn');
  350. $groupBackend = new GroupLDAP($access, $pluginManager);
  351. $groupBackend->inGroup($uid, $gid);
  352. }
  353. public function testGetGroupsWithOffset() {
  354. $access = $this->getAccessMock();
  355. $pluginManager = $this->getPluginManagerMock();
  356. $this->enableGroups($access);
  357. $access->expects($this->once())
  358. ->method('nextcloudGroupNames')
  359. ->will($this->returnValue(array('group1', 'group2')));
  360. $groupBackend = new GroupLDAP($access, $pluginManager);
  361. $groups = $groupBackend->getGroups('', 2, 2);
  362. $this->assertSame(2, count($groups));
  363. }
  364. /**
  365. * tests that a user listing is complete, if all it's members have the group
  366. * as their primary.
  367. */
  368. public function testUsersInGroupPrimaryMembersOnly() {
  369. $access = $this->getAccessMock();
  370. $pluginManager = $this->getPluginManagerMock();
  371. $this->enableGroups($access);
  372. $access->connection->expects($this->any())
  373. ->method('getFromCache')
  374. ->will($this->returnValue(null));
  375. $access->expects($this->any())
  376. ->method('readAttribute')
  377. ->will($this->returnCallback(function($dn, $attr) {
  378. if($attr === 'primaryGroupToken') {
  379. return array(1337);
  380. } else if($attr === 'gidNumber') {
  381. return [4211];
  382. }
  383. return array();
  384. }));
  385. $access->expects($this->any())
  386. ->method('groupname2dn')
  387. ->will($this->returnValue('cn=foobar,dc=foo,dc=bar'));
  388. $access->expects($this->exactly(2))
  389. ->method('nextcloudUserNames')
  390. ->willReturnOnConsecutiveCalls(['lisa', 'bart', 'kira', 'brad'], ['walle', 'dino', 'xenia']);
  391. $access->userManager = $this->createMock(Manager::class);
  392. $groupBackend = new GroupLDAP($access, $pluginManager);
  393. $users = $groupBackend->usersInGroup('foobar');
  394. $this->assertSame(7, count($users));
  395. }
  396. /**
  397. * tests that a user listing is complete, if all it's members have the group
  398. * as their primary.
  399. */
  400. public function testUsersInGroupPrimaryAndUnixMembers() {
  401. $access = $this->getAccessMock();
  402. $pluginManager = $this->getPluginManagerMock();
  403. $this->enableGroups($access);
  404. $access->connection->expects($this->any())
  405. ->method('getFromCache')
  406. ->will($this->returnValue(null));
  407. $access->expects($this->any())
  408. ->method('readAttribute')
  409. ->will($this->returnCallback(function($dn, $attr) {
  410. if($attr === 'primaryGroupToken') {
  411. return array(1337);
  412. }
  413. return array();
  414. }));
  415. $access->expects($this->any())
  416. ->method('groupname2dn')
  417. ->will($this->returnValue('cn=foobar,dc=foo,dc=bar'));
  418. $access->expects($this->once())
  419. ->method('nextcloudUserNames')
  420. ->will($this->returnValue(array('lisa', 'bart', 'kira', 'brad')));
  421. $access->userManager = $this->createMock(Manager::class);
  422. $groupBackend = new GroupLDAP($access, $pluginManager);
  423. $users = $groupBackend->usersInGroup('foobar');
  424. $this->assertSame(4, count($users));
  425. }
  426. /**
  427. * tests that a user counting is complete, if all it's members have the group
  428. * as their primary.
  429. */
  430. public function testCountUsersInGroupPrimaryMembersOnly() {
  431. $access = $this->getAccessMock();
  432. $pluginManager = $this->getPluginManagerMock();
  433. $this->enableGroups($access);
  434. $access->connection->expects($this->any())
  435. ->method('getFromCache')
  436. ->will($this->returnValue(null));
  437. $access->expects($this->any())
  438. ->method('readAttribute')
  439. ->will($this->returnCallback(function($dn, $attr) {
  440. if($attr === 'primaryGroupToken') {
  441. return array(1337);
  442. }
  443. return array();
  444. }));
  445. $access->expects($this->any())
  446. ->method('groupname2dn')
  447. ->will($this->returnValue('cn=foobar,dc=foo,dc=bar'));
  448. $access->expects($this->once())
  449. ->method('countUsers')
  450. ->will($this->returnValue(4));
  451. $groupBackend = new GroupLDAP($access, $pluginManager);
  452. $users = $groupBackend->countUsersInGroup('foobar');
  453. $this->assertSame(4, $users);
  454. }
  455. public function testGetUserGroupsMemberOf() {
  456. $access = $this->getAccessMock();
  457. $pluginManager = $this->getPluginManagerMock();
  458. $this->enableGroups($access);
  459. $dn = 'cn=userX,dc=foobar';
  460. $access->connection->hasPrimaryGroups = false;
  461. $access->connection->hasGidNumber = false;
  462. $access->expects($this->any())
  463. ->method('username2dn')
  464. ->will($this->returnValue($dn));
  465. $access->expects($this->exactly(3))
  466. ->method('readAttribute')
  467. ->will($this->onConsecutiveCalls(['cn=groupA,dc=foobar', 'cn=groupB,dc=foobar'], [], []));
  468. $access->expects($this->exactly(2))
  469. ->method('dn2groupname')
  470. ->will($this->returnArgument(0));
  471. $access->expects($this->exactly(3))
  472. ->method('groupsMatchFilter')
  473. ->will($this->returnArgument(0));
  474. $groupBackend = new GroupLDAP($access, $pluginManager);
  475. $groups = $groupBackend->getUserGroups('userX');
  476. $this->assertSame(2, count($groups));
  477. }
  478. public function testGetUserGroupsMemberOfDisabled() {
  479. $access = $this->getAccessMock();
  480. $pluginManager = $this->getPluginManagerMock();
  481. $access->connection = $this->createMock(Connection::class);
  482. $access->connection->expects($this->any())
  483. ->method('__get')
  484. ->will($this->returnCallback(function($name) {
  485. if($name === 'useMemberOfToDetectMembership') {
  486. return 0;
  487. } else if($name === 'ldapDynamicGroupMemberURL') {
  488. return '';
  489. }
  490. return 1;
  491. }));
  492. $dn = 'cn=userX,dc=foobar';
  493. $access->connection->hasPrimaryGroups = false;
  494. $access->connection->hasGidNumber = false;
  495. $access->expects($this->once())
  496. ->method('username2dn')
  497. ->will($this->returnValue($dn));
  498. $access->expects($this->never())
  499. ->method('readAttribute')
  500. ->with($dn, 'memberOf');
  501. $access->expects($this->once())
  502. ->method('nextcloudGroupNames')
  503. ->will($this->returnValue([]));
  504. $groupBackend = new GroupLDAP($access, $pluginManager);
  505. $groupBackend->getUserGroups('userX');
  506. }
  507. public function testGetGroupsByMember() {
  508. $access = $this->getAccessMock();
  509. $pluginManager = $this->getPluginManagerMock();
  510. $access->connection = $this->createMock(Connection::class);
  511. $access->connection->expects($this->any())
  512. ->method('__get')
  513. ->will($this->returnCallback(function($name) {
  514. if($name === 'useMemberOfToDetectMembership') {
  515. return 0;
  516. } else if($name === 'ldapDynamicGroupMemberURL') {
  517. return '';
  518. } else if($name === 'ldapNestedGroups') {
  519. return false;
  520. }
  521. return 1;
  522. }));
  523. $dn = 'cn=userX,dc=foobar';
  524. $access->connection->hasPrimaryGroups = false;
  525. $access->connection->hasGidNumber = false;
  526. $access->expects($this->exactly(2))
  527. ->method('username2dn')
  528. ->will($this->returnValue($dn));
  529. $access->expects($this->never())
  530. ->method('readAttribute')
  531. ->with($dn, 'memberOf');
  532. $group1 = [
  533. 'cn' => 'group1',
  534. 'dn' => ['cn=group1,ou=groups,dc=domain,dc=com'],
  535. ];
  536. $group2 = [
  537. 'cn' => 'group2',
  538. 'dn' => ['cn=group2,ou=groups,dc=domain,dc=com'],
  539. ];
  540. $access->expects($this->once())
  541. ->method('nextcloudGroupNames')
  542. ->with([$group1, $group2])
  543. ->will($this->returnValue(['group1', 'group2']));
  544. $access->expects($this->once())
  545. ->method('fetchListOfGroups')
  546. ->will($this->returnValue([$group1, $group2]));
  547. $groupBackend = new GroupLDAP($access, $pluginManager);
  548. $groups = $groupBackend->getUserGroups('userX');
  549. $this->assertEquals(['group1', 'group2'], $groups);
  550. $groupsAgain = $groupBackend->getUserGroups('userX');
  551. $this->assertEquals(['group1', 'group2'], $groupsAgain);
  552. }
  553. public function testCreateGroupWithPlugin() {
  554. /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  555. $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
  556. ->setMethods(['implementsActions','createGroup'])
  557. ->getMock();
  558. $pluginManager->expects($this->once())
  559. ->method('implementsActions')
  560. ->with(GroupInterface::CREATE_GROUP)
  561. ->willReturn(true);
  562. $pluginManager->expects($this->once())
  563. ->method('createGroup')
  564. ->with('gid')
  565. ->willReturn('result');
  566. $access = $this->getAccessMock();
  567. $access->connection = $this->createMock(Connection::class);
  568. $ldap = new GroupLDAP($access, $pluginManager);
  569. $this->assertEquals($ldap->createGroup('gid'),true);
  570. }
  571. /**
  572. * @expectedException \Exception
  573. */
  574. public function testCreateGroupFailing() {
  575. /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  576. $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
  577. ->setMethods(['implementsActions', 'createGroup'])
  578. ->getMock();
  579. $pluginManager->expects($this->once())
  580. ->method('implementsActions')
  581. ->with(GroupInterface::CREATE_GROUP)
  582. ->willReturn(false);
  583. $access = $this->getAccessMock();
  584. $access->connection = $this->createMock(Connection::class);
  585. $ldap = new GroupLDAP($access, $pluginManager);
  586. $ldap->createGroup('gid');
  587. }
  588. public function testDeleteGroupWithPlugin() {
  589. /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  590. $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
  591. ->setMethods(['implementsActions','deleteGroup'])
  592. ->getMock();
  593. $pluginManager->expects($this->once())
  594. ->method('implementsActions')
  595. ->with(GroupInterface::DELETE_GROUP)
  596. ->willReturn(true);
  597. $pluginManager->expects($this->once())
  598. ->method('deleteGroup')
  599. ->with('gid')
  600. ->willReturn('result');
  601. $mapper = $this->getMockBuilder('\OCA\User_LDAP\Mapping\GroupMapping')
  602. ->setMethods(['unmap'])
  603. ->disableOriginalConstructor()
  604. ->getMock();
  605. $access = $this->getAccessMock();
  606. $access->expects($this->any())
  607. ->method('getGroupMapper')
  608. ->will($this->returnValue($mapper));
  609. $access->connection = $this->createMock(Connection::class);
  610. $ldap = new GroupLDAP($access, $pluginManager);
  611. $this->assertEquals($ldap->deleteGroup('gid'),'result');
  612. }
  613. /**
  614. * @expectedException \Exception
  615. */
  616. public function testDeleteGroupFailing() {
  617. /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  618. $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
  619. ->setMethods(['implementsActions', 'deleteGroup'])
  620. ->getMock();
  621. $pluginManager->expects($this->once())
  622. ->method('implementsActions')
  623. ->with(GroupInterface::DELETE_GROUP)
  624. ->willReturn(false);
  625. $access = $this->getAccessMock();
  626. $access->connection = $this->createMock(Connection::class);
  627. $ldap = new GroupLDAP($access, $pluginManager);
  628. $ldap->deleteGroup('gid');
  629. }
  630. public function testAddToGroupWithPlugin() {
  631. /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  632. $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
  633. ->setMethods(['implementsActions','addToGroup'])
  634. ->getMock();
  635. $pluginManager->expects($this->once())
  636. ->method('implementsActions')
  637. ->with(GroupInterface::ADD_TO_GROUP)
  638. ->willReturn(true);
  639. $pluginManager->expects($this->once())
  640. ->method('addToGroup')
  641. ->with('uid', 'gid')
  642. ->willReturn('result');
  643. $access = $this->getAccessMock();
  644. $access->connection = $this->createMock(Connection::class);
  645. $ldap = new GroupLDAP($access, $pluginManager);
  646. $this->assertEquals($ldap->addToGroup('uid', 'gid'),'result');
  647. }
  648. /**
  649. * @expectedException \Exception
  650. */
  651. public function testAddToGroupFailing() {
  652. /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  653. $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
  654. ->setMethods(['implementsActions', 'addToGroup'])
  655. ->getMock();
  656. $pluginManager->expects($this->once())
  657. ->method('implementsActions')
  658. ->with(GroupInterface::ADD_TO_GROUP)
  659. ->willReturn(false);
  660. $access = $this->getAccessMock();
  661. $access->connection = $this->createMock(Connection::class);
  662. $ldap = new GroupLDAP($access, $pluginManager);
  663. $ldap->addToGroup('uid', 'gid');
  664. }
  665. public function testRemoveFromGroupWithPlugin() {
  666. /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  667. $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
  668. ->setMethods(['implementsActions','removeFromGroup'])
  669. ->getMock();
  670. $pluginManager->expects($this->once())
  671. ->method('implementsActions')
  672. ->with(GroupInterface::REMOVE_FROM_GROUP)
  673. ->willReturn(true);
  674. $pluginManager->expects($this->once())
  675. ->method('removeFromGroup')
  676. ->with('uid', 'gid')
  677. ->willReturn('result');
  678. $access = $this->getAccessMock();
  679. $access->connection = $this->createMock(Connection::class);
  680. $ldap = new GroupLDAP($access, $pluginManager);
  681. $this->assertEquals($ldap->removeFromGroup('uid', 'gid'),'result');
  682. }
  683. /**
  684. * @expectedException \Exception
  685. */
  686. public function testRemoveFromGroupFailing() {
  687. /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  688. $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
  689. ->setMethods(['implementsActions', 'removeFromGroup'])
  690. ->getMock();
  691. $pluginManager->expects($this->once())
  692. ->method('implementsActions')
  693. ->with(GroupInterface::REMOVE_FROM_GROUP)
  694. ->willReturn(false);
  695. $access = $this->getAccessMock();
  696. $access->connection = $this->createMock(Connection::class);
  697. $ldap = new GroupLDAP($access, $pluginManager);
  698. $ldap->removeFromGroup('uid', 'gid');
  699. }
  700. public function testGetGroupDetailsWithPlugin() {
  701. /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  702. $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
  703. ->setMethods(['implementsActions','getGroupDetails'])
  704. ->getMock();
  705. $pluginManager->expects($this->once())
  706. ->method('implementsActions')
  707. ->with(GroupInterface::GROUP_DETAILS)
  708. ->willReturn(true);
  709. $pluginManager->expects($this->once())
  710. ->method('getGroupDetails')
  711. ->with('gid')
  712. ->willReturn('result');
  713. $access = $this->getAccessMock();
  714. $access->connection = $this->createMock(Connection::class);
  715. $ldap = new GroupLDAP($access, $pluginManager);
  716. $this->assertEquals($ldap->getGroupDetails('gid'),'result');
  717. }
  718. /**
  719. * @expectedException \Exception
  720. */
  721. public function testGetGroupDetailsFailing() {
  722. /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  723. $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
  724. ->setMethods(['implementsActions', 'getGroupDetails'])
  725. ->getMock();
  726. $pluginManager->expects($this->once())
  727. ->method('implementsActions')
  728. ->with(GroupInterface::GROUP_DETAILS)
  729. ->willReturn(false);
  730. $access = $this->getAccessMock();
  731. $access->connection = $this->createMock(Connection::class);
  732. $ldap = new GroupLDAP($access, $pluginManager);
  733. $ldap->getGroupDetails('gid');
  734. }
  735. }