Add checksum validation for archived media
This commit is contained in:
129
tests/Feature/ArchiveEventMediaAssetsTest.php
Normal file
129
tests/Feature/ArchiveEventMediaAssetsTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user