Blame view

sources/apps/user_ldap/tests/user_ldap.php 14.9 KB
31b7f2792   Kload   Upgrade to ownclo...
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  <?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;
  
  	public function setUp() {
  		\OC_User::clearBackends();
  		\OC_Group::clearBackends();
  	}
  
  	private function getAccessMock() {
  		static $conMethods;
  		static $accMethods;
  
  		if(is_null($conMethods) || is_null($accMethods)) {
  			$conMethods = get_class_methods('\OCA\user_ldap\lib\Connection');
  			$accMethods = get_class_methods('\OCA\user_ldap\lib\Access');
  		}
  		$lw  = $this->getMock('\OCA\user_ldap\lib\ILDAPWrapper');
  		$connector = $this->getMock('\OCA\user_ldap\lib\Connection',
  									$conMethods,
  									array($lw, null, null));
  		$access = $this->getMock('\OCA\user_ldap\lib\Access',
  								 $accMethods,
  								 array($connector, $lw));
  
  		return $access;
  	}
  
  	private function prepareMockForUserExists(&$access) {
  		$access->expects($this->any())
  			   ->method('username2dn')
  			   ->will($this->returnCallback(function($uid) {
  					switch ($uid) {
  						case 'gunslinger':
  							return 'dnOfRoland';
  							break;
  						case 'formerUser':
  							return 'dnOfFormerUser';
  							break;
  						case 'newyorker':
  							return 'dnOfNewYorker';
  							break;
  						case 'ladyofshadows':
  							return 'dnOfLadyOfShadows';
  							break;
  						defautl:
  							return false;
  					}
  			   }));
  	}
  
  	/**
  	 * @brief Prepares the Access mock for checkPassword tests
  	 * @param $access mock of \OCA\user_ldap\lib\Access
  	 * @return void
  	 */
  	private function prepareAccessForCheckPassword(&$access) {
a293d369c   Kload   Update sources to...
86
87
88
89
90
  		$access->expects($this->once())
  			   ->method('escapeFilterPart')
  			   ->will($this->returnCallback(function($uid) {
  				   return $uid;
  			   }));
31b7f2792   Kload   Upgrade to ownclo...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  		$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') {
  						return array('dnOfRoland');
  					}
  					return array();
  			   }));
  
  		$access->expects($this->any())
  			   ->method('dn2username')
  			   ->with($this->equalTo('dnOfRoland'))
  			   ->will($this->returnValue('gunslinger'));
  
  		$access->expects($this->any())
  			   ->method('areCredentialsValid')
  			   ->will($this->returnCallback(function($dn, $pwd) {
  					if($pwd === 'dt19') {
  						return true;
  					}
  					return false;
  			   }));
  	}
a293d369c   Kload   Update sources to...
123
  	public function testCheckPasswordUidReturn() {
31b7f2792   Kload   Upgrade to ownclo...
124
  		$access = $this->getAccessMock();
a293d369c   Kload   Update sources to...
125

31b7f2792   Kload   Upgrade to ownclo...
126
127
128
129
130
131
  		$this->prepareAccessForCheckPassword($access);
  		$backend = new UserLDAP($access);
  		\OC_User::useBackend($backend);
  
  		$result = $backend->checkPassword('roland', 'dt19');
  		$this->assertEquals('gunslinger', $result);
a293d369c   Kload   Update sources to...
132
133
134
135
136
137
138
139
  	}
  
  	public function testCheckPasswordWrongPassword() {
  		$access = $this->getAccessMock();
  
  		$this->prepareAccessForCheckPassword($access);
  		$backend = new UserLDAP($access);
  		\OC_User::useBackend($backend);
31b7f2792   Kload   Upgrade to ownclo...
140
141
142
  
  		$result = $backend->checkPassword('roland', 'wrong');
  		$this->assertFalse($result);
a293d369c   Kload   Update sources to...
143
144
145
146
147
148
149
150
  	}
  
  	public function testCheckPasswordWrongUser() {
  		$access = $this->getAccessMock();
  
  		$this->prepareAccessForCheckPassword($access);
  		$backend = new UserLDAP($access);
  		\OC_User::useBackend($backend);
31b7f2792   Kload   Upgrade to ownclo...
151
152
153
154
155
156
157
158
159
160
161
162
163
  
  		$result = $backend->checkPassword('mallory', 'evil');
  		$this->assertFalse($result);
  	}
  
  	public function testCheckPasswordPublicAPI() {
  		$access = $this->getAccessMock();
  		$this->prepareAccessForCheckPassword($access);
  		$backend = new UserLDAP($access);
  		\OC_User::useBackend($backend);
  
  		$result = \OCP\User::checkPassword('roland', 'dt19');
  		$this->assertEquals('gunslinger', $result);
a293d369c   Kload   Update sources to...
164
165
166
167
168
169
170
  	}
  
  	public function testCheckPasswordPublicAPIWrongPassword() {
  		$access = $this->getAccessMock();
  		$this->prepareAccessForCheckPassword($access);
  		$backend = new UserLDAP($access);
  		\OC_User::useBackend($backend);
31b7f2792   Kload   Upgrade to ownclo...
171
172
173
  
  		$result = \OCP\User::checkPassword('roland', 'wrong');
  		$this->assertFalse($result);
a293d369c   Kload   Update sources to...
174
175
176
177
178
179
180
  	}
  
  	public function testCheckPasswordPublicAPIWrongUser() {
  		$access = $this->getAccessMock();
  		$this->prepareAccessForCheckPassword($access);
  		$backend = new UserLDAP($access);
  		\OC_User::useBackend($backend);
31b7f2792   Kload   Upgrade to ownclo...
181
182
183
184
185
186
187
188
189
190
191
  
  		$result = \OCP\User::checkPassword('mallory', 'evil');
  		$this->assertFalse($result);
  	}
  
  	/**
  	 * @brief Prepares the Access mock for getUsers tests
  	 * @param $access mock of \OCA\user_ldap\lib\Access
  	 * @return void
  	 */
  	private function prepareAccessForGetUsers(&$access) {
a293d369c   Kload   Update sources to...
192
193
194
195
196
  		$access->expects($this->once())
  			   ->method('escapeFilterPart')
  			   ->will($this->returnCallback(function($search) {
  				   return $search;
  			   }));
31b7f2792   Kload   Upgrade to ownclo...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  		$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));
  	}
a293d369c   Kload   Update sources to...
233
  	public function testGetUsersNoParam() {
31b7f2792   Kload   Upgrade to ownclo...
234
235
236
237
238
239
  		$access = $this->getAccessMock();
  		$this->prepareAccessForGetUsers($access);
  		$backend = new UserLDAP($access);
  
  		$result = $backend->getUsers();
  		$this->assertEquals(3, count($result));
a293d369c   Kload   Update sources to...
240
241
242
243
244
245
  	}
  
  	public function testGetUsersLimitOffset() {
  		$access = $this->getAccessMock();
  		$this->prepareAccessForGetUsers($access);
  		$backend = new UserLDAP($access);
31b7f2792   Kload   Upgrade to ownclo...
246
247
248
  
  		$result = $backend->getUsers('', 1, 2);
  		$this->assertEquals(1, count($result));
a293d369c   Kload   Update sources to...
249
250
251
252
253
254
  	}
  
  	public function testGetUsersLimitOffset2() {
  		$access = $this->getAccessMock();
  		$this->prepareAccessForGetUsers($access);
  		$backend = new UserLDAP($access);
31b7f2792   Kload   Upgrade to ownclo...
255
256
257
  
  		$result = $backend->getUsers('', 2, 1);
  		$this->assertEquals(2, count($result));
a293d369c   Kload   Update sources to...
258
259
260
261
262
263
  	}
  
  	public function testGetUsersSearchWithResult() {
  		$access = $this->getAccessMock();
  		$this->prepareAccessForGetUsers($access);
  		$backend = new UserLDAP($access);
31b7f2792   Kload   Upgrade to ownclo...
264
265
266
  
  		$result = $backend->getUsers('yo');
  		$this->assertEquals(2, count($result));
a293d369c   Kload   Update sources to...
267
268
269
270
271
272
  	}
  
  	public function testGetUsersSearchEmptyResult() {
  		$access = $this->getAccessMock();
  		$this->prepareAccessForGetUsers($access);
  		$backend = new UserLDAP($access);
31b7f2792   Kload   Upgrade to ownclo...
273
274
275
276
  
  		$result = $backend->getUsers('nix');
  		$this->assertEquals(0, count($result));
  	}
a293d369c   Kload   Update sources to...
277
  	public function testGetUsersViaAPINoParam() {
31b7f2792   Kload   Upgrade to ownclo...
278
279
280
281
282
283
284
  		$access = $this->getAccessMock();
  		$this->prepareAccessForGetUsers($access);
  		$backend = new UserLDAP($access);
  		\OC_User::useBackend($backend);
  
  		$result = \OCP\User::getUsers();
  		$this->assertEquals(3, count($result));
a293d369c   Kload   Update sources to...
285
286
287
288
289
290
291
  	}
  
  	public function testGetUsersViaAPILimitOffset() {
  		$access = $this->getAccessMock();
  		$this->prepareAccessForGetUsers($access);
  		$backend = new UserLDAP($access);
  		\OC_User::useBackend($backend);
31b7f2792   Kload   Upgrade to ownclo...
292
293
294
  
  		$result = \OCP\User::getUsers('', 1, 2);
  		$this->assertEquals(1, count($result));
a293d369c   Kload   Update sources to...
295
296
297
298
299
300
301
  	}
  
  	public function testGetUsersViaAPILimitOffset2() {
  		$access = $this->getAccessMock();
  		$this->prepareAccessForGetUsers($access);
  		$backend = new UserLDAP($access);
  		\OC_User::useBackend($backend);
31b7f2792   Kload   Upgrade to ownclo...
302
303
304
  
  		$result = \OCP\User::getUsers('', 2, 1);
  		$this->assertEquals(2, count($result));
a293d369c   Kload   Update sources to...
305
306
307
308
309
310
311
  	}
  
  	public function testGetUsersViaAPISearchWithResult() {
  		$access = $this->getAccessMock();
  		$this->prepareAccessForGetUsers($access);
  		$backend = new UserLDAP($access);
  		\OC_User::useBackend($backend);
31b7f2792   Kload   Upgrade to ownclo...
312
313
314
  
  		$result = \OCP\User::getUsers('yo');
  		$this->assertEquals(2, count($result));
a293d369c   Kload   Update sources to...
315
316
317
318
319
320
321
  	}
  
  	public function testGetUsersViaAPISearchEmptyResult() {
  		$access = $this->getAccessMock();
  		$this->prepareAccessForGetUsers($access);
  		$backend = new UserLDAP($access);
  		\OC_User::useBackend($backend);
31b7f2792   Kload   Upgrade to ownclo...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
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
457
458
459
460
461
462
463
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
489
490
491
492
493
494
495
496
497
498
499
500
  
  		$result = \OCP\User::getUsers('nix');
  		$this->assertEquals(0, count($result));
  	}
  
  	public function testUserExists() {
  		$access = $this->getAccessMock();
  		$backend = new UserLDAP($access);
  		$this->prepareMockForUserExists($access);
  
  		$access->expects($this->any())
  			   ->method('readAttribute')
  			   ->will($this->returnCallback(function($dn) {
  					if($dn === 'dnOfRoland') {
  						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);
  	}
  
  	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) {
  					if($dn === 'dnOfRoland') {
  						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);
  	}
  
  	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) {
  						case 'dnOfRoland':
  							if($attr === 'testAttribute') {
  								return array('/tmp/rolandshome/');
  							}
  							return array();
  							break;
  						case 'dnOfLadyOfShadows':
  							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);
  	}
  
  	private function prepareAccessForGetDisplayName(&$access) {
  		$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) {
  						case 'dnOfRoland':
  							if($attr === 'displayname') {
  								return array('Roland Deschain');
  							}
  							return array();
  							break;
  
  						default:
  							return false;
  				   }
  			   }));
  	}
  
  	public function testGetDisplayName() {
  		$access = $this->getAccessMock();
  		$this->prepareAccessForGetDisplayName($access);
  		$backend = new UserLDAP($access);
  		$this->prepareMockForUserExists($access);
  
  		//with displayName
  		$result = $backend->getDisplayName('gunslinger');
  		$this->assertEquals('Roland Deschain', $result);
  
  		//empty displayname retrieved
  		$result = $backend->getDisplayName('newyorker');
  		$this->assertEquals(null, $result);
  	}
  
  	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);
  	}
  
  	//no test for getDisplayNames, because it just invokes getUsers and
  	//getDisplayName
a293d369c   Kload   Update sources to...
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
  
  	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);
  	}
31b7f2792   Kload   Upgrade to ownclo...
555
  }