nimuli/tests/Feature/Auth/LoginRateLimitTest.php

35 lines
1.2 KiB
PHP

<?php
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;
use Livewire\Volt\Volt;
it('throttles login after 5 failed attempts', function () {
$email = 'test@test.com';
// The throttle key LoginForm uses: Str::transliterate(Str::lower($email) . '|' . request()->ip())
// In the test environment request()->ip() returns '127.0.0.1'
$throttleKey = Str::transliterate(Str::lower($email) . '|127.0.0.1');
RateLimiter::clear($throttleKey);
// Make 5 failed attempts through the real Volt component
for ($i = 0; $i < 5; $i++) {
Volt::test('pages.auth.login')
->set('form.email', $email)
->set('form.password', 'wrong' . $i)
->call('login');
}
// 6th attempt must be rate-limited — error on form.email with throttle message
$component = Volt::test('pages.auth.login')
->set('form.email', $email)
->set('form.password', 'wrong6')
->call('login');
$component->assertHasErrors(['form.email']);
$errors = $component->errors();
expect($errors->has('form.email'))->toBeTrue();
expect($errors->first('form.email'))->toContain('Too many login attempts');
});