From 8cbc84b051a47de71b009cc955ccb625bb92c222 Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Wed, 6 May 2026 22:34:29 +0300 Subject: [PATCH] test PromoteUserToAdmin use case --- .../User/UseCases/PromoteUserToAdminTest.php | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 backend/tests/Unit/User/UseCases/PromoteUserToAdminTest.php diff --git a/backend/tests/Unit/User/UseCases/PromoteUserToAdminTest.php b/backend/tests/Unit/User/UseCases/PromoteUserToAdminTest.php new file mode 100644 index 0000000..0ee5d46 --- /dev/null +++ b/backend/tests/Unit/User/UseCases/PromoteUserToAdminTest.php @@ -0,0 +1,94 @@ +userRepo = new FakeUserRepository; + $this->useCase = new PromoteUserToAdmin($this->userRepo); + } + + private function seedUser(bool $isAdmin): int + { + $user = $this->userRepo->create(new CreateUserDto( + email: new EmailAddress('user@example.com'), + displayName: 'user', + passwordHash: 'h', + isAdmin: $isAdmin, + emailConfirmedAt: null, + )); + + return $user->getId(); + } + + public function test_non_admin_requester_throws_forbidden(): void + { + $userId = $this->seedUser(false); + + $this->expectException(ForbiddenException::class); + $this->useCase->execute(new PromoteUserToAdminRequest( + targetUserId: $userId, + requesterIsAdmin: false, + )); + } + + public function test_zero_target_id_throws_bad_request(): void + { + $this->expectException(BadRequestException::class); + $this->useCase->execute(new PromoteUserToAdminRequest( + targetUserId: 0, + requesterIsAdmin: true, + )); + } + + public function test_unknown_user_throws_domain_exception(): void + { + $this->expectException(DomainException::class); + $this->useCase->execute(new PromoteUserToAdminRequest( + targetUserId: 999, + requesterIsAdmin: true, + )); + } + + public function test_admin_promotes_target(): void + { + $userId = $this->seedUser(false); + + $this->useCase->execute(new PromoteUserToAdminRequest( + targetUserId: $userId, + requesterIsAdmin: true, + )); + + $reloaded = $this->userRepo->find($userId); + $this->assertTrue($reloaded->isAdmin()); + } + + public function test_already_admin_is_idempotent(): void + { + $userId = $this->seedUser(true); + + $this->useCase->execute(new PromoteUserToAdminRequest( + targetUserId: $userId, + requesterIsAdmin: true, + )); + + $reloaded = $this->userRepo->find($userId); + $this->assertTrue($reloaded->isAdmin()); + } +}