Forgot password flow

This commit is contained in:
2026-07-27 09:43:00 -03:00
parent 7b565e7fa7
commit c37b8894e4
23 changed files with 1095 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
<?php
namespace Tests\Unit\Auth;
use App\Domains\Auth\Services\ResetPasswordAttemptService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use RuntimeException;
use Tests\TestCase;
class ResetPasswordAttemptServiceTest extends TestCase
{
public function test_it_logs_and_rethrows_unexpected_failures(): void
{
$exception = new RuntimeException('Database unavailable.');
DB::shouldReceive('transaction')
->once()
->andThrow($exception);
Log::shouldReceive('error')
->once()
->with(
'Failed to create password reset attempt.',
\Mockery::on(fn (array $context): bool => $context['email_fingerprint'] === 'b5fc85e55755'
&& $context['exception'] === $exception),
);
$this->expectExceptionObject($exception);
(new ResetPasswordAttemptService)->createForEmail('ada@example.com', 'tenant-test');
}
public function test_code_validation_logs_and_rethrows_unexpected_failures(): void
{
$exception = new RuntimeException('Database unavailable.');
DB::shouldReceive('transaction')
->once()
->andThrow($exception);
Log::shouldReceive('error')
->once()
->with(
'Failed to validate password reset code.',
\Mockery::on(fn (array $context): bool => $context['email_fingerprint'] === 'b5fc85e55755'
&& $context['exception'] === $exception),
);
$this->expectExceptionObject($exception);
(new ResetPasswordAttemptService)->validateCode('ada@example.com', '1234');
}
public function test_password_reset_logs_and_rethrows_unexpected_failures(): void
{
$exception = new RuntimeException('Database unavailable.');
DB::shouldReceive('transaction')
->once()
->andThrow($exception);
Log::shouldReceive('error')
->once()
->with(
'Failed to reset user password.',
\Mockery::on(fn (array $context): bool => $context['email_fingerprint'] === 'b5fc85e55755'
&& $context['exception'] === $exception),
);
$this->expectExceptionObject($exception);
(new ResetPasswordAttemptService)->resetPassword(
'ada@example.com',
'1234',
'NewSecret!456',
);
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Tests\Unit\Notification;
use App\Domains\Notification\Events\PasswordResetRequested;
use App\Domains\Notification\Listeners\SendPasswordResetEmail;
use App\Domains\Notification\Services\NotificationMailService;
use Illuminate\Support\Facades\Log;
use RuntimeException;
use Tests\TestCase;
class SendPasswordResetEmailTest extends TestCase
{
public function test_it_logs_and_rethrows_mail_failures(): void
{
$exception = new RuntimeException('SMTP unavailable.');
$mailService = \Mockery::mock(NotificationMailService::class);
$mailService->shouldReceive('sendPasswordResetCode')
->once()
->with(10, 'tenant-test')
->andThrow($exception);
$this->app->instance(NotificationMailService::class, $mailService);
Log::shouldReceive('error')
->once()
->with(
'Failed to send password reset email.',
\Mockery::on(fn (array $context): bool => $context['attempt_id'] === 10
&& $context['tenant_code'] === 'tenant-test'
&& $context['exception'] === $exception),
);
$this->expectExceptionObject($exception);
(new SendPasswordResetEmail)->handle(
new PasswordResetRequested(10, 'tenant-test'),
);
}
}