Гордиенко Владислав Юрьевич
8 years ago
committed by
Dmitry Naumenko
5 changed files with 222 additions and 10 deletions
@ -0,0 +1,155 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace yiiunit\framework\filters; |
||||||
|
|
||||||
|
use Yii; |
||||||
|
use yiiunit\TestCase; |
||||||
|
use Prophecy\Argument; |
||||||
|
use yiiunit\framework\filters\stubs\RateLimit; |
||||||
|
use yii\web\User; |
||||||
|
use yii\web\Request; |
||||||
|
use yii\web\Response; |
||||||
|
use yii\log\Logger; |
||||||
|
use yii\filters\RateLimiter; |
||||||
|
|
||||||
|
/** |
||||||
|
* @group filters |
||||||
|
*/ |
||||||
|
class RateLimiterTest extends TestCase |
||||||
|
{ |
||||||
|
protected function setUp() |
||||||
|
{ |
||||||
|
parent::setUp(); |
||||||
|
|
||||||
|
/* @var $logger Logger|\Prophecy\ObjectProphecy */ |
||||||
|
$logger = $this->prophesize(Logger::className()); |
||||||
|
$logger |
||||||
|
->log(Argument::any(), Argument::any(), Argument::any()) |
||||||
|
->will(function ($parameters, $logger) { |
||||||
|
$logger->messages = $parameters; |
||||||
|
}); |
||||||
|
|
||||||
|
Yii::setLogger($logger->reveal()); |
||||||
|
|
||||||
|
$this->mockWebApplication(); |
||||||
|
} |
||||||
|
protected function tearDown() |
||||||
|
{ |
||||||
|
parent::tearDown(); |
||||||
|
Yii::setLogger(null); |
||||||
|
} |
||||||
|
|
||||||
|
public function testInitFilledRequest() |
||||||
|
{ |
||||||
|
$rateLimiter = new RateLimiter(['request' => 'Request']); |
||||||
|
|
||||||
|
$this->assertEquals('Request', $rateLimiter->request); |
||||||
|
} |
||||||
|
|
||||||
|
public function testInitNotFilledRequest() |
||||||
|
{ |
||||||
|
$rateLimiter = new RateLimiter(); |
||||||
|
|
||||||
|
$this->assertInstanceOf(Request::className(), $rateLimiter->request); |
||||||
|
} |
||||||
|
|
||||||
|
public function testInitFilledResponse() |
||||||
|
{ |
||||||
|
$rateLimiter = new RateLimiter(['response' => 'Response']); |
||||||
|
|
||||||
|
$this->assertEquals('Response', $rateLimiter->response); |
||||||
|
} |
||||||
|
|
||||||
|
public function testInitNotFilledResponse() |
||||||
|
{ |
||||||
|
$rateLimiter = new RateLimiter(); |
||||||
|
|
||||||
|
$this->assertInstanceOf(Response::className(), $rateLimiter->response); |
||||||
|
} |
||||||
|
|
||||||
|
public function testBeforeActionUserInstanceOfRateLimitInterface() |
||||||
|
{ |
||||||
|
$rateLimiter = new RateLimiter(); |
||||||
|
$rateLimit = new RateLimit(); |
||||||
|
$rateLimit->setAllowance([1, time()]) |
||||||
|
->setRateLimit([1, 1]); |
||||||
|
$rateLimiter->user = $rateLimit; |
||||||
|
|
||||||
|
$result = $rateLimiter->beforeAction('test'); |
||||||
|
|
||||||
|
$this->assertContains('Check rate limit', Yii::getLogger()->messages); |
||||||
|
$this->assertTrue($result); |
||||||
|
} |
||||||
|
|
||||||
|
public function testBeforeActionUserNotInstanceOfRateLimitInterface() |
||||||
|
{ |
||||||
|
$rateLimiter = new RateLimiter(['user' => 'User']); |
||||||
|
|
||||||
|
$result = $rateLimiter->beforeAction('test'); |
||||||
|
|
||||||
|
$this->assertContains('Rate limit skipped: "user" does not implement RateLimitInterface.', Yii::getLogger()->messages); |
||||||
|
$this->assertTrue($result); |
||||||
|
} |
||||||
|
|
||||||
|
public function testBeforeActionEmptyUser() |
||||||
|
{ |
||||||
|
$user = new User(['identityClass' => RateLimit::className()]); |
||||||
|
Yii::$app->set('user', $user); |
||||||
|
$rateLimiter = new RateLimiter(); |
||||||
|
|
||||||
|
$result = $rateLimiter->beforeAction('test'); |
||||||
|
|
||||||
|
$this->assertContains('Rate limit skipped: user not logged in.', Yii::getLogger()->messages); |
||||||
|
$this->assertTrue($result); |
||||||
|
} |
||||||
|
|
||||||
|
public function testCheckRateLimitTooManyRequests() |
||||||
|
{ |
||||||
|
/* @var $rateLimit UserIdentity|\Prophecy\ObjectProphecy */ |
||||||
|
$rateLimit = new RateLimit; |
||||||
|
$rateLimit |
||||||
|
->setRateLimit([1, 1]) |
||||||
|
->setAllowance([1, time() + 2]); |
||||||
|
$rateLimiter = new RateLimiter(); |
||||||
|
|
||||||
|
$this->setExpectedException('yii\web\TooManyRequestsHttpException'); |
||||||
|
$rateLimiter->checkRateLimit($rateLimit, Yii::$app->request, Yii::$app->response, 'testAction'); |
||||||
|
} |
||||||
|
|
||||||
|
public function testCheckRateaddRateLimitHeaders() |
||||||
|
{ |
||||||
|
/* @var $user UserIdentity|\Prophecy\ObjectProphecy */ |
||||||
|
$rateLimit = new RateLimit; |
||||||
|
$rateLimit |
||||||
|
->setRateLimit([1, 1]) |
||||||
|
->setAllowance([1, time()]); |
||||||
|
$rateLimiter = $this->getMockBuilder(RateLimiter::className()) |
||||||
|
->setMethods(['addRateLimitHeaders']) |
||||||
|
->getMock(); |
||||||
|
$rateLimiter->expects(self::at(0)) |
||||||
|
->method('addRateLimitHeaders') |
||||||
|
->willReturn(null); |
||||||
|
|
||||||
|
$rateLimiter->checkRateLimit($rateLimit, Yii::$app->request, Yii::$app->response, 'testAction'); |
||||||
|
} |
||||||
|
|
||||||
|
public function testAddRateLimitHeadersDisabledRateLimitHeaders() |
||||||
|
{ |
||||||
|
$rateLimiter = new RateLimiter(); |
||||||
|
$rateLimiter->enableRateLimitHeaders = false; |
||||||
|
$response = Yii::$app->response; |
||||||
|
|
||||||
|
$rateLimiter->addRateLimitHeaders($response, 1, 0, 0); |
||||||
|
$this->assertCount(0, $response->getHeaders()); |
||||||
|
} |
||||||
|
|
||||||
|
public function testAddRateLimitHeadersEnabledRateLimitHeaders() |
||||||
|
{ |
||||||
|
$rateLimiter = new RateLimiter(); |
||||||
|
$rateLimiter->enableRateLimitHeaders = true; |
||||||
|
$response = Yii::$app->response; |
||||||
|
|
||||||
|
$rateLimiter->addRateLimitHeaders($response, 1, 0, 0); |
||||||
|
$this->assertCount(3, $response->getHeaders()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,44 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace yiiunit\framework\filters\stubs; |
||||||
|
|
||||||
|
use yii\base\Object; |
||||||
|
use yii\filters\RateLimitInterface; |
||||||
|
|
||||||
|
class RateLimit extends Object implements RateLimitInterface |
||||||
|
{ |
||||||
|
private $_rateLimit; |
||||||
|
|
||||||
|
private $_allowance; |
||||||
|
|
||||||
|
public function getRateLimit($request, $action) |
||||||
|
{ |
||||||
|
return $this->_rateLimit; |
||||||
|
} |
||||||
|
|
||||||
|
public function setRateLimit($rateLimit) |
||||||
|
{ |
||||||
|
$this->_rateLimit = $rateLimit; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
public function loadAllowance($request, $action) |
||||||
|
{ |
||||||
|
return $this->_allowance; |
||||||
|
} |
||||||
|
|
||||||
|
public function setAllowance($allowance) |
||||||
|
{ |
||||||
|
$this->_allowance = $allowance; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public function saveAllowance($request, $action, $allowance, $timestamp) |
||||||
|
{ |
||||||
|
return [$action, $allowance, $timestamp]; |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue