mirror of
https://github.com/flarum/framework.git
synced 2024-11-24 08:43:42 +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
09938f8633
commit
e226f81515
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
86
tests/Api/Controller/ShowDiscussionControllerTest.php
Normal file
86
tests/Api/Controller/ShowDiscussionControllerTest.php
Normal file
|
@ -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
tests/Api/Controller/TokenControllerTest.php
Normal file
44
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
tests/Test/Concerns/ManagesContent.php
Normal file
45
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