mirror of
https://github.com/flarum/framework.git
synced 2024-12-04 00:03:37 +08:00
additional tests for api controllers (#1433)
* added CreatePostControllerTest * added DeleteDiscussionControllerTest * added ListDiscussionControllerTest * added TokenControllerTest * minor improvement to policy, no need for Carbon object there, added ShowDiscussionControllerTest * added showDiscussionControllerTest but cant make Guests view the discussion created by a user * viewing for guests tested, we might need factories
This commit is contained in:
parent
f3bf16ca98
commit
ead24fbcb8
|
@ -11,7 +11,6 @@
|
|||
|
||||
namespace Flarum\Discussion;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Flarum\Event\ScopeModelVisibility;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\AbstractPolicy;
|
||||
|
@ -129,7 +128,7 @@ class DiscussionPolicy extends AbstractPolicy
|
|||
|
||||
if ($allowRenaming === '-1'
|
||||
|| ($allowRenaming === 'reply' && $discussion->participants_count <= 1)
|
||||
|| ($discussion->start_time->diffInMinutes(new Carbon) < $allowRenaming)) {
|
||||
|| ($discussion->start_time->diffInMinutes() < $allowRenaming)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\Api\Controller;
|
||||
|
||||
use Flarum\Api\Controller\ShowDiscussionController;
|
||||
use Flarum\Discussion\Discussion;
|
||||
use Flarum\Tests\Test\Concerns\ManagesContent;
|
||||
|
||||
class ShowDiscussionControllerTest extends ApiControllerTestCase
|
||||
{
|
||||
use ManagesContent;
|
||||
|
||||
protected $controller = ShowDiscussionController::class;
|
||||
|
||||
/**
|
||||
* @var Discussion
|
||||
*/
|
||||
protected $discussion;
|
||||
|
||||
protected function init()
|
||||
{
|
||||
$this->discussion = Discussion::start(__CLASS__, $this->getNormalUser());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function author_can_see_discussion()
|
||||
{
|
||||
$this->discussion->save();
|
||||
|
||||
$this->actor = $this->getNormalUser();
|
||||
|
||||
$response = $this->callWith([], ['id' => $this->discussion->id]);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
*/
|
||||
public function guest_cannot_see_empty_discussion()
|
||||
{
|
||||
$this->discussion->save();
|
||||
|
||||
$response = $this->callWith([], ['id' => $this->discussion->id]);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function guest_can_see_discussion()
|
||||
{
|
||||
$this->discussion->save();
|
||||
|
||||
$this->addPostByNormalUser();
|
||||
|
||||
$response = $this->callWith([], ['id' => $this->discussion->id]);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
*/
|
||||
public function guests_cannot_see_private_discussion()
|
||||
{
|
||||
$this->discussion->is_private = true;
|
||||
$this->discussion->save();
|
||||
|
||||
$this->callWith([], ['id' => $this->discussion->id]);
|
||||
}
|
||||
}
|
44
framework/core/tests/Api/Controller/TokenControllerTest.php
Normal file
44
framework/core/tests/Api/Controller/TokenControllerTest.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\Api\Controller;
|
||||
|
||||
use Flarum\Api\Controller\TokenController;
|
||||
use Flarum\Http\AccessToken;
|
||||
use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers;
|
||||
|
||||
class TokenControllerTest extends ApiControllerTestCase
|
||||
{
|
||||
use RetrievesAuthorizedUsers;
|
||||
|
||||
protected $controller = TokenController::class;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function user_generates_token()
|
||||
{
|
||||
$user = $this->getNormalUser();
|
||||
|
||||
$response = $this->call($this->controller, null, [], [
|
||||
'identification' => $user->username,
|
||||
'password' => $this->userAttributes['password']
|
||||
]);
|
||||
|
||||
$data = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
$this->assertEquals($user->id, $data['userId']);
|
||||
|
||||
$token = $data['token'];
|
||||
|
||||
$this->assertEquals($user->id, AccessToken::findOrFail($token)->user_id);
|
||||
}
|
||||
}
|
45
framework/core/tests/Test/Concerns/ManagesContent.php
Normal file
45
framework/core/tests/Test/Concerns/ManagesContent.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\Test\Concerns;
|
||||
|
||||
use Flarum\Post\CommentPost;
|
||||
use Flarum\Post\Event\Posted;
|
||||
|
||||
trait ManagesContent
|
||||
{
|
||||
use RetrievesAuthorizedUsers;
|
||||
|
||||
protected function addPostByNormalUser(): CommentPost
|
||||
{
|
||||
$actor = $this->getNormalUser();
|
||||
|
||||
$post = CommentPost::reply(
|
||||
$this->discussion->id,
|
||||
'a normal reply - too-obscure',
|
||||
$actor->id,
|
||||
'127.0.0.1'
|
||||
);
|
||||
|
||||
$post->save();
|
||||
|
||||
if (! $this->discussion->startPost) {
|
||||
$this->discussion->setStartPost($post);
|
||||
$this->discussion->setLastPost($post);
|
||||
|
||||
$this->discussion->save();
|
||||
|
||||
event(new Posted($post, $actor));
|
||||
}
|
||||
|
||||
return $post;
|
||||
}
|
||||
}
|
|
@ -21,12 +21,12 @@ trait RetrievesAuthorizedUsers
|
|||
'email' => 'normal@machine.local'
|
||||
];
|
||||
|
||||
public function getAdminUser()
|
||||
public function getAdminUser(): User
|
||||
{
|
||||
return User::find(1);
|
||||
}
|
||||
|
||||
public function getNormalUser()
|
||||
public function getNormalUser(): User
|
||||
{
|
||||
return User::unguarded(function () {
|
||||
return User::firstOrCreate([
|
||||
|
|
Loading…
Reference in New Issue
Block a user