commit melhorias compras3
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-22 10:56:58 +02:00
parent 4fac443dda
commit efd97e265b
130 changed files with 8917 additions and 363 deletions
@@ -2,13 +2,16 @@
use App\Enums\PurchasingRole;
use App\Models\AuditLog;
use App\Models\Tenant;
use App\Models\User;
use App\Services\CompanyDetails;
use App\Services\OrganizationAccess;
use App\Services\RequisitionWorkflow;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Inertia\Testing\AssertableInertia as Assert;
require_once __DIR__.'/../Support/ReceiptFixture.php';
@@ -259,3 +262,73 @@ test('a configured account has no fallback development permissions while activat
DB::table('organization_access')->where('tenant_id', $this->f['tenant']->id)->update(['active' => true]);
$this->get($this->base)->assertOk();
});
test('company details preserve identifiers and are scoped audited and versioned', function () {
$body = array_fill_keys(array_keys(CompanyDetails::FIELDS), null) + ['version' => 1, 'reason' => 'Cadastro sintético para ensaio isolado.'];
$body['legal_name'] = 'Empresa sintética QA';
$body['nuit'] = '001234567';
$account = array_fill_keys(array_keys(CompanyDetails::BANK_FIELDS), null);
$body['bank_accounts'] = [
[...$account, 'id' => (string) Str::uuid(), 'account_number' => '00001234', 'currency' => 'MZN'],
[...$account, 'id' => (string) Str::uuid(), 'account_number' => '00005678', 'currency' => 'USD'],
];
$location = array_fill_keys(array_keys(CompanyDetails::LOCATION_FIELDS), null);
$body['establishments'] = [
[...$location, 'id' => (string) Str::uuid(), 'kind' => 'headquarters', 'name' => 'Sede QA', 'address' => 'Endereço sintético A'],
[...$location, 'id' => (string) Str::uuid(), 'kind' => 'branch', 'name' => 'Sucursal QA', 'address' => 'Endereço sintético B'],
];
$other = Tenant::query()->create(['name' => 'Outra empresa sintética']);
$purchase = $this->f['r']->fresh()->toArray();
$this->postJson($this->base.'/company_details', $body)->assertRedirect();
expect($this->f['tenant']->fresh()->company_details['bank_accounts'][0]['account_number'])->toBe('00001234')
->and($this->f['tenant']->fresh()->company_details['bank_accounts'][1]['currency'])->toBe('USD')
->and($this->f['tenant']->fresh()->company_details['establishments'][1]['address'])->toBe('Endereço sintético B')
->and($other->fresh()->company_details)->toBeNull()
->and($this->f['r']->fresh()->toArray())->toBe($purchase)
->and(AuditLog::query()->where('action', 'organization.company_details')->count())->toBe(1);
$this->get($this->base)->assertInertia(fn (Assert $page) => $page->where('companyDetailsReady', true)->where('companyDetails.nuit', '001234567'));
$this->postJson($this->base.'/company_details', $body)->assertUnprocessable();
$this->postJson($this->base.'/company_details', [...$body, 'version' => 2, 'nuit' => '12', 'email' => 'invalid'])->assertUnprocessable();
$this->postJson($this->base.'/company_details', [...$body, 'version' => 2, 'tenant_id' => $other->id])->assertUnprocessable();
$invalid = $body;
$invalid['version'] = 2;
$invalid['establishments'][1]['kind'] = 'headquarters';
$this->postJson($this->base.'/company_details', $invalid)->assertUnprocessable();
$invalid = $body;
$invalid['version'] = 2;
$invalid['bank_accounts'][1]['id'] = $invalid['bank_accounts'][0]['id'];
$this->postJson($this->base.'/company_details', $invalid)->assertUnprocessable();
$this->postJson($this->base.'/company_details', [...$body, 'version' => 2])->assertRedirect();
expect($this->f['tenant']->fresh()->company_details['bank_accounts'])->toBe($body['bank_accounts']);
$invalid = $body;
$invalid['version'] = 3;
$invalid['bank_accounts'][1]['currency'] = 'US';
$this->postJson($this->base.'/company_details', $invalid)->assertUnprocessable();
$invalid['bank_accounts'][1]['currency'] = 'USD';
$invalid['establishments'][1]['tenant_id'] = $other->id;
$this->postJson($this->base.'/company_details', $invalid)->assertUnprocessable();
$this->actingAs($this->f['receiver'])->postJson($this->base.'/company_details', [...$body, 'version' => 2])->assertForbidden();
});
test('company details remain unavailable before installation without changing the existing company', function () {
$migration = require database_path('migrations/2026_11_22_000001_add_company_details_to_tenants.php');
$migration->down();
try {
$this->get($this->base)->assertInertia(fn (Assert $page) => $page->where('companyDetailsReady', false)->where('companyDetails', null));
$body = array_fill_keys(array_keys(CompanyDetails::FIELDS), null) + ['bank_accounts' => [], 'establishments' => [], 'version' => 1, 'reason' => 'Ensaio anterior à instalação dos campos.'];
$this->postJson($this->base.'/company_details', $body)->assertStatus(503);
expect(DB::table('organization_access')->where('tenant_id', $this->f['tenant']->id)->value('version'))->toBe(1)
->and(Tenant::query()->findOrFail($this->f['tenant']->id)->name)->toBe($this->f['tenant']->name);
} finally {
$migration->up();
}
});
test('company details are not saved without their audit record', function () {
$body = array_fill_keys(array_keys(CompanyDetails::FIELDS), null) + ['bank_accounts' => [], 'establishments' => [], 'version' => 1, 'reason' => 'Ensaio de recuperação sem auditoria.'];
$this->mock(RequisitionWorkflow::class)->shouldReceive('audit')->once()->andThrow(new RuntimeException('Injected audit failure'));
$this->withoutExceptionHandling();
expect(fn () => $this->postJson($this->base.'/company_details', $body))->toThrow(RuntimeException::class);
expect($this->f['tenant']->fresh()->company_details)->toBeNull()
->and(DB::table('organization_access')->where('tenant_id', $this->f['tenant']->id)->value('version'))->toBe(1);
});