From 938f1d7412acad3579eeb2e1243d7efd54c4ae7f Mon Sep 17 00:00:00 2001 From: yena Date: Fri, 18 Oct 2024 17:42:08 +0200 Subject: [PATCH] DataFixtures: Add one user with mailcrypt enabled --- src/DataFixtures/AbstractUserData.php | 3 ++- src/DataFixtures/LoadUserData.php | 26 ++++++++++++++------- tests/Controller/KeycloakControllerTest.php | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/DataFixtures/AbstractUserData.php b/src/DataFixtures/AbstractUserData.php index e0b19cc8..8ff6b358 100644 --- a/src/DataFixtures/AbstractUserData.php +++ b/src/DataFixtures/AbstractUserData.php @@ -7,6 +7,7 @@ use App\Helper\PasswordUpdater; use Doctrine\Bundle\FixturesBundle\Fixture; use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Totp\TotpAuthenticator; +use App\Handler\MailCryptKeyHandler; abstract class AbstractUserData extends Fixture { @@ -14,7 +15,7 @@ abstract class AbstractUserData extends Fixture protected string $passwordHash; - public function __construct(readonly PasswordUpdater $passwordUpdater, readonly TotpAuthenticator $totpAuthenticator) + public function __construct(readonly PasswordUpdater $passwordUpdater, readonly TotpAuthenticator $totpAuthenticator, readonly MailCryptKeyHandler $mailCryptKeyHandler) { $user = new User(); $passwordUpdater->updatePassword($user, self::PASSWORD); diff --git a/src/DataFixtures/LoadUserData.php b/src/DataFixtures/LoadUserData.php index 017ac105..64a001be 100644 --- a/src/DataFixtures/LoadUserData.php +++ b/src/DataFixtures/LoadUserData.php @@ -11,15 +11,18 @@ class LoadUserData extends AbstractUserData implements DependentFixtureInterface, FixtureGroupInterface { + private const PASSWORD = 'password'; + private array $users = [ - ['email' => 'admin@example.org', 'roles' => [Roles::ADMIN], 'totp' => false], - ['email' => 'user@example.org', 'roles' => [Roles::USER], 'totp' => false], - ['email' => 'user2@example.org', 'roles' => [Roles::USER], 'totp' => false], - ['email' => 'totp@example.org', 'roles' => [Roles::USER], 'totp' => true], - ['email' => 'spam@example.org', 'roles' => [Roles::SPAM], 'totp' => false], - ['email' => 'support@example.org', 'roles' => [Roles::MULTIPLIER], 'totp' => false], - ['email' => 'suspicious@example.org', 'roles' => [Roles::SUSPICIOUS], 'totp' => false], - ['email' => 'domain@example.com', 'roles' => [Roles::DOMAIN_ADMIN], 'totp' => false], + ['email' => 'admin@example.org', 'roles' => [Roles::ADMIN], 'totp' => false, 'mailcrypt' => false], + ['email' => 'user@example.org', 'roles' => [Roles::USER], 'totp' => false, 'mailcrypt' => false], + ['email' => 'user2@example.org', 'roles' => [Roles::USER], 'totp' => false, 'mailcrypt' => false], + ['email' => 'mailcrypt@example.org', 'roles' => [Roles::USER], 'totp' => false, 'mailcrypt' => true], + ['email' => 'totp@example.org', 'roles' => [Roles::USER], 'totp' => true, 'mailcrypt' => false], + ['email' => 'spam@example.org', 'roles' => [Roles::SPAM], 'totp' => false, 'mailcrypt' => false], + ['email' => 'support@example.org', 'roles' => [Roles::MULTIPLIER], 'totp' => false, 'mailcrypt' => false], + ['email' => 'suspicious@example.org', 'roles' => [Roles::SUSPICIOUS], 'totp' => false, 'mailcrypt' => false], + ['email' => 'domain@example.com', 'roles' => [Roles::DOMAIN_ADMIN], 'totp' => false, 'mailcrypt' => false], ]; /** @@ -36,12 +39,17 @@ public function load(ObjectManager $manager): void $domain = $manager->getRepository(Domain::class)->findOneBy(['name' => $splitted[1]]); $totpEnabled = $user['totp']; + $mailcryptEnabled = $user['mailcrypt']; + $user = $this->buildUser($domain, $email, $roles); if ($totpEnabled) { $user->setTotpSecret($this->totpAuthenticator->generateSecret()); $user->setTotpConfirmed(true); } - + if ($mailcryptEnabled) { + $this->mailCryptKeyHandler->create($user, self::PASSWORD); + $user->setMailCrypt(true); + } $manager->persist($user); } diff --git a/tests/Controller/KeycloakControllerTest.php b/tests/Controller/KeycloakControllerTest.php index 00bc41e9..63cb1db1 100644 --- a/tests/Controller/KeycloakControllerTest.php +++ b/tests/Controller/KeycloakControllerTest.php @@ -54,7 +54,7 @@ public function testGetUsersCount(): void self::assertResponseIsSuccessful(); $data = json_decode($client->getResponse()->getContent(), true, 512, JSON_THROW_ON_ERROR); - self::assertEquals(7, $data); + self::assertEquals(8, $data); } public function testGetOneUser(): void