Add checksum validation for archived media
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled

This commit is contained in:
Codex Agent
2026-01-30 11:29:40 +01:00
parent 9a8305d986
commit eeffe4c6f1
7 changed files with 406 additions and 1 deletions

View File

@@ -0,0 +1,129 @@
<?php
namespace Tests\Feature;
use App\Jobs\ArchiveEventMediaAssets;
use App\Models\Event;
use App\Models\EventMediaAsset;
use App\Models\MediaStorageTarget;
use App\Services\Storage\EventStorageManager;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class ArchiveEventMediaAssetsTest extends TestCase
{
use RefreshDatabase;
public function test_archives_asset_and_verifies_checksum(): void
{
Storage::fake('hot-disk');
Storage::fake('archive-disk');
$hotTarget = MediaStorageTarget::create([
'key' => 'hot-disk',
'name' => 'Hot Disk',
'driver' => 'local',
'config' => [],
'is_hot' => true,
'is_default' => true,
'is_active' => true,
'priority' => 100,
]);
MediaStorageTarget::create([
'key' => 'archive-disk',
'name' => 'Archive Disk',
'driver' => 'local',
'config' => [],
'is_hot' => false,
'is_default' => true,
'is_active' => true,
'priority' => 100,
]);
$event = Event::factory()->create();
$path = 'events/'.$event->id.'/photo.jpg';
Storage::disk('hot-disk')->put($path, 'photo-body');
$checksum = hash('sha256', 'photo-body');
$asset = EventMediaAsset::create([
'event_id' => $event->id,
'media_storage_target_id' => $hotTarget->id,
'photo_id' => null,
'variant' => 'original',
'disk' => 'hot-disk',
'path' => $path,
'size_bytes' => 10,
'checksum' => $checksum,
'status' => 'hot',
]);
(new ArchiveEventMediaAssets($event->id, true))->handle(app(EventStorageManager::class));
$asset->refresh();
$this->assertSame('archived', $asset->status);
$this->assertSame('archive-disk', $asset->disk);
$this->assertSame('verified', data_get($asset->meta, 'checksum_status'));
$this->assertNotEmpty(data_get($asset->meta, 'checksum_verified_at'));
Storage::disk('archive-disk')->assertExists($path);
Storage::disk('hot-disk')->assertMissing($path);
}
public function test_marks_asset_failed_on_checksum_mismatch(): void
{
Storage::fake('hot-disk');
Storage::fake('archive-disk');
$hotTarget = MediaStorageTarget::create([
'key' => 'hot-disk',
'name' => 'Hot Disk',
'driver' => 'local',
'config' => [],
'is_hot' => true,
'is_default' => true,
'is_active' => true,
'priority' => 100,
]);
MediaStorageTarget::create([
'key' => 'archive-disk',
'name' => 'Archive Disk',
'driver' => 'local',
'config' => [],
'is_hot' => false,
'is_default' => true,
'is_active' => true,
'priority' => 100,
]);
$event = Event::factory()->create();
$path = 'events/'.$event->id.'/photo.jpg';
Storage::disk('hot-disk')->put($path, 'photo-body');
$asset = EventMediaAsset::create([
'event_id' => $event->id,
'media_storage_target_id' => $hotTarget->id,
'photo_id' => null,
'variant' => 'original',
'disk' => 'hot-disk',
'path' => $path,
'size_bytes' => 10,
'checksum' => hash('sha256', 'different-body'),
'status' => 'hot',
]);
(new ArchiveEventMediaAssets($event->id, true))->handle(app(EventStorageManager::class));
$asset->refresh();
$this->assertSame('failed', $asset->status);
$this->assertSame('checksum_mismatch', $asset->error_message);
$this->assertSame('mismatch', data_get($asset->meta, 'checksum_status'));
$this->assertSame('hot-disk', $asset->disk);
Storage::disk('hot-disk')->assertExists($path);
Storage::disk('archive-disk')->assertMissing($path);
}
}

View File

@@ -75,4 +75,62 @@ class MonitorStorageCommandTest extends TestCase
$this->assertSame(2, $snapshot['targets'][0]['assets']['total']);
$this->assertGreaterThanOrEqual(1, count($snapshot['alerts']));
}
public function test_monitor_command_flags_checksum_mismatches(): void
{
config([
'storage-monitor.checksum_validation.enabled' => true,
'storage-monitor.checksum_validation.alert_window_minutes' => 60,
'storage-monitor.checksum_validation.thresholds.warning' => 1,
'storage-monitor.checksum_validation.thresholds.critical' => 5,
]);
$target = MediaStorageTarget::create([
'key' => 'local-ssd',
'name' => 'Local SSD',
'driver' => 'local',
'config' => ['monitor_path' => storage_path('app')],
'is_hot' => true,
'is_default' => true,
'is_active' => true,
'priority' => 100,
]);
$event = Event::factory()->create();
EventMediaAsset::create([
'event_id' => $event->id,
'media_storage_target_id' => $target->id,
'photo_id' => null,
'variant' => 'original',
'disk' => 'local-ssd',
'path' => 'events/'.$event->id.'/photo.jpg',
'size_bytes' => 2048,
'status' => 'failed',
'meta' => [
'checksum_status' => 'mismatch',
'checksum_verified_at' => now()->toIso8601String(),
],
]);
$health = Mockery::mock(StorageHealthService::class);
$health->shouldReceive('getCapacity')->andReturn([
'status' => 'ok',
'total' => 100,
'free' => 90,
'used' => 10,
'percentage' => 10,
'path' => storage_path('app'),
]);
$this->app->instance(StorageHealthService::class, $health);
$this->artisan('storage:monitor')
->assertExitCode(0);
$snapshot = Cache::get('storage:monitor:last');
$this->assertNotNull($snapshot);
$alerts = collect($snapshot['alerts'] ?? []);
$this->assertTrue($alerts->contains(fn ($alert) => ($alert['type'] ?? null) === 'checksum_mismatch'));
}
}