From df32275fd9510ef3e2234da5c18aa28961b99249 Mon Sep 17 00:00:00 2001 From: boban Date: Sat, 20 Jun 2026 17:40:40 +0200 Subject: [PATCH] test(auth): BruteforceGuard record/ban/unban/cache Co-Authored-By: Claude Sonnet 4.6 --- tests/Feature/BruteforceGuardBanTest.php | 76 ++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tests/Feature/BruteforceGuardBanTest.php diff --git a/tests/Feature/BruteforceGuardBanTest.php b/tests/Feature/BruteforceGuardBanTest.php new file mode 100644 index 0000000..b7e9920 --- /dev/null +++ b/tests/Feature/BruteforceGuardBanTest.php @@ -0,0 +1,76 @@ +guard(); + Setting::put('bruteforce_maxretry', '3'); + + $g->record('203.0.113.5', 'login'); + $g->record('203.0.113.5', 'login'); + $this->assertFalse($g->isBanned('203.0.113.5')); + + $g->record('203.0.113.5', 'login'); // 3rd → ban + $this->assertTrue($g->isBanned('203.0.113.5')); + $this->assertDatabaseHas('banned_ips', ['ip' => '203.0.113.5', 'reason' => 'login']); + $this->assertDatabaseHas('audit_events', ['action' => 'auth.ip_banned', 'ip' => '203.0.113.5']); + } + + public function test_exempt_ip_is_never_banned(): void + { + $g = $this->guard(); + Setting::put('bruteforce_maxretry', '1'); + for ($i = 0; $i < 5; $i++) { + $g->record('192.168.1.50', 'login'); // private = default whitelist + } + $this->assertFalse($g->isBanned('192.168.1.50')); + $this->assertDatabaseCount('banned_ips', 0); + } + + public function test_disabled_records_nothing(): void + { + Setting::put('bruteforce_enabled', '0'); + Setting::put('bruteforce_maxretry', '1'); + $this->guard()->record('203.0.113.9', 'login'); + $this->assertDatabaseCount('banned_ips', 0); + } + + public function test_unban_clears_row_and_cache(): void + { + $g = $this->guard(); + BannedIp::create(['ip' => '203.0.113.5', 'banned_until' => now()->addHour(), 'attempts' => 10]); + $this->assertTrue($g->isBanned('203.0.113.5')); // populates cache + $g->unban('203.0.113.5'); + $this->assertFalse($g->isBanned('203.0.113.5')); // no stale positive + $this->assertDatabaseCount('banned_ips', 0); + } + + public function test_expired_ban_is_not_active(): void + { + BannedIp::create(['ip' => '203.0.113.5', 'banned_until' => now()->subMinute(), 'attempts' => 10]); + $this->assertFalse($this->guard()->isBanned('203.0.113.5')); + } +}