fix LDAP install for systems without ldap extension (#846)
This commit is contained in:
@@ -24,44 +24,11 @@ use Symfony\Component\Security\Core\User\UserChecker;
|
||||
*/
|
||||
class LdapAuthenticationProviderTest extends TestCase
|
||||
{
|
||||
public function testDeactivatedSupports()
|
||||
{
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
|
||||
$config = new LdapConfiguration(['active' => false]);
|
||||
$userProvider = new LdapUserProvider($manager, $config);
|
||||
$providerKey = 'secured_area';
|
||||
$userChecker = new UserChecker();
|
||||
|
||||
$token = new UsernamePasswordToken('foo', 'bar', $providerKey);
|
||||
|
||||
$sut = new LdapAuthenticationProvider($userChecker, $providerKey, $userProvider, $manager, $config, false);
|
||||
$result = $sut->supports($token);
|
||||
self::assertFalse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
|
||||
* @expectedExceptionMessage The token is not supported by this authentication provider.
|
||||
*/
|
||||
public function testDeactivatedAuthenticate()
|
||||
{
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
|
||||
$config = new LdapConfiguration(['active' => false]);
|
||||
$userProvider = new LdapUserProvider($manager, $config);
|
||||
$providerKey = 'secured_area';
|
||||
$userChecker = new UserChecker();
|
||||
|
||||
$token = new UsernamePasswordToken('foo', 'bar', $providerKey);
|
||||
|
||||
$sut = new LdapAuthenticationProvider($userChecker, $providerKey, $userProvider, $manager, $config, false);
|
||||
$sut->authenticate($token);
|
||||
}
|
||||
|
||||
public function testSupports()
|
||||
{
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
|
||||
$config = new LdapConfiguration(['active' => true]);
|
||||
$userProvider = new LdapUserProvider($manager, $config);
|
||||
$config = new LdapConfiguration([]);
|
||||
$userProvider = new LdapUserProvider($manager);
|
||||
$providerKey = 'secured_area';
|
||||
$userChecker = new UserChecker();
|
||||
|
||||
@@ -76,11 +43,11 @@ class LdapAuthenticationProviderTest extends TestCase
|
||||
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
|
||||
* @expectedExceptionMessage The password in the token is empty. Check `erase_credentials` in your `security.yaml`
|
||||
*/
|
||||
public function testAuthenticateWithTokenUserButEnptyPasswordThrowsException()
|
||||
public function testAuthenticateWithTokenUserButEmptyPasswordThrowsException()
|
||||
{
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
|
||||
$config = new LdapConfiguration(['active' => true]);
|
||||
$userProvider = new LdapUserProvider($manager, $config);
|
||||
$config = new LdapConfiguration([]);
|
||||
$userProvider = new LdapUserProvider($manager);
|
||||
$providerKey = 'secured_area';
|
||||
$userChecker = new UserChecker();
|
||||
|
||||
@@ -99,7 +66,7 @@ class LdapAuthenticationProviderTest extends TestCase
|
||||
{
|
||||
$user = (new User())->setUsername('foo')->setEnabled(true);
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
|
||||
$config = new LdapConfiguration(['active' => true]);
|
||||
$config = new LdapConfiguration([]);
|
||||
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
|
||||
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
|
||||
$providerKey = 'secured_area';
|
||||
@@ -120,7 +87,7 @@ class LdapAuthenticationProviderTest extends TestCase
|
||||
$user = (new User())->setUsername('foo')->setEnabled(true);
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['bind'])->getMock();
|
||||
$manager->expects($this->once())->method('bind')->willReturn(false);
|
||||
$config = new LdapConfiguration(['active' => true]);
|
||||
$config = new LdapConfiguration([]);
|
||||
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
|
||||
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
|
||||
$providerKey = 'secured_area';
|
||||
@@ -141,7 +108,7 @@ class LdapAuthenticationProviderTest extends TestCase
|
||||
$user = (new User())->setUsername('foo')->setEnabled(true);
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['bind'])->getMock();
|
||||
$manager->expects($this->once())->method('bind')->willReturn(false);
|
||||
$config = new LdapConfiguration(['active' => true]);
|
||||
$config = new LdapConfiguration([]);
|
||||
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
|
||||
$userProvider->expects($this->never())->method('loadUserByUsername');
|
||||
$providerKey = 'secured_area';
|
||||
@@ -162,7 +129,7 @@ class LdapAuthenticationProviderTest extends TestCase
|
||||
$manager->expects($this->once())->method('updateUser')->willReturnCallback(function ($updateUser) use ($user) {
|
||||
self::assertSame($updateUser, $user);
|
||||
});
|
||||
$config = new LdapConfiguration(['active' => true]);
|
||||
$config = new LdapConfiguration([]);
|
||||
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
|
||||
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
|
||||
$providerKey = 'secured_area';
|
||||
@@ -184,7 +151,7 @@ class LdapAuthenticationProviderTest extends TestCase
|
||||
$manager->expects($this->once())->method('updateUser')->willReturnCallback(function ($updateUser) use ($user) {
|
||||
self::assertSame($updateUser, $user);
|
||||
});
|
||||
$config = new LdapConfiguration(['active' => true]);
|
||||
$config = new LdapConfiguration([]);
|
||||
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
|
||||
$userProvider->expects($this->never())->method('loadUserByUsername');
|
||||
$providerKey = 'secured_area';
|
||||
@@ -204,7 +171,7 @@ class LdapAuthenticationProviderTest extends TestCase
|
||||
public function testAuthenticateThrowsExceptionOnLdapNotFound()
|
||||
{
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
|
||||
$config = new LdapConfiguration(['active' => true]);
|
||||
$config = new LdapConfiguration([]);
|
||||
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
|
||||
$userProvider->expects($this->once())->method('loadUserByUsername')->willThrowException(new UsernameNotFoundException('blub foo bar'));
|
||||
$providerKey = 'secured_area';
|
||||
@@ -224,7 +191,7 @@ class LdapAuthenticationProviderTest extends TestCase
|
||||
public function testAuthenticateThrowsExceptionOnLdapDown()
|
||||
{
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
|
||||
$config = new LdapConfiguration(['active' => true]);
|
||||
$config = new LdapConfiguration([]);
|
||||
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
|
||||
$userProvider->expects($this->once())->method('loadUserByUsername')->willThrowException(new \Exception('server away', 1234));
|
||||
$providerKey = 'secured_area';
|
||||
|
||||
@@ -20,13 +20,21 @@ use Zend\Ldap\Ldap;
|
||||
*/
|
||||
class LdapDriverTest extends TestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
if (!class_exists('Zend\Ldap\Ldap')) {
|
||||
$this->markTestSkipped('LDAP is not installed');
|
||||
}
|
||||
}
|
||||
|
||||
public function testBindSuccess()
|
||||
{
|
||||
$zendLdap = $this->getMockBuilder(Ldap::class)->disableOriginalConstructor()->setMethods(['bind'])->getMock();
|
||||
$zendLdap->expects($this->once())->method('bind')->willReturnSelf();
|
||||
|
||||
$user = new User();
|
||||
$sut = new LdapDriver($zendLdap);
|
||||
$sut = new TestLdapDriver($zendLdap);
|
||||
$result = $sut->bind($user, 'test123');
|
||||
self::assertTrue($result);
|
||||
}
|
||||
@@ -37,7 +45,7 @@ class LdapDriverTest extends TestCase
|
||||
$zendLdap->expects($this->once())->method('bind')->willThrowException(new LdapException());
|
||||
|
||||
$user = new User();
|
||||
$sut = new LdapDriver($zendLdap);
|
||||
$sut = new TestLdapDriver($zendLdap);
|
||||
$result = $sut->bind($user, 'test123');
|
||||
self::assertFalse($result);
|
||||
}
|
||||
@@ -48,7 +56,7 @@ class LdapDriverTest extends TestCase
|
||||
$zendLdap->expects($this->once())->method('bind');
|
||||
$zendLdap->expects($this->once())->method('searchEntries')->willReturn([1, 2, 3]);
|
||||
|
||||
$sut = new LdapDriver($zendLdap);
|
||||
$sut = new TestLdapDriver($zendLdap);
|
||||
$result = $sut->search('', '', []);
|
||||
self::assertEquals(['count' => 3, 1, 2, 3], $result);
|
||||
}
|
||||
@@ -65,7 +73,15 @@ class LdapDriverTest extends TestCase
|
||||
new LdapException($zendLdap, '', LdapException::LDAP_SERVER_DOWN)
|
||||
);
|
||||
|
||||
$sut = new LdapDriver($zendLdap);
|
||||
$sut = new TestLdapDriver($zendLdap);
|
||||
$sut->search('', '', []);
|
||||
}
|
||||
}
|
||||
|
||||
class TestLdapDriver extends LdapDriver
|
||||
{
|
||||
public function __construct(Ldap $ldap)
|
||||
{
|
||||
$this->driver = $ldap;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ class LdapUserHydratorTest extends TestCase
|
||||
public function testEmptyHydrate()
|
||||
{
|
||||
$config = new LdapConfiguration([
|
||||
'active' => false,
|
||||
'connection' => [
|
||||
'host' => '1.1.1.1'
|
||||
],
|
||||
@@ -44,7 +43,6 @@ class LdapUserHydratorTest extends TestCase
|
||||
public function testHydrate()
|
||||
{
|
||||
$config = new LdapConfiguration([
|
||||
'active' => false,
|
||||
'connection' => [
|
||||
'host' => '1.1.1.1'
|
||||
],
|
||||
@@ -88,7 +86,6 @@ class LdapUserHydratorTest extends TestCase
|
||||
public function testHydrateUser()
|
||||
{
|
||||
$config = new LdapConfiguration([
|
||||
'active' => false,
|
||||
'connection' => [
|
||||
'host' => '1.1.1.1'
|
||||
],
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
namespace App\Tests\Ldap;
|
||||
|
||||
use App\Configuration\LdapConfiguration;
|
||||
use App\Entity\User;
|
||||
use App\Ldap\LdapManager;
|
||||
use App\Ldap\LdapUserProvider;
|
||||
@@ -20,41 +19,6 @@ use PHPUnit\Framework\TestCase;
|
||||
*/
|
||||
class LdapUserProviderTest extends TestCase
|
||||
{
|
||||
public function testDeactivatedSupportsClass()
|
||||
{
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
|
||||
$config = new LdapConfiguration(['active' => false]);
|
||||
|
||||
$sut = new LdapUserProvider($manager, $config);
|
||||
self::assertFalse($sut->supportsClass(User::class));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
|
||||
* @expectedExceptionMessage LDAP is deactivated, user "test" not searched
|
||||
*/
|
||||
public function testDeactivatedLoadUserByUsername()
|
||||
{
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
|
||||
$config = new LdapConfiguration(['active' => false]);
|
||||
|
||||
$sut = new LdapUserProvider($manager, $config);
|
||||
$sut->loadUserByUsername('test');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Security\Core\Exception\UnsupportedUserException
|
||||
* @expectedExceptionMessage Instances of "App\Entity\User" are not supported.
|
||||
*/
|
||||
public function testDeactivatedRefreshUser()
|
||||
{
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
|
||||
$config = new LdapConfiguration(['active' => false]);
|
||||
|
||||
$sut = new LdapUserProvider($manager, $config);
|
||||
$sut->refreshUser(new User());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
|
||||
* @expectedExceptionMessage User "test" not found
|
||||
@@ -63,9 +27,8 @@ class LdapUserProviderTest extends TestCase
|
||||
{
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['findUserByUsername'])->getMock();
|
||||
$manager->expects($this->once())->method('findUserByUsername')->willReturn(null);
|
||||
$config = new LdapConfiguration(['active' => true]);
|
||||
|
||||
$sut = new LdapUserProvider($manager, $config);
|
||||
$sut = new LdapUserProvider($manager);
|
||||
$sut->loadUserByUsername('test');
|
||||
}
|
||||
|
||||
@@ -76,9 +39,8 @@ class LdapUserProviderTest extends TestCase
|
||||
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['findUserByUsername'])->getMock();
|
||||
$manager->expects($this->once())->method('findUserByUsername')->willReturn($user);
|
||||
$config = new LdapConfiguration(['active' => true]);
|
||||
|
||||
$sut = new LdapUserProvider($manager, $config);
|
||||
$sut = new LdapUserProvider($manager);
|
||||
$actual = $sut->loadUserByUsername('test');
|
||||
self::assertInstanceOf(User::class, $actual);
|
||||
self::assertSame($user, $actual);
|
||||
@@ -91,9 +53,8 @@ class LdapUserProviderTest extends TestCase
|
||||
$user->setPreferenceValue('ldap.dn', 'sdfdsf');
|
||||
|
||||
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['updateUser'])->getMock();
|
||||
$config = new LdapConfiguration(['active' => true]);
|
||||
|
||||
$sut = new LdapUserProvider($manager, $config);
|
||||
$sut = new LdapUserProvider($manager);
|
||||
$actual = $sut->refreshUser($user);
|
||||
|
||||
self::assertInstanceOf(User::class, $actual);
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Ldap;
|
||||
|
||||
use App\Configuration\LdapConfiguration;
|
||||
use App\Ldap\ZendLdap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Ldap\ZendLdap
|
||||
*/
|
||||
class ZendLdapTest extends TestCase
|
||||
{
|
||||
public function testConstructDeactivated()
|
||||
{
|
||||
$config = new LdapConfiguration([
|
||||
'active' => false,
|
||||
'connection' => [
|
||||
'host' => '1.1.1.1'
|
||||
]
|
||||
]);
|
||||
|
||||
$sut = new ZendLdap($config);
|
||||
$options = $sut->getOptions();
|
||||
self::assertNull($options['host']);
|
||||
}
|
||||
|
||||
public function testConstructActivatedPassesOptions()
|
||||
{
|
||||
$config = new LdapConfiguration([
|
||||
'active' => true,
|
||||
'connection' => [
|
||||
'host' => '1.1.1.1'
|
||||
]
|
||||
]);
|
||||
|
||||
$sut = new ZendLdap($config);
|
||||
$options = $sut->getOptions();
|
||||
self::assertEquals('1.1.1.1', $options['host']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user