diff --git a/backend/tests/Unit/Auth/UseCases/AuthenticateUserTest.php b/backend/tests/Unit/Auth/UseCases/AuthenticateUserTest.php index 6d00862..1fc1d17 100644 --- a/backend/tests/Unit/Auth/UseCases/AuthenticateUserTest.php +++ b/backend/tests/Unit/Auth/UseCases/AuthenticateUserTest.php @@ -33,13 +33,16 @@ class AuthenticateUserTest extends TestCase private function seedUser( string $email, + string $displayName, string $password, bool $isAdmin, ): User { return $this->userRepo->create(new CreateUserDto( email: new EmailAddress($email), + displayName: $displayName, passwordHash: $this->hasher->hash($password), isAdmin: $isAdmin, + emailConfirmedAt: null, )); } @@ -101,6 +104,7 @@ class AuthenticateUserTest extends TestCase { $this->seedUser( email: 'user@example.com', + displayName: 'user', password: 'correctpassword', isAdmin: false, ); @@ -116,6 +120,7 @@ class AuthenticateUserTest extends TestCase { $seeded = $this->seedUser( email: 'user@example.com', + displayName: 'user', password: 'correctpassword', isAdmin: false, ); @@ -140,6 +145,7 @@ class AuthenticateUserTest extends TestCase { $this->seedUser( email: 'admin@example.com', + displayName: 'admin', password: 'adminpassword', isAdmin: true, ); diff --git a/backend/tests/Unit/User/UseCases/SignupUserTest.php b/backend/tests/Unit/User/UseCases/SignupUserTest.php index 1ae841e..9babb7e 100644 --- a/backend/tests/Unit/User/UseCases/SignupUserTest.php +++ b/backend/tests/Unit/User/UseCases/SignupUserTest.php @@ -36,6 +36,7 @@ class SignupUserTest extends TestCase $this->expectException(BadRequestException::class); $this->useCase->execute(new SignupUserRequest( email: null, + displayName: 'alice', password: 'longenoughpassword', )); } @@ -45,6 +46,7 @@ class SignupUserTest extends TestCase $this->expectException(BadRequestException::class); $this->useCase->execute(new SignupUserRequest( email: '', + displayName: 'alice', password: 'longenoughpassword', )); } @@ -54,6 +56,37 @@ class SignupUserTest extends TestCase $this->expectException(BadRequestException::class); $this->useCase->execute(new SignupUserRequest( email: 'not-an-email', + displayName: 'alice', + password: 'longenoughpassword', + )); + } + + public function test_null_display_name_throws_bad_request(): void + { + $this->expectException(BadRequestException::class); + $this->useCase->execute(new SignupUserRequest( + email: 'user@example.com', + displayName: null, + password: 'longenoughpassword', + )); + } + + public function test_short_display_name_throws_bad_request(): void + { + $this->expectException(BadRequestException::class); + $this->useCase->execute(new SignupUserRequest( + email: 'user@example.com', + displayName: 'ab', + password: 'longenoughpassword', + )); + } + + public function test_display_name_with_invalid_chars_throws(): void + { + $this->expectException(BadRequestException::class); + $this->useCase->execute(new SignupUserRequest( + email: 'user@example.com', + displayName: 'Has Spaces', password: 'longenoughpassword', )); } @@ -63,6 +96,7 @@ class SignupUserTest extends TestCase $this->expectException(BadRequestException::class); $this->useCase->execute(new SignupUserRequest( email: 'user@example.com', + displayName: 'alice', password: null, )); } @@ -72,6 +106,7 @@ class SignupUserTest extends TestCase $this->expectException(BadRequestException::class); $this->useCase->execute(new SignupUserRequest( email: 'user@example.com', + displayName: 'alice', password: 'short', )); } @@ -80,13 +115,34 @@ class SignupUserTest extends TestCase { $this->userRepo->create(new CreateUserDto( email: new EmailAddress('user@example.com'), + displayName: 'first', passwordHash: $this->hasher->hash('original-password'), isAdmin: false, + emailConfirmedAt: null, )); $this->expectException(DomainException::class); $this->useCase->execute(new SignupUserRequest( email: 'user@example.com', + displayName: 'second', + password: 'second-attempt-password', + )); + } + + public function test_duplicate_display_name_throws_domain_exception(): void + { + $this->userRepo->create(new CreateUserDto( + email: new EmailAddress('first@example.com'), + displayName: 'taken', + passwordHash: $this->hasher->hash('original-password'), + isAdmin: false, + emailConfirmedAt: null, + )); + + $this->expectException(DomainException::class); + $this->useCase->execute(new SignupUserRequest( + email: 'second@example.com', + displayName: 'taken', password: 'second-attempt-password', )); } @@ -95,22 +151,26 @@ class SignupUserTest extends TestCase { $created = $this->useCase->execute(new SignupUserRequest( email: 'new@example.com', + displayName: 'newuser', password: 'longenoughpassword', )); $this->assertInstanceOf(User::class, $created); $this->assertSame('new@example.com', $created->getEmail()->value()); + $this->assertSame('newuser', $created->getDisplayName()); $this->assertSame( $this->hasher->hash('longenoughpassword'), $created->getPasswordHash(), ); $this->assertFalse($created->isAdmin()); + $this->assertFalse($created->isEmailConfirmed()); } public function test_created_user_is_findable_by_email(): void { $created = $this->useCase->execute(new SignupUserRequest( email: 'lookup@example.com', + displayName: 'lookup', password: 'longenoughpassword', )); @@ -121,10 +181,24 @@ class SignupUserTest extends TestCase $this->assertSame($created->getId(), $found->getId()); } + public function test_created_user_is_findable_by_display_name(): void + { + $created = $this->useCase->execute(new SignupUserRequest( + email: 'lookup@example.com', + displayName: 'lookupbyname', + password: 'longenoughpassword', + )); + + $found = $this->userRepo->findByDisplayName('lookupbyname'); + $this->assertNotNull($found); + $this->assertSame($created->getId(), $found->getId()); + } + public function test_signup_normalizes_email_domain(): void { $created = $this->useCase->execute(new SignupUserRequest( email: 'Mixed@CASE.com', + displayName: 'mixed', password: 'longenoughpassword', ));