Files
Investidor-V2/tests/Feature/PortalOperationsTest.php
T
Claude 03e54c7a85
tests / ci (push) Canceled after 0s
Avisa a equipa de novos interesses e pedidos do Balcão
Em produção, os pedidos de interesse feitos em /apoio nunca foram vistos:
só o caminho da API enfileirava o aviso à equipa. O aviso passa para o
InterestSubmission::receive, por onde passam todos os caminhos de entrada,
e a submissão de um pedido no Balcão Virtual passa a avisar também a equipa
(até aqui só o proponente e um analista já atribuído recebiam aviso interno).

- TeamNotifications: fila transaccional comum (só existe aviso se o registo
  ficou gravado); destinatários de PORTAL_NOTIFICATION_RECIPIENTS, sem dados
  pessoais nas mensagens.
- NotifyDeskRequestSubmitted: protocolo e ligações protegidas de triagem.
- DemographicInstallation aceita também o domínio público como a mesma
  instalação de produção, para o APP_URL poder passar a investemoz.tempo.co.mz.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jm
2026-12-04 13:51:56 +02:00

129 lines
6.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Jobs\NotifyInterestReceived;
use App\Models\InterestRequest;
use App\Models\Sector;
use App\Models\User;
use App\Services\PortalAudit;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Mail\MailManager;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Tests\Support\AuthenticatesQaActors;
use Tests\TestCase;
class PortalOperationsTest extends TestCase
{
use AuthenticatesQaActors;
use RefreshDatabase;
public function test_audit_records_actor_and_changes_without_free_text(): void
{
$user = User::factory()->create();
$sector = new Sector;
PortalAudit::save($sector, ['name' => 'Nome privado', 'slug' => 'test', 'description' => 'Texto confidencial', 'is_active' => true], $user->id);
PortalAudit::save($sector, ['is_active' => false], $user->id);
$events = DB::table('audit_events')->where('source_system', 'portal-management')->get();
$this->assertCount(2, $events);
$this->assertSame($user->id, $events[0]->actor_user_id);
$this->assertStringNotContainsString('Texto confidencial', json_encode($events));
$this->assertStringNotContainsString('Nome privado', json_encode($events));
$updated = $events->firstWhere('action', 'updated');
$this->assertFalse(json_decode($updated->after_data, true)['values']['is_active']);
$this->actingAs($user)->get('/management/history')->assertForbidden();
config(['portal.managers' => [$user->email]]);
$this->actingAsWithMfa($user);
$this->get('/management/history')->assertOk();
}
public function test_audit_failure_rolls_back_the_business_change(): void
{
$sector = Sector::create(['name' => 'Energia', 'slug' => 'energia', 'is_active' => true]);
try {
PortalAudit::save($sector, ['is_active' => false], 9999999);
$this->fail('Expected invalid actor foreign key to reject event.');
} catch (QueryException) {
$this->assertTrue($sector->fresh()->is_active);
}
}
public function test_public_notifications_are_disabled_by_default_and_enqueued_when_enabled(): void
{
$this->actingAs(User::factory()->create());
$payload = ['fullName' => 'Pessoa de teste', 'email' => 'portal-integration-test@gmail.com',
'investorType' => 'empresa', 'country' => 'Moçambique', 'sectorInterest' => 'Energia',
'message' => 'Pedido de teste na base isolada.', 'privacyConsent' => true];
config(['portal.notifications_enabled' => false]);
$this->postJson('/api/v1/interest-requests', $payload, ['Idempotency-Key' => (string) Str::uuid()])->assertCreated();
$this->assertDatabaseCount('jobs', 0);
config(['portal.notifications_enabled' => true, 'portal.notification_recipients' => ['team@example.invalid']]);
$this->postJson('/api/v1/interest-requests', $payload, ['Idempotency-Key' => (string) Str::uuid()])->assertCreated();
$this->assertDatabaseCount('jobs', 1);
$this->assertDatabaseCount('interest_requests', 2);
config(['queue.connections.database.after_commit' => true]);
$this->postJson('/api/v1/interest-requests', $payload, ['Idempotency-Key' => (string) Str::uuid()])->assertStatus(500);
$this->assertDatabaseCount('jobs', 1);
$this->assertDatabaseCount('interest_requests', 2);
}
public function test_public_support_form_notifies_the_team_once_per_new_interest(): void
{
// Production received interests through /apoio for months without anyone being told.
$payload = ['fullName' => 'Pessoa sintética QA', 'email' => 'portal-integration-test@gmail.com',
'investorType' => 'individual', 'country' => 'Moçambique', 'sectorInterest' => 'Energia',
'message' => 'Pedido exclusivamente sintético na base QA.', 'privacyConsent' => true, 'kind' => 'support'];
config(['portal.notifications_enabled' => true, 'portal.notification_recipients' => ['equipa@example.invalid', 'gestao@example.invalid', 'equipa@example.invalid']]);
$key = (string) Str::uuid();
$this->post('/apoio', [...$payload, 'submission_key' => $key])->assertSessionHasNoErrors()->assertRedirect();
$this->assertDatabaseCount('interest_requests', 1);
$this->assertDatabaseCount('jobs', 2); // one per distinct recipient
$queued = DB::table('jobs')->pluck('payload')->implode(' ');
$this->assertStringContainsString('NotifyInterestReceived', $queued);
$this->assertStringNotContainsString('portal-integration-test@gmail.com', $queued);
// Replaying the same submission returns the stored interest and does not notify again.
$this->post('/apoio', [...$payload, 'submission_key' => $key])->assertSessionHasNoErrors()->assertRedirect();
$this->assertDatabaseCount('interest_requests', 1);
$this->assertDatabaseCount('jobs', 2);
config(['portal.notifications_enabled' => false]);
$this->post('/apoio', [...$payload, 'submission_key' => (string) Str::uuid()])->assertSessionHasNoErrors();
$this->assertDatabaseCount('interest_requests', 2);
$this->assertDatabaseCount('jobs', 2);
}
public function test_job_checks_current_configuration_and_excludes_personal_data(): void
{
$interest = InterestRequest::create(['reference' => 'IR-TEST', 'full_name' => 'Pessoa confidencial',
'email' => 'personal@example.invalid', 'investor_type' => 'empresa', 'country' => 'Moçambique',
'sector_interest' => 'Energia', 'message' => 'Mensagem confidencial', 'privacy_consent' => true]);
$job = new NotifyInterestReceived($interest->id, 'team@example.invalid');
config(['portal.notifications_enabled' => false]);
Mail::shouldReceive('raw')->never();
$job->handle();
Mail::clearResolvedInstance('mail.manager');
Mail::swap(\Mockery::mock(MailManager::class));
config(['portal.notifications_enabled' => true, 'portal.notification_recipients' => ['team@example.invalid']]);
Mail::shouldReceive('raw')->once()->withArgs(function ($body, $callback) {
$this->assertStringContainsString('IR-TEST', $body);
$this->assertStringContainsString('/management/interests/', $body);
$this->assertStringNotContainsString('confidencial', $body);
$this->assertStringNotContainsString('personal@example.invalid', $body);
return is_callable($callback);
});
$job->handle();
}
public function test_readiness_reports_non_production_without_mutating_data(): void
{
$this->artisan('portal:readiness')->expectsOutput('[PENDENTE] Ambiente production')->assertExitCode(1);
$this->assertDatabaseCount('users', 0);
$this->assertDatabaseCount('jobs', 0);
}
}