|
| 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\PreventSavingStacheItemsToDisk; |
| 10 | +use Tests\TestCase; |
| 11 | + |
| 12 | +class RateLimitingTest extends TestCase |
| 13 | +{ |
| 14 | + use PreventSavingStacheItemsToDisk; |
| 15 | + |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + parent::setUp(); |
| 19 | + Cache::flush(); |
| 20 | + } |
| 21 | + |
| 22 | + #[Test] |
| 23 | + public function login_endpoint_is_rate_limited() |
| 24 | + { |
| 25 | + collect(range(1, 4))->each(fn () => $this->post('/!/auth/login')->assertNotRateLimited()); |
| 26 | + $this->post('/!/auth/login')->assertRateLimited(); |
| 27 | + } |
| 28 | + |
| 29 | + #[Test] |
| 30 | + public function register_endpoint_is_rate_limited() |
| 31 | + { |
| 32 | + collect(range(1, 4))->each(fn () => $this->post('/!/auth/register')->assertNotRateLimited()); |
| 33 | + $this->post('/!/auth/register')->assertRateLimited(); |
| 34 | + } |
| 35 | + |
| 36 | + #[Test] |
| 37 | + public function password_email_endpoint_is_rate_limited() |
| 38 | + { |
| 39 | + collect(range(1, 4))->each(fn () => $this->post('/!/auth/password/email')->assertNotRateLimited()); |
| 40 | + $this->post('/!/auth/password/email')->assertRateLimited(); |
| 41 | + } |
| 42 | + |
| 43 | + #[Test] |
| 44 | + public function password_reset_endpoint_is_rate_limited() |
| 45 | + { |
| 46 | + collect(range(1, 4))->each(fn () => $this->post('/!/auth/password/reset')->assertNotRateLimited()); |
| 47 | + $this->post('/!/auth/password/reset')->assertRateLimited(); |
| 48 | + } |
| 49 | + |
| 50 | + #[Test] |
| 51 | + public function forms_endpoint_is_rate_limited() |
| 52 | + { |
| 53 | + collect(range(1, 10))->each(fn () => $this->post('/!/forms/contact')->assertNotRateLimited()); |
| 54 | + $this->post('/!/forms/contact')->assertRateLimited(); |
| 55 | + } |
| 56 | + |
| 57 | + #[Test] |
| 58 | + public function cp_login_endpoint_is_rate_limited() |
| 59 | + { |
| 60 | + collect(range(1, 4))->each(fn () => $this->post('/cp/auth/login')->assertNotRateLimited()); |
| 61 | + $this->post('/cp/auth/login')->assertRateLimited(); |
| 62 | + } |
| 63 | + |
| 64 | + #[Test] |
| 65 | + public function cp_password_email_endpoint_is_rate_limited() |
| 66 | + { |
| 67 | + collect(range(1, 4))->each(fn () => $this->post('/cp/auth/password/email')->assertNotRateLimited()); |
| 68 | + $this->post('/cp/auth/password/email')->assertRateLimited(); |
| 69 | + } |
| 70 | + |
| 71 | + #[Test] |
| 72 | + public function cp_password_reset_endpoint_is_rate_limited() |
| 73 | + { |
| 74 | + collect(range(1, 4))->each(fn () => $this->post('/cp/auth/password/reset')->assertNotRateLimited()); |
| 75 | + $this->post('/cp/auth/password/reset')->assertRateLimited(); |
| 76 | + } |
| 77 | + |
| 78 | + #[Test] |
| 79 | + public function cp_and_frontend_auth_have_independent_buckets() |
| 80 | + { |
| 81 | + collect(range(1, 4))->each(fn () => $this->post('/!/auth/login')->assertNotRateLimited()); |
| 82 | + $this->post('/!/auth/login')->assertRateLimited(); |
| 83 | + |
| 84 | + $this->post('/cp/auth/login')->assertNotRateLimited(); |
| 85 | + } |
| 86 | + |
| 87 | + #[Test] |
| 88 | + public function auth_rate_limiter_can_be_overridden() |
| 89 | + { |
| 90 | + // Simulate a developer overriding the default 4/min limit to 2/min |
| 91 | + RateLimiter::for('statamic.auth', fn ($request) => Limit::perMinute(2)->by($request->ip())); |
| 92 | + |
| 93 | + $this->post('/!/auth/login')->assertNotRateLimited(); |
| 94 | + $this->post('/!/auth/login')->assertNotRateLimited(); |
| 95 | + $this->post('/!/auth/login')->assertRateLimited(); |
| 96 | + } |
| 97 | + |
| 98 | + #[Test] |
| 99 | + public function cp_auth_rate_limiter_inherits_overrides_to_statamic_auth() |
| 100 | + { |
| 101 | + RateLimiter::for('statamic.auth', fn ($request) => Limit::perMinute(2)->by($request->ip())); |
| 102 | + |
| 103 | + $this->post('/cp/auth/login')->assertNotRateLimited(); |
| 104 | + $this->post('/cp/auth/login')->assertNotRateLimited(); |
| 105 | + $this->post('/cp/auth/login')->assertRateLimited(); |
| 106 | + } |
| 107 | + |
| 108 | + #[Test] |
| 109 | + public function cp_auth_rate_limiter_can_be_overridden_independently() |
| 110 | + { |
| 111 | + RateLimiter::for('statamic.cp.auth', fn ($request) => Limit::perMinute(2)->by($request->ip())); |
| 112 | + |
| 113 | + $this->post('/cp/auth/login')->assertNotRateLimited(); |
| 114 | + $this->post('/cp/auth/login')->assertNotRateLimited(); |
| 115 | + $this->post('/cp/auth/login')->assertRateLimited(); |
| 116 | + |
| 117 | + // Frontend auth still uses the default 4/min |
| 118 | + collect(range(1, 4))->each(fn () => $this->post('/!/auth/login')->assertNotRateLimited()); |
| 119 | + } |
| 120 | + |
| 121 | + #[Test] |
| 122 | + public function forms_rate_limiter_can_be_overridden() |
| 123 | + { |
| 124 | + // Simulate a developer overriding the default 10/min limit to 2/min |
| 125 | + RateLimiter::for('statamic.forms', fn ($request) => Limit::perMinute(2)->by($request->ip())); |
| 126 | + |
| 127 | + $this->post('/!/forms/contact')->assertNotRateLimited(); |
| 128 | + $this->post('/!/forms/contact')->assertNotRateLimited(); |
| 129 | + $this->post('/!/forms/contact')->assertRateLimited(); |
| 130 | + } |
| 131 | + |
| 132 | + #[Test] |
| 133 | + public function passkey_endpoint_is_rate_limited() |
| 134 | + { |
| 135 | + collect(range(1, 30))->each(fn () => $this->post('/!/auth/passkeys/auth')->assertNotRateLimited()); |
| 136 | + $this->post('/!/auth/passkeys/auth')->assertRateLimited(); |
| 137 | + } |
| 138 | + |
| 139 | + #[Test] |
| 140 | + public function cp_passkey_endpoint_is_rate_limited() |
| 141 | + { |
| 142 | + collect(range(1, 30))->each(fn () => $this->post('/cp/auth/passkeys')->assertNotRateLimited()); |
| 143 | + $this->post('/cp/auth/passkeys')->assertRateLimited(); |
| 144 | + } |
| 145 | + |
| 146 | + #[Test] |
| 147 | + public function cp_and_frontend_passkeys_have_independent_buckets() |
| 148 | + { |
| 149 | + RateLimiter::for('statamic.passkeys', fn ($request) => Limit::perMinute(2)->by($request->ip())); |
| 150 | + |
| 151 | + $this->post('/!/auth/passkeys/auth')->assertNotRateLimited(); |
| 152 | + $this->post('/!/auth/passkeys/auth')->assertNotRateLimited(); |
| 153 | + $this->post('/!/auth/passkeys/auth')->assertRateLimited(); |
| 154 | + |
| 155 | + $this->post('/cp/auth/passkeys')->assertNotRateLimited(); |
| 156 | + } |
| 157 | + |
| 158 | + #[Test] |
| 159 | + public function passkeys_bucket_is_independent_from_auth_bucket() |
| 160 | + { |
| 161 | + collect(range(1, 4))->each(fn () => $this->post('/!/auth/login')->assertNotRateLimited()); |
| 162 | + $this->post('/!/auth/login')->assertRateLimited(); |
| 163 | + |
| 164 | + $this->post('/!/auth/passkeys/auth')->assertNotRateLimited(); |
| 165 | + } |
| 166 | + |
| 167 | + #[Test] |
| 168 | + public function passkeys_rate_limiter_can_be_overridden() |
| 169 | + { |
| 170 | + RateLimiter::for('statamic.passkeys', fn ($request) => Limit::perMinute(2)->by($request->ip())); |
| 171 | + |
| 172 | + $this->post('/!/auth/passkeys/auth')->assertNotRateLimited(); |
| 173 | + $this->post('/!/auth/passkeys/auth')->assertNotRateLimited(); |
| 174 | + $this->post('/!/auth/passkeys/auth')->assertRateLimited(); |
| 175 | + } |
| 176 | + |
| 177 | + #[Test] |
| 178 | + public function cp_passkeys_rate_limiter_inherits_overrides_to_statamic_passkeys() |
| 179 | + { |
| 180 | + RateLimiter::for('statamic.passkeys', fn ($request) => Limit::perMinute(2)->by($request->ip())); |
| 181 | + |
| 182 | + $this->post('/cp/auth/passkeys')->assertNotRateLimited(); |
| 183 | + $this->post('/cp/auth/passkeys')->assertNotRateLimited(); |
| 184 | + $this->post('/cp/auth/passkeys')->assertRateLimited(); |
| 185 | + } |
| 186 | + |
| 187 | + #[Test] |
| 188 | + public function cp_passkeys_rate_limiter_can_be_overridden_independently() |
| 189 | + { |
| 190 | + RateLimiter::for('statamic.cp.passkeys', fn ($request) => Limit::perMinute(2)->by($request->ip())); |
| 191 | + |
| 192 | + $this->post('/cp/auth/passkeys')->assertNotRateLimited(); |
| 193 | + $this->post('/cp/auth/passkeys')->assertNotRateLimited(); |
| 194 | + $this->post('/cp/auth/passkeys')->assertRateLimited(); |
| 195 | + |
| 196 | + // Frontend passkey still uses the default 30/min |
| 197 | + collect(range(1, 30))->each(fn () => $this->post('/!/auth/passkeys/auth')->assertNotRateLimited()); |
| 198 | + } |
| 199 | +} |
0 commit comments