|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use Illuminate\Cache\RateLimiting\Limit; |
| 6 | +use Illuminate\Support\Facades\Cache; |
| 7 | +use Illuminate\Support\Facades\RateLimiter; |
| 8 | +use PHPUnit\Framework\Attributes\Test; |
| 9 | +use Tests\TestCase; |
| 10 | + |
| 11 | +class RateLimitingTest extends TestCase |
| 12 | +{ |
| 13 | + protected function setUp(): void |
| 14 | + { |
| 15 | + parent::setUp(); |
| 16 | + Cache::flush(); |
| 17 | + } |
| 18 | + |
| 19 | + private function assertNotRateLimited($response): void |
| 20 | + { |
| 21 | + $this->assertNotEquals(429, $response->getStatusCode(), 'Request was unexpectedly rate limited.'); |
| 22 | + } |
| 23 | + |
| 24 | + #[Test] |
| 25 | + public function login_endpoint_is_rate_limited() |
| 26 | + { |
| 27 | + collect(range(1, 4))->each(fn () => $this->assertNotRateLimited($this->post('/!/auth/login'))); |
| 28 | + $this->post('/!/auth/login')->assertStatus(429); |
| 29 | + } |
| 30 | + |
| 31 | + #[Test] |
| 32 | + public function register_endpoint_is_rate_limited() |
| 33 | + { |
| 34 | + collect(range(1, 4))->each(fn () => $this->assertNotRateLimited($this->post('/!/auth/register'))); |
| 35 | + $this->post('/!/auth/register')->assertStatus(429); |
| 36 | + } |
| 37 | + |
| 38 | + #[Test] |
| 39 | + public function password_email_endpoint_is_rate_limited() |
| 40 | + { |
| 41 | + collect(range(1, 4))->each(fn () => $this->assertNotRateLimited($this->post('/!/auth/password/email'))); |
| 42 | + $this->post('/!/auth/password/email')->assertStatus(429); |
| 43 | + } |
| 44 | + |
| 45 | + #[Test] |
| 46 | + public function password_reset_endpoint_is_rate_limited() |
| 47 | + { |
| 48 | + collect(range(1, 4))->each(fn () => $this->assertNotRateLimited($this->post('/!/auth/password/reset'))); |
| 49 | + $this->post('/!/auth/password/reset')->assertStatus(429); |
| 50 | + } |
| 51 | + |
| 52 | + #[Test] |
| 53 | + public function forms_endpoint_is_rate_limited() |
| 54 | + { |
| 55 | + collect(range(1, 10))->each(fn () => $this->assertNotRateLimited($this->post('/!/forms/contact'))); |
| 56 | + $this->post('/!/forms/contact')->assertStatus(429); |
| 57 | + } |
| 58 | + |
| 59 | + #[Test] |
| 60 | + public function auth_rate_limiter_can_be_overridden() |
| 61 | + { |
| 62 | + // Simulate a developer overriding the default 4/min limit to 2/min |
| 63 | + RateLimiter::for('statamic.auth', fn ($request) => Limit::perMinute(2)->by($request->ip())); |
| 64 | + |
| 65 | + $this->assertNotRateLimited($this->post('/!/auth/login')); |
| 66 | + $this->assertNotRateLimited($this->post('/!/auth/login')); |
| 67 | + $this->post('/!/auth/login')->assertStatus(429); |
| 68 | + } |
| 69 | + |
| 70 | + #[Test] |
| 71 | + public function forms_rate_limiter_can_be_overridden() |
| 72 | + { |
| 73 | + // Simulate a developer overriding the default 10/min limit to 2/min |
| 74 | + RateLimiter::for('statamic.forms', fn ($request) => Limit::perMinute(2)->by($request->ip())); |
| 75 | + |
| 76 | + $this->assertNotRateLimited($this->post('/!/forms/contact')); |
| 77 | + $this->assertNotRateLimited($this->post('/!/forms/contact')); |
| 78 | + $this->post('/!/forms/contact')->assertStatus(429); |
| 79 | + } |
| 80 | +} |
0 commit comments