90 lines
4.4 KiB
PHP
90 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Jobs\RunSimulation;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Queue;
|
|
use Illuminate\Support\Str;
|
|
use Tests\TestCase;
|
|
|
|
class ApiContractTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Queue::fake();
|
|
}
|
|
|
|
public function test_simulation_jobs_follow_simulation_access_scope_and_contract_shape(): void
|
|
{
|
|
$token = Str::random(48);
|
|
$create = $this->postJson('/api/v1/simulations', ['base' => 0, 'rate' => 0], [
|
|
'Idempotency-Key' => (string) Str::uuid(),
|
|
'X-Simulation-Token' => $token,
|
|
])->assertAccepted();
|
|
|
|
$simulationId = $create->json('data.id');
|
|
$this->getJson("/api/v1/simulations/{$simulationId}/jobs", ['X-Simulation-Token' => $token])
|
|
->assertOk()
|
|
->assertJsonStructure(['data' => [['id', 'simulation_id', 'job_type', 'status', 'attempts', 'available_at']]])
|
|
->assertJsonPath('data.0.job_type', 'execute')
|
|
->assertJsonPath('data.0.status', 'pending');
|
|
|
|
$this->getJson("/api/v1/simulations/{$simulationId}/jobs", ['X-Simulation-Token' => Str::random(48)])
|
|
->assertNotFound();
|
|
}
|
|
|
|
public function test_public_legislation_lists_only_validated_current_sources_with_etag_and_correlation(): void
|
|
{
|
|
$owner = User::factory()->create();
|
|
$validator = User::factory()->create();
|
|
$validId = (string) Str::uuid();
|
|
$today = today()->toDateString();
|
|
DB::table('investment_legal_sources')->insert([
|
|
['id' => $validId, 'title' => 'Lei de teste', 'url' => 'https://example.org/lei', 'diploma' => 'Diploma A',
|
|
'article' => 'Artigo 1', 'competent_entity' => 'Entidade A', 'valid_from' => $today, 'valid_until' => null,
|
|
'validation_status' => 'validated', 'owner_id' => $owner->id, 'validator_id' => $validator->id,
|
|
'validated_at' => now(), 'version_no' => 3, 'created_at' => now(), 'updated_at' => now()],
|
|
['id' => (string) Str::uuid(), 'title' => 'Lei pendente', 'url' => 'https://example.org/pendente', 'diploma' => 'Diploma B',
|
|
'article' => 'Artigo 2', 'competent_entity' => 'Entidade B', 'valid_from' => $today, 'valid_until' => null,
|
|
'validation_status' => 'pending', 'owner_id' => $owner->id, 'validator_id' => null,
|
|
'validated_at' => null, 'version_no' => 1, 'created_at' => now(), 'updated_at' => now()],
|
|
['id' => (string) Str::uuid(), 'title' => 'Lei expirada', 'url' => 'https://example.org/expirada', 'diploma' => 'Diploma C',
|
|
'article' => 'Artigo 3', 'competent_entity' => 'Entidade C', 'valid_from' => today()->subDay()->toDateString(),
|
|
'valid_until' => today()->subDay()->toDateString(), 'validation_status' => 'validated', 'owner_id' => $owner->id,
|
|
'validator_id' => $validator->id, 'validated_at' => now(), 'version_no' => 2, 'created_at' => now(), 'updated_at' => now()],
|
|
]);
|
|
|
|
$correlationId = (string) Str::uuid();
|
|
$response = $this->getJson('/api/v1/legislation/sources?q=Lei&per_page=1', ['X-Correlation-ID' => $correlationId])
|
|
->assertOk()
|
|
->assertHeader('ETag')
|
|
->assertHeader('X-Correlation-ID', $correlationId)
|
|
->assertJsonPath('meta.total', 1)
|
|
->assertJsonPath('data.0.id', $validId)
|
|
->assertJsonPath('data.0.validation_status', 'validated');
|
|
|
|
$this->assertStringStartsWith('W/"legal-sources-', $response->headers->get('ETag'));
|
|
}
|
|
|
|
public function test_contract_errors_include_correlation_and_required_preconditions(): void
|
|
{
|
|
$missingKey = $this->postJson('/api/v1/simulations', ['base' => 1, 'rate' => 1])
|
|
->assertStatus(428)
|
|
->assertJsonStructure(['message'])
|
|
->assertHeader('X-Correlation-ID');
|
|
$this->assertTrue(Str::isUuid($missingKey->headers->get('X-Correlation-ID')));
|
|
|
|
$invalidCorrelation = $this->getJson('/api/v1/legislation/sources', ['X-Correlation-ID' => 'invalid'])
|
|
->assertUnprocessable()
|
|
->assertJsonPath('code', 'invalid_correlation_id')
|
|
->assertHeader('X-Correlation-ID');
|
|
$this->assertTrue(Str::isUuid($invalidCorrelation->headers->get('X-Correlation-ID')));
|
|
}
|
|
}
|