102 lines
2.9 KiB
PHP
102 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Set;
|
|
|
|
use App\Auth\CreateSessionDto;
|
|
use App\Auth\SessionRepository;
|
|
use App\Http\Middleware\AuthMiddleware;
|
|
use App\Set\CreateSetDto;
|
|
use App\Set\SetRepository;
|
|
use App\Shared\ValueObject\EmailAddress;
|
|
use App\User\CreateUserDto;
|
|
use App\User\User;
|
|
use App\User\UserRepository;
|
|
use DateTimeImmutable;
|
|
use DateTimeZone;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class ListSetsEndpointTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_it_returns_every_set_without_creator_details(): void
|
|
{
|
|
$authenticatedUser = $this->createUser('reader@example.com');
|
|
$otherUser = $this->createUser('creator@example.com');
|
|
$repository = app(SetRepository::class);
|
|
$course = $repository->create(new CreateSetDto(
|
|
name: 'Course',
|
|
creator: $authenticatedUser,
|
|
));
|
|
$bible = $repository->create(new CreateSetDto(
|
|
name: 'Bible',
|
|
creator: $otherUser,
|
|
));
|
|
$this->createSession($authenticatedUser);
|
|
|
|
$response = $this->withCredentials()
|
|
->withUnencryptedCookie(
|
|
AuthMiddleware::COOKIE_NAME,
|
|
'valid-token',
|
|
)->getJson('/api/sets');
|
|
|
|
$response->assertOk()->assertExactJson([
|
|
'sets' => [
|
|
[
|
|
'id' => $bible->getId(),
|
|
'name' => 'Bible',
|
|
],
|
|
[
|
|
'id' => $course->getId(),
|
|
'name' => 'Course',
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function test_it_returns_an_empty_catalog(): void
|
|
{
|
|
$authenticatedUser = $this->createUser('reader@example.com');
|
|
$this->createSession($authenticatedUser);
|
|
|
|
$response = $this->withCredentials()
|
|
->withUnencryptedCookie(
|
|
AuthMiddleware::COOKIE_NAME,
|
|
'valid-token',
|
|
)->getJson('/api/sets');
|
|
|
|
$response->assertOk()->assertExactJson(['sets' => []]);
|
|
}
|
|
|
|
public function test_it_rejects_an_unauthenticated_request(): void
|
|
{
|
|
$response = $this->getJson('/api/sets');
|
|
|
|
$response
|
|
->assertStatus(401)
|
|
->assertExactJson(['error' => 'unauthenticated']);
|
|
}
|
|
|
|
private function createUser(string $email): User
|
|
{
|
|
return app(UserRepository::class)->create(new CreateUserDto(
|
|
email: new EmailAddress($email),
|
|
passwordHash: 'hashed-password',
|
|
));
|
|
}
|
|
|
|
private function createSession(User $user): void
|
|
{
|
|
$createdAt = new DateTimeImmutable(
|
|
'2026-08-03T12:00:00',
|
|
new DateTimeZone('UTC'),
|
|
);
|
|
app(SessionRepository::class)->create(new CreateSessionDto(
|
|
token: 'valid-token',
|
|
user: $user,
|
|
createdAt: $createdAt,
|
|
expiresAt: $createdAt->modify('+10 years'),
|
|
));
|
|
}
|
|
}
|