. */ namespace App\Tests\Security; use App\Entity\UserSystem\User; use App\Security\SamlUserFactory; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class SamlUserFactoryTest extends WebTestCase { /** @var SamlUserFactory */ protected $service; protected function setUp(): void { self::bootKernel(); $this->service = self::getContainer()->get(SamlUserFactory::class); } public function testCreateUser(): void { $user = $this->service->createUser('sso_user', [ 'email' => ['j.doe@invalid.invalid'], 'urn:oid:2.5.4.42' => ['John'], 'urn:oid:2.5.4.4' => ['Doe'], 'department' => ['IT'] ]); $this->assertInstanceOf(User::class, $user); $this->assertSame('sso_user', $user->getUserIdentifier()); //User must not change his password $this->assertFalse($user->isNeedPwChange()); //And must not be disabled $this->assertFalse($user->isDisabled()); //Password should not be set $this->assertSame('!!SAML!!', $user->getPassword()); //Info should be set $this->assertSame('John', $user->getFirstName()); $this->assertSame('Doe', $user->getLastName()); $this->assertSame('IT', $user->getDepartment()); $this->assertSame('j.doe@invalid.invalid', $user->getEmail()); } public function testMapSAMLRolesToLocalGroupID(): void { $mapping = [ 'admin' => 2, //This comes first, as this should have higher priority 'employee' => 1, 'manager' => 3, 'administrator' => 2, '*' => 4, ]; //Test if mapping works $this->assertSame(1, $this->service->mapSAMLRolesToLocalGroupID(['employee'], $mapping)); //Only the first valid mapping should be used $this->assertSame(2, $this->service->mapSAMLRolesToLocalGroupID(['employee', 'admin'], $mapping)); $this->assertSame(2, $this->service->mapSAMLRolesToLocalGroupID(['does_not_matter', 'admin', 'employee'], $mapping)); $this->assertSame(1, $this->service->mapSAMLRolesToLocalGroupID(['employee', 'does_not_matter', 'manager'], $mapping)); $this->assertSame(3, $this->service->mapSAMLRolesToLocalGroupID(['administrator', 'does_not_matter', 'manager'], $mapping)); //Test if mapping is case-sensitive $this->assertSame(4, $this->service->mapSAMLRolesToLocalGroupID(['ADMIN'], $mapping)); //Test that wildcard mapping works $this->assertSame(4, $this->service->mapSAMLRolesToLocalGroupID(['entry1', 'entry2'], $mapping)); $this->assertSame(4, $this->service->mapSAMLRolesToLocalGroupID([], $mapping)); } }