mirror of
https://github.com/flarum/framework.git
synced 2024-12-03 07:33:36 +08:00
86 lines
2.4 KiB
PHP
86 lines
2.4 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Flarum.
|
|
*
|
|
* For detailed copyright and license information, please view the
|
|
* LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Flarum\Tests\integration\extenders;
|
|
|
|
use Flarum\Extend;
|
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
|
use Flarum\Tests\integration\TestCase;
|
|
|
|
class ThrottleApiTest extends TestCase
|
|
{
|
|
use RetrievesAuthorizedUsers;
|
|
|
|
protected function prepDb(): void
|
|
{
|
|
$this->prepareDatabase([
|
|
'users' => [
|
|
$this->normalUser(),
|
|
],
|
|
'groups' => [
|
|
$this->memberGroup(),
|
|
],
|
|
'group_user' => [
|
|
['user_id' => 2, 'group_id' => 3],
|
|
],
|
|
'group_permission' => [
|
|
['permission' => 'viewDiscussions', 'group_id' => 3],
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function list_discussions_not_restricted_by_default()
|
|
{
|
|
$this->prepDb();
|
|
|
|
$response = $this->send($this->request('GET', '/api/discussions', ['authenticatedAs' => 2]));
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function list_discussions_can_be_restricted()
|
|
{
|
|
$this->extend((new Extend\ThrottleApi)->set('blockListDiscussions', function ($request) {
|
|
if ($request->getAttribute('routeName') === 'discussions.index') {
|
|
return true;
|
|
}
|
|
}));
|
|
$response = $this->send($this->request('GET', '/api/discussions', ['authenticatedAs' => 2]));
|
|
|
|
$this->assertEquals(429, $response->getStatusCode());
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function false_overrides_true_for_evaluating_throttlers()
|
|
{
|
|
$this->extend((new Extend\ThrottleApi)->set('blockListDiscussions', function ($request) {
|
|
if ($request->getAttribute('routeName') === 'discussions.index') {
|
|
return true;
|
|
}
|
|
}));
|
|
$this->extend((new Extend\ThrottleApi)->set('blockListDiscussionsOverride', function ($request) {
|
|
if ($request->getAttribute('routeName') === 'discussions.index') {
|
|
return false;
|
|
}
|
|
}));
|
|
|
|
$response = $this->send($this->request('GET', '/api/discussions', ['authenticatedAs' => 2]));
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
}
|
|
}
|