test icon uploads

This commit is contained in:
Yisroel Baum 2026-06-02 10:17:21 +03:00
parent 77dc4173b6
commit 0f3bb6de6b
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
4 changed files with 356 additions and 1 deletions

View file

@ -14,6 +14,8 @@ use App\User\UserRepository;
use DateTimeImmutable;
use DateTimeZone;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class ElementsEndpointTest extends TestCase
@ -189,6 +191,84 @@ class ElementsEndpointTest extends TestCase
);
}
public function testIconImageUploadRequiresAuthentication(): void
{
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $setRepository->create(new CreateSetDto(
name: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
));
$element = $elementRepository->create(new CreateElementDto(
set: $set,
title: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: null,
richText: '<p>A structured path for growth</p>',
pdfPath: null,
youtubeUrl: null,
parentElement: null,
));
$response = $this->post(
"/api/elements/{$element->getId()}/icon-image",
['iconImage' => $this->iconImageUpload()],
);
$response->assertUnauthorized();
$response->assertExactJson([
'error' => 'unauthenticated',
]);
}
public function testAuthenticatedIconImageUploadReturnsElementPayload(): void
{
Storage::fake('public');
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $setRepository->create(new CreateSetDto(
name: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
));
$element = $elementRepository->create(new CreateElementDto(
set: $set,
title: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: null,
richText: '<p>A structured path for growth</p>',
pdfPath: null,
youtubeUrl: null,
parentElement: null,
));
$this->createSession('valid-token');
$response = $this->withCredentials()
->withUnencryptedCookie('auth_token', 'valid-token')
->post(
"/api/elements/{$element->getId()}/icon-image",
['iconImage' => $this->iconImageUpload()],
);
$response->assertOk();
$body = $response->json();
$this->assertSame($element->getId(), $body['element']['id']);
$this->assertStringContainsString(
'/storage/element-icons/',
$body['element']['iconImageUrl'],
);
$updatedElement = $elementRepository->find($element->getId());
$this->assertNotNull($updatedElement);
$updatedIconImageUrl = $updatedElement->getIconImageUrl();
$this->assertIsString($updatedIconImageUrl);
$this->assertStringStartsWith(
'element-icons/',
$updatedIconImageUrl,
);
Storage::disk('public')->assertExists($updatedIconImageUrl);
}
private function createSession(string $token): void
{
$userRepository = app(UserRepository::class);
@ -211,4 +291,19 @@ class ElementsEndpointTest extends TestCase
),
));
}
private function iconImageUpload(): UploadedFile
{
$fixturePath = __DIR__
. '/../../../frontend/rabbi_gerzi/public/assets/'
. 'baderech-haavodah-icon.png';
return new UploadedFile(
$fixturePath,
'icon.png',
'image/png',
null,
true,
);
}
}