oportunidades 5
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-10-10 14:15:28 +02:00
parent 6ddafc95ba
commit 01405cbf8a
144 changed files with 28903 additions and 1103 deletions
+50
View File
@@ -5,12 +5,14 @@ namespace Tests\Feature;
use App\Console\Commands\ImportDocumentBatch;
use App\Jobs\ScanDocumentForMalware;
use App\Models\User;
use App\Services\Malware\MalwareScanner;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Tests\Support\SyntheticDocument;
use Tests\TestCase;
class ImportDocumentBatchTest extends TestCase
@@ -50,6 +52,7 @@ class ImportDocumentBatchTest extends TestCase
];
file_put_contents($this->staging.'/manifest.json', json_encode(['documents' => [$record, $record]], JSON_THROW_ON_ERROR));
$actor = User::factory()->create();
config(['portal.managers' => [$actor->email]]);
$this->artisan(new ImportDocumentBatch, ['manifest' => $this->batchName.'/manifest.json'])->assertSuccessful();
$this->assertDatabaseCount('documents', 0);
@@ -87,4 +90,51 @@ class ImportDocumentBatchTest extends TestCase
$this->artisan(new ImportDocumentBatch, ['manifest' => $this->batchName.'/manifest.json'])->assertFailed();
$this->assertDatabaseCount('documents', 0);
}
public function test_qa_batch_real_scan_preserves_original_replay_identity_conflict_and_profile_guards(): void
{
config(['services.malware.scanner' => 'clamd', 'services.malware.host' => '127.0.0.1', 'services.malware.port' => 3310]);
$scanner = app(MalwareScanner::class);
if (! $scanner->health()['available']) {
$this->markTestSkipped('Real ClamD required for batch evidence.');
}
$bytes = SyntheticDocument::pdf('Synthetic QA batch - no official or legal value');
file_put_contents($this->staging.'/source.pdf', $bytes);
$record = ['external_id' => 'qa-batch-v1', 'relative_path' => $this->batchName.'/source.pdf', 'title' => 'Synthetic QA batch',
'issuer' => 'Synthetic QA issuer', 'source_url' => 'https://example.invalid/qa', 'version_label' => 'QA-1'];
file_put_contents($this->staging.'/manifest.json', json_encode(['documents' => [$record, $record]], JSON_THROW_ON_ERROR));
$actor = User::factory()->create();
$options = ['manifest' => $this->batchName.'/manifest.json', '--apply' => true, '--actor' => (string) $actor->id];
$this->artisan(new ImportDocumentBatch, $options)->assertFailed();
$this->assertDatabaseCount('documents', 0);
$this->assertSame([], Storage::disk('local')->allFiles('portal-documents'));
config(['portal.managers' => [$actor->email]]);
$this->app->detectEnvironment(fn () => 'production');
$this->artisan(new ImportDocumentBatch, $options)->assertFailed();
$this->app->detectEnvironment(fn () => 'testing');
$this->assertDatabaseCount('documents', 0);
$this->artisan(new ImportDocumentBatch, $options)->assertSuccessful();
$document = DB::table('documents')->firstOrFail();
$this->assertSame($bytes, Storage::disk('local')->get($document->storage_key));
$this->assertDatabaseCount('documents', 1);
$this->assertDatabaseCount('portal_resources', 1);
(new ScanDocumentForMalware($document->id))->handle($scanner);
$scanned = DB::table('documents')->where('id', $document->id)->first();
$this->assertSame('clean', $scanned->malware_scan_status);
$this->assertSame('ClamAV', $scanned->malware_engine);
$this->assertSame(hash('sha256', $bytes), $scanned->malware_scanned_sha256);
$response = $this->actingAs($actor)->get('/management/documents/'.$document->id.'/download')->assertOk();
$this->assertSame($bytes, file_get_contents($response->baseResponse->getFile()->getPathname()));
$resource = DB::table('portal_resources')->first();
$this->get('/documentos/'.$resource->id.'/download')->assertNotFound();
$this->artisan(new ImportDocumentBatch, $options)->assertSuccessful();
file_put_contents($this->staging.'/source.pdf', SyntheticDocument::pdf('Different QA bytes - same identity'));
$this->artisan(new ImportDocumentBatch, $options)->assertSuccessful();
$this->assertDatabaseCount('documents', 1);
$this->assertDatabaseCount('portal_resources', 1);
$this->assertSame($bytes, Storage::disk('local')->get($document->storage_key));
$this->assertDatabaseHas('portal_resources', ['id' => $resource->id, 'published' => false]);
$this->assertSame(1, DB::table('audit_events')->where('action', 'private_document_batch_imported')->count());
$this->actingAs(User::factory()->create())->get('/management/documents/'.$document->id.'/download')->assertForbidden();
}
}