Files
portal-api/tests/Feature/Api/Admin/LoginThrottleTest.php
T
2026-09-27 11:42:40 +07:00

41 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\Admin;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
class LoginThrottleTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
// Đảm bảo state rate-limit sạch, không bị ảnh hưởng bởi request từ test khác
// chạy cùng tiến trình PHPUnit (cache 'array' không tự reset giữa các test file).
Cache::flush();
}
public function test_admin_login_is_throttled_after_5_attempts_per_minute(): void
{
for ($i = 0; $i < 5; $i++) {
$response = $this->postJson('/api/v1/administrator/login', [
'username' => 'nobody',
'password' => 'wrong-password',
]);
$this->assertNotSame(429, $response->status());
}
$response = $this->postJson('/api/v1/administrator/login', [
'username' => 'nobody',
'password' => 'wrong-password',
]);
$response->assertStatus(429);
}
}