Files
kimai/tests/Saml/SamlProviderTest.php
Kevin Papst 677f8d776c [3.0] Update Symfony to 7.4 and dependencies, removed deprecated code, removed API password (#5622)
* deactivate deprecated auto-mapping
* use native month-picker with fallback on old browser
* bump dependencies to new major versions
* replace deprecated ARRAY column type with JSON
* named arguments
* nullable arguments
* native lazy ghosts
* update routes to use php config
* disable PHP < 8.4
* use static RTL configuration
* new qr code package
* clear out env file
* recipe updates
* fix deprecations
* preset new env variable if not existing
* bump maria db version examples
* remove deprecated api-token support
* fix validator deprecations
* remove timesheet category column
* fix broken validator autoconfiguration
* stabilize tests
* fix migration syntax compatibility
* fix doctrine deprecation
* fix datetime format
* remove constructor dependency
* only include visible preferences in invoice templates
* fix test container dependency
* removed ProjectConstraint multi-constraint logic
* allow to disable plugins in certain environments
* refactor TimesheetConstraint to non-service
* activate new interface methods
* prevent update issues with .env
* improved customer fixture
* removed $user->isExportDecimal()
2026-01-04 15:04:25 +01:00

157 lines
5.5 KiB
PHP

<?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\Saml;
use App\Configuration\SamlConfiguration;
use App\Entity\User;
use App\Saml\SamlLoginAttributes;
use App\Saml\SamlProvider;
use App\Tests\Configuration\TestConfigLoader;
use App\Tests\Mocks\SystemConfigurationFactory;
use App\User\UserService;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
#[CoversClass(SamlProvider::class)]
class SamlProviderTest extends TestCase
{
protected function getSamlProvider(?array $mapping = null, ?User $user = null): SamlProvider
{
if (null === $mapping) {
$mapping = [
'mapping' => [
['saml' => '$Email', 'kimai' => 'email'],
['saml' => '$title', 'kimai' => 'title'],
],
'roles' => [
'attribute' => '',
'mapping' => []
]
];
}
$configuration = SystemConfigurationFactory::create(new TestConfigLoader([]), [
'saml' => $mapping
]);
$samlConfig = new SamlConfiguration($configuration);
// can be replaced, once loadUserByIdentifier is in the interface with SF6?
$userProvider = $this->getMockBuilder(UserProviderInterface::class)->disableOriginalConstructor();
$userProvider->onlyMethods(['refreshUser', 'supportsClass', 'loadUserByIdentifier']);
$userProvider = $userProvider->getMock();
$userService = $this->getMockBuilder(UserService::class)->disableOriginalConstructor()->getMock();
if ($user !== null) {
$userProvider->method('loadUserByIdentifier')->willReturn($user);
} else {
$userProvider->method('loadUserByIdentifier')->willReturn(new User());
}
$provider = new SamlProvider($userService, $userProvider, $samlConfig, $this->createMock(LoggerInterface::class));
return $provider;
}
public function testFindUserHydratesUser(): void
{
$user = new User();
$user->setAuth(User::AUTH_INTERNAL);
$user->setUserIdentifier('foo1@example.com');
$user->setTitle('jagfkjhsgf');
$token = new SamlLoginAttributes();
$token->setUserIdentifier($user->getUserIdentifier());
$token->setAttributes([
'Email' => ['foo@example.com'],
'title' => ['Tralalala'],
]);
$sut = $this->getSamlProvider(null, $user);
$tokenUser = $sut->findUser($token);
self::assertSame($user, $tokenUser);
self::assertTrue($tokenUser->isSamlUser());
self::assertEquals('foo1@example.com', $tokenUser->getUserIdentifier());
self::assertEquals('Tralalala', $tokenUser->getTitle());
self::assertEquals('foo@example.com', $tokenUser->getEmail());
}
public function testFindUserCreatesNewUser(): void
{
$token = new SamlLoginAttributes();
$token->setUserIdentifier('foo2@example.com');
$token->setAttributes([
'Email' => ['foo@example.com'],
'title' => ['Tralalala'],
]);
$sut = $this->getSamlProvider(null);
$tokenUser = $sut->findUser($token);
self::assertTrue($tokenUser->isSamlUser());
self::assertEquals('foo2@example.com', $tokenUser->getUserIdentifier());
self::assertEquals('Tralalala', $tokenUser->getTitle());
self::assertEquals('foo@example.com', $tokenUser->getEmail());
}
public function testAuthenticateThrowsAuthenticationException(): void
{
$this->expectException(AuthenticationException::class);
$this->expectExceptionMessage('Failed creating or hydrating user "foo1@example.com": Missing SAML attribute in response: Email');
$user = new User();
$user->setAuth(User::AUTH_SAML);
$user->setUserIdentifier('foo1@example.com');
$token = new SamlLoginAttributes();
$token->setUserIdentifier($user->getUserIdentifier());
$token->setAttributes([
'Chicken' => ['foo@example.com'],
]);
$sut = $this->getSamlProvider(null, $user);
$sut->findUser($token);
}
public function testAuthenticateNotThrowsOnOptionalAttribute(): void
{
$mapping = [
'mapping' => [
['saml' => '$Chicken', 'kimai' => 'alias'],
['saml' => '$$Email', 'kimai' => 'title'],
],
'roles' => [
'attribute' => '',
'mapping' => []
]
];
$user = new User();
$user->setAuth(User::AUTH_SAML);
$user->setUserIdentifier('foo1@example.com');
$user->setTitle('I will not be overwritten');
$token = new SamlLoginAttributes();
$token->setUserIdentifier($user->getUserIdentifier());
$token->setAttributes([
'Chicken' => ['foo@example.com'],
]);
$sut = $this->getSamlProvider($mapping, $user);
$tokenUser = $sut->findUser($token);
self::assertSame($user, $tokenUser);
self::assertTrue($tokenUser->isSamlUser());
self::assertEquals('foo@example.com', $tokenUser->getAlias());
self::assertEquals('I will not be overwritten', $tokenUser->getTitle());
}
}