Files
compras/tests/Feature/PrivateAuditAccessTest.php
T
nala 060c119ba1
tests / ci (push) Waiting to run
commit compras2 2811
2026-11-28 15:45:52 +02:00

60 lines
3.6 KiB
PHP

<?php
use App\Enums\PurchasingRole;
use App\Models\User;
use App\Services\PrivateAuditAccess;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpKernel\Exception\HttpException;
require_once __DIR__.'/../Support/ReceiptFixture.php';
require_once __DIR__.'/../Support/SupplyFixture.php';
beforeEach(function () {
$this->f = receiptFixture();
$this->actor = $this->f['issuer'];
$this->actingAs($this->actor);
$this->access = app(PrivateAuditAccess::class);
});
test('private audit requires its explicit family authority and never inherits metadata finance or administration', function (string $permission) {
config(['private-audit.qa_enabled' => true]);
supplyGrant($this->actor, [$permission]);
expect(fn () => $this->access->authorise($this->actor, ['finance']))->toThrow(HttpException::class);
})->with(['audit_metadata_read', 'finance', 'admin']);
test('private audit family grants remain independent and cannot activate banking people or financial commands', function () {
config(['private-audit.qa_enabled' => true]);
supplyGrant($this->actor, ['audit_private_finance_read']);
$scope = $this->access->authorise($this->actor, ['finance', 'finance']);
expect($scope['families'])->toBe(['finance'])->and($scope['context'])->toBe('legacy')
->and($this->actor->hasFunctions(['finance', 'admin', 'procurement']))->toBeFalse();
expect(fn () => $this->access->authorise($this->actor, ['purchasing']))->toThrow(HttpException::class);
expect(fn () => $this->access->authorise($this->actor, ['complete_bank_identifiers']))->toThrow(HttpException::class);
expect(fn () => $this->access->authorise($this->actor, ['people']))->toThrow(HttpException::class);
$this->get('/auditoria/eventos')->assertForbidden();
$this->get('/orcamentos')->assertForbidden();
});
test('private audit revalidates membership expiry revocation and maintenance without updating company rights', function () {
config(['private-audit.qa_enabled' => true]);
supplyGrant($this->actor, array_column(PrivateAuditAccess::FAMILIES, 'permission'));
$before = DB::table('organization_memberships')->orderBy('id')->get()->toJson();
expect($this->access->authorise($this->actor, ['stock', 'purchasing', 'finance', 'configuration'])['families'])
->toBe(['configuration', 'finance', 'purchasing', 'stock']);
expect(DB::table('organization_memberships')->orderBy('id')->get()->toJson())->toBe($before);
DB::table('organization_memberships')->where('user_id', $this->actor->id)->update(['expires_at' => now()->subMinute()]);
expect(fn () => $this->access->authorise($this->actor, ['stock']))->toThrow(HttpException::class);
DB::table('organization_memberships')->where('user_id', $this->actor->id)->update(['expires_at' => null, 'maintenance' => true]);
expect(fn () => $this->access->authorise($this->actor, ['stock']))->toThrow(HttpException::class);
DB::table('organization_memberships')->where('user_id', $this->actor->id)->delete();
expect(fn () => $this->access->authorise($this->actor, ['stock']))->toThrow(HttpException::class);
});
test('private audit capability is disabled by default and the caller cannot select a different actor', function () {
supplyGrant($this->actor, ['audit_private_stock_read']);
expect(fn () => $this->access->authorise($this->actor, ['stock']))->toThrow(HttpException::class);
config(['private-audit.qa_enabled' => true]);
$other = User::factory()->create(['tenant_id' => $this->actor->tenant_id, 'purchasing_role' => PurchasingRole::Configured]);
expect(fn () => $this->access->authorise($other, ['stock']))->toThrow(HttpException::class);
});