From e226f81515c8d6a832f13d4dfa96d00fe3dbbcba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Klabbers?= Date: Wed, 16 May 2018 09:25:48 +0200 Subject: [PATCH] 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 --- src/Discussion/DiscussionPolicy.php | 3 +- .../ShowDiscussionControllerTest.php | 86 +++++++++++++++++++ tests/Api/Controller/TokenControllerTest.php | 44 ++++++++++ tests/Test/Concerns/ManagesContent.php | 45 ++++++++++ .../Concerns/RetrievesAuthorizedUsers.php | 4 +- 5 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 tests/Api/Controller/ShowDiscussionControllerTest.php create mode 100644 tests/Api/Controller/TokenControllerTest.php create mode 100644 tests/Test/Concerns/ManagesContent.php diff --git a/src/Discussion/DiscussionPolicy.php b/src/Discussion/DiscussionPolicy.php index 269648638..3f002e291 100644 --- a/src/Discussion/DiscussionPolicy.php +++ b/src/Discussion/DiscussionPolicy.php @@ -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; } } diff --git a/tests/Api/Controller/ShowDiscussionControllerTest.php b/tests/Api/Controller/ShowDiscussionControllerTest.php new file mode 100644 index 000000000..5c4b8ae28 --- /dev/null +++ b/tests/Api/Controller/ShowDiscussionControllerTest.php @@ -0,0 +1,86 @@ + + * + * 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]); + } +} diff --git a/tests/Api/Controller/TokenControllerTest.php b/tests/Api/Controller/TokenControllerTest.php new file mode 100644 index 000000000..a350fa823 --- /dev/null +++ b/tests/Api/Controller/TokenControllerTest.php @@ -0,0 +1,44 @@ + + * + * 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); + } +} diff --git a/tests/Test/Concerns/ManagesContent.php b/tests/Test/Concerns/ManagesContent.php new file mode 100644 index 000000000..9d1d687cb --- /dev/null +++ b/tests/Test/Concerns/ManagesContent.php @@ -0,0 +1,45 @@ + + * + * 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; + } +} diff --git a/tests/Test/Concerns/RetrievesAuthorizedUsers.php b/tests/Test/Concerns/RetrievesAuthorizedUsers.php index c11e9c911..07f33b693 100644 --- a/tests/Test/Concerns/RetrievesAuthorizedUsers.php +++ b/tests/Test/Concerns/RetrievesAuthorizedUsers.php @@ -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([