47 lines
1.3 KiB
PHP
47 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Auth;
|
|
|
|
use App\Auth\PasswordHasher;
|
|
use App\Auth\SessionRepository;
|
|
use App\Http\Middleware\AuthMiddleware;
|
|
use App\Shared\ValueObject\EmailAddress;
|
|
use App\User\CreateUserDto;
|
|
use App\User\UserRepository;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class LoginEndpointTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_login_returns_user_and_sets_session_cookie(): void
|
|
{
|
|
$email = 'user@example.com';
|
|
$password = 'correct-password';
|
|
$passwordHash = app(PasswordHasher::class)->hash($password);
|
|
app(UserRepository::class)->create(new CreateUserDto(
|
|
email: new EmailAddress($email),
|
|
passwordHash: $passwordHash,
|
|
));
|
|
|
|
$response = $this->postJson('/api/login', [
|
|
'email' => $email,
|
|
'password' => $password,
|
|
]);
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonPath('user.email', $email);
|
|
|
|
$cookie = $response->getCookie(
|
|
AuthMiddleware::COOKIE_NAME,
|
|
false,
|
|
);
|
|
$this->assertNotNull($cookie);
|
|
$this->assertNotNull(
|
|
app(SessionRepository::class)->findByToken(
|
|
$cookie->getValue(),
|
|
),
|
|
);
|
|
}
|
|
}
|