2023-03-16 15:57:22 +01:00
|
|
|
<?php
|
|
|
|
|
2023-03-17 17:54:05 +01:00
|
|
|
namespace Tests\Api\v1;
|
2023-03-16 15:57:22 +01:00
|
|
|
|
|
|
|
use App\Models\User;
|
2023-08-01 11:28:27 +02:00
|
|
|
use App\Providers\RouteServiceProvider;
|
2023-03-16 15:57:22 +01:00
|
|
|
use Illuminate\Support\Facades\Config;
|
2023-08-01 11:28:27 +02:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
2024-06-26 14:29:13 +02:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2023-03-16 15:57:22 +01:00
|
|
|
use Tests\FeatureTestCase;
|
|
|
|
|
|
|
|
/**
|
2023-08-01 11:28:27 +02:00
|
|
|
* ThrottlingTest test class
|
2023-03-16 15:57:22 +01:00
|
|
|
*/
|
2023-08-01 11:28:27 +02:00
|
|
|
#[CoversClass(RouteServiceProvider::class)]
|
2023-03-16 15:57:22 +01:00
|
|
|
class ThrottlingTest extends FeatureTestCase
|
|
|
|
{
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-16 15:57:22 +01:00
|
|
|
public function test_api_calls_are_throttled_using_config()
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
|
|
|
|
*/
|
2023-03-17 17:54:05 +01:00
|
|
|
$user = User::factory()->create();
|
2023-03-16 15:57:22 +01:00
|
|
|
$throttle = 5;
|
|
|
|
|
|
|
|
Config::set('2fauth.api.throttle', $throttle);
|
|
|
|
|
|
|
|
$this->actingAs($user, 'api-guard');
|
|
|
|
|
2023-03-17 17:54:05 +01:00
|
|
|
for ($i = 0; $i < $throttle - 1; $i++) {
|
2023-03-16 15:57:22 +01:00
|
|
|
$this->json('GET', '/api/v1/twofaccounts/count');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->json('GET', '/api/v1/twofaccounts/count')
|
|
|
|
->assertOk();
|
|
|
|
|
|
|
|
$this->json('GET', '/api/v1/twofaccounts/count')
|
|
|
|
->assertStatus(429);
|
|
|
|
}
|
|
|
|
}
|