feat: verify active opportunity management and prepare versioned resources

This commit is contained in:
2026-10-09 13:16:41 +02:00
parent 60784a2ed2
commit 9d6176f55b
16 changed files with 419 additions and 43 deletions
+35 -2
View File
@@ -12,6 +12,7 @@ use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use Tests\Support\CleanScannerFixture;
use Tests\TestCase;
@@ -75,7 +76,7 @@ class OpportunityResourcesTest extends TestCase
[$user,$o] = $this->setupDraft();
$province = (string) Str::uuid();
$district = (string) Str::uuid();
DB::table('geo_locations')->insert([['id' => $province, 'name' => 'QA province', 'location_type' => 'province', 'parent_id'=>null, 'data_classification' => 'public'], ['id' => $district, 'name' => 'QA district', 'location_type' => 'district', 'parent_id' => $province, 'data_classification' => 'public']]);
DB::table('geo_locations')->insert([['id' => $province, 'code' => $province, 'name' => 'QA province', 'location_type' => 'province', 'parent_id' => null, 'data_classification' => 'public'], ['id' => $district, 'code' => $district, 'name' => 'QA district', 'location_type' => 'district', 'parent_id' => $province, 'data_classification' => 'public']]);
$p = ['title' => 'QA production', 'province_id' => $province, 'district_id' => $district, 'role' => 'production', 'is_primary' => true, 'allocation_percentage' => 60, 'territorial_purpose' => 'implementation'];
$id = $this->postJson('/management/opportunities/'.$o->id.'/resources/locations', $this->payload($o, $p))->assertCreated()->json('id');
$this->postJson('/management/opportunities/'.$o->id.'/resources/locations', $this->payload($o->fresh(), $p))->assertUnprocessable();
@@ -93,7 +94,39 @@ class OpportunityResourcesTest extends TestCase
[$user,$o] = $this->setupDraft();
$this->post('/management/opportunities/'.$o->id.'/resources/images', [...$this->payload($o, ['title' => 'Bad image']), 'file' => UploadedFile::fake()->createWithContent('bad.png', 'invalid')], ['Accept' => 'application/json'])->assertUnprocessable();
$this->postJson('/management/opportunities/'.$o->id.'/resources/images', $this->payload($o, ['title' => 'QA image', 'location_id' => (string) Str::uuid()]))->assertUnprocessable()->assertJsonValidationErrors('location_id');
$this->assertSame(0,DB::table('opportunity_images')->count());
$this->assertSame(0, DB::table('opportunity_images')->count());
$this->actingAs(User::factory()->create())->get('/management/opportunity-resources-install')->assertForbidden();
}
public function test_image_processing_is_explicit_and_every_derivative_requires_its_own_scan(): void
{
[$user,$o] = $this->setupDraft();
$chunk = fn ($name, $bytes) => pack('N', strlen($bytes)).$name.$bytes.pack('N', crc32($name.$bytes));
$png = "\x89PNG\r\n\x1a\n".$chunk('IHDR', pack('NNCCCCC', 500, 500, 8, 2, 0, 0, 0)).$chunk('IDAT', gzcompress(str_repeat("\0", 500 * 1501))).$chunk('IEND', '');
$result = $this->post('/management/opportunities/'.$o->id.'/resources/images', [...$this->payload($o, ['title' => 'Synthetic QA image', 'alt_text' => 'Synthetic white square for QA', 'caption' => 'Synthetic image not used in active data', 'rights_status' => 'owned', 'is_cover' => true]), 'file' => UploadedFile::fake()->createWithContent('qa.png', $png)], ['Accept' => 'application/json'])->assertCreated()->json();
$image = DB::table('opportunity_images')->where('id', $result['id'])->first();
(new ScanDocumentForMalware($image->document_id))->handle(new CleanScannerFixture);
$url = '/management/opportunities/'.$o->id.'/resources/images/'.$image->id.'/prepare';
$input = ['etag' => app(OpportunityManagement::class)->etag($o->fresh()), 'version' => 1, 'reason' => 'Processamento de imagem sintética em QA'];
if (! function_exists('imagecreatefromstring')) {
$this->postJson($url, $input)->assertStatus(423);
$this->assertDatabaseHas('opportunity_images', ['id' => $image->id, 'version' => 1]);
return;
}
$this->postJson($url, $input)->assertOk();
$payload = json_decode(DB::table('opportunity_images')->where('id', $image->id)->value('payload'), true);
$this->assertCount(2, $payload['derivatives']);
foreach ($payload['derivatives'] as $derivative) {
$document = DB::table('documents')->where('id', $derivative['document_id'])->first();
$this->assertSame('pending', $document->malware_scan_status);
$webp = Crypt::decryptString(Storage::disk('local')->get($document->storage_key));
$this->assertStringNotContainsString('EXIF', $webp);
$this->assertSame('image/webp', getimagesizefromstring($webp)['mime']);
(new ScanDocumentForMalware($document->id))->handle(new CleanScannerFixture);
}
$preview = URL::temporarySignedRoute('opportunity-image-preview', now()->addMinutes(10), ['id' => $o->id, 'resourceId' => $image->id, 'documentId' => $payload['derivatives'][0]['document_id']]);
$this->get($preview)->assertOk()->assertHeader('Content-Type', 'image/webp');
$this->actingAs(User::factory()->create())->get($preview)->assertForbidden();
}
}