feat(auth): enhance password reset attempt handling with new reasons and update related services

This commit is contained in:
2026-08-13 16:49:19 -03:00
parent 630b49cad7
commit a4a4c9afbc
9 changed files with 149 additions and 23 deletions

View File

@@ -3,13 +3,16 @@
namespace Tests\Feature\Auth;
use App\Domains\Auth\Models\LoginAttempt;
use App\Domains\Auth\Models\ResetPasswordAttempt;
use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\PermissionCode;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Authorization\Models\Permission;
use App\Domains\Authorization\Models\Role;
use App\Domains\Notification\Events\PasswordResetRequested;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
@@ -113,4 +116,51 @@ class ScannerLoginControllerTest extends TestCase
'outcome' => LoginAttempt::OUTCOME_INVALID_CREDENTIALS,
]);
}
public function test_locking_a_scanner_sends_the_scanner_password_reset_flow(): void
{
Event::fake([PasswordResetRequested::class]);
config([
'login-security.max_attempts' => 3,
'login-security.rate_limit_per_minute' => 100,
'login-security.ip_rate_limit_per_minute' => 100,
]);
$role = Role::query()->create([
'codigo' => RoleCode::Scanner->value,
'nombre' => 'Scanner',
]);
$permission = Permission::query()->create([
'codigo' => PermissionCode::ScanTickets->value,
'nombre' => 'Escanear tickets',
]);
$role->permissions()->attach($permission->codigo);
$tenant = Tenant::query()->create([
'codigo' => 'acme',
'nombre' => 'Acme',
'dominio' => 'acme.test',
]);
$user = User::factory()->create([
'email' => 'scanner@example.com',
'password' => Hash::make('correct-password'),
'rol_codigo' => $role->codigo,
'tenant_codigo' => $tenant->codigo,
]);
$payload = [
'email' => $user->email,
'password' => 'wrong-password',
];
$this->postJson('/api/v1/scanner/login', $payload)->assertUnprocessable();
$this->postJson('/api/v1/scanner/login', $payload)->assertUnprocessable();
$this->postJson('/api/v1/scanner/login', $payload)->assertTooManyRequests();
$attempt = $user->resetPasswordAttempts()->sole();
$this->assertSame(ResetPasswordAttempt::REASON_ACCOUNT_LOCKED, $attempt->reason);
Event::assertDispatched(
PasswordResetRequested::class,
fn (PasswordResetRequested $event): bool => $event->attemptId === $attempt->id
&& $event->tenantCode === $tenant->codigo
&& $event->channel === PasswordResetRequested::CHANNEL_SCANNER,
);
}
}