test initial admin
This commit is contained in:
parent
bba8f272bf
commit
b55769516e
1 changed files with 78 additions and 0 deletions
78
backend/tests/Unit/User/UseCases/EnsureInitialAdminTest.php
Normal file
78
backend/tests/Unit/User/UseCases/EnsureInitialAdminTest.php
Normal 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(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue