mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Добавлен рейт-лимитер для запросов к hasJoined для незарегистрированных серверов
This commit is contained in:
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
namespace tests\codeception\api\unit\modules\session\filters;
|
||||
|
||||
use api\modules\session\filters\RateLimiter;
|
||||
use common\models\OauthClient;
|
||||
use Faker\Provider\Internet;
|
||||
use tests\codeception\api\unit\TestCase;
|
||||
use Yii;
|
||||
use yii\redis\Connection;
|
||||
use yii\web\Request;
|
||||
|
||||
class RateLimiterTest extends TestCase {
|
||||
|
||||
public function testCheckRateLimiterWithValidServerId() {
|
||||
/** @var Connection|\PHPUnit_Framework_MockObject_MockObject $redis */
|
||||
$redis = $this->getMockBuilder(Connection::class)
|
||||
->setMethods(['executeCommand'])
|
||||
->getMock();
|
||||
|
||||
$redis->expects($this->never())
|
||||
->method('executeCommand');
|
||||
|
||||
Yii::$app->set('redis', $redis);
|
||||
|
||||
/** @var RateLimiter|\PHPUnit_Framework_MockObject_MockObject $filter */
|
||||
$filter = $this->getMockBuilder(RateLimiter::class)
|
||||
->setMethods(['getServer'])
|
||||
->getMock();
|
||||
|
||||
$filter->expects($this->any())
|
||||
->method('getServer')
|
||||
->will($this->returnValue(new OauthClient()));
|
||||
|
||||
$filter->checkRateLimit(null, new Request(), null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \yii\web\TooManyRequestsHttpException
|
||||
*/
|
||||
public function testCheckRateLimiter() {
|
||||
/** @var Connection|\PHPUnit_Framework_MockObject_MockObject $redis */
|
||||
$redis = $this->getMockBuilder(Connection::class)
|
||||
->setMethods(['executeCommand'])
|
||||
->getMock();
|
||||
|
||||
$redis->expects($this->exactly(5))
|
||||
->method('executeCommand')
|
||||
->will($this->onConsecutiveCalls('1', '1', '2', '3', '4'));
|
||||
|
||||
Yii::$app->set('redis', $redis);
|
||||
|
||||
/** @var Request|\PHPUnit_Framework_MockObject_MockObject $request */
|
||||
$request = $this->getMockBuilder(Request::class)
|
||||
->setMethods(['getUserIP'])
|
||||
->getMock();
|
||||
|
||||
$request->expects($this->any())
|
||||
->method('getUserIp')
|
||||
->will($this->returnValue(Internet::localIpv4()));
|
||||
|
||||
/** @var RateLimiter|\PHPUnit_Framework_MockObject_MockObject $filter */
|
||||
$filter = $this->getMockBuilder(RateLimiter::class)
|
||||
->setConstructorArgs([[
|
||||
'limit' => 3,
|
||||
]])
|
||||
->setMethods(['getServer'])
|
||||
->getMock();
|
||||
|
||||
$filter->expects($this->any())
|
||||
->method('getServer')
|
||||
->will($this->returnValue(null));
|
||||
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
$filter->checkRateLimit(null, $request, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user