@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\PortalProfileAccess;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use PragmaRX\Google2FA\Google2FA;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CountryCandidateMfaTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_only_a_valid_fortify_challenge_records_recent_mfa_without_the_profile_module(): void
|
||||
{
|
||||
$this->withoutVite();
|
||||
config(['portal.profile_access_enabled' => false]);
|
||||
$actor = User::factory()->create();
|
||||
$totp = new Google2FA;
|
||||
$secret = $totp->generateSecretKey();
|
||||
$actor->forceFill(['two_factor_secret' => Crypt::encrypt($secret), 'two_factor_confirmed_at' => now()])->save();
|
||||
$this->post('/login', ['email' => $actor->email, 'password' => 'password'])->assertRedirect('/two-factor-challenge');
|
||||
$this->assertGuest();
|
||||
$this->assertFalse(app(PortalProfileAccess::class)->mfa($actor));
|
||||
$this->post('/two-factor-challenge', ['code' => 'not-a-code'])->assertSessionHasErrors('code');
|
||||
$this->assertGuest();
|
||||
$this->assertFalse(app(PortalProfileAccess::class)->mfa($actor));
|
||||
$this->post('/two-factor-challenge', ['code' => $totp->getCurrentOtp($secret)])->assertRedirect();
|
||||
$this->assertAuthenticatedAs($actor);
|
||||
$this->assertTrue(app(PortalProfileAccess::class)->mfa($actor));
|
||||
$this->post('/logout')->assertRedirect('/');
|
||||
$this->assertGuest();
|
||||
$this->assertFalse(app(PortalProfileAccess::class)->mfa($actor));
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,20 @@ class LandAssistanceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_filled_print_preserves_country_origin_and_private_ownership(): void
|
||||
{
|
||||
$owner = User::factory()->create(['email_verified_at' => now()]);
|
||||
$this->actingAs($owner)->postJson('/investidor/terra-duat', ['payload' => ['full_name' => 'Pessoa sintética QA', 'country' => 'MZ', 'declared_origin_country_code' => 'PT']])->assertCreated();
|
||||
$case = LandAssistanceCase::firstOrFail();
|
||||
$this->actingAs($owner)->get('/investidor/terra-duat/'.$case->id.'/imprimir')
|
||||
->assertOk()->assertInertia(fn (Assert $page) => $page->component('land-assistance/print')
|
||||
->where('country.code', 'MZ')->where('declaredOrigin.code', 'PT')
|
||||
->where('case.version', 1)->where('case.payload.country', 'MZ')->where('case.payload.declared_origin_country_code', 'PT'));
|
||||
$this->actingAs(User::factory()->create(['email_verified_at' => now()]))
|
||||
->get('/investidor/terra-duat/'.$case->id.'/imprimir')->assertNotFound();
|
||||
$this->assertSame(1, $case->fresh()->version);
|
||||
}
|
||||
|
||||
public function test_public_guide_and_blank_form_are_available_without_claiming_official_status(): void
|
||||
{
|
||||
$this->get('/terra-duat')->assertOk()->assertInertia(fn (Assert $page) => $page->component('land-assistance/guide'));
|
||||
|
||||
@@ -107,6 +107,7 @@ class LegacyContentTest extends TestCase
|
||||
'file_name' => 'source.json', 'media_type' => 'application/json', 'storage_key' => 'legacy/'.$document,
|
||||
'sha256' => hash('sha256', $bytes), 'malware_scanned_sha256' => hash('sha256', $bytes), 'malware_scan_status' => 'clean',
|
||||
'size_bytes' => strlen($bytes), 'status' => 'active', 'rights_status' => 'authorized', 'data_classification' => 'public']);
|
||||
|
||||
return DB::table('portal_resources')->insertGetId(['document_id' => $document, 'title' => $title, 'category' => 'education',
|
||||
'topic' => $identity['sector_slug'], 'published' => true, 'rights_status' => 'authorized',
|
||||
'source_url' => $identity['source_url'], 'source_reference' => 'Exemplar sintético.', 'metadata_version' => 1]);
|
||||
@@ -117,8 +118,7 @@ class LegacyContentTest extends TestCase
|
||||
$this->sectorArticle('energia-mineracao', 'Energia e Mineração', '<a href="https://investemoz.tempo.co.mz/energia/">Energia</a><a href="https://investemoz.tempo.co.mz/mineracao">Mineração</a>');
|
||||
$energy = $this->sectorArticle('energia', 'Energia', '<p>Rede e geração.</p>');
|
||||
$mining = $this->sectorArticle('mineracao', 'Mineração', '<p>Recursos minerais.</p>');
|
||||
$this->get('/explorar/energia-mineracao?q=geração')->assertOk()->assertInertia(fn (Assert $page) =>
|
||||
$page->has('articles', 1)->where('articles.0.id', $energy));
|
||||
$this->get('/explorar/energia-mineracao?q=geração')->assertOk()->assertInertia(fn (Assert $page) => $page->has('articles', 1)->where('articles.0.id', $energy));
|
||||
$index = app(LegacyContent::class)->article('energia-mineracao');
|
||||
$this->assertStringContainsString('href="/energia"', $index['html']);
|
||||
$this->assertStringContainsString('href="/mineracao"', $index['html']);
|
||||
|
||||
@@ -5,15 +5,21 @@ namespace Tests\Feature;
|
||||
use App\Models\Opportunity;
|
||||
use App\Models\User;
|
||||
use App\Services\OpportunityCanonicalContract;
|
||||
use App\Services\OpportunityCanonicalMetadata;
|
||||
use App\Services\OpportunityCanonicalResponse;
|
||||
use App\Services\OpportunityCanonicalValue;
|
||||
use App\Services\OpportunityCategoryFields;
|
||||
use App\Services\OpportunityCompleteness;
|
||||
use App\Services\OpportunityManagement;
|
||||
use App\Services\OpportunityModularProfiles;
|
||||
use App\Services\OpportunityReviewMateriality;
|
||||
use App\Services\PortalProfileAccess;
|
||||
use App\Services\PortalProfileAdministration;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
@@ -84,6 +90,132 @@ class OpportunityCanonicalWorkflowTest extends TestCase
|
||||
$this->assertNotEmpty($schema['$defs']['businessTransactionCreate']['allOf']);
|
||||
}
|
||||
|
||||
public function test_full_canonical_summary_survives_the_editor_echo_without_breaking_the_legacy_limit(): void
|
||||
{
|
||||
[$id, $promoter, , , $payload] = $this->fixture();
|
||||
$payload['executive_summary'] = trim(str_repeat('Resumo canónico completo sintético QA. ', 30));
|
||||
$this->assertGreaterThan(500, mb_strlen($payload['executive_summary']));
|
||||
$this->loginFixture($promoter)->putJson('/minhas-oportunidades/'.$id.'/draft', [
|
||||
'etag' => app(OpportunityManagement::class)->etag(Opportunity::findOrFail($id)),
|
||||
'canonical' => $payload, 'summary' => $payload['executive_summary'], 'reason' => self::REASON,
|
||||
])->assertOk();
|
||||
$record = Opportunity::findOrFail($id);
|
||||
$this->assertSame($payload['executive_summary'], app(OpportunityManagement::class)->values($record)['canonical']['executive_summary']);
|
||||
$this->assertLessThanOrEqual(500, mb_strlen($record->summary));
|
||||
$this->assertStringEndsWith('…', $record->summary);
|
||||
$this->loginFixture($promoter)->putJson('/minhas-oportunidades/'.$id.'/draft', [
|
||||
'etag' => app(OpportunityManagement::class)->etag($record),
|
||||
'summary' => $payload['executive_summary'], 'reason' => self::REASON,
|
||||
])->assertUnprocessable()->assertJsonValidationErrors('summary');
|
||||
}
|
||||
|
||||
public function test_environmental_editor_reuses_canonical_fields_and_preserves_incomplete_drafts(): void
|
||||
{
|
||||
$service = app(OpportunityCanonicalContract::class);
|
||||
$schema = $service->schema(true);
|
||||
$this->assertSame('#/$defs/environmentalSocialCreate', $schema['$defs']['opportunityCreate']['properties']['environmental_social']['$ref']);
|
||||
$this->assertArrayHasKey('assessment_status', $schema['$defs']['environmentalSocialCreate']['properties']);
|
||||
$this->assertArrayHasKey('environmental_category', $schema['$defs']['environmentalSocialCreate']['properties']);
|
||||
$incomplete = ['environmental_social' => ['assessment_status' => 'Pending QA']];
|
||||
$this->assertSame($incomplete, $service->draft($incomplete));
|
||||
$this->assertSame(['assessment_status', 'environmental_category'], $service->schema()['$defs']['environmentalSocialCreate']['required']);
|
||||
}
|
||||
|
||||
public function test_editorial_change_retains_signed_reviews_and_price_reopens_only_property_review(): void
|
||||
{
|
||||
[$id, $promoter, $validator, , $payload, $document] = $this->fixture();
|
||||
$this->action($promoter, $id, 'submit');
|
||||
foreach (['property_review', 'registry_urban_review', 'building_compliance_review'] as $gate) {
|
||||
$this->action($validator, $id, 'review_gate', ['gate' => $gate, 'gate_decision' => 'approved', 'evidence_document_ids' => [$document]]);
|
||||
}
|
||||
$this->action($validator, $id, 'approve');
|
||||
$original = Opportunity::findOrFail($id)->workflow_context['reviews'];
|
||||
$this->action($promoter, $id, 'request_revision');
|
||||
$payload['official_title'] .= ' — correção editorial QA';
|
||||
$this->loginFixture($promoter)->putJson('/minhas-oportunidades/'.$id.'/draft', ['etag' => app(OpportunityManagement::class)->etag(Opportunity::findOrFail($id)), 'canonical' => $payload, 'reason' => self::REASON])->assertOk();
|
||||
$this->action($promoter, $id, 'submit');
|
||||
$context = Opportunity::findOrFail($id)->workflow_context;
|
||||
$this->assertSame([], $context['materiality']['reopened']);
|
||||
foreach ($original as $gate => $review) {
|
||||
$this->assertSame($review['hash'], $context['reviews'][$gate]['hash']);
|
||||
$this->assertSame($review['at'], $context['reviews'][$gate]['at']);
|
||||
$this->assertSame($review['actor_id'], $context['reviews'][$gate]['actor_id']);
|
||||
}
|
||||
$this->action($validator, $id, 'approve');
|
||||
$this->action($promoter, $id, 'request_revision');
|
||||
$payload['real_estate']['price'] += 25;
|
||||
$this->loginFixture($promoter)->putJson('/minhas-oportunidades/'.$id.'/draft', ['etag' => app(OpportunityManagement::class)->etag(Opportunity::findOrFail($id)), 'canonical' => $payload, 'reason' => self::REASON])->assertOk();
|
||||
$this->action($promoter, $id, 'submit');
|
||||
$context = Opportunity::findOrFail($id)->workflow_context;
|
||||
$this->assertSame(['property_review'], $context['materiality']['reopened']);
|
||||
$this->assertSame(['registry_urban_review', 'building_compliance_review'], $context['materiality']['retained']);
|
||||
$this->action($validator, $id, 'approve', [], 422);
|
||||
$this->action($validator, $id, 'review_gate', ['gate' => 'property_review', 'gate_decision' => 'approved', 'evidence_document_ids' => [$document]]);
|
||||
$this->action($validator, $id, 'approve');
|
||||
// A retained decision still requires a currently authorised independent reviewer.
|
||||
DB::table('portal_role_assignments')->where('user_id', $validator->id)->update(['revoked_at' => now()]);
|
||||
$response = $this->loginFixture($promoter)->getJson('/minhas-oportunidades/'.$id.'/response')->assertOk()->json();
|
||||
$this->assertNotNull($response['materiality']);
|
||||
}
|
||||
|
||||
public function test_geometry_order_is_material_and_object_key_order_is_not(): void
|
||||
{
|
||||
$materiality = app(OpportunityReviewMateriality::class);
|
||||
$a = ['payload' => ['opportunity_category' => 'real_estate'], 'resources' => ['locations' => [['payload' => ['geometry' => ['type' => 'LineString', 'coordinates' => [[1, 2], [3, 4], [5, 6]]]]]]]];
|
||||
$b = $a;
|
||||
$b['resources']['locations'][0]['payload']['geometry']['coordinates'] = [[3, 4], [1, 2], [5, 6]];
|
||||
$this->assertSame($materiality->hash($a, 'property_review', [], true), $materiality->hash($b, 'property_review', [], true));
|
||||
$this->assertNotSame($materiality->hash($a, 'property_review'), $materiality->hash($b, 'property_review'));
|
||||
$this->assertSame(OpportunityCanonicalValue::hash(['price' => 100, 'currency' => 'MZN']), OpportunityCanonicalValue::hash(['currency' => 'MZN', 'price' => 100]));
|
||||
$this->assertSame($materiality->hash($a, 'land_legal_review', [], true), $materiality->hash($b, 'land_legal_review', [], true));
|
||||
$this->assertNotSame($materiality->hash($a, 'land_legal_review'), $materiality->hash($b, 'land_legal_review'));
|
||||
}
|
||||
|
||||
public function test_inline_identity_reorder_delete_and_completion_do_not_forge_creation_or_approval(): void
|
||||
{
|
||||
$actor = User::factory()->create();
|
||||
$metadata = app(OpportunityCanonicalMetadata::class);
|
||||
$first = ['land_rights' => ['parcels' => [['cadastral_number' => 'A'], ['cadastral_number' => 'B']]]];
|
||||
$m = $metadata->mutate([], $first, [], $actor, self::REASON);
|
||||
$a = $m['active']['land_rights.parcels.0']['id'];
|
||||
$b = $m['active']['land_rights.parcels.1']['id'];
|
||||
$second = ['land_rights' => ['parcels' => array_reverse($first['land_rights']['parcels'])]];
|
||||
$m = $metadata->mutate($first, $second, $m, $actor, self::REASON);
|
||||
$this->assertSame($a, $m['active']['land_rights.parcels.1']['id']);
|
||||
$this->assertSame($b, $m['active']['land_rights.parcels.0']['id']);
|
||||
$third = ['land_rights' => ['parcels' => [$second['land_rights']['parcels'][0]]]];
|
||||
$m = $metadata->mutate($second, $third, $m, $actor, self::REASON);
|
||||
$this->assertSame($a, $m['deleted'][$a]['id']);
|
||||
$this->assertSame($actor->id, $m['deleted'][$a]['deleted_by']);
|
||||
$this->assertSame(self::REASON, $m['deleted'][$a]['delete_reason']);
|
||||
$legacy = $metadata->mutate($first, $first, [], $actor, self::REASON);
|
||||
$this->assertNull($legacy['active']['land_rights']['created_by']);
|
||||
$this->assertSame('legacy_first_observation_not_creation', $legacy['active']['land_rights']['identity_source']);
|
||||
$completion = app(OpportunityCompleteness::class)->calculate(['opportunity_category' => 'real_estate']);
|
||||
$this->assertLessThan(100, $completion['percentage']);
|
||||
$this->assertNotContains('total_investment', $completion['missing_paths']);
|
||||
$this->assertNotContains('land_rights', $completion['missing_paths']);
|
||||
}
|
||||
|
||||
public function test_private_resource_download_and_valid_signed_preview_require_organization_access(): void
|
||||
{
|
||||
[$id, $promoter, , , , $document] = $this->fixture();
|
||||
$file = DB::table('opportunity_documents')->where('id', $document)->value('document_id');
|
||||
$image = (string) Str::uuid();
|
||||
DB::table('opportunity_images')->insert(['id' => $image, 'opportunity_id' => $id, 'document_id' => $file, 'created_by' => $promoter->id, 'version' => 1,
|
||||
'payload' => json_encode(['title' => 'Synthetic permission fixture only', 'derivatives' => [['document_id' => $file, 'width' => 400, 'height' => 400]]])]);
|
||||
$original = '/minhas-oportunidades/'.$id.'/resources/documents/'.$document.'/download';
|
||||
$preview = URL::temporarySignedRoute('promoter.opportunity-image-preview', now()->addMinutes(10), ['id' => $id, 'resourceId' => $image, 'documentId' => $file]);
|
||||
$this->loginFixture($promoter)->get($original)->assertOk();
|
||||
$this->get($preview)->assertOk();
|
||||
$organization = (string) Str::uuid();
|
||||
DB::table('organizations')->insert(['id' => $organization, 'name' => 'Other synthetic QA organization', 'organization_type' => 'private']);
|
||||
$outsider = $this->actor('promoter', $organization);
|
||||
$this->loginFixture($outsider)->get($original)->assertForbidden();
|
||||
$this->get($preview)->assertForbidden();
|
||||
$this->assertDatabaseHas('documents', ['id' => $file, 'deleted_at' => null]);
|
||||
}
|
||||
|
||||
public function test_read_response_preserves_ids_resources_permissions_and_does_not_invent_validation(): void
|
||||
{
|
||||
[$id, $promoter, $validator, , $payload, $document] = $this->fixture();
|
||||
@@ -95,13 +227,19 @@ class OpportunityCanonicalWorkflowTest extends TestCase
|
||||
$this->assertSame(OpportunityCanonicalResponse::VERSION, $response['adapter_version']);
|
||||
$this->assertSame($id, $response['data']['id']);
|
||||
$this->assertSame($record->reference, $response['data']['opportunity_code']);
|
||||
$this->assertSame($promoter->id, $response['data']['created_by']);
|
||||
$this->assertSame($promoter->id, $response['data']['updated_by']);
|
||||
$this->assertSame($record->created_at->toIso8601String(), $response['data']['created_at']);
|
||||
$this->assertNull($response['data']['deleted_by']);
|
||||
$this->assertSame([], $response['data']['mutation_metadata_gaps']);
|
||||
$this->assertSame($document, $response['data']['documents'][0]['id']);
|
||||
$this->assertSame($payload['locations'], $response['resource_references']['locations']);
|
||||
$this->assertNull($response['data']['approved_by']);
|
||||
$this->assertFalse($response['compatibility']['normative_response_complete']);
|
||||
$this->assertFalse($response['validation_result']['revalidated_on_read']);
|
||||
$this->assertTrue($response['validation_result']['revalidated_on_read']);
|
||||
$this->assertArrayNotHasKey('storage_key', $response['data']['documents'][0]);
|
||||
$this->assertArrayNotHasKey('completion_percentage', $response['data']);
|
||||
$this->assertSame(100, $response['data']['completion_percentage']);
|
||||
$this->assertSame([], $response['data']['validation_result']['errors']);
|
||||
$this->assertSame($before, $record->fresh()->getRawOriginal());
|
||||
$this->loginFixture($validator)->getJson($url)->assertOk();
|
||||
$otherOrg = (string) Str::uuid();
|
||||
@@ -114,8 +252,10 @@ class OpportunityCanonicalWorkflowTest extends TestCase
|
||||
$this->assertNotNull($submitted['data']['submitted_at']);
|
||||
$this->assertNull($submitted['data']['approved_by']);
|
||||
$this->assertNotNull($submitted['validation_result']['submission_hash']);
|
||||
$this->assertEquals($payload['real_estate'], $submitted['data']['real_estate']);
|
||||
$this->assertArrayNotHasKey('id', $submitted['data']['real_estate']);
|
||||
$this->assertSame($payload['real_estate']['price'], $submitted['data']['real_estate']['price']);
|
||||
$this->assertSame($payload['real_estate']['title_document_id'], $submitted['data']['real_estate']['title_document_id']);
|
||||
$this->assertNotEmpty($submitted['data']['real_estate']['id']);
|
||||
$this->assertSame($promoter->id, $submitted['data']['real_estate']['created_by']);
|
||||
}
|
||||
|
||||
public function test_cadastral_uniqueness_and_original_succession_modality_are_enforced_without_filling_drafts(): void
|
||||
@@ -220,7 +360,14 @@ class OpportunityCanonicalWorkflowTest extends TestCase
|
||||
$this->assertDatabaseHas('tasks', ['id' => Opportunity::findOrFail($id)->workflow_context['review_task_id'], 'status' => 'done']);
|
||||
$this->assertSame(self::REASON, Opportunity::findOrFail($id)->workflow_context['rejection']['reason']);
|
||||
$this->action($promoter, $id, 'appeal');
|
||||
app(PortalProfileAdministration::class)->insertGrant((string) Str::uuid(), User::factory()->create(), [
|
||||
'user_id' => $validator->id, 'role' => 'validator', 'opportunity_types' => ['real_estate'],
|
||||
'responsibilities' => ['appeal_reviewer'], 'source' => 'synthetic-policy-test-fixture',
|
||||
]);
|
||||
$validator->refresh();
|
||||
$this->assertTrue(app(PortalProfileAccess::class)->has($validator, 'validator'));
|
||||
$this->action($validator, $id, 'appeal_reopen', [], 403);
|
||||
$this->assertTrue(Opportunity::findOrFail($id)->workflow_context['appeal']['pending']);
|
||||
$appeal = $this->actor('validator', $promoter->id.'unused', ['appeal_reviewer']);
|
||||
$this->action($appeal, $id, 'appeal_reopen');
|
||||
$this->assertFalse(Opportunity::findOrFail($id)->workflow_context['appeal']['pending']);
|
||||
@@ -230,6 +377,53 @@ class OpportunityCanonicalWorkflowTest extends TestCase
|
||||
$this->loginFixture($promoter)->get('/minhas-oportunidades/'.$id.'/resources/documents/'.$document.'/download')->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_long_unicode_reason_completes_existing_task_and_preserves_full_decision(): void
|
||||
{
|
||||
[$id, $promoter, $validator] = $this->fixture();
|
||||
$this->action($promoter, $id, 'submit');
|
||||
$this->action($validator, $id, 'start_review');
|
||||
$task = Opportunity::findOrFail($id)->workflow_context['review_task_id'];
|
||||
$reason = trim(str_repeat('Fundamentação íntegra com evidências e condições necessárias. ', 8));
|
||||
$this->action($validator, $id, 'request_correction', ['reason' => $reason, 'correction_items' => ['canonical.executive_summary']]);
|
||||
$this->assertDatabaseHas('tasks', ['id' => $task, 'status' => 'done', 'outcome' => mb_substr($reason, 0, 79).'…']);
|
||||
$context = Opportunity::findOrFail($id)->workflow_context;
|
||||
$this->assertSame($reason, $context['last_decision']['reason']);
|
||||
$this->assertSame($reason, $context['events'][array_key_last($context['events'])]['reason']);
|
||||
$this->assertSame('needs_information', $context['working_state']);
|
||||
}
|
||||
|
||||
public function test_resubmission_without_content_edit_creates_an_open_review_cycle_and_preserves_old_task(): void
|
||||
{
|
||||
[$id, $promoter, $validator, $publisher, $payload, $document] = $this->fixture();
|
||||
$this->action($promoter, $id, 'submit');
|
||||
$this->action($validator, $id, 'start_review');
|
||||
foreach (['property_review', 'registry_urban_review', 'building_compliance_review'] as $gate) {
|
||||
$this->action($validator, $id, 'review_gate', ['gate' => $gate, 'gate_decision' => 'approved', 'evidence_document_ids' => [$document]]);
|
||||
}
|
||||
$this->action($validator, $id, 'approve');
|
||||
$this->action($publisher, $id, 'publish');
|
||||
$before = Opportunity::findOrFail($id)->workflow_context;
|
||||
$task = $before['review_task_id'];
|
||||
$this->action($publisher, $id, 'suspend');
|
||||
$this->action($promoter, $id, 'request_revision');
|
||||
$this->action($promoter, $id, 'submit');
|
||||
$this->action($validator, $id, 'start_review');
|
||||
$after = Opportunity::findOrFail($id)->workflow_context;
|
||||
$this->assertSame($before['content_revision'], $after['content_revision']);
|
||||
$this->assertNotSame($task, $after['review_task_id']);
|
||||
$this->assertDatabaseHas('tasks', ['id' => $task, 'status' => 'done']);
|
||||
$this->assertDatabaseHas('tasks', ['id' => $after['review_task_id'], 'status' => 'in_progress', 'assignee_user_id' => $validator->id, 'due_at' => null]);
|
||||
$this->assertCount(2, DB::table('tasks')->where('source_system', 'opportunity-workflow')->where('task_type', 'opportunity_review')->get());
|
||||
foreach (['property_review', 'registry_urban_review', 'building_compliance_review'] as $gate) {
|
||||
$this->action($validator, $id, 'review_gate', ['gate' => $gate, 'gate_decision' => 'approved', 'evidence_document_ids' => [$document]]);
|
||||
}
|
||||
$this->action($validator, $id, 'approve');
|
||||
$this->action($publisher, $id, 'publish');
|
||||
$published = Opportunity::findOrFail($id)->workflow_context;
|
||||
$this->assertSame($before['publication']['hash'], $published['publication']['hash']);
|
||||
$this->assertNotSame($before['approval']['review_cycle'], $published['approval']['review_cycle']);
|
||||
}
|
||||
|
||||
public function test_semantic_errors_are_keyed_to_fields_and_incomplete_drafts_remain_recoverable(): void
|
||||
{
|
||||
[$id,$promoter,$validator,$publisher,$payload,$document] = $this->fixture();
|
||||
@@ -265,7 +459,7 @@ class OpportunityCanonicalWorkflowTest extends TestCase
|
||||
$this->action($opinionReviewer, $id, 'respond_opinion', ['opinion_id' => $opinion, 'gate_decision' => 'approved', 'evidence_document_ids' => [$document]]);
|
||||
$context = Opportunity::findOrFail($id)->workflow_context;
|
||||
$this->assertSame('answered', $context['opinions'][$opinion]['status']);
|
||||
$this->assertArrayNotHasKey('reviews', $context);
|
||||
$this->assertSame([], $context['reviews']);
|
||||
$this->assertDatabaseHas('tasks', ['id' => $context['opinions'][$opinion]['task_id'], 'status' => 'done']);
|
||||
$this->action($validator, $id, 'approve', [], 422);
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ class OpportunityRealDocumentOperationsTest extends TestCase
|
||||
$this->get($download)->assertContent($newBytes);
|
||||
$life = ['etag' => app(OpportunityManagement::class)->etag($o->fresh()), 'version' => 2, 'action' => 'delete', 'confirm' => true, 'reason' => 'Exclusão lógica do recurso apenas em QA'];
|
||||
$this->postJson($url.'/'.$r['id'].'/lifecycle', $life)->assertOk();
|
||||
$this->get($download)->assertStatus(423);
|
||||
$this->get($download)->assertNotFound();
|
||||
$this->postJson($url.'/'.$r['id'].'/lifecycle', [...$life, 'etag' => app(OpportunityManagement::class)->etag($o->fresh()), 'version' => 3, 'action' => 'restore'])->assertOk();
|
||||
$this->get($download)->assertContent($newBytes);
|
||||
$this->assertDatabaseHas('documents', ['id' => $doc, 'deleted_at' => null]);
|
||||
@@ -122,7 +122,7 @@ class OpportunityRealDocumentOperationsTest extends TestCase
|
||||
$this->get($url)->assertOk()->assertHeader('Content-Type', 'image/webp')->assertContent($webp);
|
||||
$this->get(explode('?', $url)[0])->assertForbidden();
|
||||
Storage::disk('local')->put($d->storage_key, Crypt::encryptString($webp.'tampered QA'));
|
||||
$this->get($url)->assertConflict();
|
||||
$this->get($url)->assertNotFound();
|
||||
Storage::disk('local')->put($d->storage_key, Crypt::encryptString($webp));
|
||||
}
|
||||
$this->assertSame(4, DB::table('document_malware_scans')->where('result', 'clean')->count());
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\OrganizationCountryInstallation;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
class OrganizationCountryInstallationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private function actor(): User
|
||||
{
|
||||
$user = User::factory()->create(['two_factor_secret' => Crypt::encryptString('JBSWY3DPEHPK3PXP'), 'two_factor_confirmed_at' => now()]);
|
||||
config(['portal.managers' => [$user->email]]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
private function login(User $user, bool $mfa = true): static
|
||||
{
|
||||
return $this->actingAs($user)->withSession(['portal.mfa_actor' => $mfa ? $user->id : null, 'portal.mfa_at' => $mfa ? time() : 0]);
|
||||
}
|
||||
|
||||
private function readiness(bool $ready): void
|
||||
{
|
||||
// Unit fixture only. Real activation requires the existing installer's verified backup evidence.
|
||||
$this->partialMock(OrganizationCountryInstallation::class, function ($mock) use ($ready) {
|
||||
$mock->shouldReceive('evidence')->andReturn(['ready' => $ready, 'backupDate' => null]);
|
||||
});
|
||||
}
|
||||
|
||||
private function input(): array
|
||||
{
|
||||
return ['hash' => $this->getJson('/management/opportunity-resources-install/organization-country')->assertOk()->json('hash'),
|
||||
'confirm' => true, 'idempotency_key' => (string) Str::uuid(), 'reason' => 'Extensão sintética QA, sem contas nem preenchimento automático'];
|
||||
}
|
||||
|
||||
public function test_cold_install_requires_readiness_is_atomic_and_repetition_has_no_duplicates(): void
|
||||
{
|
||||
$actor = $this->actor();
|
||||
$this->login($actor);
|
||||
// TestCase has verified the isolated unit database before RefreshDatabase or these writes.
|
||||
Schema::table('organizations', fn ($table) => $table->dropColumn(['incorporation_country_code', 'incorporation_country_provenance']));
|
||||
DB::table('migrations')->where('migration', OrganizationCountryInstallation::MIGRATION)->delete();
|
||||
$this->readiness(false);
|
||||
$this->getJson('/management/opportunity-resources-install/organization-country')->assertOk()->assertJsonPath('installed', false)->assertJsonPath('consistent', true)->assertJsonPath('ready', false);
|
||||
$this->postJson('/management/opportunity-resources-install/organization-country', $this->input())->assertUnprocessable();
|
||||
$this->readiness(true);
|
||||
$organizations = DB::table('organizations')->count();
|
||||
$users = DB::table('users')->count();
|
||||
$input = $this->input();
|
||||
$this->postJson('/management/opportunity-resources-install/organization-country', $input)->assertOk()->assertJsonPath('changed', true)->assertJsonPath('new_columns', 2);
|
||||
$this->postJson('/management/opportunity-resources-install/organization-country', $input)->assertOk()->assertJsonPath('changed', true);
|
||||
$this->postJson('/management/opportunity-resources-install/organization-country', [...$input, 'reason' => 'Outro conteúdo na mesma chave de repetição'])->assertConflict();
|
||||
$this->postJson('/management/opportunity-resources-install/organization-country', $this->input())->assertOk()->assertJsonPath('changed', false)->assertJsonPath('new_columns', 0);
|
||||
$this->assertSame($organizations, DB::table('organizations')->count());
|
||||
$this->assertSame($users, DB::table('users')->count());
|
||||
$this->assertSame(1, DB::table('migrations')->where('migration', OrganizationCountryInstallation::MIGRATION)->count());
|
||||
$this->assertSame(2, DB::table('audit_events')->where('source_system', 'organization-country-installation')->count());
|
||||
$this->assertFalse(config('organization_country.authenticated_installation'));
|
||||
}
|
||||
|
||||
public function test_missing_mfa_other_user_and_partial_schema_are_denied(): void
|
||||
{
|
||||
$actor = $this->actor();
|
||||
$this->readiness(true);
|
||||
$this->login($actor, false);
|
||||
$this->postJson('/management/opportunity-resources-install/organization-country', $this->input())->assertForbidden();
|
||||
$this->login(User::factory()->create());
|
||||
$this->getJson('/management/opportunity-resources-install/organization-country')->assertForbidden();
|
||||
$this->login($actor);
|
||||
Schema::table('organizations', fn ($table) => $table->dropColumn('incorporation_country_provenance'));
|
||||
$this->getJson('/management/opportunity-resources-install/organization-country')->assertOk()->assertJsonPath('consistent', false);
|
||||
$this->postJson('/management/opportunity-resources-install/organization-country', $this->input())->assertConflict();
|
||||
$this->assertSame(0, DB::table('audit_events')->where('source_system', 'organization-country-installation')->count());
|
||||
}
|
||||
|
||||
public function test_designated_holder_uses_existing_authority_without_legacy_manager_allowlist(): void
|
||||
{
|
||||
$holder = $this->actor();
|
||||
$second = User::factory()->create();
|
||||
$approver = User::factory()->create();
|
||||
config(['portal.managers' => []]);
|
||||
DB::table('portal_superuser_pair')->insert(['id' => 1, 'first_user_id' => $holder->id, 'second_user_id' => $second->id, 'approved_by' => $approver->id, 'version' => 1]);
|
||||
$this->readiness(false);
|
||||
$this->login($holder);
|
||||
$this->getJson('/management/opportunity-resources-install/organization-country')->assertOk()->assertJsonPath('installed', true);
|
||||
$this->postJson('/management/opportunity-resources-install/organization-country', $this->input())->assertOk()->assertJsonPath('changed', false);
|
||||
$this->login($approver);
|
||||
$this->getJson('/management/opportunity-resources-install/organization-country')->assertForbidden();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\InvestmentSubmission;
|
||||
use App\Models\User;
|
||||
use App\Services\PortalProfileAdministration;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
class OrganizationCountryTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
config(['portal.organization_country_enabled' => true]);
|
||||
}
|
||||
|
||||
public function test_auditor_with_officer_membership_cannot_change_the_master_country(): void
|
||||
{
|
||||
config(['portal.profile_access_enabled' => true]);
|
||||
$auditor = User::factory()->create();
|
||||
$approver = User::factory()->create();
|
||||
$org = (string) Str::uuid();
|
||||
DB::table('organizations')->insert(['id' => $org, 'name' => 'Synthetic QA auditor scope', 'source_system' => 'unit-qa', 'organization_type' => 'private']);
|
||||
DB::table('memberships')->insert(['user_id' => $auditor->id, 'organization_id' => $org, 'role' => 'officer']);
|
||||
app(PortalProfileAdministration::class)->insertGrant((string) Str::uuid(), $approver, ['user_id' => $auditor->id, 'role' => 'auditor', 'opportunity_types' => [], 'responsibilities' => []]);
|
||||
$auditor->refresh();
|
||||
$this->actingAs($auditor)->withSession(['portal.access_revision' => (int) $auditor->portal_access_revision]);
|
||||
$this->getJson('/api/v1/investment/profile')->assertOk()->assertJsonPath('organizations.0.canEdit', false);
|
||||
$this->putJson('/api/v1/investment/organizations/'.$org.'/country', ['version' => 1, 'country' => 'FR', 'source_reference' => 'Synthetic declaration blocked by auditor policy'])->assertForbidden();
|
||||
$this->assertNull(DB::table('organizations')->where('id', $org)->value('incorporation_country_code'));
|
||||
$this->assertSame(1, DB::table('organizations')->where('id', $org)->value('version_no'));
|
||||
}
|
||||
|
||||
public function test_master_country_is_explicit_authorized_versioned_and_never_rewrites_previous_submissions(): void
|
||||
{
|
||||
$this->withoutVite();
|
||||
$owner = User::factory()->create();
|
||||
$viewer = User::factory()->create();
|
||||
$other = User::factory()->create();
|
||||
$org = (string) Str::uuid();
|
||||
DB::table('organizations')->insert(['id' => $org, 'name' => 'Synthetic QA master', 'source_system' => 'unit-qa', 'external_id' => 'stable-master', 'organization_type' => 'private']);
|
||||
foreach ([[$owner, 'officer'], [$viewer, 'viewer']] as [$actor,$role]) {
|
||||
DB::table('memberships')->insert(['user_id' => $actor->id, 'organization_id' => $org, 'role' => $role]);
|
||||
}
|
||||
$this->actingAs($owner)->putJson('/api/v1/investment/profile', ['version' => 0, 'payload' => ['country' => 'Historical value', 'residence_country_code' => 'PT']])->assertOk();
|
||||
$company = $this->postJson('/api/v1/investment/companies', ['version' => 0, 'payload' => ['name' => 'Private declared entity', 'role' => 'QA representative', 'incorporation_country_code' => 'MZ']])->assertOk()->json('id');
|
||||
$old = $this->postJson('/api/v1/investment/submissions', ['company_id' => $company])->assertCreated()->json('id');
|
||||
$before = InvestmentSubmission::findOrFail($old)->getRawOriginal('payload');
|
||||
$url = '/api/v1/investment/organizations/'.$org.'/country';
|
||||
$this->putJson($url, ['version' => 1, 'country' => 'ZZ', 'source_reference' => 'Synthetic source'])->assertUnprocessable();
|
||||
$this->putJson($url, ['version' => 1, 'country' => 'FR', 'source_reference' => 'Synthetic declaration; no real registration'])->assertOk()->assertJsonPath('version', 2);
|
||||
$this->putJson($url, ['version' => 1, 'country' => 'ZA', 'source_reference' => 'Stale synthetic declaration'])->assertConflict();
|
||||
$reuse = '/api/v1/investment/companies/'.$company.'/organization-country';
|
||||
$this->postJson($reuse, ['version' => 1, 'organization_id' => $org, 'organization_version' => 2])->assertOk()->assertJsonPath('country', 'FR');
|
||||
$new = $this->postJson('/api/v1/investment/submissions', ['company_id' => $company])->assertCreated()->json('id');
|
||||
$source = InvestmentSubmission::findOrFail($new)->payload['organization_country_snapshot'];
|
||||
$this->assertSame($org, $source['organization_id']);
|
||||
$this->assertSame(2, $source['organization_version']);
|
||||
$this->putJson('/api/v1/investment/submissions/'.$new, ['version' => 1, 'payload' => ['incorporation_country_code' => 'FR', 'organization_country_snapshot' => array_reverse($source, true)]])->assertOk();
|
||||
$forged = $source;
|
||||
$forged['country'] = 'ZA';
|
||||
$this->putJson('/api/v1/investment/submissions/'.$new, ['version' => 2, 'payload' => ['incorporation_country_code' => 'FR', 'organization_country_snapshot' => $forged]])->assertUnprocessable();
|
||||
$this->putJson($url, ['version' => 2, 'country' => 'ZA', 'source_reference' => 'Synthetic second declaration'])->assertOk();
|
||||
$this->assertSame($source, InvestmentSubmission::findOrFail($new)->payload['organization_country_snapshot']);
|
||||
$this->assertSame($before, InvestmentSubmission::findOrFail($old)->getRawOriginal('payload'));
|
||||
$this->assertSame('MZ', InvestmentSubmission::findOrFail($old)->payload['incorporation_country_code']);
|
||||
$this->assertSame('PT', InvestmentSubmission::findOrFail($old)->payload['residence_country_code']);
|
||||
$this->assertSame(1, DB::table('organizations')->count());
|
||||
$row = DB::table('organizations')->where('id', $org)->first();
|
||||
$this->assertSame('unit-qa', $row->source_system);
|
||||
$this->assertSame('stable-master', $row->external_id);
|
||||
$audit = DB::table('audit_events')->where('action', 'incorporation_country_saved')->orderByRaw("(after_data->>'version')::integer")->get();
|
||||
$this->assertCount(2, $audit);
|
||||
$snapshot = json_decode(Crypt::decryptString(json_decode($audit[1]->before_data, true)['snapshot']), true);
|
||||
$this->assertSame('FR', $snapshot['incorporation_country_code']);
|
||||
$this->actingAs($viewer)->getJson('/api/v1/investment/profile')->assertOk()->assertJsonPath('organizations.0.canEdit', false);
|
||||
$this->putJson($url, ['version' => 3, 'country' => 'MZ', 'source_reference' => 'Not authorized'])->assertForbidden();
|
||||
$this->actingAs($other)->getJson('/api/v1/investment/profile')->assertOk()->assertJsonCount(0, 'organizations');
|
||||
$this->putJson($url, ['version' => 3, 'country' => 'MZ', 'source_reference' => 'Wrong organization'])->assertForbidden();
|
||||
DB::table('memberships')->where('user_id', $owner->id)->update(['deleted_at' => now()]);
|
||||
$this->actingAs($owner)->postJson($reuse, ['version' => 2, 'organization_id' => $org, 'organization_version' => 3])->assertForbidden();
|
||||
$this->assertSame(3, DB::table('organizations')->where('id', $org)->value('version_no'));
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,9 @@ class PortalQaMfaTracePrivacyTest extends TestCase
|
||||
$this->assertFalse($second['accepted_by_replay_check']);
|
||||
$this->assertFalse(auth()->check());
|
||||
$encoded = json_encode($second, JSON_THROW_ON_ERROR);
|
||||
foreach ([$secret, $code, md5($code)] as $value) $this->assertStringNotContainsString($value, $encoded);
|
||||
foreach ([$secret, $code, md5($code)] as $value) {
|
||||
$this->assertStringNotContainsString($value, $encoded);
|
||||
}
|
||||
$this->assertSame(18, $session->get('login.id'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,25 @@ class PortalServicesTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_service_intake_preserves_same_origin_entry_page_and_replay_without_trusting_external_referers(): void
|
||||
{
|
||||
$key = (string) Str::uuid();
|
||||
$payload = ['kind' => 'investor', 'fullName' => 'Pessoa sintética QA', 'email' => 'qa-entry-source@example.invalid',
|
||||
'country' => 'PT', 'declared_origin_country_code' => 'ZA', 'sectorInterest' => 'Ensaio da origem do formulário',
|
||||
'message' => 'Descrição sintética QA para testar a rastreabilidade do formulário.', 'privacyConsent' => true, 'submission_key' => $key];
|
||||
$this->post('http://localhost/apoio', $payload, ['Referer' => 'http://localhost/formularios/investidor?context=qa'])->assertRedirect();
|
||||
$row = InterestRequest::where('email', $payload['email'])->firstOrFail();
|
||||
$this->assertSame('/formularios/investidor', $row->source_page);
|
||||
$this->assertSame('ZA', $row->metadata['declared_origin_country_code']);
|
||||
$this->post('http://localhost/apoio', $payload, ['Referer' => 'http://localhost/apoio'])->assertRedirect();
|
||||
$this->assertDatabaseCount('interest_requests', 1);
|
||||
$this->assertSame('/formularios/investidor', $row->fresh()->source_page);
|
||||
foreach (['https://foreign.example/formularios/empresa', 'http://localhost/management/interests', 'http://localhost:9999/formularios/investidor'] as $referer) {
|
||||
$this->post('http://localhost/apoio', [...$payload, 'submission_key' => (string) Str::uuid()], ['Referer' => $referer])->assertRedirect();
|
||||
$this->assertSame('/apoio', InterestRequest::latest('id')->first()->source_page);
|
||||
}
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Reference in New Issue
Block a user