f = receiptFixture(); $this->actingAs($this->f['issuer']); Storage::fake('supplier_documents'); $this->supplier = Supplier::query()->sole(); $this->bytes = 'EVIDENCIA-SINTETICA-ORIGINAL'; $this->path = $this->supplier->tenant_id.'/'.$this->supplier->id.'/evidence.pdf'; Storage::disk('supplier_documents')->put($this->path, $this->bytes); $this->document = SupplierDocument::query()->create([ 'tenant_id' => $this->supplier->tenant_id, 'supplier_id' => $this->supplier->id, 'uploaded_by' => $this->f['issuer']->id, 'document_type' => 'other', 'original_name' => 'evidence.pdf', 'path' => $this->path, 'mime_type' => 'application/pdf', 'size' => strlen($this->bytes), 'sha256' => hash('sha256', $this->bytes), ]); $this->url = '/fornecedores/'.$this->supplier->id.'/documentos/'.$this->document->id; }); test('supplier download returns verified private bytes and audits only successful preparation', function () { $before = AuditLog::query()->count(); $response = $this->get($this->url)->assertOk()->assertHeader('Cache-Control', 'no-store, private') ->assertHeader('Content-Type', 'application/octet-stream') ->assertHeader('Content-Security-Policy', "default-src 'none'; sandbox"); // Changes after response preparation must not replace the verified evidence. Storage::disk('supplier_documents')->put($this->path, 'SUBSTITUIDO-DEPOIS-DA-VERIFICACAO'); expect($response->streamedContent())->toBe($this->bytes) ->and(AuditLog::query()->count())->toBe($before + 1) ->and(AuditLog::query()->latest('id')->first()->action)->toBe('supplier.document_downloaded'); }); test('supplier upload preserves the authenticated author and reopens the native independent review', function () { $bytes = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Wl6E4sAAAAASUVORK5CYII='); $bytes .= str_repeat("\0", 1100); // Meet the existing 1 KiB upload minimum. $this->post('/fornecedores/'.$this->supplier->id.'/documentos', ['document_type' => 'other', 'file' => UploadedFile::fake()->createWithContent('evidence.png', $bytes)])->assertRedirect()->assertSessionHasNoErrors(); $document = SupplierDocument::query()->latest('id')->first(); expect($document->uploaded_by)->toBe($this->f['issuer']->id) ->and($document->sha256)->toBe(hash('sha256', $bytes)) ->and($this->supplier->refresh()->status)->toBe('pending') ->and($this->supplier->last_changed_by)->toBe($this->f['issuer']->id); }); test('supplier download refuses altered evidence of the same size without auditing a successful download', function () { $changed = $this->bytes; $changed[0] = 'X'; Storage::disk('supplier_documents')->put($this->path, $changed); $before = AuditLog::query()->count(); $this->get($this->url)->assertStatus(409); expect(AuditLog::query()->count())->toBe($before) ->and(Storage::disk('supplier_documents')->get($this->path))->toBe($changed); }); test('supplier download refuses absent empty oversized and inconsistent-length evidence', function () { $disk = Storage::disk('supplier_documents'); $disk->delete($this->path); $this->get($this->url)->assertNotFound(); foreach (['', str_repeat('x', 2097153), $this->bytes.'extra'] as $content) { $disk->put($this->path, $content); $this->get($this->url)->assertStatus(409); } }); test('supplier download refuses references outside the owning supplier and traversal paths', function () { foreach (['999/999/evidence.pdf', $this->supplier->tenant_id.'/'.$this->supplier->id.'/../evidence.pdf'] as $path) { $this->document->update(['path' => $path]); $this->get($this->url)->assertStatus(409); } }); test('supplier document integrity does not grant documents to department heads or other companies', function () { $head = User::factory()->create(['tenant_id' => $this->supplier->tenant_id, 'department_id' => $this->f['issuer']->department_id, 'purchasing_role' => PurchasingRole::DepartmentHead]); $this->actingAs($head)->get($this->url)->assertForbidden(); $other = receiptFixture(); $this->actingAs($other['issuer'])->get($this->url)->assertNotFound(); auth()->logout(); $this->get($this->url)->assertRedirect('/login'); }); test('supplier download reloads staff authority instead of trusting the cached authenticated object', function () { DB::table('users')->where('id', $this->f['issuer']->id)->update(['purchasing_role' => 'requester']); $this->get($this->url)->assertForbidden(); }); test('supplier upload rechecks authority after the company lock and removes only its uncommitted file on refusal', function () { $guard = Mockery::mock(ConflictGuard::class)->makePartial(); $guard->shouldReceive('lockCompany')->once()->andReturnUsing(function () { DB::table('users')->where('id', $this->f['issuer']->id)->update(['purchasing_role' => 'requester']); }); app()->instance(ConflictGuard::class, $guard); $bytes = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Wl6E4sAAAAASUVORK5CYII=').str_repeat("\0", 1100); $this->post('/fornecedores/'.$this->supplier->id.'/documentos', ['document_type' => 'other', 'file' => UploadedFile::fake()->createWithContent('revoked.png', $bytes)])->assertForbidden(); expect(SupplierDocument::query()->count())->toBe(1) ->and(Storage::disk('supplier_documents')->allFiles())->toBe([$this->path]) ->and($this->supplier->refresh()->status)->toBe('active'); });