clusev/tests/Feature/BlockBannedIpMiddlewareTest...

59 lines
1.6 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\BannedIp;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
class BlockBannedIpMiddlewareTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Cache::flush();
}
private function banned(string $ip = '203.0.113.5'): void
{
BannedIp::create(['ip' => $ip, 'banned_until' => now()->addHour(), 'attempts' => 10]);
}
public function test_guest_from_banned_ip_is_blocked_with_403(): void
{
$this->banned();
$this->withServerVariables(['REMOTE_ADDR' => '203.0.113.5'])
->get('/login')
->assertStatus(403)
->assertSee(__('errors.blocked_title'));
}
public function test_guest_from_clean_ip_passes(): void
{
$this->banned();
$this->withServerVariables(['REMOTE_ADDR' => '198.51.100.9'])->get('/login')->assertStatus(200);
}
public function test_authenticated_user_from_banned_ip_passes(): void
{
$this->banned();
$user = User::factory()->create(['must_change_password' => false]);
$this->actingAs($user)
->withServerVariables(['REMOTE_ADDR' => '203.0.113.5'])
->get('/')
->assertSuccessful();
}
public function test_feature_disabled_lets_banned_ip_through(): void
{
Setting::put('bruteforce_enabled', '0');
$this->banned();
$this->withServerVariables(['REMOTE_ADDR' => '203.0.113.5'])->get('/login')->assertStatus(200);
}
}