2013-09-11 20:51:45 +04:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
|
* @author Arthur Schiwon <blizzz@owncloud.com>
|
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
|
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
|
|
|
|
|
*
|
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
|
* @license AGPL-3.0
|
|
|
|
|
*
|
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program 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, version 3,
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
*
|
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
|
2013-09-11 20:51:45 +04:00
|
|
|
|
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;
|
|
|
|
|
|
2014-11-11 00:28:12 +03:00
|
|
|
|
class Test_User_Ldap_Direct extends \Test\TestCase {
|
2013-09-11 20:51:45 +04:00
|
|
|
|
protected $backend;
|
2014-04-01 14:28:23 +04:00
|
|
|
|
protected $access;
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
2014-11-11 00:28:12 +03:00
|
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
2013-09-11 20:51:45 +04:00
|
|
|
|
\OC_User::clearBackends();
|
|
|
|
|
\OC_Group::clearBackends();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getAccessMock() {
|
|
|
|
|
static $conMethods;
|
|
|
|
|
static $accMethods;
|
2014-04-01 14:28:23 +04:00
|
|
|
|
static $uMethods;
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
2014-04-25 20:52:00 +04:00
|
|
|
|
if(is_null($conMethods) || is_null($accMethods)) {
|
2013-09-11 20:51:45 +04:00
|
|
|
|
$conMethods = get_class_methods('\OCA\user_ldap\lib\Connection');
|
|
|
|
|
$accMethods = get_class_methods('\OCA\user_ldap\lib\Access');
|
2014-04-01 14:28:23 +04:00
|
|
|
|
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)]);
|
2013-09-11 20:51:45 +04:00
|
|
|
|
}
|
|
|
|
|
$lw = $this->getMock('\OCA\user_ldap\lib\ILDAPWrapper');
|
|
|
|
|
$connector = $this->getMock('\OCA\user_ldap\lib\Connection',
|
|
|
|
|
$conMethods,
|
|
|
|
|
array($lw, null, null));
|
2014-04-01 14:28:23 +04:00
|
|
|
|
|
|
|
|
|
$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'),
|
2015-01-07 02:52:18 +03:00
|
|
|
|
$this->getMock('\OCP\Image'),
|
|
|
|
|
$this->getMock('\OCP\IDBConnection')
|
2014-04-01 14:28:23 +04:00
|
|
|
|
);
|
|
|
|
|
|
2013-09-11 20:51:45 +04:00
|
|
|
|
$access = $this->getMock('\OCA\user_ldap\lib\Access',
|
|
|
|
|
$accMethods,
|
2014-04-01 14:28:23 +04:00
|
|
|
|
array($connector, $lw, $um));
|
|
|
|
|
|
|
|
|
|
$um->setLdapAccess($access);
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
return $access;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function prepareMockForUserExists(&$access) {
|
|
|
|
|
$access->expects($this->any())
|
|
|
|
|
->method('username2dn')
|
|
|
|
|
->will($this->returnCallback(function($uid) {
|
|
|
|
|
switch ($uid) {
|
|
|
|
|
case 'gunslinger':
|
2014-04-01 14:28:23 +04:00
|
|
|
|
return 'dnOfRoland,dc=test';
|
2013-09-11 20:51:45 +04:00
|
|
|
|
break;
|
|
|
|
|
case 'formerUser':
|
2014-04-01 14:28:23 +04:00
|
|
|
|
return 'dnOfFormerUser,dc=test';
|
2013-09-11 20:51:45 +04:00
|
|
|
|
break;
|
|
|
|
|
case 'newyorker':
|
2014-04-01 14:28:23 +04:00
|
|
|
|
return 'dnOfNewYorker,dc=test';
|
2013-09-11 20:51:45 +04:00
|
|
|
|
break;
|
|
|
|
|
case 'ladyofshadows':
|
2014-04-01 14:28:23 +04:00
|
|
|
|
return 'dnOfLadyOfShadows,dc=test';
|
2013-09-11 20:51:45 +04:00
|
|
|
|
break;
|
2014-05-11 17:24:42 +04:00
|
|
|
|
default:
|
2013-09-11 20:51:45 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-26 00:14:02 +04:00
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
|
* Prepares the Access mock for checkPassword tests
|
2014-05-13 15:29:25 +04:00
|
|
|
|
* @param \OCA\user_ldap\lib\Access $access mock
|
2014-10-27 17:58:23 +03:00
|
|
|
|
* @param bool noDisplayName
|
2013-09-26 00:14:02 +04:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2014-10-27 17:58:23 +03:00
|
|
|
|
private function prepareAccessForCheckPassword(&$access, $noDisplayName = false) {
|
2013-09-11 20:51:45 +04:00
|
|
|
|
$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') {
|
2014-08-21 19:59:13 +04:00
|
|
|
|
return array(array('dn' => 'dnOfRoland,dc=test'));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
}
|
|
|
|
|
return array();
|
|
|
|
|
}));
|
|
|
|
|
|
2015-04-09 13:00:11 +03:00
|
|
|
|
$access->expects($this->any())
|
|
|
|
|
->method('fetchUsersByLoginName')
|
|
|
|
|
->will($this->returnCallback(function($uid) {
|
|
|
|
|
if($uid === 'roland') {
|
|
|
|
|
return array(array('dn' => 'dnOfRoland,dc=test'));
|
|
|
|
|
}
|
|
|
|
|
return array();
|
|
|
|
|
}));
|
|
|
|
|
|
2014-10-27 17:58:23 +03:00
|
|
|
|
$retVal = 'gunslinger';
|
|
|
|
|
if($noDisplayName === true) {
|
|
|
|
|
$retVal = false;
|
|
|
|
|
}
|
2013-09-11 20:51:45 +04:00
|
|
|
|
$access->expects($this->any())
|
|
|
|
|
->method('dn2username')
|
2014-04-01 14:28:23 +04:00
|
|
|
|
->with($this->equalTo('dnOfRoland,dc=test'))
|
2014-10-27 17:58:23 +03:00
|
|
|
|
->will($this->returnValue($retVal));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
2014-08-12 18:13:17 +04:00
|
|
|
|
$access->expects($this->any())
|
|
|
|
|
->method('stringResemblesDN')
|
|
|
|
|
->with($this->equalTo('dnOfRoland,dc=test'))
|
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
2013-09-11 20:51:45 +04:00
|
|
|
|
$access->expects($this->any())
|
|
|
|
|
->method('areCredentialsValid')
|
|
|
|
|
->will($this->returnCallback(function($dn, $pwd) {
|
|
|
|
|
if($pwd === 'dt19') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 17:05:45 +04:00
|
|
|
|
public function testCheckPasswordUidReturn() {
|
2013-09-26 00:14:02 +04:00
|
|
|
|
$access = $this->getAccessMock();
|
2014-02-20 17:05:45 +04:00
|
|
|
|
|
2013-09-26 00:14:02 +04:00
|
|
|
|
$this->prepareAccessForCheckPassword($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
\OC_User::useBackend($backend);
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
$result = $backend->checkPassword('roland', 'dt19');
|
|
|
|
|
$this->assertEquals('gunslinger', $result);
|
2014-02-20 17:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCheckPasswordWrongPassword() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
|
|
|
|
|
$this->prepareAccessForCheckPassword($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
\OC_User::useBackend($backend);
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
$result = $backend->checkPassword('roland', 'wrong');
|
|
|
|
|
$this->assertFalse($result);
|
2014-02-20 17:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCheckPasswordWrongUser() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
|
|
|
|
|
$this->prepareAccessForCheckPassword($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
\OC_User::useBackend($backend);
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
$result = $backend->checkPassword('mallory', 'evil');
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 17:58:23 +03:00
|
|
|
|
public function testCheckPasswordNoDisplayName() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
|
|
|
|
|
$this->prepareAccessForCheckPassword($access, true);
|
|
|
|
|
$access->expects($this->once())
|
|
|
|
|
->method('username2dn')
|
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2014-10-27 17:58:23 +03:00
|
|
|
|
\OC_User::useBackend($backend);
|
|
|
|
|
|
|
|
|
|
$result = $backend->checkPassword('roland', 'dt19');
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-26 00:14:02 +04:00
|
|
|
|
public function testCheckPasswordPublicAPI() {
|
2013-09-11 20:51:45 +04:00
|
|
|
|
$access = $this->getAccessMock();
|
2013-09-26 00:14:02 +04:00
|
|
|
|
$this->prepareAccessForCheckPassword($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
\OC_User::useBackend($backend);
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
2013-09-26 00:14:02 +04:00
|
|
|
|
$result = \OCP\User::checkPassword('roland', 'dt19');
|
|
|
|
|
$this->assertEquals('gunslinger', $result);
|
2014-02-20 17:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCheckPasswordPublicAPIWrongPassword() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForCheckPassword($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
\OC_User::useBackend($backend);
|
2013-09-26 00:14:02 +04:00
|
|
|
|
|
|
|
|
|
$result = \OCP\User::checkPassword('roland', 'wrong');
|
|
|
|
|
$this->assertFalse($result);
|
2014-02-20 17:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCheckPasswordPublicAPIWrongUser() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForCheckPassword($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
\OC_User::useBackend($backend);
|
2013-09-26 00:14:02 +04:00
|
|
|
|
|
|
|
|
|
$result = \OCP\User::checkPassword('mallory', 'evil');
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-21 19:59:13 +04:00
|
|
|
|
public function testDeleteUserCancel() {
|
|
|
|
|
$access = $this->getAccessMock();
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2014-08-21 19:59:13 +04:00
|
|
|
|
$result = $backend->deleteUser('notme');
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDeleteUserSuccess() {
|
|
|
|
|
$access = $this->getAccessMock();
|
2014-12-20 18:09:04 +03:00
|
|
|
|
$mapping = $this->getMockBuilder('\OCA\User_LDAP\Mapping\UserMapping')
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
$mapping->expects($this->once())
|
|
|
|
|
->method('unmap')
|
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
$access->expects($this->once())
|
|
|
|
|
->method('getUserMapper')
|
|
|
|
|
->will($this->returnValue($mapping));
|
|
|
|
|
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$config = $this->getMock('\OCP\IConfig');
|
|
|
|
|
$config->expects($this->exactly(2))
|
|
|
|
|
->method('getUserValue')
|
|
|
|
|
->will($this->returnValue(1));
|
2014-08-21 19:59:13 +04:00
|
|
|
|
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $config);
|
2014-08-21 19:59:13 +04:00
|
|
|
|
|
|
|
|
|
$result = $backend->deleteUser('jeremy');
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-26 00:14:02 +04:00
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
|
* Prepares the Access mock for getUsers tests
|
2014-05-13 15:29:25 +04:00
|
|
|
|
* @param \OCA\user_ldap\lib\Access $access mock
|
2013-09-26 00:14:02 +04:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function prepareAccessForGetUsers(&$access) {
|
2014-02-20 17:05:45 +04:00
|
|
|
|
$access->expects($this->once())
|
|
|
|
|
->method('escapeFilterPart')
|
|
|
|
|
->will($this->returnCallback(function($search) {
|
|
|
|
|
return $search;
|
|
|
|
|
}));
|
|
|
|
|
|
2013-09-11 20:51:45 +04:00
|
|
|
|
$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));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 17:05:45 +04:00
|
|
|
|
public function testGetUsersNoParam() {
|
2013-09-26 00:14:02 +04:00
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForGetUsers($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
$result = $backend->getUsers();
|
|
|
|
|
$this->assertEquals(3, count($result));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetUsersLimitOffset() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForGetUsers($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
$result = $backend->getUsers('', 1, 2);
|
|
|
|
|
$this->assertEquals(1, count($result));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetUsersLimitOffset2() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForGetUsers($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
$result = $backend->getUsers('', 2, 1);
|
|
|
|
|
$this->assertEquals(2, count($result));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetUsersSearchWithResult() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForGetUsers($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
$result = $backend->getUsers('yo');
|
|
|
|
|
$this->assertEquals(2, count($result));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetUsersSearchEmptyResult() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForGetUsers($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
$result = $backend->getUsers('nix');
|
|
|
|
|
$this->assertEquals(0, count($result));
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 17:05:45 +04:00
|
|
|
|
public function testGetUsersViaAPINoParam() {
|
2013-09-26 00:14:02 +04:00
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForGetUsers($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
\OC_User::useBackend($backend);
|
|
|
|
|
|
|
|
|
|
$result = \OCP\User::getUsers();
|
|
|
|
|
$this->assertEquals(3, count($result));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetUsersViaAPILimitOffset() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForGetUsers($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
\OC_User::useBackend($backend);
|
2013-09-26 00:14:02 +04:00
|
|
|
|
|
|
|
|
|
$result = \OCP\User::getUsers('', 1, 2);
|
|
|
|
|
$this->assertEquals(1, count($result));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetUsersViaAPILimitOffset2() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForGetUsers($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
\OC_User::useBackend($backend);
|
2013-09-26 00:14:02 +04:00
|
|
|
|
|
|
|
|
|
$result = \OCP\User::getUsers('', 2, 1);
|
|
|
|
|
$this->assertEquals(2, count($result));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetUsersViaAPISearchWithResult() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForGetUsers($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
\OC_User::useBackend($backend);
|
2013-09-26 00:14:02 +04:00
|
|
|
|
|
|
|
|
|
$result = \OCP\User::getUsers('yo');
|
|
|
|
|
$this->assertEquals(2, count($result));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetUsersViaAPISearchEmptyResult() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForGetUsers($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2014-02-20 17:05:45 +04:00
|
|
|
|
\OC_User::useBackend($backend);
|
2013-09-26 00:14:02 +04:00
|
|
|
|
|
|
|
|
|
$result = \OCP\User::getUsers('nix');
|
|
|
|
|
$this->assertEquals(0, count($result));
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 20:51:45 +04:00
|
|
|
|
public function testUserExists() {
|
|
|
|
|
$access = $this->getAccessMock();
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
$this->prepareMockForUserExists($access);
|
|
|
|
|
|
|
|
|
|
$access->expects($this->any())
|
2015-03-31 16:33:44 +03:00
|
|
|
|
->method('readAttribute')
|
|
|
|
|
->will($this->returnCallback(function($dn) {
|
|
|
|
|
if($dn === 'dnOfRoland,dc=test') {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
//test for existing user
|
|
|
|
|
$result = $backend->userExists('gunslinger');
|
|
|
|
|
$this->assertTrue($result);
|
2015-03-31 16:33:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @expectedException \Exception
|
|
|
|
|
*/
|
|
|
|
|
public function testUserExistsForDeleted() {
|
|
|
|
|
$access = $this->getAccessMock();
|
2015-04-09 15:03:30 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2015-03-31 16:33:44 +03:00
|
|
|
|
$this->prepareMockForUserExists($access);
|
|
|
|
|
|
|
|
|
|
$access->expects($this->any())
|
|
|
|
|
->method('readAttribute')
|
|
|
|
|
->will($this->returnCallback(function($dn) {
|
|
|
|
|
if($dn === 'dnOfRoland,dc=test') {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
//test for deleted user
|
|
|
|
|
$result = $backend->userExists('formerUser');
|
2015-03-31 16:33:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUserExistsForNeverExisting() {
|
|
|
|
|
$access = $this->getAccessMock();
|
2015-04-09 15:03:30 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2015-03-31 16:33:44 +03:00
|
|
|
|
$this->prepareMockForUserExists($access);
|
|
|
|
|
|
|
|
|
|
$access->expects($this->any())
|
|
|
|
|
->method('readAttribute')
|
|
|
|
|
->will($this->returnCallback(function($dn) {
|
|
|
|
|
if($dn === 'dnOfRoland,dc=test') {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
//test for never-existing user
|
|
|
|
|
$result = $backend->userExists('mallory');
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-26 00:14:02 +04:00
|
|
|
|
public function testUserExistsPublicAPI() {
|
|
|
|
|
$access = $this->getAccessMock();
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
$this->prepareMockForUserExists($access);
|
|
|
|
|
\OC_User::useBackend($backend);
|
|
|
|
|
|
|
|
|
|
$access->expects($this->any())
|
2015-03-31 16:33:44 +03:00
|
|
|
|
->method('readAttribute')
|
|
|
|
|
->will($this->returnCallback(function($dn) {
|
|
|
|
|
if($dn === 'dnOfRoland,dc=test') {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
|
|
|
|
|
//test for existing user
|
|
|
|
|
$result = \OCP\User::userExists('gunslinger');
|
|
|
|
|
$this->assertTrue($result);
|
2015-03-31 16:33:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @expectedException \Exception
|
|
|
|
|
*/
|
|
|
|
|
public function testUserExistsPublicAPIForDeleted() {
|
|
|
|
|
$access = $this->getAccessMock();
|
2015-04-09 15:03:30 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2015-03-31 16:33:44 +03:00
|
|
|
|
$this->prepareMockForUserExists($access);
|
|
|
|
|
\OC_User::useBackend($backend);
|
|
|
|
|
|
|
|
|
|
$access->expects($this->any())
|
|
|
|
|
->method('readAttribute')
|
|
|
|
|
->will($this->returnCallback(function($dn) {
|
|
|
|
|
if($dn === 'dnOfRoland,dc=test') {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
|
|
|
|
|
//test for deleted user
|
|
|
|
|
$result = \OCP\User::userExists('formerUser');
|
2015-03-31 16:33:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUserExistsPublicAPIForNeverExisting() {
|
|
|
|
|
$access = $this->getAccessMock();
|
2015-04-09 15:03:30 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2015-03-31 16:33:44 +03:00
|
|
|
|
$this->prepareMockForUserExists($access);
|
|
|
|
|
\OC_User::useBackend($backend);
|
|
|
|
|
|
|
|
|
|
$access->expects($this->any())
|
|
|
|
|
->method('readAttribute')
|
|
|
|
|
->will($this->returnCallback(function($dn) {
|
|
|
|
|
if($dn === 'dnOfRoland,dc=test') {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
|
|
|
|
|
//test for never-existing user
|
|
|
|
|
$result = \OCP\User::userExists('mallory');
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 20:51:45 +04:00
|
|
|
|
public function testDeleteUser() {
|
|
|
|
|
$access = $this->getAccessMock();
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
//we do not support deleting users at all
|
|
|
|
|
$result = $backend->deleteUser('gunslinger');
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 16:33:44 +03:00
|
|
|
|
public function testGetHomeAbsolutePath() {
|
2013-09-11 20:51:45 +04:00
|
|
|
|
$access = $this->getAccessMock();
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$config = $this->getMock('\OCP\IConfig');
|
|
|
|
|
$backend = new UserLDAP($access, $config);
|
2013-09-11 20:51:45 +04:00
|
|
|
|
$this->prepareMockForUserExists($access);
|
|
|
|
|
|
|
|
|
|
$access->connection->expects($this->any())
|
2015-03-31 16:33:44 +03:00
|
|
|
|
->method('__get')
|
|
|
|
|
->will($this->returnCallback(function($name) {
|
|
|
|
|
if($name === 'homeFolderNamingRule') {
|
|
|
|
|
return 'attr:testAttribute';
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
$access->expects($this->any())
|
2015-03-31 16:33:44 +03:00
|
|
|
|
->method('readAttribute')
|
|
|
|
|
->will($this->returnCallback(function($dn, $attr) {
|
|
|
|
|
switch ($dn) {
|
|
|
|
|
case 'dnOfRoland,dc=test':
|
|
|
|
|
if($attr === 'testAttribute') {
|
|
|
|
|
return array('/tmp/rolandshome/');
|
|
|
|
|
}
|
|
|
|
|
return array();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
//absolut path
|
|
|
|
|
$result = $backend->getHome('gunslinger');
|
|
|
|
|
$this->assertEquals('/tmp/rolandshome/', $result);
|
2015-03-31 16:33:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-09 15:03:30 +03:00
|
|
|
|
public function testGetHomeRelative() {
|
2015-03-31 16:33:44 +03:00
|
|
|
|
$access = $this->getAccessMock();
|
2015-04-09 15:03:30 +03:00
|
|
|
|
$config = $this->getMock('\OCP\IConfig');
|
|
|
|
|
$backend = new UserLDAP($access, $config);
|
2015-03-31 16:33:44 +03:00
|
|
|
|
$this->prepareMockForUserExists($access);
|
|
|
|
|
|
|
|
|
|
$access->connection->expects($this->any())
|
|
|
|
|
->method('__get')
|
|
|
|
|
->will($this->returnCallback(function($name) {
|
|
|
|
|
if($name === 'homeFolderNamingRule') {
|
|
|
|
|
return 'attr:testAttribute';
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
2015-03-31 16:33:44 +03:00
|
|
|
|
$access->expects($this->any())
|
|
|
|
|
->method('readAttribute')
|
|
|
|
|
->will($this->returnCallback(function($dn, $attr) {
|
|
|
|
|
switch ($dn) {
|
|
|
|
|
case 'dnOfLadyOfShadows,dc=test':
|
|
|
|
|
if($attr === 'testAttribute') {
|
|
|
|
|
return array('susannah/');
|
|
|
|
|
}
|
|
|
|
|
return array();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
//datadir-relativ path
|
2015-04-09 15:03:30 +03:00
|
|
|
|
$datadir = '/my/data/dir';
|
|
|
|
|
$config->expects($this->once())
|
|
|
|
|
->method('getSystemValue')
|
|
|
|
|
->will($this->returnValue($datadir));
|
|
|
|
|
|
2013-09-11 20:51:45 +04:00
|
|
|
|
$result = $backend->getHome('ladyofshadows');
|
|
|
|
|
$this->assertEquals($datadir.'/susannah/', $result);
|
2015-03-31 16:33:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @expectedException \Exception
|
|
|
|
|
*/
|
|
|
|
|
public function testGetHomeNoPath() {
|
|
|
|
|
$access = $this->getAccessMock();
|
2015-04-09 15:03:30 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2015-03-31 16:33:44 +03:00
|
|
|
|
$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) {
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}));
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
|
|
|
|
//no path at all – triggers OC default behaviour
|
|
|
|
|
$result = $backend->getHome('newyorker');
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-26 00:14:02 +04:00
|
|
|
|
private function prepareAccessForGetDisplayName(&$access) {
|
2013-09-11 20:51:45 +04:00
|
|
|
|
$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) {
|
2014-04-01 14:28:23 +04:00
|
|
|
|
case 'dnOfRoland,dc=test':
|
2013-09-11 20:51:45 +04:00
|
|
|
|
if($attr === 'displayname') {
|
|
|
|
|
return array('Roland Deschain');
|
|
|
|
|
}
|
|
|
|
|
return array();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetDisplayName() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
$this->prepareAccessForGetDisplayName($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
$this->prepareMockForUserExists($access);
|
2013-09-11 20:51:45 +04:00
|
|
|
|
|
2015-03-31 16:33:44 +03:00
|
|
|
|
$access->connection->expects($this->any())
|
|
|
|
|
->method('getConnectionResource')
|
|
|
|
|
->will($this->returnCallback(function() {
|
|
|
|
|
return true;
|
|
|
|
|
}));
|
|
|
|
|
|
2013-09-11 20:51:45 +04:00
|
|
|
|
//with displayName
|
|
|
|
|
$result = $backend->getDisplayName('gunslinger');
|
|
|
|
|
$this->assertEquals('Roland Deschain', $result);
|
|
|
|
|
|
|
|
|
|
//empty displayname retrieved
|
|
|
|
|
$result = $backend->getDisplayName('newyorker');
|
|
|
|
|
$this->assertEquals(null, $result);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-26 00:14:02 +04:00
|
|
|
|
public function testGetDisplayNamePublicAPI() {
|
|
|
|
|
$access = $this->getAccessMock();
|
2015-03-31 16:33:44 +03:00
|
|
|
|
$access->expects($this->any())
|
|
|
|
|
->method('username2dn')
|
|
|
|
|
->will($this->returnCallback(function($uid) {
|
|
|
|
|
switch ($uid) {
|
|
|
|
|
case 'gunslinger':
|
|
|
|
|
return 'dnOfRoland,dc=test';
|
|
|
|
|
break;
|
|
|
|
|
case 'formerUser':
|
|
|
|
|
return 'dnOfFormerUser,dc=test';
|
|
|
|
|
break;
|
|
|
|
|
case 'newyorker':
|
|
|
|
|
return 'dnOfNewYorker,dc=test';
|
|
|
|
|
break;
|
|
|
|
|
case 'ladyofshadows':
|
|
|
|
|
return 'dnOfLadyOfShadows,dc=test';
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
$this->prepareAccessForGetDisplayName($access);
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2013-09-26 00:14:02 +04:00
|
|
|
|
$this->prepareMockForUserExists($access);
|
2015-03-31 16:33:44 +03:00
|
|
|
|
|
|
|
|
|
$access->connection->expects($this->any())
|
|
|
|
|
->method('getConnectionResource')
|
|
|
|
|
->will($this->returnCallback(function() {
|
|
|
|
|
return true;
|
|
|
|
|
}));
|
|
|
|
|
|
2013-09-26 00:14:02 +04:00
|
|
|
|
\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);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 20:51:45 +04:00
|
|
|
|
//no test for getDisplayNames, because it just invokes getUsers and
|
|
|
|
|
//getDisplayName
|
2014-01-08 15:24:29 +04:00
|
|
|
|
|
|
|
|
|
public function testCountUsers() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
|
|
|
|
|
$access->expects($this->once())
|
|
|
|
|
->method('countUsers')
|
2014-10-28 01:39:30 +03:00
|
|
|
|
->will($this->returnValue(5));
|
2014-01-08 15:24:29 +04:00
|
|
|
|
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2014-01-08 15:24:29 +04:00
|
|
|
|
|
|
|
|
|
$result = $backend->countUsers();
|
|
|
|
|
$this->assertEquals(5, $result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCountUsersFailing() {
|
|
|
|
|
$access = $this->getAccessMock();
|
|
|
|
|
|
|
|
|
|
$access->expects($this->once())
|
|
|
|
|
->method('countUsers')
|
2014-10-28 01:39:30 +03:00
|
|
|
|
->will($this->returnValue(false));
|
2014-01-08 15:24:29 +04:00
|
|
|
|
|
2015-01-07 01:28:49 +03:00
|
|
|
|
$backend = new UserLDAP($access, $this->getMock('\OCP\IConfig'));
|
2014-01-08 15:24:29 +04:00
|
|
|
|
|
|
|
|
|
$result = $backend->countUsers();
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
}
|
2014-05-13 15:29:25 +04:00
|
|
|
|
}
|