From b55769516e047470809097f67652b7ba50f43bd9 Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Thu, 2 Jul 2026 01:14:47 +0300 Subject: [PATCH] test initial admin --- .../User/UseCases/EnsureInitialAdminTest.php | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 backend/tests/Unit/User/UseCases/EnsureInitialAdminTest.php diff --git a/backend/tests/Unit/User/UseCases/EnsureInitialAdminTest.php b/backend/tests/Unit/User/UseCases/EnsureInitialAdminTest.php new file mode 100644 index 0000000..f1d624c --- /dev/null +++ b/backend/tests/Unit/User/UseCases/EnsureInitialAdminTest.php @@ -0,0 +1,78 @@ +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(), + ); + } +}