Forgot password flow
This commit is contained in:
79
tests/Unit/Auth/ResetPasswordAttemptServiceTest.php
Normal file
79
tests/Unit/Auth/ResetPasswordAttemptServiceTest.php
Normal 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',
|
||||
);
|
||||
}
|
||||
}
|
||||
39
tests/Unit/Notification/SendPasswordResetEmailTest.php
Normal file
39
tests/Unit/Notification/SendPasswordResetEmailTest.php
Normal 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'),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user