Repository navigation
add evento de auditoria no envio de e-mails de recurso publicado #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
CaffeineIssues
merged 2 commits into
develop
from
feature/published-recourse-email-audit
Dec 4, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| namespace App\Events; | ||
|
|
||
| use Carbon\CarbonInterface; | ||
| use Illuminate\Foundation\Events\Dispatchable; | ||
|
|
||
| /** | ||
| * Disparado após um e-mail ser aceito pelo servidor de envio, | ||
| * para que o envio fique registrado em email_dispatches. | ||
| */ | ||
| class EmailSentEvent | ||
| { | ||
| use Dispatchable; | ||
|
|
||
| public function __construct( | ||
| public readonly string $to, | ||
| public readonly string $subject, | ||
| public readonly string $mailableType, | ||
| public readonly CarbonInterface $sentAt, | ||
| public readonly array $meta = [], | ||
| public readonly ?string $content = null, | ||
| ) {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| namespace App\Listeners; | ||
|
|
||
| use App\Events\EmailSentEvent; | ||
| use App\Models\EmailDispatch; | ||
|
|
||
| /** | ||
| * Registra o envio em email_dispatches; o Auditable do model gera a entrada em audits. | ||
| * | ||
| * Roda de forma síncrona: não há worker de queue:work no supervisord, | ||
| * então um listener enfileirado nunca seria executado. | ||
| */ | ||
| class RecordEmailDispatch | ||
| { | ||
| public function handle(EmailSentEvent $event): void | ||
| { | ||
| EmailDispatch::create([ | ||
| 'to' => $event->to, | ||
| 'subject' => $event->subject, | ||
| 'content' => $event->content, | ||
| 'mailable_type' => $event->mailableType, | ||
| 'meta' => $event->meta, | ||
| 'status' => 'sent', | ||
| 'attempted_at' => $event->sentAt, | ||
| 'dispatched_at' => $event->sentAt, | ||
| ]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Feature; | ||
|
|
||
| use App\Events\EmailSentEvent; | ||
| use App\Mail\PublishedRecourse; | ||
| use App\Models\EmailDispatch; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Illuminate\Support\Carbon; | ||
| use Tests\TestCase; | ||
|
|
||
| class RecordEmailDispatchTest extends TestCase | ||
| { | ||
| use RefreshDatabase; | ||
|
|
||
| public function test_evento_de_envio_registra_auditoria_no_banco(): void | ||
| { | ||
| config(['audit.enabled' => true, 'audit.console' => true]); | ||
|
|
||
| $sentAt = Carbon::parse('2026-12-02 10:30:00'); | ||
|
|
||
| EmailSentEvent::dispatch( | ||
| 'agente@example.com', | ||
| 'Resposta do recurso publicada', | ||
| PublishedRecourse::class, | ||
| $sentAt, | ||
| ['type' => 'published_recourse', 'recourse_id' => 789, 'agent_id' => 123456, 'opportunity_name' => 'Edital Teste'], | ||
| '<p>conteúdo</p>', | ||
| ); | ||
|
|
||
| $dispatch = EmailDispatch::query()->sole(); | ||
|
|
||
| $this->assertSame('agente@example.com', $dispatch->to); | ||
| $this->assertSame('Resposta do recurso publicada', $dispatch->subject); | ||
| $this->assertSame('<p>conteúdo</p>', $dispatch->content); | ||
| $this->assertSame(PublishedRecourse::class, $dispatch->mailable_type); | ||
| $this->assertSame('sent', $dispatch->status); | ||
| $this->assertSame( | ||
| ['type' => 'published_recourse', 'recourse_id' => 789, 'agent_id' => 123456, 'opportunity_name' => 'Edital Teste'], | ||
| $dispatch->meta, | ||
| ); | ||
| $this->assertTrue($dispatch->dispatched_at->equalTo($sentAt)); | ||
| $this->assertTrue($dispatch->attempted_at->equalTo($sentAt)); | ||
|
|
||
| $this->assertDatabaseHas('audits', [ | ||
| 'auditable_type' => EmailDispatch::class, | ||
| 'auditable_id' => $dispatch->id, | ||
| 'event' => 'created', | ||
| ]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,13 +3,18 @@ | |
| namespace Tests\Unit\Console\Commands; | ||
|
|
||
| use App\Console\Commands\ConsumePublishedRecourseEmails; | ||
| use App\Events\EmailSentEvent; | ||
| use App\Mail\PublishedRecourse; | ||
| use Illuminate\Console\OutputStyle; | ||
| use Illuminate\Support\Facades\Event; | ||
| use Illuminate\Support\Facades\Mail; | ||
| use Mockery; | ||
| use Mockery\MockInterface; | ||
| use PhpAmqpLib\Channel\AMQPChannel; | ||
| use PhpAmqpLib\Message\AMQPMessage; | ||
| use RuntimeException; | ||
| use Sentry\SentrySdk; | ||
| use Sentry\State\Hub; | ||
| use Symfony\Component\Process\Process; | ||
| use Tests\TestCase; | ||
|
|
||
|
|
@@ -50,10 +55,12 @@ public function test_send_email_true(): void | |
| $reflection = new \ReflectionClass($command); | ||
| $method = $reflection->getMethod('sendEmail'); | ||
| $method->setAccessible(true); | ||
| $method->invoke($command, json_decode($msgMock->body, true)); | ||
| $data = json_decode($msgMock->getBody(), true); | ||
| $mailable = new PublishedRecourse($data); | ||
| $send = $method->invoke($command, $data, $mailable); | ||
|
|
||
| // Asserts that the email was not sent | ||
| Mail::assertSent(PublishedRecourse::class); | ||
| $this->assertTrue($send); | ||
| Mail::assertSent(PublishedRecourse::class, fn ($mail) => $mail === $mailable); | ||
| } | ||
|
|
||
| public function test_send_email_false(): void | ||
|
|
@@ -73,7 +80,8 @@ public function test_send_email_false(): void | |
| $reflection = new \ReflectionClass($command); | ||
| $method = $reflection->getMethod('sendEmail'); | ||
| $method->setAccessible(true); | ||
| $send = $method->invoke($command, json_decode($msgMock->body, true)); | ||
| $data = json_decode($msgMock->getBody(), true); | ||
| $send = $method->invoke($command, $data, new PublishedRecourse($data)); | ||
|
|
||
| // Asserts that the email was not sent | ||
| $this->assertFalse($send); | ||
|
|
@@ -83,6 +91,7 @@ public function test_send_email_false(): void | |
| public function test_process_message_with_success(): void | ||
| { | ||
| Mail::fake(); | ||
| Event::fake([EmailSentEvent::class]); | ||
|
|
||
| $msgMock = $this->createMockAMQPMessage( | ||
| json_encode([ | ||
|
|
@@ -107,17 +116,92 @@ public function test_process_message_with_success(): void | |
| Mail::assertSent(PublishedRecourse::class); | ||
| } | ||
|
|
||
| // public function test_process_message_with_failure(): void | ||
| // { | ||
| // @TODO: Needs discovery | ||
| // } | ||
| public function test_process_message_dispara_evento_de_auditoria_apos_envio(): void | ||
| { | ||
| Mail::fake(); | ||
| Event::fake([EmailSentEvent::class]); | ||
| $this->freezeTime(); | ||
|
|
||
| $msgMock = $this->createMockAMQPMessage(json_encode([ | ||
| 'email' => 'test@example.com', | ||
| 'recourseId' => 789, | ||
| 'agentId' => 123456, | ||
| 'opportunityName' => 'Test Opportunity', | ||
| ])); | ||
| $msgMock->shouldReceive('ack')->once()->andReturnNull(); | ||
|
|
||
| $this->invokeProcessMessage($msgMock); | ||
|
|
||
| Event::assertDispatchedTimes(EmailSentEvent::class, 1); | ||
| Event::assertDispatched(EmailSentEvent::class, function (EmailSentEvent $event) { | ||
| return $event->to === 'test@example.com' | ||
| && $event->subject === 'Resposta do recurso publicada' | ||
| && $event->mailableType === PublishedRecourse::class | ||
| && $event->sentAt->equalTo(now()) | ||
| && $event->meta === [ | ||
| 'type' => 'published_recourse', | ||
| 'recourse_id' => 789, | ||
| 'agent_id' => 123456, | ||
| 'opportunity_name' => 'Test Opportunity', | ||
| ] | ||
| && str_contains((string) $event->content, 'Test Opportunity'); | ||
| }); | ||
| } | ||
|
|
||
| public function test_process_message_sem_envio_nao_dispara_evento_nem_ack(): void | ||
| { | ||
| Mail::fake(); | ||
| Event::fake([EmailSentEvent::class]); | ||
| SentrySdk::setCurrentHub(new Hub()); | ||
|
|
||
| // Sem a chave email o envio falha | ||
| $msgMock = $this->createMockAMQPMessage(json_encode(['agentId' => 123456])); | ||
| $msgMock->shouldNotReceive('ack'); | ||
|
|
||
| // A mensagem de erro do command também acessa $data['email'] | ||
| try { | ||
| $this->invokeProcessMessage($msgMock); | ||
| } catch (\ErrorException) { | ||
| } | ||
|
|
||
| Mail::assertNotSent(PublishedRecourse::class); | ||
| Event::assertNotDispatched(EmailSentEvent::class); | ||
| } | ||
|
|
||
| public function test_falha_na_auditoria_nao_interrompe_o_consumer(): void | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Colocar os testes em ingles mantendo o padrao |
||
| { | ||
| Mail::fake(); | ||
| SentrySdk::setCurrentHub(new Hub()); | ||
| Event::forget(EmailSentEvent::class); | ||
| Event::listen(EmailSentEvent::class, fn () => throw new RuntimeException('banco indisponível')); | ||
|
|
||
| $msgMock = $this->createMockAMQPMessage(json_encode([ | ||
| 'email' => 'test@example.com', | ||
| 'agentId' => 123456, | ||
| 'opportunityName' => 'Test Opportunity', | ||
| ])); | ||
| $msgMock->shouldReceive('ack')->once()->andReturnNull(); | ||
|
|
||
| $this->invokeProcessMessage($msgMock); | ||
|
|
||
| Mail::assertSent(PublishedRecourse::class); | ||
| } | ||
|
|
||
| private function invokeProcessMessage(MockInterface $msgMock): void | ||
| { | ||
| $command = new ConsumePublishedRecourseEmails; | ||
| $command->setOutput($this->createMock(OutputStyle::class)); | ||
|
|
||
| $method = (new \ReflectionClass($command))->getMethod('processMessage'); | ||
| $method->invoke($command, $msgMock); | ||
| } | ||
|
|
||
| private function createMockAMQPMessage(string $body): MockInterface | ||
| { | ||
| $channelMock = Mockery::mock(AMQPChannel::class); | ||
|
|
||
| $msgMock = Mockery::mock(AMQPMessage::class); | ||
| $msgMock->body = $body; | ||
| $msgMock->shouldReceive('getBody')->andReturn($body); | ||
| $msgMock->delivery_info = [ | ||
| 'channel' => $channelMock, | ||
| 'delivery_tag' => 1, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Evitar de mascarar erros errais. Usar
$this->expectException()