commit compras1 2811
tests / ci (push) Waiting to run

This commit is contained in:
2026-11-28 07:11:35 +02:00
parent d58b75c87a
commit eb32c74c56
202 changed files with 17463 additions and 1526 deletions
+41 -3
View File
@@ -24,6 +24,28 @@ function maintenanceSupplierData(): array
return ['name' => 'Fornecedor existente', 'nuit' => '123456789', 'contact' => 'Contacto comercial', 'email' => 'fornecedor@example.test', 'phone' => '123456789', 'address' => 'Maputo', 'category' => 'Material', 'beneficiaries' => 'Beneficiário de teste', 'document_references' => 'Documentos verificados no processo de teste.', 'diligence_notes' => 'Documentação conferida para testes de manutenção.', 'blocked' => false, 'next_review' => now()->addYear()->format('Y-m-d')];
}
/** Real synthetic PDF bytes: fake()->create reports a virtual size over an empty file. */
function maintenanceSupplierDocument(string $name): UploadedFile
{
$objects = ['<< /Type /Catalog /Pages 2 0 R >>', '<< /Type /Pages /Count 1 /Kids [3 0 R] >>',
'<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Contents 4 0 R >>', "<< /Length 0 >>\nstream\n\nendstream"];
// The uploader requires at least 1 KB of actual content, rather than a virtual size.
$bytes = "%PDF-1.4\n% ".str_repeat('Synthetic QA evidence only; not an official certificate. ', 24)."\n";
$offsets = [];
foreach ($objects as $index => $object) {
$offsets[] = strlen($bytes);
$bytes .= ($index + 1)." 0 obj\n".$object."\nendobj\n";
}
$xref = strlen($bytes);
$bytes .= "xref\n0 5\n0000000000 65535 f \n";
foreach ($offsets as $offset) {
$bytes .= sprintf("%010d 00000 n \n", $offset);
}
$bytes .= "trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n".$xref."\n%%EOF\n";
return UploadedFile::fake()->createWithContent($name, $bytes);
}
beforeEach(function () {
Storage::fake('supplier_documents');
$this->tenant = Tenant::query()->create(['name' => 'Manutenção A']);
@@ -46,6 +68,14 @@ test('procurement can edit and renew a supplier but cannot forge validation or o
expect($audit->before['name'])->toBe('Fornecedor existente')->and($audit->after['name'])->toBe('Nome actualizado');
});
test('supplier edit preparation is encrypted and never renews diligence or changes validation by reading', function () {
$before = $this->supplier->fresh()->getAttributes();
$audits = AuditLog::query()->count();
$this->get($this->url.'/editar')->assertOk()->assertViewHas('page', fn ($page) => $page['encryptHistory'] === true)
->assertInertia(fn (Assert $page) => $page->component('suppliers/edit')->where('supplier.version', 1));
expect($this->supplier->fresh()->getAttributes())->toBe($before)->and(AuditLog::query()->count())->toBe($audits);
});
test('unchanged edits preserve validation and do not create audit noise', function () {
$this->put($this->url, [...maintenanceSupplierData(), 'version' => 1])->assertSessionHasNoErrors();
expect($this->supplier->refresh()->status)->toBe('active')->and($this->supplier->version)->toBe(1);
@@ -73,11 +103,16 @@ test('non procurement roles cannot change suppliers or upload documents', functi
})->with([PurchasingRole::Finance, PurchasingRole::Admin, PurchasingRole::Requester, PurchasingRole::DepartmentHead, PurchasingRole::Warehouse]);
test('private upload saves metadata invalidates validation and audits without leaking storage paths', function () {
$this->post($this->url.'/documentos', ['document_type' => 'license', 'file' => UploadedFile::fake()->create('alvara.pdf', 10, 'application/pdf')])->assertSessionHasNoErrors()->assertRedirect();
$file = maintenanceSupplierDocument('alvara.pdf');
$bytes = file_get_contents($file->getPathname());
expect(strlen($bytes))->toBeGreaterThanOrEqual(1024)->toBeLessThanOrEqual(2097152);
$this->post($this->url.'/documentos', ['document_type' => 'license', 'file' => $file])->assertSessionHasNoErrors()->assertRedirect();
$document = SupplierDocument::query()->firstOrFail();
Storage::disk('supplier_documents')->assertExists($document->path);
expect($document->path)->toStartWith($this->tenant->id.'/'.$this->supplier->id.'/');
expect($document->original_name)->toBe('alvara.pdf')->and(strlen($document->sha256))->toBe(64);
expect((int) $document->size)->toBe(strlen($bytes))->and($document->sha256)->toBe(hash('sha256', $bytes))
->and(Storage::disk('supplier_documents')->get($document->path))->toBe($bytes);
expect($document->toArray())->not->toHaveKey('path');
expect($this->supplier->refresh()->status)->toBe('pending')->and($this->supplier->version)->toBe(2);
expect(AuditLog::query()->where('action', 'supplier.document_uploaded')->firstOrFail()->after['document'])->not->toHaveKey('path');
@@ -86,10 +121,13 @@ test('private upload saves metadata invalidates validation and audits without le
});
test('authorised downloads are attachments and recorded in audit', function (PurchasingRole $role) {
$this->post($this->url.'/documentos', ['document_type' => 'tax', 'file' => UploadedFile::fake()->create('certidao.pdf', 10, 'application/pdf')]);
$file = maintenanceSupplierDocument('certidao.pdf');
$bytes = file_get_contents($file->getPathname());
$this->post($this->url.'/documentos', ['document_type' => 'tax', 'file' => $file])->assertSessionHasNoErrors();
$document = SupplierDocument::query()->firstOrFail();
$user = User::factory()->create(['tenant_id' => $this->tenant->id, 'purchasing_role' => $role]);
$this->actingAs($user)->get($this->url.'/documentos/'.$document->id)->assertOk()->assertDownload('certidao.pdf')->assertHeader('X-Content-Type-Options', 'nosniff')->assertHeader('Content-Type', 'application/octet-stream');
$response = $this->actingAs($user)->get($this->url.'/documentos/'.$document->id)->assertOk()->assertDownload('certidao.pdf')->assertHeader('X-Content-Type-Options', 'nosniff')->assertHeader('Content-Type', 'application/octet-stream');
expect($response->streamedContent())->toBe($bytes);
expect(AuditLog::query()->where('action', 'supplier.document_downloaded')->count())->toBe(1);
})->with([PurchasingRole::Admin, PurchasingRole::Procurement, PurchasingRole::Finance]);