test initial admin

This commit is contained in:
Yisroel Baum 2026-07-02 01:14:47 +03:00
parent bba8f272bf
commit b55769516e
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -0,0 +1,78 @@
<?php
namespace Tests\Unit\User\UseCases;
use App\Auth\PasswordHasher;
use App\Shared\ValueObject\EmailAddress;
use App\User\CreateUserDto;
use App\User\UseCases\EnsureInitialAdmin\EnsureInitialAdmin;
use App\User\UseCases\EnsureInitialAdmin\EnsureInitialAdminRequest;
use Tests\Fakes\FakeHasher;
use Tests\Fakes\FakeUserRepository;
use Tests\TestCase;
class EnsureInitialAdminTest extends TestCase
{
private FakeUserRepository $userRepository;
private PasswordHasher $passwordHasher;
private EnsureInitialAdmin $ensureInitialAdmin;
protected function setUp(): void
{
$this->userRepository = new FakeUserRepository();
$this->passwordHasher = new FakeHasher();
$this->ensureInitialAdmin = new EnsureInitialAdmin(
$this->userRepository,
$this->passwordHasher,
);
}
public function testCreatesInitialAdminWhenEmailIsMissing(): void
{
$this->ensureInitialAdmin->execute(
new EnsureInitialAdminRequest(
email: 'admin@example.com',
password: 'secret-password',
)
);
$createdUser = $this->userRepository->findByEmail(
new EmailAddress('admin@example.com')
);
$this->assertNotNull($createdUser);
$this->assertSame(
'hashed-secret-password',
$createdUser->getPasswordHash(),
);
}
public function testLeavesExistingAdminPasswordUnchanged(): void
{
$this->userRepository->create(
new CreateUserDto(
email: new EmailAddress('admin@example.com'),
passwordHash: 'existing-password-hash',
)
);
$this->ensureInitialAdmin->execute(
new EnsureInitialAdminRequest(
email: 'admin@example.com',
password: 'new-secret-password',
)
);
$existingUser = $this->userRepository->findByEmail(
new EmailAddress('admin@example.com')
);
$this->assertNotNull($existingUser);
$this->assertSame(
'existing-password-hash',
$existingUser->getPasswordHash(),
);
}
}