mirror of
https://github.com/flarum/framework.git
synced 2024-12-05 09:03:36 +08:00
ae280016e7
Previously, the `prepareDatabase` method would directly modify the database, booting the app in the process. This would prevent any extenders from being applied, since `->extend()` has no effect once the app is booted. Since the new implementation of `prepareDatabase` simply registers seed data to be applied during app boot, the workaround of sticking this seed data into `prepDb` is no longer necessary, and seed data common to all test cases in a class can be provided in `setUp`. When needed, app boot is explicitly triggered in individual test cases by calling `$this->app()`.
83 lines
2.2 KiB
PHP
83 lines
2.2 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;
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->prepareDatabase([
|
|
'users' => [
|
|
$this->normalUser(),
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function list_discussions_not_restricted_by_default()
|
|
{
|
|
$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;
|
|
}
|
|
}),
|
|
(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());
|
|
}
|
|
}
|