fix ldap issues due to new security components (#2689)

This commit is contained in:
Kevin Papst
2021-07-27 19:31:49 +02:00
committed by GitHub
parent 6d5244aa5d
commit 5b8510be56
6 changed files with 36 additions and 23 deletions

View File

@@ -14,8 +14,7 @@ use App\Configuration\SystemConfiguration;
use App\Entity\User;
use App\Ldap\LdapAuthenticationProvider;
use App\Ldap\LdapManager;
use App\Repository\UserRepository;
use App\Security\DoctrineUserProvider;
use App\Ldap\LdapUserProvider;
use App\Tests\Configuration\TestConfigLoader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
@@ -23,15 +22,25 @@ use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserChecker;
use Symfony\Component\Security\Core\User\UserProviderInterface;
/**
* @covers \App\Ldap\LdapAuthenticationProvider
*/
class LdapAuthenticationProviderTest extends TestCase
{
private function getUserProvider(): DoctrineUserProvider
public const USER_PROVIDER_CLASS = LdapUserProvider::class;
private function getUserProvider(?User $user = null): UserProviderInterface
{
return new DoctrineUserProvider($this->createMock(UserRepository::class));
if ($user === null) {
return new LdapUserProvider($this->createMock(LdapManager::class));
}
$userProvider = $this->getMockBuilder(self::USER_PROVIDER_CLASS)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
return $userProvider;
}
private function getConfiguration(bool $active = true): LdapConfiguration
@@ -98,8 +107,7 @@ class LdapAuthenticationProviderTest extends TestCase
$user = (new User())->setUsername('foo')->setEnabled(true);
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(DoctrineUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
$userProvider = $this->getUserProvider($user);
$providerKey = 'secured_area';
$userChecker = new UserChecker();
@@ -118,8 +126,7 @@ class LdapAuthenticationProviderTest extends TestCase
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->onlyMethods(['bind'])->getMock();
$manager->expects($this->once())->method('bind')->willReturn(false);
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(DoctrineUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
$userProvider = $this->getUserProvider($user);
$providerKey = 'secured_area';
$userChecker = new UserChecker();
@@ -138,7 +145,7 @@ class LdapAuthenticationProviderTest extends TestCase
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->onlyMethods(['bind'])->getMock();
$manager->expects($this->once())->method('bind')->willReturn(false);
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(DoctrineUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider = $this->getMockBuilder(self::USER_PROVIDER_CLASS)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->never())->method('loadUserByUsername');
$providerKey = 'secured_area';
$userChecker = new UserChecker();
@@ -159,8 +166,7 @@ class LdapAuthenticationProviderTest extends TestCase
self::assertSame($updateUser, $user);
});
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(DoctrineUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
$userProvider = $this->getUserProvider($user);
$providerKey = 'secured_area';
$userChecker = new UserChecker();
@@ -181,7 +187,7 @@ class LdapAuthenticationProviderTest extends TestCase
self::assertSame($updateUser, $user);
});
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(DoctrineUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider = $this->getMockBuilder(self::USER_PROVIDER_CLASS)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->never())->method('loadUserByUsername');
$providerKey = 'secured_area';
$userChecker = new UserChecker();
@@ -200,7 +206,7 @@ class LdapAuthenticationProviderTest extends TestCase
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(DoctrineUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider = $this->getMockBuilder(self::USER_PROVIDER_CLASS)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willThrowException(new UsernameNotFoundException('blub foo bar'));
$providerKey = 'secured_area';
$userChecker = new UserChecker();
@@ -219,7 +225,7 @@ class LdapAuthenticationProviderTest extends TestCase
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(DoctrineUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider = $this->getMockBuilder(self::USER_PROVIDER_CLASS)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willThrowException(new \Exception('server away', 1234));
$providerKey = 'secured_area';
$userChecker = new UserChecker();

View File

@@ -203,8 +203,16 @@ class DateTimeFactoryTest extends TestCase
{
$sut = $this->createDateTimeFactory(self::TEST_TIMEZONE);
$expected = $sut->createDateTime('2021-07-22 23:59:59 ');
$financial = $sut->createStartOfFinancialYear('2020-07-23 15:30:00');
$now = $sut->createDateTime();
$expected = $sut->createDateTime();
$expected->setDate((int) $expected->format('Y'), 7, 22);
$expected->setTime(23, 59, 59);
if ($now > $expected) {
$expected->modify('+1 year');
}
$financial = $sut->createStartOfFinancialYear('2018-07-23 15:30:00');
$end = $sut->createEndOfFinancialYear($financial);
self::assertEquals($expected, $end);