1
0
mirror of https://github.com/flarum/core.git synced 2025-01-17 22:29:15 +01:00

Convert more controller tests to request tests

This commit is contained in:
Franz Liedke 2020-03-27 12:22:22 +01:00
parent bc7cea6e61
commit e3f1e69748
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
5 changed files with 158 additions and 125 deletions

View File

@ -1,87 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\api\Controller;
use Flarum\Api\Controller\CreateGroupController;
use Flarum\Group\Group;
use Flarum\User\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class CreateGroupControllerTest extends ApiControllerTestCase
{
protected $controller = CreateGroupController::class;
protected $data = [
'nameSingular' => '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());
}
}

View File

@ -0,0 +1,115 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\api\groups;
use Flarum\Group\Group;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Illuminate\Support\Arr;
class CreationTest extends TestCase
{
use RetrievesAuthorizedUsers;
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()
{
$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());
}
}

View File

@ -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);

View File

@ -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());
}

View File

@ -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());