-
Notifications
You must be signed in to change notification settings - Fork 20
Issue/190 rate limit config and feature tests #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
armanist
merged 4 commits into
softberg:master
from
armanist:issue/190-rate-limit-config-and-feature-tests
May 13, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
035711c
[#190] Add shared rate limit config and API feature tests
armanist 6f393ef
[#190] Add feature test rate-limit storage cleanup and local path rep…
armanist e5e6200
[#190] Tighten rate limit test setup and assertions
armanist b5f8ae6
[#190] Stabilize rate-limit test storage setup
armanist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| return [ | ||
| /** | ||
| * --------------------------------------------------------- | ||
| * Default rate limit adapter | ||
| * --------------------------------------------------------- | ||
| */ | ||
| 'default' => env('RATE_LIMIT_ADAPTER', 'file'), | ||
|
|
||
| /** | ||
| * --------------------------------------------------------- | ||
| * Rate limit connections | ||
| * --------------------------------------------------------- | ||
| */ | ||
| 'file' => [ | ||
| 'prefix' => str_replace(' ', '', env('APP_NAME') ?? ''), | ||
| 'path' => base_dir() . DS . 'cache' . DS . 'data', | ||
| 'ttl' => 60, | ||
| ], | ||
| 'redis' => [ | ||
| 'prefix' => str_replace(' ', '', env('APP_NAME') ?? ''), | ||
| 'host' => env('REDIS_HOST', '127.0.0.1'), | ||
| 'port' => env('REDIS_PORT', 6379), | ||
| 'ttl' => 60, | ||
| ], | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?php | ||
|
|
||
| namespace Quantum\Tests\Feature\modules\Api; | ||
|
|
||
| use Quantum\Tests\Feature\AppTestCase; | ||
| use Quantum\Http\Response; | ||
|
|
||
| class RateLimitTest extends AppTestCase | ||
| { | ||
| public function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| $this->clearRateLimitStorage(); | ||
| } | ||
|
|
||
| public function tearDown(): void | ||
| { | ||
| $this->clearRateLimitStorage(); | ||
| parent::tearDown(); | ||
| } | ||
|
|
||
| public function testSignInEndpointReturns429WhenLimitExceeded(): void | ||
| { | ||
| for ($i = 0; $i < 10; $i++) { | ||
| $response = $this->attemptFailedSignin(); | ||
| $this->assertNotSame(429, $response->getStatusCode()); | ||
| } | ||
|
|
||
| $blocked = $this->attemptFailedSignin(); | ||
|
|
||
| $this->assertSame(429, $blocked->getStatusCode()); | ||
| $this->assertSame('10', $blocked->getHeader('X-RateLimit-Limit')); | ||
| $this->assertSame('0', $blocked->getHeader('X-RateLimit-Remaining')); | ||
| $this->assertNotNull($blocked->getHeader('Retry-After')); | ||
| $this->assertSame('Too Many Requests', $blocked->get('message')); | ||
| } | ||
|
|
||
| public function testRateLimitBucketIsRouteSpecific(): void | ||
| { | ||
| for ($i = 0; $i < 10; $i++) { | ||
| $this->attemptFailedSignin(); | ||
| } | ||
|
|
||
| $blocked = $this->attemptFailedSignin(); | ||
| $this->assertSame(429, $blocked->getStatusCode()); | ||
|
|
||
| response()->flush(); | ||
|
|
||
| $postsResponse = $this->request('get', '/api/en/posts'); | ||
| $this->assertSame('success', $postsResponse->get('status')); | ||
| $this->assertNotSame(429, $postsResponse->getStatusCode()); | ||
|
armanist marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private function attemptFailedSignin(): Response | ||
| { | ||
| response()->flush(); | ||
|
|
||
| return $this->request('post', '/api/en/signin', [ | ||
| 'email' => 'non-existing-user@example.com', | ||
| 'password' => 'invalid-password', | ||
| ]); | ||
| } | ||
|
|
||
| private function clearRateLimitStorage(): void | ||
| { | ||
| $rateLimitDir = PROJECT_ROOT . DS . 'cache' . DS . 'data'; | ||
|
|
||
| foreach (glob($rateLimitDir . DS . '*.rate') ?: [] as $file) { | ||
| @unlink($file); | ||
| } | ||
|
|
||
| foreach (glob($rateLimitDir . DS . '*.lock') ?: [] as $file) { | ||
| @unlink($file); | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| return [ | ||
| /** | ||
| * --------------------------------------------------------- | ||
| * Default rate limit adapter | ||
| * --------------------------------------------------------- | ||
| */ | ||
| 'default' => env('RATE_LIMIT_ADAPTER', 'file'), | ||
|
|
||
| /** | ||
| * --------------------------------------------------------- | ||
| * Rate limit connections | ||
| * --------------------------------------------------------- | ||
| */ | ||
| 'file' => [ | ||
| 'prefix' => str_replace(' ', '', env('APP_NAME') ?? ''), | ||
| 'path' => base_dir() . DS . 'cache' . DS . 'data', | ||
| 'ttl' => 60, | ||
| ], | ||
| 'redis' => [ | ||
| 'prefix' => str_replace(' ', '', env('APP_NAME') ?? ''), | ||
| 'host' => env('REDIS_HOST', '127.0.0.1'), | ||
| 'port' => env('REDIS_PORT', 6379), | ||
| 'ttl' => 60, | ||
| ], | ||
| ]; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.