diff --git a/tests/integration/api/Controller/CreateGroupControllerTest.php b/tests/integration/api/Controller/CreateGroupControllerTest.php deleted file mode 100644 index e3d72580a..000000000 --- a/tests/integration/api/Controller/CreateGroupControllerTest.php +++ /dev/null @@ -1,87 +0,0 @@ - 'flarumite', - 'namePlural' => 'flarumites', - 'icon' => 'test', - 'color' => null - ]; - - public function setUp() - { - parent::setUp(); - - $this->prepareDatabase([ - 'users' => [ - $this->adminUser(), - $this->normalUser(), - ], - 'groups' => [ - $this->adminGroup(), - ], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], - ], - ]); - } - - /** - * @test - */ - public function admin_cannot_create_group_without_data() - { - $this->actor = User::find(1); - - $this->assertEquals(422, $this->callWith()->getStatusCode()); - } - - /** - * @test - */ - public function admin_can_create_group() - { - $this->actor = User::find(1); - - $response = $this->callWith($this->data); - - $this->assertEquals(201, $response->getStatusCode()); - - $data = json_decode($response->getBody()->getContents(), true); - $group = Group::where('icon', $this->data['icon'])->firstOrFail(); - - foreach ($this->data as $property => $value) { - $this->assertEquals($value, Arr::get($data, "data.attributes.$property"), "$property not matching to json response"); - $property = Str::snake($property); - $this->assertEquals($value, $group->{$property}, "$property not matching to database result"); - } - } - - /** - * @test - */ - public function normal_user_cannot_create_group() - { - $this->actor = User::find(2); - - $this->assertEquals(403, $this->callWith($this->data)->getStatusCode()); - } -} diff --git a/tests/integration/api/groups/CreationTest.php b/tests/integration/api/groups/CreationTest.php new file mode 100644 index 000000000..d62371f59 --- /dev/null +++ b/tests/integration/api/groups/CreationTest.php @@ -0,0 +1,115 @@ +prepareDatabase([ + 'users' => [ + $this->adminUser(), + $this->normalUser(), + ], + 'groups' => [ + $this->adminGroup(), + ], + 'group_user' => [ + ['user_id' => 1, 'group_id' => 1], + ], + ]); + } + + /** + * @test + */ + public function admin_cannot_create_group_without_data() + { + $response = $this->send( + $this->request('POST', '/api/groups', [ + 'authenticatedAs' => 1, + 'json' => [], + ]) + ); + + $this->assertEquals(422, $response->getStatusCode()); + } + + /** + * @test + */ + public function admin_can_create_group() + { + $response = $this->send( + $this->request('POST', '/api/groups', [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'nameSingular' => 'flarumite', + 'namePlural' => 'flarumites', + 'icon' => 'test', + 'color' => null, + ], + ], + ], + ]) + ); + + $this->assertEquals(201, $response->getStatusCode()); + + // Verify API response body + $data = json_decode($response->getBody()->getContents(), true); + $this->assertEquals('flarumite', Arr::get($data, 'data.attributes.nameSingular')); + $this->assertEquals('flarumites', Arr::get($data, 'data.attributes.namePlural')); + $this->assertEquals('test', Arr::get($data, 'data.attributes.icon')); + $this->assertNull(Arr::get($data, 'data.attributes.color')); + + // Verify database entry + $group = Group::where('icon', 'test')->firstOrFail(); + $this->assertEquals('flarumite', $group->name_singular); + $this->assertEquals('flarumites', $group->name_plural); + $this->assertEquals('test', $group->icon); + $this->assertNull($group->color); + } + + /** + * @test + */ + public function normal_user_cannot_create_group() + { + $response = $this->send( + $this->request('POST', '/api/groups', [ + 'authenticatedAs' => 2, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'nameSingular' => 'flarumite', + 'namePlural' => 'flarumites', + 'icon' => 'test', + 'color' => null, + ], + ], + ], + ]) + ); + + $this->assertEquals(403, $response->getStatusCode()); + } +} diff --git a/tests/integration/api/Controller/ListGroupsControllerTest.php b/tests/integration/api/groups/ListTest.php similarity index 65% rename from tests/integration/api/Controller/ListGroupsControllerTest.php rename to tests/integration/api/groups/ListTest.php index 8cb90f123..591df53ca 100644 --- a/tests/integration/api/Controller/ListGroupsControllerTest.php +++ b/tests/integration/api/groups/ListTest.php @@ -7,21 +7,21 @@ * LICENSE file that was distributed with this source code. */ -namespace Flarum\Tests\integration\api\Controller; +namespace Flarum\Tests\integration\api\groups; -use Flarum\Api\Controller\ListGroupsController; use Flarum\Group\Group; +use Flarum\Tests\integration\TestCase; -class ListGroupsControllerTest extends ApiControllerTestCase +class ListTest extends TestCase { - protected $controller = ListGroupsController::class; - /** * @test */ public function shows_index_for_guest() { - $response = $this->callWith(); + $response = $this->send( + $this->request('GET', '/api/groups') + ); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($response->getBody()->getContents(), true); diff --git a/tests/integration/api/Controller/CreatePostControllerTest.php b/tests/integration/api/posts/CreationTest.php similarity index 56% rename from tests/integration/api/Controller/CreatePostControllerTest.php rename to tests/integration/api/posts/CreationTest.php index a388e3a61..fa12dd946 100644 --- a/tests/integration/api/Controller/CreatePostControllerTest.php +++ b/tests/integration/api/posts/CreationTest.php @@ -7,20 +7,15 @@ * LICENSE file that was distributed with this source code. */ -namespace Flarum\Tests\integration\api\Controller; +namespace Flarum\Tests\integration\api\posts; use Carbon\Carbon; -use Flarum\Api\Controller\CreatePostController; -use Flarum\User\User; -use Illuminate\Support\Arr; +use Flarum\Tests\integration\RetrievesAuthorizedUsers; +use Flarum\Tests\integration\TestCase; -class CreatePostControllerTest extends ApiControllerTestCase +class CreationTest extends TestCase { - protected $controller = CreatePostController::class; - - protected $data = [ - 'content' => 'reply with predetermined content for automated testing - too-obscure' - ]; + use RetrievesAuthorizedUsers; public function setUp() { @@ -51,13 +46,21 @@ class CreatePostControllerTest extends ApiControllerTestCase */ public function can_create_reply() { - $this->actor = User::find(2); - - $body = []; - Arr::set($body, 'data.attributes', $this->data); - Arr::set($body, 'data.relationships.discussion.data.id', 1); - - $response = $this->callWith($body); + $response = $this->send( + $this->request('POST', '/api/posts', [ + 'authenticatedAs' => 2, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'content' => 'reply with predetermined content for automated testing - too-obscure', + ], + 'relationships' => [ + 'discussion' => ['data' => ['id' => 1]], + ], + ], + ], + ]) + ); $this->assertEquals(201, $response->getStatusCode()); } diff --git a/tests/integration/api/Controller/UpdateUserControllerTest.php b/tests/integration/api/users/UpdateTest.php similarity index 71% rename from tests/integration/api/Controller/UpdateUserControllerTest.php rename to tests/integration/api/users/UpdateTest.php index 159c2f2e4..2e9beb6ec 100644 --- a/tests/integration/api/Controller/UpdateUserControllerTest.php +++ b/tests/integration/api/users/UpdateTest.php @@ -7,18 +7,14 @@ * LICENSE file that was distributed with this source code. */ -namespace Flarum\Tests\integration\api\Controller; +namespace Flarum\Tests\integration\api\users; -use Flarum\Api\Controller\UpdateUserController; -use Flarum\User\User; +use Flarum\Tests\integration\RetrievesAuthorizedUsers; +use Flarum\Tests\integration\TestCase; -class UpdateUserControllerTest extends ApiControllerTestCase +class UpdateTest extends TestCase { - protected $controller = UpdateUserController::class; - - protected $data = [ - 'email' => 'newemail@machine.local', - ]; + use RetrievesAuthorizedUsers; public function setUp() { @@ -48,9 +44,12 @@ class UpdateUserControllerTest extends ApiControllerTestCase */ public function users_can_see_their_private_information() { - $this->actor = User::find(2); - - $response = $this->callWith([], ['id' => 2]); + $response = $this->send( + $this->request('PATCH', '/api/users/2', [ + 'authenticatedAs' => 2, + 'json' => [], + ]) + ); // Test for successful response and that the email is included in the response $this->assertEquals(200, $response->getStatusCode()); @@ -62,9 +61,12 @@ class UpdateUserControllerTest extends ApiControllerTestCase */ public function users_can_not_see_other_users_private_information() { - $this->actor = User::find(2); - - $response = $this->callWith([], ['id' => 1]); + $response = $this->send( + $this->request('PATCH', '/api/users/1', [ + 'authenticatedAs' => 2, + 'json' => [], + ]) + ); // Make sure sensitive information is not made public $this->assertEquals(200, $response->getStatusCode());