test AuthenticateUser use case
9 cases: null/empty/malformed email -> BadRequest; null/empty password -> BadRequest; unknown email -> Unauthorized; wrong password -> Unauthorized; valid creds return the User entity; isAdmin flag survives the auth round-trip. fails red - the AuthenticateUser class does not exist yet.
This commit is contained in:
parent
a108b29d19
commit
2731e610e5
1 changed files with 156 additions and 0 deletions
156
backend/tests/Unit/Auth/UseCases/AuthenticateUserTest.php
Normal file
156
backend/tests/Unit/Auth/UseCases/AuthenticateUserTest.php
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Auth\UseCases;
|
||||
|
||||
use App\Auth\UseCases\AuthenticateUser\AuthenticateUser;
|
||||
use App\Auth\UseCases\AuthenticateUser\AuthenticateUserRequest;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\UnauthorizedException;
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use App\User\CreateUserDto;
|
||||
use App\User\User;
|
||||
use Tests\Fakes\FakePasswordHasher;
|
||||
use Tests\Fakes\FakeUserRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AuthenticateUserTest extends TestCase
|
||||
{
|
||||
private FakeUserRepository $userRepo;
|
||||
|
||||
private FakePasswordHasher $hasher;
|
||||
|
||||
private AuthenticateUser $useCase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->userRepo = new FakeUserRepository;
|
||||
$this->hasher = new FakePasswordHasher;
|
||||
$this->useCase = new AuthenticateUser(
|
||||
$this->userRepo,
|
||||
$this->hasher,
|
||||
);
|
||||
}
|
||||
|
||||
private function seedUser(
|
||||
string $email,
|
||||
string $password,
|
||||
bool $isAdmin,
|
||||
): User {
|
||||
return $this->userRepo->create(new CreateUserDto(
|
||||
email: new EmailAddress($email),
|
||||
passwordHash: $this->hasher->hash($password),
|
||||
isAdmin: $isAdmin,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_null_email_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: null,
|
||||
password: 'correctpassword',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_empty_email_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: '',
|
||||
password: 'correctpassword',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_null_password_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: null,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_empty_password_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: '',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_malformed_email_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'not-an-email',
|
||||
password: 'correctpassword',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_unknown_email_throws_unauthorized(): void
|
||||
{
|
||||
$this->expectException(UnauthorizedException::class);
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'nobody@example.com',
|
||||
password: 'correctpassword',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_wrong_password_throws_unauthorized(): void
|
||||
{
|
||||
$this->seedUser(
|
||||
email: 'user@example.com',
|
||||
password: 'correctpassword',
|
||||
isAdmin: false,
|
||||
);
|
||||
|
||||
$this->expectException(UnauthorizedException::class);
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: 'wrongpassword',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_valid_credentials_return_user(): void
|
||||
{
|
||||
$seeded = $this->seedUser(
|
||||
email: 'user@example.com',
|
||||
password: 'correctpassword',
|
||||
isAdmin: false,
|
||||
);
|
||||
|
||||
$authenticated = $this->useCase->execute(
|
||||
new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: 'correctpassword',
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(User::class, $authenticated);
|
||||
$this->assertSame($seeded->getId(), $authenticated->getId());
|
||||
$this->assertSame(
|
||||
'user@example.com',
|
||||
$authenticated->getEmail()->value(),
|
||||
);
|
||||
$this->assertFalse($authenticated->isAdmin());
|
||||
}
|
||||
|
||||
public function test_admin_flag_is_preserved_on_authentication(): void
|
||||
{
|
||||
$this->seedUser(
|
||||
email: 'admin@example.com',
|
||||
password: 'adminpassword',
|
||||
isAdmin: true,
|
||||
);
|
||||
|
||||
$authenticated = $this->useCase->execute(
|
||||
new AuthenticateUserRequest(
|
||||
email: 'admin@example.com',
|
||||
password: 'adminpassword',
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertTrue($authenticated->isAdmin());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue