test auth contracts
This commit is contained in:
parent
3da9c586c3
commit
b3266b38c8
15 changed files with 457 additions and 165 deletions
104
backend/tests/Unit/Auth/UseCases/AuthenticateUserTest.php
Normal file
104
backend/tests/Unit/Auth/UseCases/AuthenticateUserTest.php
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
<?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 PHPUnit\Framework\TestCase;
|
||||
use Tests\Fakes\FakePasswordHasher;
|
||||
use Tests\Fakes\FakeUserRepository;
|
||||
|
||||
class AuthenticateUserTest extends TestCase
|
||||
{
|
||||
private FakeUserRepository $userRepository;
|
||||
|
||||
private FakePasswordHasher $passwordHasher;
|
||||
|
||||
private AuthenticateUser $useCase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->userRepository = new FakeUserRepository;
|
||||
$this->passwordHasher = new FakePasswordHasher;
|
||||
$this->useCase = new AuthenticateUser(
|
||||
$this->userRepository,
|
||||
$this->passwordHasher,
|
||||
);
|
||||
}
|
||||
|
||||
public function test_null_email_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->expectExceptionMessage('email is required');
|
||||
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: null,
|
||||
password: 'correct-password',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_null_password_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->expectExceptionMessage('password is required');
|
||||
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: null,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_unknown_email_throws_unauthorized(): void
|
||||
{
|
||||
$this->expectException(UnauthorizedException::class);
|
||||
$this->expectExceptionMessage('invalid credentials');
|
||||
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'unknown@example.com',
|
||||
password: 'correct-password',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_wrong_password_throws_unauthorized(): void
|
||||
{
|
||||
$this->createUser('correct-password');
|
||||
$this->expectException(UnauthorizedException::class);
|
||||
$this->expectExceptionMessage('invalid credentials');
|
||||
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: 'wrong-password',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_valid_credentials_return_user(): void
|
||||
{
|
||||
$user = $this->createUser('correct-password');
|
||||
|
||||
$authenticatedUser = $this->useCase->execute(
|
||||
new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: 'correct-password',
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertSame($user->getId(), $authenticatedUser->getId());
|
||||
$this->assertSame(
|
||||
$user->getEmail()->value(),
|
||||
$authenticatedUser->getEmail()->value(),
|
||||
);
|
||||
}
|
||||
|
||||
private function createUser(string $password): User
|
||||
{
|
||||
return $this->userRepository->create(new CreateUserDto(
|
||||
email: new EmailAddress('user@example.com'),
|
||||
passwordHash: $this->passwordHasher->hash($password),
|
||||
));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue