From 9438534232e01d606e6aff8d18fd9635e3dc98bb Mon Sep 17 00:00:00 2001 From: Daniel Klabbers Date: Fri, 13 Apr 2018 07:52:39 +0200 Subject: [PATCH] adds api controller tests --- .../Api/Controller/ApiControllerTestCase.php | 46 ++++++++ .../CreateGroupControllerTestTestCase.php | 79 ++++++++++++++ .../CreateUserControllerTestTestCase.php | 102 ++++++++++++++++++ 3 files changed, 227 insertions(+) create mode 100644 framework/core/tests/Api/Controller/ApiControllerTestCase.php create mode 100644 framework/core/tests/Api/Controller/CreateGroupControllerTestTestCase.php create mode 100644 framework/core/tests/Api/Controller/CreateUserControllerTestTestCase.php diff --git a/framework/core/tests/Api/Controller/ApiControllerTestCase.php b/framework/core/tests/Api/Controller/ApiControllerTestCase.php new file mode 100644 index 000000000..61172d212 --- /dev/null +++ b/framework/core/tests/Api/Controller/ApiControllerTestCase.php @@ -0,0 +1,46 @@ + + * + * 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\Http\Controller\ControllerInterface; +use Flarum\Tests\Test\TestCase; +use Flarum\User\User; +use Psr\Http\Message\ResponseInterface; + +abstract class ApiControllerTestCase extends TestCase +{ + /** + * @var ControllerInterface + */ + protected $controller; + + /** + * @var null|User + */ + protected $actor = null; + + protected function callWith(array $body = []): ResponseInterface + { + return $this->call( + $this->controller, + $this->actor, + [], + $body ? ['data' => ['attributes' => $body]] : [] + ); + } + + protected function tearDown() + { + $this->actor = null; + parent::tearDown(); + } +} diff --git a/framework/core/tests/Api/Controller/CreateGroupControllerTestTestCase.php b/framework/core/tests/Api/Controller/CreateGroupControllerTestTestCase.php new file mode 100644 index 000000000..bf6399642 --- /dev/null +++ b/framework/core/tests/Api/Controller/CreateGroupControllerTestTestCase.php @@ -0,0 +1,79 @@ + + * + * 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\CreateGroupController; +use Flarum\Group\Group; +use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers; +use Illuminate\Support\Str; + +class CreateGroupControllerTestTestCase extends ApiControllerTestCase +{ + use RetrievesAuthorizedUsers; + + protected $controller = CreateGroupController::class; + + protected $data = [ + 'nameSingular' => 'flarumite', + 'namePlural' => 'flarumites', + 'icon' => 'test', + 'color' => null + ]; + + /** + * @test + * @expectedException \Illuminate\Validation\ValidationException + * @expectedExceptionMessage The given data was invalid. + */ + public function admin_cannot_create_group_without_data() + { + $this->actor = $this->getAdminUser(); + + $this->callWith(); + } + + /** + * @test + */ + public function admin_can_create_group() + { + $this->actor = $this->getAdminUser(); + + $response = $this->callWith($this->data); + + $this->assertEquals(201, $response->getStatusCode()); + + $group = Group::where('icon', $this->data['icon'])->firstOrFail(); + + foreach ($this->data as $property => $value) { + $property = Str::snake($property); + $this->assertEquals($value, $group->{$property}); + } + } + + /** + * @test + * @expectedException \Flarum\User\Exception\PermissionDeniedException + */ + public function unauthorized_user_cannot_create_group() + { + $this->actor = $this->getNormalUser(); + + $this->callWith($this->data); + } + + public function tearDown() + { + Group::where('icon', $this->data['icon'])->delete(); + parent::tearDown(); + } +} diff --git a/framework/core/tests/Api/Controller/CreateUserControllerTestTestCase.php b/framework/core/tests/Api/Controller/CreateUserControllerTestTestCase.php new file mode 100644 index 000000000..e0f85bf2d --- /dev/null +++ b/framework/core/tests/Api/Controller/CreateUserControllerTestTestCase.php @@ -0,0 +1,102 @@ + + * + * 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\CreateUserController; +use Flarum\Settings\SettingsRepositoryInterface; +use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers; +use Flarum\User\User; +use Illuminate\Support\Arr; + +class CreateUserControllerTestTestCase extends ApiControllerTestCase +{ + use RetrievesAuthorizedUsers; + + protected $controller = CreateUserController::class; + + protected $data = [ + 'username' => 'test', + 'password' => 'too-obscure', + 'email' => 'test@machine.local' + ]; + + /** + * @test + * @expectedException \Illuminate\Validation\ValidationException + * @expectedExceptionMessage The given data was invalid. + */ + public function cannot_create_user_without_data() + { + $this->callWith(); + } + + /** + * @test + */ + public function can_create_user() + { + $response = $this->callWith($this->data); + + $this->assertEquals(201, $response->getStatusCode()); + + /** @var User $user */ + $user = User::where('username', 'test')->firstOrFail(); + + $this->assertEquals(0, $user->is_activated); + + foreach (Arr::except($this->data, 'password') as $property => $value) { + $this->assertEquals($value, $user->{$property}); + } + } + + /** + * @test + */ + public function admins_can_create_activated_users() + { + $this->actor = $this->getAdminUser(); + + $response = $this->callWith(array_merge($this->data, [ + 'isActivated' => 1 + ])); + + $this->assertEquals(201, $response->getStatusCode()); + + /** @var User $user */ + $user = User::where('username', 'test')->firstOrFail(); + + $this->assertEquals(1, $user->is_activated); + } + + /** + * @test + * @expectedException \Flarum\User\Exception\PermissionDeniedException + */ + public function disabling_sign_up_prevents_user_creation() + { + /** @var SettingsRepositoryInterface $settings */ + $settings = $this->app->make(SettingsRepositoryInterface::class); + $settings->set('allow_sign_up', false); + + try { + $this->callWith($this->data); + } finally { + $settings->set('allow_sign_up', true); + } + } + + public function tearDown() + { + User::where('username', $this->data['username'])->delete(); + parent::tearDown(); + } +}