WizardTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  11. * @author Viktor Szépe <viktor@szepe.net>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\User_LDAP\Tests;
  29. use OCA\User_LDAP\Access;
  30. use OCA\User_LDAP\Configuration;
  31. use OCA\User_LDAP\ILDAPWrapper;
  32. use \OCA\User_LDAP\Wizard;
  33. use Test\TestCase;
  34. /**
  35. * Class Test_Wizard
  36. *
  37. * @group DB
  38. *
  39. * @package OCA\User_LDAP\Tests
  40. */
  41. class WizardTest extends TestCase {
  42. protected function setUp() {
  43. parent::setUp();
  44. //we need to make sure the consts are defined, otherwise tests will fail
  45. //on systems without php5_ldap
  46. $ldapConsts = array('LDAP_OPT_PROTOCOL_VERSION',
  47. 'LDAP_OPT_REFERRALS', 'LDAP_OPT_NETWORK_TIMEOUT');
  48. foreach($ldapConsts as $const) {
  49. if(!defined($const)) {
  50. define($const, 42);
  51. }
  52. }
  53. }
  54. private function getWizardAndMocks() {
  55. static $confMethods;
  56. static $connMethods;
  57. static $accMethods;
  58. if(is_null($confMethods)) {
  59. $confMethods = get_class_methods('\OCA\User_LDAP\Configuration');
  60. $connMethods = get_class_methods('\OCA\User_LDAP\Connection');
  61. $accMethods = get_class_methods('\OCA\User_LDAP\Access');
  62. }
  63. /** @var ILDAPWrapper|\PHPUnit_Framework_MockObject_MockObject $lw */
  64. $lw = $this->createMock(ILDAPWrapper::class);
  65. /** @var Configuration|\PHPUnit_Framework_MockObject_MockObject $conf */
  66. $conf = $this->getMockBuilder(Configuration::class)
  67. ->setMethods($confMethods)
  68. ->setConstructorArgs([$lw, null, null])
  69. ->getMock();
  70. /** @var Access|\PHPUnit_Framework_MockObject_MockObject $access */
  71. $access = $this->createMock(Access::class);
  72. return array(new Wizard($conf, $lw, $access), $conf, $lw, $access);
  73. }
  74. private function prepareLdapWrapperForConnections(\PHPUnit_Framework_MockObject_MockObject &$ldap) {
  75. $ldap->expects($this->once())
  76. ->method('connect')
  77. //dummy value, usually invalid
  78. ->will($this->returnValue(true));
  79. $ldap->expects($this->exactly(3))
  80. ->method('setOption')
  81. ->will($this->returnValue(true));
  82. $ldap->expects($this->once())
  83. ->method('bind')
  84. ->will($this->returnValue(true));
  85. }
  86. public function testCumulativeSearchOnAttributeLimited() {
  87. list($wizard, $configuration, $ldap) = $this->getWizardAndMocks();
  88. $configuration->expects($this->any())
  89. ->method('__get')
  90. ->will($this->returnCallback(function($name) {
  91. if($name === 'ldapBase') {
  92. return array('base');
  93. }
  94. return null;
  95. }));
  96. $this->prepareLdapWrapperForConnections($ldap);
  97. $ldap->expects($this->any())
  98. ->method('isResource')
  99. ->will($this->returnValue(true));
  100. $ldap->expects($this->exactly(2))
  101. ->method('search')
  102. //dummy value, usually invalid
  103. ->will($this->returnValue(true));
  104. $ldap->expects($this->exactly(2))
  105. ->method('countEntries')
  106. //an is_resource check will follow, so we need to return a dummy resource
  107. ->will($this->returnValue(23));
  108. //5 DNs per filter means 2x firstEntry and 8x nextEntry
  109. $ldap->expects($this->exactly(2))
  110. ->method('firstEntry')
  111. //dummy value, usually invalid
  112. ->will($this->returnValue(true));
  113. $ldap->expects($this->exactly(8))
  114. ->method('nextEntry')
  115. //dummy value, usually invalid
  116. ->will($this->returnValue(true));
  117. $ldap->expects($this->exactly(10))
  118. ->method('getAttributes')
  119. //dummy value, usually invalid
  120. ->will($this->returnValue(array('cn' => array('foo'), 'count' => 1)));
  121. global $uidnumber;
  122. $uidnumber = 1;
  123. $ldap->expects($this->exactly(10))
  124. ->method('getDN')
  125. //dummy value, usually invalid
  126. ->will($this->returnCallback(function($a, $b) {
  127. global $uidnumber;
  128. return $uidnumber++;
  129. }));
  130. // The following expectations are the real test
  131. $filters = array('f1', 'f2', '*');
  132. $wizard->cumulativeSearchOnAttribute($filters, 'cn', 5);
  133. unset($uidnumber);
  134. }
  135. public function testCumulativeSearchOnAttributeUnlimited() {
  136. list($wizard, $configuration, $ldap) = $this->getWizardAndMocks();
  137. $configuration->expects($this->any())
  138. ->method('__get')
  139. ->will($this->returnCallback(function($name) {
  140. if($name === 'ldapBase') {
  141. return array('base');
  142. }
  143. return null;
  144. }));
  145. $this->prepareLdapWrapperForConnections($ldap);
  146. $ldap->expects($this->any())
  147. ->method('isResource')
  148. ->will($this->returnCallback(function($r) {
  149. if($r === true) {
  150. return true;
  151. }
  152. if($r % 24 === 0) {
  153. global $uidnumber;
  154. $uidnumber++;
  155. return false;
  156. }
  157. return true;
  158. }));
  159. $ldap->expects($this->exactly(2))
  160. ->method('search')
  161. //dummy value, usually invalid
  162. ->will($this->returnValue(true));
  163. $ldap->expects($this->exactly(2))
  164. ->method('countEntries')
  165. //an is_resource check will follow, so we need to return a dummy resource
  166. ->will($this->returnValue(23));
  167. //5 DNs per filter means 2x firstEntry and 8x nextEntry
  168. $ldap->expects($this->exactly(2))
  169. ->method('firstEntry')
  170. //dummy value, usually invalid
  171. ->will($this->returnCallback(function($r) {
  172. global $uidnumber;
  173. return $uidnumber;
  174. }));
  175. $ldap->expects($this->exactly(46))
  176. ->method('nextEntry')
  177. //dummy value, usually invalid
  178. ->will($this->returnCallback(function($r) {
  179. global $uidnumber;
  180. return $uidnumber;
  181. }));
  182. $ldap->expects($this->exactly(46))
  183. ->method('getAttributes')
  184. //dummy value, usually invalid
  185. ->will($this->returnValue(array('cn' => array('foo'), 'count' => 1)));
  186. global $uidnumber;
  187. $uidnumber = 1;
  188. $ldap->expects($this->exactly(46))
  189. ->method('getDN')
  190. //dummy value, usually invalid
  191. ->will($this->returnCallback(function($a, $b) {
  192. global $uidnumber;
  193. return $uidnumber++;
  194. }));
  195. // The following expectations are the real test
  196. $filters = array('f1', 'f2', '*');
  197. $wizard->cumulativeSearchOnAttribute($filters, 'cn', 0);
  198. unset($uidnumber);
  199. }
  200. public function testDetectEmailAttributeAlreadySet() {
  201. list($wizard, $configuration, $ldap, $access)
  202. = $this->getWizardAndMocks();
  203. $configuration->expects($this->any())
  204. ->method('__get')
  205. ->will($this->returnCallback(function ($name) {
  206. if($name === 'ldapEmailAttribute') {
  207. return 'myEmailAttribute';
  208. } else {
  209. //for requirement checks
  210. return 'let me pass';
  211. }
  212. }));
  213. $access->expects($this->once())
  214. ->method('countUsers')
  215. ->will($this->returnValue(42));
  216. $wizard->detectEmailAttribute();
  217. }
  218. public function testDetectEmailAttributeOverrideSet() {
  219. list($wizard, $configuration, $ldap, $access)
  220. = $this->getWizardAndMocks();
  221. $configuration->expects($this->any())
  222. ->method('__get')
  223. ->will($this->returnCallback(function ($name) {
  224. if($name === 'ldapEmailAttribute') {
  225. return 'myEmailAttribute';
  226. } else {
  227. //for requirement checks
  228. return 'let me pass';
  229. }
  230. }));
  231. $access->expects($this->exactly(3))
  232. ->method('combineFilterWithAnd')
  233. ->will($this->returnCallback(function ($filterParts) {
  234. return str_replace('=*', '', array_pop($filterParts));
  235. }));
  236. $access->expects($this->exactly(3))
  237. ->method('countUsers')
  238. ->will($this->returnCallback(function ($filter) {
  239. if($filter === 'myEmailAttribute') {
  240. return 0;
  241. } else if($filter === 'mail') {
  242. return 3;
  243. } else if($filter === 'mailPrimaryAddress') {
  244. return 17;
  245. }
  246. throw new \Exception('Untested filter: ' . $filter);
  247. }));
  248. $result = $wizard->detectEmailAttribute()->getResultArray();
  249. $this->assertSame('mailPrimaryAddress',
  250. $result['changes']['ldap_email_attr']);
  251. }
  252. public function testDetectEmailAttributeFind() {
  253. list($wizard, $configuration, $ldap, $access)
  254. = $this->getWizardAndMocks();
  255. $configuration->expects($this->any())
  256. ->method('__get')
  257. ->will($this->returnCallback(function ($name) {
  258. if($name === 'ldapEmailAttribute') {
  259. return '';
  260. } else {
  261. //for requirement checks
  262. return 'let me pass';
  263. }
  264. }));
  265. $access->expects($this->exactly(2))
  266. ->method('combineFilterWithAnd')
  267. ->will($this->returnCallback(function ($filterParts) {
  268. return str_replace('=*', '', array_pop($filterParts));
  269. }));
  270. $access->expects($this->exactly(2))
  271. ->method('countUsers')
  272. ->will($this->returnCallback(function ($filter) {
  273. if($filter === 'myEmailAttribute') {
  274. return 0;
  275. } else if($filter === 'mail') {
  276. return 3;
  277. } else if($filter === 'mailPrimaryAddress') {
  278. return 17;
  279. }
  280. throw new \Exception('Untested filter: ' . $filter);
  281. }));
  282. $result = $wizard->detectEmailAttribute()->getResultArray();
  283. $this->assertSame('mailPrimaryAddress',
  284. $result['changes']['ldap_email_attr']);
  285. }
  286. public function testDetectEmailAttributeFindNothing() {
  287. list($wizard, $configuration, $ldap, $access)
  288. = $this->getWizardAndMocks();
  289. $configuration->expects($this->any())
  290. ->method('__get')
  291. ->will($this->returnCallback(function ($name) {
  292. if($name === 'ldapEmailAttribute') {
  293. return 'myEmailAttribute';
  294. } else {
  295. //for requirement checks
  296. return 'let me pass';
  297. }
  298. }));
  299. $access->expects($this->exactly(3))
  300. ->method('combineFilterWithAnd')
  301. ->will($this->returnCallback(function ($filterParts) {
  302. return str_replace('=*', '', array_pop($filterParts));
  303. }));
  304. $access->expects($this->exactly(3))
  305. ->method('countUsers')
  306. ->will($this->returnCallback(function ($filter) {
  307. if($filter === 'myEmailAttribute') {
  308. return 0;
  309. } else if($filter === 'mail') {
  310. return 0;
  311. } else if($filter === 'mailPrimaryAddress') {
  312. return 0;
  313. }
  314. throw new \Exception('Untested filter: ' . $filter);
  315. }));
  316. $result = $wizard->detectEmailAttribute();
  317. $this->assertSame(false, $result->hasChanges());
  318. }
  319. public function testCumulativeSearchOnAttributeSkipReadDN() {
  320. // tests that there is no infinite loop, when skipping already processed
  321. // DNs (they can be returned multiple times for multiple filters )
  322. list($wizard, $configuration, $ldap) = $this->getWizardAndMocks();
  323. $configuration->expects($this->any())
  324. ->method('__get')
  325. ->will($this->returnCallback(function($name) {
  326. if($name === 'ldapBase') {
  327. return array('base');
  328. }
  329. return null;
  330. }));
  331. $this->prepareLdapWrapperForConnections($ldap);
  332. $ldap->expects($this->any())
  333. ->method('isResource')
  334. ->will($this->returnCallback(function($res) {
  335. return (bool)$res;
  336. }));
  337. $ldap->expects($this->any())
  338. ->method('search')
  339. //dummy value, usually invalid
  340. ->will($this->returnValue(true));
  341. $ldap->expects($this->any())
  342. ->method('countEntries')
  343. //an is_resource check will follow, so we need to return a dummy resource
  344. ->will($this->returnValue(7));
  345. //5 DNs per filter means 2x firstEntry and 8x nextEntry
  346. $ldap->expects($this->any())
  347. ->method('firstEntry')
  348. //dummy value, usually invalid
  349. ->will($this->returnValue(1));
  350. global $mark;
  351. $mark = false;
  352. // entries return order: 1, 2, 3, 4, 4, 5, 6
  353. $ldap->expects($this->any())
  354. ->method('nextEntry')
  355. //dummy value, usually invalid
  356. ->will($this->returnCallback(function($a, $prev){
  357. $current = $prev + 1;
  358. if($current === 7) {
  359. return false;
  360. }
  361. global $mark;
  362. if($prev === 4 && !$mark) {
  363. $mark = true;
  364. return 4;
  365. }
  366. return $current;
  367. }));
  368. $ldap->expects($this->any())
  369. ->method('getAttributes')
  370. //dummy value, usually invalid
  371. ->will($this->returnCallback(function($a, $entry) {
  372. return array('cn' => array($entry), 'count' => 1);
  373. }));
  374. $ldap->expects($this->any())
  375. ->method('getDN')
  376. //dummy value, usually invalid
  377. ->will($this->returnCallback(function($a, $b) {
  378. return $b;
  379. }));
  380. // The following expectations are the real test
  381. $filters = array('f1', 'f2', '*');
  382. $resultArray = $wizard->cumulativeSearchOnAttribute($filters, 'cn', 0);
  383. $this->assertSame(6, count($resultArray));
  384. unset($mark);
  385. }
  386. }