user_ldap.php 16.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
/**
* ownCloud
*
* @author Arthur Schiwon
* @copyright 2013 Arthur Schiwon blizzz@owncloud.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\user_ldap\tests;

use \OCA\user_ldap\USER_LDAP as UserLDAP;
use \OCA\user_ldap\lib\Access;
use \OCA\user_ldap\lib\Connection;
use \OCA\user_ldap\lib\ILDAPWrapper;

class Test_User_Ldap_Direct extends \PHPUnit_Framework_TestCase {
	protected $backend;
Arthur Schiwon's avatar
Arthur Schiwon committed
32
	protected $access;
33
34
35
36
37
38
39
40
41

	public function setUp() {
		\OC_User::clearBackends();
		\OC_Group::clearBackends();
	}

	private function getAccessMock() {
		static $conMethods;
		static $accMethods;
Arthur Schiwon's avatar
Arthur Schiwon committed
42
		static $uMethods;
43

Arthur Schiwon's avatar
Arthur Schiwon committed
44
		if(is_null($conMethods) || is_null($accMethods)) {
45
46
			$conMethods = get_class_methods('\OCA\user_ldap\lib\Connection');
			$accMethods = get_class_methods('\OCA\user_ldap\lib\Access');
Arthur Schiwon's avatar
Arthur Schiwon committed
47
48
49
50
51
			unset($accMethods[array_search('getConnection', $accMethods)]);
			$uMethods   = get_class_methods('\OCA\user_ldap\lib\user\User');
			unset($uMethods[array_search('getUsername', $uMethods)]);
			unset($uMethods[array_search('getDN', $uMethods)]);
			unset($uMethods[array_search('__construct', $uMethods)]);
52
53
54
55
56
		}
		$lw  = $this->getMock('\OCA\user_ldap\lib\ILDAPWrapper');
		$connector = $this->getMock('\OCA\user_ldap\lib\Connection',
									$conMethods,
									array($lw, null, null));
Arthur Schiwon's avatar
Arthur Schiwon committed
57
58
59
60
61
62
63
64
65

		$um = new \OCA\user_ldap\lib\user\Manager(
				$this->getMock('\OCP\IConfig'),
				$this->getMock('\OCA\user_ldap\lib\FilesystemHelper'),
				$this->getMock('\OCA\user_ldap\lib\LogWrapper'),
				$this->getMock('\OCP\IAvatarManager'),
				$this->getMock('\OCP\Image')
			);

66
67
		$access = $this->getMock('\OCA\user_ldap\lib\Access',
								 $accMethods,
Arthur Schiwon's avatar
Arthur Schiwon committed
68
69
70
								 array($connector, $lw, $um));

		$um->setLdapAccess($access);
71
72
73
74
75
76
77
78
79
80

		return $access;
	}

	private function prepareMockForUserExists(&$access) {
		$access->expects($this->any())
			   ->method('username2dn')
			   ->will($this->returnCallback(function($uid) {
					switch ($uid) {
						case 'gunslinger':
Arthur Schiwon's avatar
Arthur Schiwon committed
81
							return 'dnOfRoland,dc=test';
82
83
							break;
						case 'formerUser':
Arthur Schiwon's avatar
Arthur Schiwon committed
84
							return 'dnOfFormerUser,dc=test';
85
86
							break;
						case 'newyorker':
Arthur Schiwon's avatar
Arthur Schiwon committed
87
							return 'dnOfNewYorker,dc=test';
88
89
							break;
						case 'ladyofshadows':
Arthur Schiwon's avatar
Arthur Schiwon committed
90
							return 'dnOfLadyOfShadows,dc=test';
91
							break;
Lukas Reschke's avatar
Lukas Reschke committed
92
						default:
93
94
95
96
97
							return false;
					}
			   }));
	}

98
	/**
99
	 * Prepares the Access mock for checkPassword tests
Robin McCorkell's avatar
Robin McCorkell committed
100
	 * @param \OCA\user_ldap\lib\Access $access mock
101
	 * @param bool noDisplayName
102
103
	 * @return void
	 */
104
	private function prepareAccessForCheckPassword(&$access, $noDisplayName = false) {
Arthur Schiwon's avatar
Arthur Schiwon committed
105
106
107
108
109
110
		$access->expects($this->once())
			   ->method('escapeFilterPart')
			   ->will($this->returnCallback(function($uid) {
				   return $uid;
			   }));

111
112
113
114
115
116
117
118
119
120
121
122
123
		$access->connection->expects($this->any())
			   ->method('__get')
			   ->will($this->returnCallback(function($name) {
					if($name === 'ldapLoginFilter') {
						return '%uid';
					}
					return null;
			   }));

		$access->expects($this->any())
			   ->method('fetchListOfUsers')
			   ->will($this->returnCallback(function($filter) {
					if($filter === 'roland') {
Arthur Schiwon's avatar
Arthur Schiwon committed
124
						return array('dnOfRoland,dc=test');
125
126
127
128
					}
					return array();
			   }));

129
130
131
132
		$retVal = 'gunslinger';
		if($noDisplayName === true) {
			$retVal = false;
		}
133
134
		$access->expects($this->any())
			   ->method('dn2username')
Arthur Schiwon's avatar
Arthur Schiwon committed
135
			   ->with($this->equalTo('dnOfRoland,dc=test'))
136
			   ->will($this->returnValue($retVal));
137

138
139
140
141
142
		$access->expects($this->any())
			   ->method('stringResemblesDN')
			   ->with($this->equalTo('dnOfRoland,dc=test'))
			   ->will($this->returnValue(true));

143
144
145
146
147
148
149
150
		$access->expects($this->any())
			   ->method('areCredentialsValid')
			   ->will($this->returnCallback(function($dn, $pwd) {
					if($pwd === 'dt19') {
						return true;
					}
					return false;
			   }));
151
152
	}

Arthur Schiwon's avatar
Arthur Schiwon committed
153
	public function testCheckPasswordUidReturn() {
154
		$access = $this->getAccessMock();
Arthur Schiwon's avatar
Arthur Schiwon committed
155

156
157
158
		$this->prepareAccessForCheckPassword($access);
		$backend = new UserLDAP($access);
		\OC_User::useBackend($backend);
159
160
161

		$result = $backend->checkPassword('roland', 'dt19');
		$this->assertEquals('gunslinger', $result);
Arthur Schiwon's avatar
Arthur Schiwon committed
162
163
164
165
166
167
168
169
	}

	public function testCheckPasswordWrongPassword() {
		$access = $this->getAccessMock();

		$this->prepareAccessForCheckPassword($access);
		$backend = new UserLDAP($access);
		\OC_User::useBackend($backend);
170
171
172

		$result = $backend->checkPassword('roland', 'wrong');
		$this->assertFalse($result);
Arthur Schiwon's avatar
Arthur Schiwon committed
173
174
175
176
177
178
179
180
	}

	public function testCheckPasswordWrongUser() {
		$access = $this->getAccessMock();

		$this->prepareAccessForCheckPassword($access);
		$backend = new UserLDAP($access);
		\OC_User::useBackend($backend);
181
182
183
184
185

		$result = $backend->checkPassword('mallory', 'evil');
		$this->assertFalse($result);
	}

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
	public function testCheckPasswordNoDisplayName() {
		$access = $this->getAccessMock();

		$this->prepareAccessForCheckPassword($access, true);
		$access->expects($this->once())
			->method('username2dn')
			->will($this->returnValue(false));

		$backend = new UserLDAP($access);
		\OC_User::useBackend($backend);

		$result = $backend->checkPassword('roland', 'dt19');
		$this->assertFalse($result);
	}

201
	public function testCheckPasswordPublicAPI() {
202
		$access = $this->getAccessMock();
203
		$this->prepareAccessForCheckPassword($access);
204
		$backend = new UserLDAP($access);
205
		\OC_User::useBackend($backend);
206

207
208
		$result = \OCP\User::checkPassword('roland', 'dt19');
		$this->assertEquals('gunslinger', $result);
Arthur Schiwon's avatar
Arthur Schiwon committed
209
210
211
212
213
214
215
	}

	public function testCheckPasswordPublicAPIWrongPassword() {
		$access = $this->getAccessMock();
		$this->prepareAccessForCheckPassword($access);
		$backend = new UserLDAP($access);
		\OC_User::useBackend($backend);
216
217
218

		$result = \OCP\User::checkPassword('roland', 'wrong');
		$this->assertFalse($result);
Arthur Schiwon's avatar
Arthur Schiwon committed
219
220
221
222
223
224
225
	}

	public function testCheckPasswordPublicAPIWrongUser() {
		$access = $this->getAccessMock();
		$this->prepareAccessForCheckPassword($access);
		$backend = new UserLDAP($access);
		\OC_User::useBackend($backend);
226
227
228
229
230
231

		$result = \OCP\User::checkPassword('mallory', 'evil');
		$this->assertFalse($result);
	}

	/**
232
	 * Prepares the Access mock for getUsers tests
Robin McCorkell's avatar
Robin McCorkell committed
233
	 * @param \OCA\user_ldap\lib\Access $access mock
234
235
236
	 * @return void
	 */
	private function prepareAccessForGetUsers(&$access) {
Arthur Schiwon's avatar
Arthur Schiwon committed
237
238
239
240
241
242
		$access->expects($this->once())
			   ->method('escapeFilterPart')
			   ->will($this->returnCallback(function($search) {
				   return $search;
			   }));

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
		$access->expects($this->any())
			   ->method('getFilterPartForUserSearch')
			   ->will($this->returnCallback(function($search) {
					return $search;
			   }));

		$access->expects($this->any())
			   ->method('combineFilterWithAnd')
			   ->will($this->returnCallback(function($param) {
					return $param[1];
			   }));

		$access->expects($this->any())
			   ->method('fetchListOfUsers')
			   ->will($this->returnCallback(function($search, $a, $l, $o) {
					$users = array('gunslinger', 'newyorker', 'ladyofshadows');
					if(empty($search)) {
						$result = $users;
					} else {
						$result = array();
						foreach($users as $user) {
							if(stripos($user,  $search) !== false) {
								$result[] = $user;
							}
						}
					}
					if(!is_null($l) || !is_null($o)) {
						$result = array_slice($result, $o, $l);
					}
					return $result;
			   }));

		$access->expects($this->any())
			   ->method('ownCloudUserNames')
			   ->will($this->returnArgument(0));
278
279
	}

Arthur Schiwon's avatar
Arthur Schiwon committed
280
	public function testGetUsersNoParam() {
281
282
283
		$access = $this->getAccessMock();
		$this->prepareAccessForGetUsers($access);
		$backend = new UserLDAP($access);
284
285
286

		$result = $backend->getUsers();
		$this->assertEquals(3, count($result));
Arthur Schiwon's avatar
Arthur Schiwon committed
287
288
289
290
291
292
	}

	public function testGetUsersLimitOffset() {
		$access = $this->getAccessMock();
		$this->prepareAccessForGetUsers($access);
		$backend = new UserLDAP($access);
293
294
295

		$result = $backend->getUsers('', 1, 2);
		$this->assertEquals(1, count($result));
Arthur Schiwon's avatar
Arthur Schiwon committed
296
297
298
299
300
301
	}

	public function testGetUsersLimitOffset2() {
		$access = $this->getAccessMock();
		$this->prepareAccessForGetUsers($access);
		$backend = new UserLDAP($access);
302
303
304

		$result = $backend->getUsers('', 2, 1);
		$this->assertEquals(2, count($result));
Arthur Schiwon's avatar
Arthur Schiwon committed
305
306
307
308
309
310
	}

	public function testGetUsersSearchWithResult() {
		$access = $this->getAccessMock();
		$this->prepareAccessForGetUsers($access);
		$backend = new UserLDAP($access);
311
312
313

		$result = $backend->getUsers('yo');
		$this->assertEquals(2, count($result));
Arthur Schiwon's avatar
Arthur Schiwon committed
314
315
316
317
318
319
	}

	public function testGetUsersSearchEmptyResult() {
		$access = $this->getAccessMock();
		$this->prepareAccessForGetUsers($access);
		$backend = new UserLDAP($access);
320
321
322
323
324

		$result = $backend->getUsers('nix');
		$this->assertEquals(0, count($result));
	}

Arthur Schiwon's avatar
Arthur Schiwon committed
325
	public function testGetUsersViaAPINoParam() {
326
327
328
329
330
331
332
		$access = $this->getAccessMock();
		$this->prepareAccessForGetUsers($access);
		$backend = new UserLDAP($access);
		\OC_User::useBackend($backend);

		$result = \OCP\User::getUsers();
		$this->assertEquals(3, count($result));
Arthur Schiwon's avatar
Arthur Schiwon committed
333
334
335
336
337
338
339
	}

	public function testGetUsersViaAPILimitOffset() {
		$access = $this->getAccessMock();
		$this->prepareAccessForGetUsers($access);
		$backend = new UserLDAP($access);
		\OC_User::useBackend($backend);
340
341
342

		$result = \OCP\User::getUsers('', 1, 2);
		$this->assertEquals(1, count($result));
Arthur Schiwon's avatar
Arthur Schiwon committed
343
344
345
346
347
348
349
	}

	public function testGetUsersViaAPILimitOffset2() {
		$access = $this->getAccessMock();
		$this->prepareAccessForGetUsers($access);
		$backend = new UserLDAP($access);
		\OC_User::useBackend($backend);
350
351
352

		$result = \OCP\User::getUsers('', 2, 1);
		$this->assertEquals(2, count($result));
Arthur Schiwon's avatar
Arthur Schiwon committed
353
354
355
356
357
358
359
	}

	public function testGetUsersViaAPISearchWithResult() {
		$access = $this->getAccessMock();
		$this->prepareAccessForGetUsers($access);
		$backend = new UserLDAP($access);
		\OC_User::useBackend($backend);
360
361
362

		$result = \OCP\User::getUsers('yo');
		$this->assertEquals(2, count($result));
Arthur Schiwon's avatar
Arthur Schiwon committed
363
364
365
366
367
368
369
	}

	public function testGetUsersViaAPISearchEmptyResult() {
		$access = $this->getAccessMock();
		$this->prepareAccessForGetUsers($access);
		$backend = new UserLDAP($access);
		\OC_User::useBackend($backend);
370
371
372
373
374

		$result = \OCP\User::getUsers('nix');
		$this->assertEquals(0, count($result));
	}

375
376
377
378
379
380
381
382
	public function testUserExists() {
		$access = $this->getAccessMock();
		$backend = new UserLDAP($access);
		$this->prepareMockForUserExists($access);

		$access->expects($this->any())
			   ->method('readAttribute')
			   ->will($this->returnCallback(function($dn) {
Arthur Schiwon's avatar
Arthur Schiwon committed
383
					if($dn === 'dnOfRoland,dc=test') {
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
						return array();
					}
					return false;
			   }));

		//test for existing user
		$result = $backend->userExists('gunslinger');
		$this->assertTrue($result);

		//test for deleted user
		$result = $backend->userExists('formerUser');
		$this->assertFalse($result);

		//test for never-existing user
		$result = $backend->userExists('mallory');
		$this->assertFalse($result);
	}

402
403
404
405
406
407
408
409
410
	public function testUserExistsPublicAPI() {
		$access = $this->getAccessMock();
		$backend = new UserLDAP($access);
		$this->prepareMockForUserExists($access);
		\OC_User::useBackend($backend);

		$access->expects($this->any())
			   ->method('readAttribute')
			   ->will($this->returnCallback(function($dn) {
Arthur Schiwon's avatar
Arthur Schiwon committed
411
					if($dn === 'dnOfRoland,dc=test') {
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
						return array();
					}
					return false;
			   }));

		//test for existing user
		$result = \OCP\User::userExists('gunslinger');
		$this->assertTrue($result);

		//test for deleted user
		$result = \OCP\User::userExists('formerUser');
		$this->assertFalse($result);

		//test for never-existing user
		$result = \OCP\User::userExists('mallory');
		$this->assertFalse($result);
	}

430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
	public function testDeleteUser() {
		$access = $this->getAccessMock();
		$backend = new UserLDAP($access);

		//we do not support deleting users at all
		$result = $backend->deleteUser('gunslinger');
		$this->assertFalse($result);
	}

	public function testGetHome() {
		$access = $this->getAccessMock();
		$backend = new UserLDAP($access);
		$this->prepareMockForUserExists($access);

		$access->connection->expects($this->any())
			   ->method('__get')
			   ->will($this->returnCallback(function($name) {
					if($name === 'homeFolderNamingRule') {
						return 'attr:testAttribute';
					}
					return null;
			   }));

		$access->expects($this->any())
			   ->method('readAttribute')
			   ->will($this->returnCallback(function($dn, $attr) {
					switch ($dn) {
Arthur Schiwon's avatar
Arthur Schiwon committed
457
						case 'dnOfRoland,dc=test':
458
459
460
461
462
							if($attr === 'testAttribute') {
								return array('/tmp/rolandshome/');
							}
							return array();
							break;
Arthur Schiwon's avatar
Arthur Schiwon committed
463
						case 'dnOfLadyOfShadows,dc=test':
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
							if($attr === 'testAttribute') {
								return array('susannah/');
							}
							return array();
							break;
						default:
							return false;
				   }
			   }));

		//absolut path
		$result = $backend->getHome('gunslinger');
		$this->assertEquals('/tmp/rolandshome/', $result);

		//datadir-relativ path
		$result = $backend->getHome('ladyofshadows');
		$datadir = \OCP\Config::getSystemValue('datadirectory',
											   \OC::$SERVERROOT.'/data');
		$this->assertEquals($datadir.'/susannah/', $result);

		//no path at all – triggers OC default behaviour
		$result = $backend->getHome('newyorker');
		$this->assertFalse($result);
	}

489
	private function prepareAccessForGetDisplayName(&$access) {
490
491
492
493
494
495
496
497
498
499
500
501
502
		$access->connection->expects($this->any())
			   ->method('__get')
			   ->will($this->returnCallback(function($name) {
					if($name === 'ldapUserDisplayName') {
						return 'displayname';
					}
					return null;
			   }));

		$access->expects($this->any())
			   ->method('readAttribute')
			   ->will($this->returnCallback(function($dn, $attr) {
					switch ($dn) {
Arthur Schiwon's avatar
Arthur Schiwon committed
503
						case 'dnOfRoland,dc=test':
504
505
506
507
508
509
510
511
512
513
							if($attr === 'displayname') {
								return array('Roland Deschain');
							}
							return array();
							break;

						default:
							return false;
				   }
			   }));
514
515
516
517
518
519
520
	}

	public function testGetDisplayName() {
		$access = $this->getAccessMock();
		$this->prepareAccessForGetDisplayName($access);
		$backend = new UserLDAP($access);
		$this->prepareMockForUserExists($access);
521
522
523
524
525
526
527
528
529
530

		//with displayName
		$result = $backend->getDisplayName('gunslinger');
		$this->assertEquals('Roland Deschain', $result);

		//empty displayname retrieved
		$result = $backend->getDisplayName('newyorker');
		$this->assertEquals(null, $result);
	}

531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
	public function testGetDisplayNamePublicAPI() {
		$access = $this->getAccessMock();
		$this->prepareAccessForGetDisplayName($access);
		$backend = new UserLDAP($access);
		$this->prepareMockForUserExists($access);
		\OC_User::useBackend($backend);

		//with displayName
		$result = \OCP\User::getDisplayName('gunslinger');
		$this->assertEquals('Roland Deschain', $result);

		//empty displayname retrieved
		$result = \OCP\User::getDisplayName('newyorker');
		$this->assertEquals('newyorker', $result);
	}

547
548
	//no test for getDisplayNames, because it just invokes getUsers and
	//getDisplayName
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602

	public function testCountUsers() {
		$access = $this->getAccessMock();

		$access->connection->expects($this->once())
			   ->method('__get')
			   ->will($this->returnCallback(function($name) {
					if($name === 'ldapLoginFilter') {
						return 'uid=%uid';
					}
					return null;
			   }));

		$access->expects($this->once())
			   ->method('countUsers')
			   ->will($this->returnCallback(function($filter, $a, $b, $c) {
				   if($filter !== 'uid=*') {
					   return false;
				   }
				   return 5;
			   }));

		$backend = new UserLDAP($access);

		$result = $backend->countUsers();
		$this->assertEquals(5, $result);
	}

	public function testCountUsersFailing() {
		$access = $this->getAccessMock();

		$access->connection->expects($this->once())
			   ->method('__get')
			   ->will($this->returnCallback(function($name) {
					if($name === 'ldapLoginFilter') {
						return 'invalidFilter';
					}
					return null;
			   }));

		$access->expects($this->once())
			   ->method('countUsers')
			   ->will($this->returnCallback(function($filter, $a, $b, $c) {
				   if($filter !== 'uid=*') {
					   return false;
				   }
				   return 5;
			   }));

		$backend = new UserLDAP($access);

		$result = $backend->countUsers();
		$this->assertFalse($result);
	}
Robin McCorkell's avatar
Robin McCorkell committed
603
}