|
6 | 6 | use Illuminate\Cache\RateLimiting\GlobalLimit;
|
7 | 7 | use Illuminate\Cache\RateLimiting\Limit;
|
8 | 8 | use Illuminate\Container\Container;
|
| 9 | +use Illuminate\Database\Eloquent\Model; |
| 10 | +use Illuminate\Foundation\Auth\User; |
| 11 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
9 | 12 | use Illuminate\Http\Exceptions\ThrottleRequestsException;
|
| 13 | +use Illuminate\Routing\Exceptions\MissingRateLimiterException; |
10 | 14 | use Illuminate\Routing\Middleware\ThrottleRequests;
|
11 | 15 | use Illuminate\Support\Carbon;
|
12 | 16 | use Illuminate\Support\Facades\Route;
|
13 | 17 | use Orchestra\Testbench\Attributes\WithConfig;
|
| 18 | +use Orchestra\Testbench\Attributes\WithMigration; |
14 | 19 | use Orchestra\Testbench\TestCase;
|
15 | 20 | use PHPUnit\Framework\Attributes\DataProvider;
|
16 | 21 | use Throwable;
|
17 | 22 |
|
18 | 23 | #[WithConfig('hashing.driver', 'bcrypt')]
|
| 24 | +#[WithMigration] |
19 | 25 | class ThrottleRequestsTest extends TestCase
|
20 | 26 | {
|
| 27 | + use RefreshDatabase; |
| 28 | + |
21 | 29 | public function testLockOpensImmediatelyAfterDecay()
|
22 | 30 | {
|
23 | 31 | Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 0));
|
@@ -233,4 +241,102 @@ public function testItCanThrottlePerSecond()
|
233 | 241 | $response = $this->get('/');
|
234 | 242 | $response->assertOk();
|
235 | 243 | }
|
| 244 | + |
| 245 | + public function testItFailsIfNamedLimiterDoesNotExist() |
| 246 | + { |
| 247 | + $this->expectException(MissingRateLimiterException::class); |
| 248 | + $this->expectExceptionMessage('Rate limiter [test] is not defined.'); |
| 249 | + |
| 250 | + Route::get('/', fn () => 'ok')->middleware(ThrottleRequests::using('test')); |
| 251 | + |
| 252 | + $this->withoutExceptionHandling()->get('/'); |
| 253 | + } |
| 254 | + |
| 255 | + public function testItFailsIfNamedLimiterDoesNotExistAndAuthenticatedUserDoesNotHaveFallbackProperty() |
| 256 | + { |
| 257 | + $this->expectException(MissingRateLimiterException::class); |
| 258 | + $this->expectExceptionMessage('Rate limiter [' . User::class . '::rateLimiting] is not defined.'); |
| 259 | + |
| 260 | + Route::get('/', fn () => 'ok')->middleware(['auth', ThrottleRequests::using('rateLimiting')]); |
| 261 | + |
| 262 | + // The reason we're enabling strict mode and actually creating a user is to ensure we never even try to access |
| 263 | + // a property within the user model that does not exist. If an application is in strict mode and there is |
| 264 | + // no matching rate limiter, it should throw a rate limiter exception, not a property access exception. |
| 265 | + Model::shouldBeStrict(); |
| 266 | + $user = User::forceCreate([ |
| 267 | + 'name' => 'Mateus', |
| 268 | + 'email' => 'mateus@example.org', |
| 269 | + 'password' => 'password', |
| 270 | + ]); |
| 271 | + |
| 272 | + $this->withoutExceptionHandling()->actingAs($user)->get('/'); |
| 273 | + } |
| 274 | + |
| 275 | + public function testItFallbacksToUserPropertyWhenThereIsNoNamedLimiterWhenAuthenticated() |
| 276 | + { |
| 277 | + $user = User::make()->forceFill([ |
| 278 | + 'rateLimiting' => 1, |
| 279 | + ]); |
| 280 | + |
| 281 | + Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 0)); |
| 282 | + |
| 283 | + // The `rateLimiting` named limiter does not exist, but the `rateLimiting` property on the |
| 284 | + // User model does, so it should fallback to that property within the authenticated model. |
| 285 | + Route::get('/', fn () => 'yes')->middleware(['auth', ThrottleRequests::using('rateLimiting')]); |
| 286 | + |
| 287 | + $response = $this->withoutExceptionHandling()->actingAs($user)->get('/'); |
| 288 | + $this->assertSame('yes', $response->getContent()); |
| 289 | + $this->assertEquals(1, $response->headers->get('X-RateLimit-Limit')); |
| 290 | + $this->assertEquals(0, $response->headers->get('X-RateLimit-Remaining')); |
| 291 | + |
| 292 | + Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 58)); |
| 293 | + |
| 294 | + try { |
| 295 | + $this->withoutExceptionHandling()->actingAs($user)->get('/'); |
| 296 | + } catch (Throwable $e) { |
| 297 | + $this->assertInstanceOf(ThrottleRequestsException::class, $e); |
| 298 | + $this->assertEquals(429, $e->getStatusCode()); |
| 299 | + $this->assertEquals(1, $e->getHeaders()['X-RateLimit-Limit']); |
| 300 | + $this->assertEquals(0, $e->getHeaders()['X-RateLimit-Remaining']); |
| 301 | + $this->assertEquals(2, $e->getHeaders()['Retry-After']); |
| 302 | + $this->assertEquals(Carbon::now()->addSeconds(2)->getTimestamp(), $e->getHeaders()['X-RateLimit-Reset']); |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + public function testItFallbacksToUserAccessorWhenThereIsNoNamedLimiterWhenAuthenticated() |
| 307 | + { |
| 308 | + $user = UserWithAcessor::make(); |
| 309 | + |
| 310 | + Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 0)); |
| 311 | + |
| 312 | + // The `rateLimiting` named limiter does not exist, but the `rateLimiting` accessor (not property!) |
| 313 | + // on the User model does, so it should fallback to that accessor within the authenticated model. |
| 314 | + Route::get('/', fn () => 'yes')->middleware(['auth', ThrottleRequests::using('rateLimiting')]); |
| 315 | + |
| 316 | + $response = $this->withoutExceptionHandling()->actingAs($user)->get('/'); |
| 317 | + $this->assertSame('yes', $response->getContent()); |
| 318 | + $this->assertEquals(1, $response->headers->get('X-RateLimit-Limit')); |
| 319 | + $this->assertEquals(0, $response->headers->get('X-RateLimit-Remaining')); |
| 320 | + |
| 321 | + Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 58)); |
| 322 | + |
| 323 | + try { |
| 324 | + $this->withoutExceptionHandling()->actingAs($user)->get('/'); |
| 325 | + } catch (Throwable $e) { |
| 326 | + $this->assertInstanceOf(ThrottleRequestsException::class, $e); |
| 327 | + $this->assertEquals(429, $e->getStatusCode()); |
| 328 | + $this->assertEquals(1, $e->getHeaders()['X-RateLimit-Limit']); |
| 329 | + $this->assertEquals(0, $e->getHeaders()['X-RateLimit-Remaining']); |
| 330 | + $this->assertEquals(2, $e->getHeaders()['Retry-After']); |
| 331 | + $this->assertEquals(Carbon::now()->addSeconds(2)->getTimestamp(), $e->getHeaders()['X-RateLimit-Reset']); |
| 332 | + } |
| 333 | + } |
| 334 | +} |
| 335 | + |
| 336 | +class UserWithAcessor extends User |
| 337 | +{ |
| 338 | + public function getRateLimitingAttribute(): int |
| 339 | + { |
| 340 | + return 1; |
| 341 | + } |
236 | 342 | }
|
0 commit comments