post('/api/admin/media', ['file' => UploadedFile::fake()->image('cover.jpg')]) ->assertUnauthorized(); } public function test_non_admins_cannot_upload(): void { Sanctum::actingAs(User::factory()->create()); $this->post('/api/admin/media', ['file' => UploadedFile::fake()->image('cover.jpg')]) ->assertForbidden(); } public function test_admins_can_upload_an_image(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->post('/api/admin/media', ['file' => UploadedFile::fake()->image('cover.jpg', 400, 300)]) ->assertCreated() ->assertJsonStructure(['data' => ['url', 'path', 'name', 'size', 'mime']]); Storage::disk('public')->assertExists($this->storedPath()); } public function test_upload_requires_a_file(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->postJson('/api/admin/media')->assertStatus(422)->assertJsonValidationErrors('file'); } public function test_upload_rejects_a_non_image_disguised_as_an_image(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->post('/api/admin/media', [ 'file' => UploadedFile::fake()->createWithContent('payload.php', 'assertStatus(422)->assertJsonValidationErrors('file'); } /** * SVG is refused on purpose: it can carry script, and the site serves the * upload directory from the same origin as the admin panel. */ public function test_upload_rejects_svg(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->post('/api/admin/media', [ 'file' => UploadedFile::fake()->createWithContent( 'payload.svg', '', ), ])->assertStatus(422)->assertJsonValidationErrors('file'); } public function test_upload_rejects_a_file_over_the_size_limit(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->post('/api/admin/media', [ 'file' => UploadedFile::fake()->create('big.jpg', 5120, 'image/jpeg'), ])->assertStatus(422)->assertJsonValidationErrors('file'); } /** * The client-supplied filename must never reach the filesystem verbatim, * otherwise a double extension or a traversal could be smuggled in. */ public function test_upload_ignores_the_client_filename(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->post('/api/admin/media', [ 'file' => UploadedFile::fake()->image('../../evil.php.jpg', 200, 200), ])->assertCreated(); $stored = $this->storedPath(); $this->assertStringStartsWith('media/', $stored); $this->assertStringNotContainsString('..', $stored); $this->assertStringNotContainsString('evil.php', $stored); } public function test_admins_can_delete_an_uploaded_image(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $path = UploadedFile::fake()->image('cover.jpg')->store('media', 'public'); $this->deleteJson("/api/admin/media/{$path}")->assertOk(); Storage::disk('public')->assertMissing($path); } public function test_delete_rejects_a_path_traversal(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); Storage::disk('public')->put('.env', 'SECRET=1'); // The route pattern itself already blocks `..`, so this asserts the // defence survives even if that pattern is ever loosened. $this->deleteJson('/api/admin/media/media/../../.env')->assertStatus(404); Storage::disk('public')->assertExists('.env'); } public function test_delete_rejects_a_path_outside_the_media_directory(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); Storage::disk('public')->put('secret.txt', 'nope'); $this->deleteJson('/api/admin/media/secret.txt')->assertStatus(404); Storage::disk('public')->assertExists('secret.txt'); } public function test_deleting_a_missing_file_is_a_404(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->deleteJson('/api/admin/media/media/2026/01/missing.jpg')->assertNotFound(); } public function test_uploads_are_rate_limited_harder_than_other_admin_writes(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); // 12 allowed, the 13th is throttled. Keeps the disk-exhaustion cost // of the upload endpoint far below the general admin API budget. foreach (range(1, 12) as $ignored) { $this->post('/api/admin/media', [ 'file' => UploadedFile::fake()->image("cover{$ignored}.jpg", 10, 10), ])->assertCreated(); } $this->post('/api/admin/media', ['file' => UploadedFile::fake()->image('overflow.jpg', 10, 10)]) ->assertStatus(429); } /** * Read the single path written by the fake disk during a request. */ private function storedPath(): string { $files = Storage::disk('public')->allFiles('media'); $this->assertCount(1, $files, 'expected exactly one stored upload'); return $files[0]; } }