|
|
|
|
@ -28,15 +28,22 @@ |
|
|
|
|
|
|
|
|
|
namespace OCA\User_LDAP\Tests; |
|
|
|
|
|
|
|
|
|
use OCA\User_LDAP\Access; |
|
|
|
|
use OCA\User_LDAP\Connection; |
|
|
|
|
use OCA\User_LDAP\FilesystemHelper; |
|
|
|
|
use OCA\User_LDAP\Helper; |
|
|
|
|
use OCA\User_LDAP\ILDAPWrapper; |
|
|
|
|
use OCA\User_LDAP\LogWrapper; |
|
|
|
|
use OCA\User_LDAP\User\Manager; |
|
|
|
|
use OCA\User_LDAP\User\OfflineUser; |
|
|
|
|
use OCA\User_LDAP\User\User; |
|
|
|
|
use OCA\User_LDAP\User_LDAP as UserLDAP; |
|
|
|
|
use OCP\IAvatarManager; |
|
|
|
|
use OCP\IConfig; |
|
|
|
|
use OCP\IDBConnection; |
|
|
|
|
use OCP\Image; |
|
|
|
|
use OCP\IUserManager; |
|
|
|
|
use Test\TestCase; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Class Test_User_Ldap_Direct |
|
|
|
|
@ -45,7 +52,7 @@ use OCP\IUserManager; |
|
|
|
|
* |
|
|
|
|
* @package OCA\User_LDAP\Tests |
|
|
|
|
*/ |
|
|
|
|
class User_LDAPTest extends \Test\TestCase { |
|
|
|
|
class User_LDAPTest extends TestCase { |
|
|
|
|
protected $backend; |
|
|
|
|
protected $access; |
|
|
|
|
protected $configMock; |
|
|
|
|
@ -57,24 +64,15 @@ class User_LDAPTest extends \Test\TestCase { |
|
|
|
|
\OC_Group::clearBackends(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Access |
|
|
|
|
*/ |
|
|
|
|
private function getAccessMock() { |
|
|
|
|
static $conMethods; |
|
|
|
|
static $accMethods; |
|
|
|
|
static $uMethods; |
|
|
|
|
|
|
|
|
|
if(is_null($conMethods) || is_null($accMethods)) { |
|
|
|
|
$conMethods = get_class_methods('\OCA\User_LDAP\Connection'); |
|
|
|
|
$accMethods = get_class_methods('\OCA\User_LDAP\Access'); |
|
|
|
|
unset($accMethods[array_search('getConnection', $accMethods)]); |
|
|
|
|
$uMethods = get_class_methods('\OCA\User_LDAP\User\User'); |
|
|
|
|
unset($uMethods[array_search('getUsername', $uMethods)]); |
|
|
|
|
unset($uMethods[array_search('getDN', $uMethods)]); |
|
|
|
|
unset($uMethods[array_search('__construct', $uMethods)]); |
|
|
|
|
} |
|
|
|
|
$lw = $this->createMock(ILDAPWrapper::class); |
|
|
|
|
$connector = $this->getMock('\OCA\User_LDAP\Connection', |
|
|
|
|
$conMethods, |
|
|
|
|
array($lw, null, null)); |
|
|
|
|
$connector = $this->getMockBuilder(Connection::class) |
|
|
|
|
->setMethodsExcept(['getConnection']) |
|
|
|
|
->setConstructorArgs([$lw, null, null]) |
|
|
|
|
->getMock(); |
|
|
|
|
|
|
|
|
|
$this->configMock = $this->createMock(IConfig::class); |
|
|
|
|
|
|
|
|
|
@ -82,7 +80,7 @@ class User_LDAPTest extends \Test\TestCase { |
|
|
|
|
->disableOriginalConstructor() |
|
|
|
|
->getMock(); |
|
|
|
|
|
|
|
|
|
$um = $this->getMockBuilder('\OCA\User_LDAP\User\Manager') |
|
|
|
|
$um = $this->getMockBuilder(Manager::class) |
|
|
|
|
->setMethods(['getDeletedUser']) |
|
|
|
|
->setConstructorArgs([ |
|
|
|
|
$this->configMock, |
|
|
|
|
@ -99,11 +97,12 @@ class User_LDAPTest extends \Test\TestCase { |
|
|
|
|
->method('getDeletedUser') |
|
|
|
|
->will($this->returnValue($offlineUser)); |
|
|
|
|
|
|
|
|
|
$helper = new \OCA\User_LDAP\Helper(); |
|
|
|
|
|
|
|
|
|
$access = $this->getMock('\OCA\User_LDAP\Access', |
|
|
|
|
$accMethods, |
|
|
|
|
array($connector, $lw, $um, $helper)); |
|
|
|
|
$helper = new Helper(); |
|
|
|
|
|
|
|
|
|
$access = $this->getMockBuilder(Access::class) |
|
|
|
|
->setMethodsExcept(['getConnection']) |
|
|
|
|
->setConstructorArgs([$connector, $lw, $um, $helper]) |
|
|
|
|
->getMock(); |
|
|
|
|
|
|
|
|
|
$um->setLdapAccess($access); |
|
|
|
|
|
|
|
|
|
@ -135,7 +134,7 @@ class User_LDAPTest extends \Test\TestCase { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Prepares the Access mock for checkPassword tests |
|
|
|
|
* @param \OCA\User_LDAP\Access $access mock |
|
|
|
|
* @param Access|\PHPUnit_Framework_MockObject_MockObject $access mock |
|
|
|
|
* @param bool $noDisplayName |
|
|
|
|
* @return void |
|
|
|
|
*/ |
|
|
|
|
@ -304,7 +303,7 @@ class User_LDAPTest extends \Test\TestCase { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Prepares the Access mock for getUsers tests |
|
|
|
|
* @param \OCA\User_LDAP\Access $access mock |
|
|
|
|
* @param Access $access mock |
|
|
|
|
* @return void |
|
|
|
|
*/ |
|
|
|
|
private function prepareAccessForGetUsers(&$access) { |
|
|
|
|
@ -848,4 +847,116 @@ class User_LDAPTest extends \Test\TestCase { |
|
|
|
|
$result = $backend->countUsers(); |
|
|
|
|
$this->assertFalse($result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testLoginName2UserNameSuccess() { |
|
|
|
|
$loginName = 'Alice'; |
|
|
|
|
$username = 'alice'; |
|
|
|
|
$dn = 'uid=alice,dc=what,dc=ever'; |
|
|
|
|
|
|
|
|
|
$access = $this->getAccessMock(); |
|
|
|
|
$access->expects($this->once()) |
|
|
|
|
->method('fetchUsersByLoginName') |
|
|
|
|
->with($this->equalTo($loginName)) |
|
|
|
|
->willReturn([['dn' => [$dn]]]); |
|
|
|
|
$access->expects($this->once()) |
|
|
|
|
->method('stringResemblesDN') |
|
|
|
|
->with($this->equalTo($dn)) |
|
|
|
|
->willReturn(true); |
|
|
|
|
$access->expects($this->once()) |
|
|
|
|
->method('dn2username') |
|
|
|
|
->with($this->equalTo($dn)) |
|
|
|
|
->willReturn($username); |
|
|
|
|
|
|
|
|
|
$access->connection->expects($this->exactly(2)) |
|
|
|
|
->method('getFromCache') |
|
|
|
|
->with($this->equalTo('loginName2UserName-'.$loginName)) |
|
|
|
|
->willReturnOnConsecutiveCalls(null, $username); |
|
|
|
|
$access->connection->expects($this->once()) |
|
|
|
|
->method('writeToCache') |
|
|
|
|
->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo($username)); |
|
|
|
|
|
|
|
|
|
$backend = new UserLDAP($access, $this->createMock(IConfig::class)); |
|
|
|
|
$name = $backend->loginName2UserName($loginName); |
|
|
|
|
$this->assertSame($username, $name); |
|
|
|
|
|
|
|
|
|
// and once again to verify that caching works |
|
|
|
|
$backend->loginName2UserName($loginName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testLoginName2UserNameNoUsersOnLDAP() { |
|
|
|
|
$loginName = 'Loki'; |
|
|
|
|
|
|
|
|
|
$access = $this->getAccessMock(); |
|
|
|
|
$access->expects($this->once()) |
|
|
|
|
->method('fetchUsersByLoginName') |
|
|
|
|
->with($this->equalTo($loginName)) |
|
|
|
|
->willReturn([]); |
|
|
|
|
$access->expects($this->never()) |
|
|
|
|
->method('stringResemblesDN'); |
|
|
|
|
$access->expects($this->never()) |
|
|
|
|
->method('dn2username'); |
|
|
|
|
|
|
|
|
|
$access->connection->expects($this->exactly(2)) |
|
|
|
|
->method('getFromCache') |
|
|
|
|
->with($this->equalTo('loginName2UserName-'.$loginName)) |
|
|
|
|
->willReturnOnConsecutiveCalls(null, false); |
|
|
|
|
$access->connection->expects($this->once()) |
|
|
|
|
->method('writeToCache') |
|
|
|
|
->with($this->equalTo('loginName2UserName-'.$loginName), false); |
|
|
|
|
|
|
|
|
|
$backend = new UserLDAP($access, $this->createMock(IConfig::class)); |
|
|
|
|
$name = $backend->loginName2UserName($loginName); |
|
|
|
|
$this->assertSame(false, $name); |
|
|
|
|
|
|
|
|
|
// and once again to verify that caching works |
|
|
|
|
$backend->loginName2UserName($loginName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testLoginName2UserNameOfflineUser() { |
|
|
|
|
$loginName = 'Alice'; |
|
|
|
|
$username = 'alice'; |
|
|
|
|
$dn = 'uid=alice,dc=what,dc=ever'; |
|
|
|
|
|
|
|
|
|
$offlineUser = $this->getMockBuilder(OfflineUser::class) |
|
|
|
|
->disableOriginalConstructor() |
|
|
|
|
->getMock(); |
|
|
|
|
|
|
|
|
|
$access = $this->getAccessMock(); |
|
|
|
|
$access->expects($this->once()) |
|
|
|
|
->method('fetchUsersByLoginName') |
|
|
|
|
->with($this->equalTo($loginName)) |
|
|
|
|
->willReturn([['dn' => [$dn]]]); |
|
|
|
|
$access->expects($this->once()) |
|
|
|
|
->method('stringResemblesDN') |
|
|
|
|
->with($this->equalTo($dn)) |
|
|
|
|
->willReturn(true); |
|
|
|
|
$access->expects($this->once()) |
|
|
|
|
->method('dn2username') |
|
|
|
|
->willReturn(false); // this is fake, but allows us to force-enter the OfflineUser path |
|
|
|
|
|
|
|
|
|
$access->connection->expects($this->exactly(2)) |
|
|
|
|
->method('getFromCache') |
|
|
|
|
->with($this->equalTo('loginName2UserName-'.$loginName)) |
|
|
|
|
->willReturnOnConsecutiveCalls(null, false); |
|
|
|
|
$access->connection->expects($this->once()) |
|
|
|
|
->method('writeToCache') |
|
|
|
|
->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo(false)); |
|
|
|
|
|
|
|
|
|
$access->userManager->expects($this->once()) |
|
|
|
|
->method('getDeletedUser') |
|
|
|
|
->will($this->returnValue($offlineUser)); |
|
|
|
|
|
|
|
|
|
//$config = $this->createMock(IConfig::class); |
|
|
|
|
$this->configMock->expects($this->once()) |
|
|
|
|
->method('getUserValue') |
|
|
|
|
->willReturn(1); |
|
|
|
|
|
|
|
|
|
$backend = new UserLDAP($access, $this->createMock(IConfig::class)); |
|
|
|
|
$name = $backend->loginName2UserName($loginName); |
|
|
|
|
$this->assertSame(false, $name); |
|
|
|
|
|
|
|
|
|
// and once again to verify that caching works |
|
|
|
|
$backend->loginName2UserName($loginName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|