Skip to content

Commit

Permalink
DataFixtures: Add one user with mailcrypt enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
y3n4 committed Nov 8, 2024
1 parent d9541dc commit 938f1d7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/DataFixtures/AbstractUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
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
{
private const PASSWORD = 'password';

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);
Expand Down
26 changes: 17 additions & 9 deletions src/DataFixtures/LoadUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@

class LoadUserData extends AbstractUserData implements DependentFixtureInterface, FixtureGroupInterface
{
private const PASSWORD = 'password';

private array $users = [
['email' => '[email protected]', 'roles' => [Roles::ADMIN], 'totp' => false],
['email' => '[email protected]', 'roles' => [Roles::USER], 'totp' => false],
['email' => '[email protected]', 'roles' => [Roles::USER], 'totp' => false],
['email' => '[email protected]', 'roles' => [Roles::USER], 'totp' => true],
['email' => '[email protected]', 'roles' => [Roles::SPAM], 'totp' => false],
['email' => '[email protected]', 'roles' => [Roles::MULTIPLIER], 'totp' => false],
['email' => '[email protected]', 'roles' => [Roles::SUSPICIOUS], 'totp' => false],
['email' => '[email protected]', 'roles' => [Roles::DOMAIN_ADMIN], 'totp' => false],
['email' => '[email protected]', 'roles' => [Roles::ADMIN], 'totp' => false, 'mailcrypt' => false],
['email' => '[email protected]', 'roles' => [Roles::USER], 'totp' => false, 'mailcrypt' => false],
['email' => '[email protected]', 'roles' => [Roles::USER], 'totp' => false, 'mailcrypt' => false],
['email' => '[email protected]', 'roles' => [Roles::USER], 'totp' => false, 'mailcrypt' => true],
['email' => '[email protected]', 'roles' => [Roles::USER], 'totp' => true, 'mailcrypt' => false],
['email' => '[email protected]', 'roles' => [Roles::SPAM], 'totp' => false, 'mailcrypt' => false],
['email' => '[email protected]', 'roles' => [Roles::MULTIPLIER], 'totp' => false, 'mailcrypt' => false],
['email' => '[email protected]', 'roles' => [Roles::SUSPICIOUS], 'totp' => false, 'mailcrypt' => false],
['email' => '[email protected]', 'roles' => [Roles::DOMAIN_ADMIN], 'totp' => false, 'mailcrypt' => false],
];

/**
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Controller/KeycloakControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 938f1d7

Please sign in to comment.