Files
services_core/tests/Feature/StoredFileApiTest.php
T
2026-07-27 16:24:09 +07:00

87 lines
2.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Enums\UserAccessLevel;
use App\Models\StoredFile;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class StoredFileApiTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_upload_an_image_and_set_it_as_profile_photo(): void
{
Storage::fake('minio');
$user = User::factory()->create([
'access_level' => UserAccessLevel::CUSTOMER,
'is_master' => false,
]);
$uploadResponse = $this->actingAs($user)
->post('/api/files', [
'file' => UploadedFile::fake()->create('avatar.png', 100, 'image/png'),
'category' => 'profile',
])
->assertCreated()
->assertJsonPath('success', true)
->assertJsonPath('data.category', 'profile');
$fileId = $uploadResponse->json('data.id');
$this->actingAs($user)
->putJson('/api/me/profile', [
'name' => $user->name,
'username' => $user->username,
'email' => $user->email,
'profile_photo_file_id' => $fileId,
'user_profile' => null,
])
->assertOk()
->assertJsonPath('data.profile_photo.id', $fileId);
$this->assertDatabaseHas('users', [
'id' => $user->id,
'profile_photo_file_id' => $fileId,
]);
$storedFile = StoredFile::findOrFail($fileId);
Storage::disk('minio')->assertExists($storedFile->object_key);
}
public function test_user_cannot_use_another_users_file_as_profile_photo(): void
{
$owner = User::factory()->create();
$otherUser = User::factory()->create();
$file = StoredFile::create([
'uuid' => fake()->uuid(),
'uploaded_by' => $owner->id,
'disk' => 'minio',
'bucket' => 'file',
'object_key' => 'profiles/test.png',
'original_name' => 'test.png',
'extension' => 'png',
'mime_type' => 'image/png',
'size' => 100,
'category' => 'profile',
'visibility' => 'private',
]);
$this->actingAs($otherUser)
->putJson('/api/me/profile', [
'name' => $otherUser->name,
'username' => $otherUser->username,
'email' => $otherUser->email,
'profile_photo_file_id' => $file->id,
'user_profile' => null,
])
->assertUnprocessable()
->assertJsonValidationErrors('profile_photo_file_id');
}
}