PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/user_ldap/tests/group_ldap.php

https://gitlab.com/Red54/core
PHP | 445 lines | 297 code | 105 blank | 43 comment | 6 complexity | 3cc04fca6aa3430c9eb9986c95077451 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\user_ldap\tests;
  24. use \OCA\user_ldap\GROUP_LDAP as GroupLDAP;
  25. use \OCA\user_ldap\lib\Access;
  26. use \OCA\user_ldap\lib\Connection;
  27. use \OCA\user_ldap\lib\ILDAPWrapper;
  28. class Test_Group_Ldap extends \Test\TestCase {
  29. private function getAccessMock() {
  30. static $conMethods;
  31. static $accMethods;
  32. if(is_null($conMethods) || is_null($accMethods)) {
  33. $conMethods = get_class_methods('\OCA\user_ldap\lib\Connection');
  34. $accMethods = get_class_methods('\OCA\user_ldap\lib\Access');
  35. }
  36. $lw = $this->getMock('\OCA\user_ldap\lib\ILDAPWrapper');
  37. $connector = $this->getMock('\OCA\user_ldap\lib\Connection',
  38. $conMethods,
  39. array($lw, null, null));
  40. $um = new \OCA\user_ldap\lib\user\Manager(
  41. $this->getMock('\OCP\IConfig'),
  42. $this->getMock('\OCA\user_ldap\lib\FilesystemHelper'),
  43. $this->getMock('\OCA\user_ldap\lib\LogWrapper'),
  44. $this->getMock('\OCP\IAvatarManager'),
  45. $this->getMock('\OCP\Image'),
  46. $this->getMock('\OCP\IDBConnection')
  47. );
  48. $access = $this->getMock('\OCA\user_ldap\lib\Access',
  49. $accMethods,
  50. array($connector, $lw, $um));
  51. return $access;
  52. }
  53. private function enableGroups($access) {
  54. $access->connection->expects($this->any())
  55. ->method('__get')
  56. ->will($this->returnCallback(function() {
  57. return 1;
  58. }));
  59. }
  60. public function testCountEmptySearchString() {
  61. $access = $this->getAccessMock();
  62. $this->enableGroups($access);
  63. $access->expects($this->any())
  64. ->method('groupname2dn')
  65. ->will($this->returnValue('cn=group,dc=foo,dc=bar'));
  66. $access->expects($this->any())
  67. ->method('readAttribute')
  68. ->will($this->returnValue(array('u11', 'u22', 'u33', 'u34')));
  69. // for primary groups
  70. $access->expects($this->once())
  71. ->method('countUsers')
  72. ->will($this->returnValue(2));
  73. $groupBackend = new GroupLDAP($access);
  74. $users = $groupBackend->countUsersInGroup('group');
  75. $this->assertSame(6, $users);
  76. }
  77. public function testCountWithSearchString() {
  78. $access = $this->getAccessMock();
  79. $this->enableGroups($access);
  80. $access->expects($this->any())
  81. ->method('groupname2dn')
  82. ->will($this->returnValue('cn=group,dc=foo,dc=bar'));
  83. $access->expects($this->any())
  84. ->method('fetchListOfUsers')
  85. ->will($this->returnValue(array()));
  86. $access->expects($this->any())
  87. ->method('readAttribute')
  88. ->will($this->returnCallback(function($name) {
  89. //the search operation will call readAttribute, thus we need
  90. //to anaylze the "dn". All other times we just need to return
  91. //something that is neither null or false, but once an array
  92. //with the users in the group – so we do so all other times for
  93. //simplicicity.
  94. if(strpos($name, 'u') === 0) {
  95. return strpos($name, '3');
  96. }
  97. return array('u11', 'u22', 'u33', 'u34');
  98. }));
  99. $access->expects($this->any())
  100. ->method('dn2username')
  101. ->will($this->returnCallback(function() {
  102. return 'foobar' . \OCP\Util::generateRandomBytes(7);
  103. }));
  104. $groupBackend = new GroupLDAP($access);
  105. $users = $groupBackend->countUsersInGroup('group', '3');
  106. $this->assertSame(2, $users);
  107. }
  108. public function testPrimaryGroupID2NameSuccess() {
  109. $access = $this->getAccessMock();
  110. $this->enableGroups($access);
  111. $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar';
  112. $access->expects($this->once())
  113. ->method('getSID')
  114. ->with($userDN)
  115. ->will($this->returnValue('S-1-5-21-249921958-728525901-1594176202'));
  116. $access->expects($this->once())
  117. ->method('searchGroups')
  118. ->will($this->returnValue(array('cn=foo,dc=barfoo,dc=bar')));
  119. $access->expects($this->once())
  120. ->method('dn2groupname')
  121. ->with('cn=foo,dc=barfoo,dc=bar')
  122. ->will($this->returnValue('MyGroup'));
  123. $groupBackend = new GroupLDAP($access);
  124. $group = $groupBackend->primaryGroupID2Name('3117', $userDN);
  125. $this->assertSame('MyGroup', $group);
  126. }
  127. public function testPrimaryGroupID2NameNoSID() {
  128. $access = $this->getAccessMock();
  129. $this->enableGroups($access);
  130. $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar';
  131. $access->expects($this->once())
  132. ->method('getSID')
  133. ->with($userDN)
  134. ->will($this->returnValue(false));
  135. $access->expects($this->never())
  136. ->method('searchGroups');
  137. $access->expects($this->never())
  138. ->method('dn2groupname');
  139. $groupBackend = new GroupLDAP($access);
  140. $group = $groupBackend->primaryGroupID2Name('3117', $userDN);
  141. $this->assertSame(false, $group);
  142. }
  143. public function testPrimaryGroupID2NameNoGroup() {
  144. $access = $this->getAccessMock();
  145. $this->enableGroups($access);
  146. $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar';
  147. $access->expects($this->once())
  148. ->method('getSID')
  149. ->with($userDN)
  150. ->will($this->returnValue('S-1-5-21-249921958-728525901-1594176202'));
  151. $access->expects($this->once())
  152. ->method('searchGroups')
  153. ->will($this->returnValue(array()));
  154. $access->expects($this->never())
  155. ->method('dn2groupname');
  156. $groupBackend = new GroupLDAP($access);
  157. $group = $groupBackend->primaryGroupID2Name('3117', $userDN);
  158. $this->assertSame(false, $group);
  159. }
  160. public function testPrimaryGroupID2NameNoName() {
  161. $access = $this->getAccessMock();
  162. $this->enableGroups($access);
  163. $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar';
  164. $access->expects($this->once())
  165. ->method('getSID')
  166. ->with($userDN)
  167. ->will($this->returnValue('S-1-5-21-249921958-728525901-1594176202'));
  168. $access->expects($this->once())
  169. ->method('searchGroups')
  170. ->will($this->returnValue(array('cn=foo,dc=barfoo,dc=bar')));
  171. $access->expects($this->once())
  172. ->method('dn2groupname')
  173. ->will($this->returnValue(false));
  174. $groupBackend = new GroupLDAP($access);
  175. $group = $groupBackend->primaryGroupID2Name('3117', $userDN);
  176. $this->assertSame(false, $group);
  177. }
  178. public function testGetEntryGroupIDValue() {
  179. //tests getEntryGroupID via getGroupPrimaryGroupID
  180. //which is basically identical to getUserPrimaryGroupIDs
  181. $access = $this->getAccessMock();
  182. $this->enableGroups($access);
  183. $dn = 'cn=foobar,cn=foo,dc=barfoo,dc=bar';
  184. $attr = 'primaryGroupToken';
  185. $access->expects($this->once())
  186. ->method('readAttribute')
  187. ->with($dn, $attr)
  188. ->will($this->returnValue(array('3117')));
  189. $groupBackend = new GroupLDAP($access);
  190. $gid = $groupBackend->getGroupPrimaryGroupID($dn);
  191. $this->assertSame('3117', $gid);
  192. }
  193. public function testGetEntryGroupIDNoValue() {
  194. //tests getEntryGroupID via getGroupPrimaryGroupID
  195. //which is basically identical to getUserPrimaryGroupIDs
  196. $access = $this->getAccessMock();
  197. $this->enableGroups($access);
  198. $dn = 'cn=foobar,cn=foo,dc=barfoo,dc=bar';
  199. $attr = 'primaryGroupToken';
  200. $access->expects($this->once())
  201. ->method('readAttribute')
  202. ->with($dn, $attr)
  203. ->will($this->returnValue(false));
  204. $groupBackend = new GroupLDAP($access);
  205. $gid = $groupBackend->getGroupPrimaryGroupID($dn);
  206. $this->assertSame(false, $gid);
  207. }
  208. /**
  209. * tests whether Group Backend behaves correctly when cache with uid and gid
  210. * is hit
  211. */
  212. public function testInGroupHitsUidGidCache() {
  213. $access = $this->getAccessMock();
  214. $this->enableGroups($access);
  215. $uid = 'someUser';
  216. $gid = 'someGroup';
  217. $cacheKey = 'inGroup'.$uid.':'.$gid;
  218. $access->connection->expects($this->once())
  219. ->method('isCached')
  220. ->with($cacheKey)
  221. ->will($this->returnValue(true));
  222. $access->connection->expects($this->once())
  223. ->method('getFromCache')
  224. ->with($cacheKey)
  225. ->will($this->returnValue(true));
  226. $access->expects($this->never())
  227. ->method('username2dn');
  228. $groupBackend = new GroupLDAP($access);
  229. $groupBackend->inGroup($uid, $gid);
  230. }
  231. public function testGetGroupsWithOffset() {
  232. $access = $this->getAccessMock();
  233. $this->enableGroups($access);
  234. $access->expects($this->once())
  235. ->method('ownCloudGroupNames')
  236. ->will($this->returnValue(array('group1', 'group2')));
  237. $groupBackend = new GroupLDAP($access);
  238. $groups = $groupBackend->getGroups('', 2, 2);
  239. $this->assertSame(2, count($groups));
  240. }
  241. /**
  242. * tests that a user listing is complete, if all it's members have the group
  243. * as their primary.
  244. */
  245. public function testUsersInGroupPrimaryMembersOnly() {
  246. $access = $this->getAccessMock();
  247. $this->enableGroups($access);
  248. $access->connection->expects($this->any())
  249. ->method('getFromCache')
  250. ->will($this->returnValue(null));
  251. $access->expects($this->any())
  252. ->method('readAttribute')
  253. ->will($this->returnCallback(function($dn, $attr) {
  254. if($attr === 'primaryGroupToken') {
  255. return array(1337);
  256. }
  257. return array();
  258. }));
  259. $access->expects($this->any())
  260. ->method('groupname2dn')
  261. ->will($this->returnValue('cn=foobar,dc=foo,dc=bar'));
  262. $access->expects($this->once())
  263. ->method('ownCloudUserNames')
  264. ->will($this->returnValue(array('lisa', 'bart', 'kira', 'brad')));
  265. $groupBackend = new GroupLDAP($access);
  266. $users = $groupBackend->usersInGroup('foobar');
  267. $this->assertSame(4, count($users));
  268. }
  269. /**
  270. * tests that a user counting is complete, if all it's members have the group
  271. * as their primary.
  272. */
  273. public function testCountUsersInGroupPrimaryMembersOnly() {
  274. $access = $this->getAccessMock();
  275. $this->enableGroups($access);
  276. $access->connection->expects($this->any())
  277. ->method('getFromCache')
  278. ->will($this->returnValue(null));
  279. $access->expects($this->any())
  280. ->method('readAttribute')
  281. ->will($this->returnCallback(function($dn, $attr) {
  282. if($attr === 'primaryGroupToken') {
  283. return array(1337);
  284. }
  285. return array();
  286. }));
  287. $access->expects($this->any())
  288. ->method('groupname2dn')
  289. ->will($this->returnValue('cn=foobar,dc=foo,dc=bar'));
  290. $access->expects($this->once())
  291. ->method('countUsers')
  292. ->will($this->returnValue(4));
  293. $groupBackend = new GroupLDAP($access);
  294. $users = $groupBackend->countUsersInGroup('foobar');
  295. $this->assertSame(4, $users);
  296. }
  297. public function testGetUserGroupsMemberOf() {
  298. $access = $this->getAccessMock();
  299. $this->enableGroups($access);
  300. $dn = 'cn=userX,dc=foobar';
  301. $access->connection->hasPrimaryGroups = false;
  302. $access->expects($this->once())
  303. ->method('username2dn')
  304. ->will($this->returnValue($dn));
  305. $access->expects($this->once())
  306. ->method('readAttribute')
  307. ->with($dn, 'memberOf')
  308. ->will($this->returnValue(['cn=groupA,dc=foobar', 'cn=groupB,dc=foobar']));
  309. $access->expects($this->exactly(2))
  310. ->method('dn2groupname')
  311. ->will($this->returnArgument(0));
  312. $groupBackend = new GroupLDAP($access);
  313. $groups = $groupBackend->getUserGroups('userX');
  314. $this->assertSame(2, count($groups));
  315. }
  316. public function testGetUserGroupsMemberOfDisabled() {
  317. $access = $this->getAccessMock();
  318. $access->connection->expects($this->any())
  319. ->method('__get')
  320. ->will($this->returnCallback(function($name) {
  321. if($name === 'useMemberOfToDetectMembership') {
  322. return 0;
  323. }
  324. return 1;
  325. }));
  326. $dn = 'cn=userX,dc=foobar';
  327. $access->connection->hasPrimaryGroups = false;
  328. $access->expects($this->once())
  329. ->method('username2dn')
  330. ->will($this->returnValue($dn));
  331. $access->expects($this->never())
  332. ->method('readAttribute')
  333. ->with($dn, 'memberOf');
  334. $access->expects($this->once())
  335. ->method('ownCloudGroupNames')
  336. ->will($this->returnValue([]));
  337. $groupBackend = new GroupLDAP($access);
  338. $groupBackend->getUserGroups('userX');
  339. }
  340. }