mirror of
https://github.com/flarum/framework.git
synced 2025-03-10 20:31:24 +08:00
Convert more controller tests to request tests
This commit is contained in:
parent
b0fd63adc3
commit
4e2f91fa00
@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
115
framework/core/tests/integration/api/groups/CreationTest.php
Normal file
115
framework/core/tests/integration/api/groups/CreationTest.php
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
@ -7,21 +7,21 @@
|
|||||||
* LICENSE file that was distributed with this source code.
|
* 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\Group\Group;
|
||||||
|
use Flarum\Tests\integration\TestCase;
|
||||||
|
|
||||||
class ListGroupsControllerTest extends ApiControllerTestCase
|
class ListTest extends TestCase
|
||||||
{
|
{
|
||||||
protected $controller = ListGroupsController::class;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
public function shows_index_for_guest()
|
public function shows_index_for_guest()
|
||||||
{
|
{
|
||||||
$response = $this->callWith();
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api/groups')
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
$data = json_decode($response->getBody()->getContents(), true);
|
$data = json_decode($response->getBody()->getContents(), true);
|
@ -7,20 +7,15 @@
|
|||||||
* LICENSE file that was distributed with this source code.
|
* 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 Carbon\Carbon;
|
||||||
use Flarum\Api\Controller\CreatePostController;
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||||
use Flarum\User\User;
|
use Flarum\Tests\integration\TestCase;
|
||||||
use Illuminate\Support\Arr;
|
|
||||||
|
|
||||||
class CreatePostControllerTest extends ApiControllerTestCase
|
class CreationTest extends TestCase
|
||||||
{
|
{
|
||||||
protected $controller = CreatePostController::class;
|
use RetrievesAuthorizedUsers;
|
||||||
|
|
||||||
protected $data = [
|
|
||||||
'content' => 'reply with predetermined content for automated testing - too-obscure'
|
|
||||||
];
|
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
@ -51,13 +46,21 @@ class CreatePostControllerTest extends ApiControllerTestCase
|
|||||||
*/
|
*/
|
||||||
public function can_create_reply()
|
public function can_create_reply()
|
||||||
{
|
{
|
||||||
$this->actor = User::find(2);
|
$response = $this->send(
|
||||||
|
$this->request('POST', '/api/posts', [
|
||||||
$body = [];
|
'authenticatedAs' => 2,
|
||||||
Arr::set($body, 'data.attributes', $this->data);
|
'json' => [
|
||||||
Arr::set($body, 'data.relationships.discussion.data.id', 1);
|
'data' => [
|
||||||
|
'attributes' => [
|
||||||
$response = $this->callWith($body);
|
'content' => 'reply with predetermined content for automated testing - too-obscure',
|
||||||
|
],
|
||||||
|
'relationships' => [
|
||||||
|
'discussion' => ['data' => ['id' => 1]],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(201, $response->getStatusCode());
|
$this->assertEquals(201, $response->getStatusCode());
|
||||||
}
|
}
|
@ -7,18 +7,14 @@
|
|||||||
* LICENSE file that was distributed with this source code.
|
* 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\Tests\integration\RetrievesAuthorizedUsers;
|
||||||
use Flarum\User\User;
|
use Flarum\Tests\integration\TestCase;
|
||||||
|
|
||||||
class UpdateUserControllerTest extends ApiControllerTestCase
|
class UpdateTest extends TestCase
|
||||||
{
|
{
|
||||||
protected $controller = UpdateUserController::class;
|
use RetrievesAuthorizedUsers;
|
||||||
|
|
||||||
protected $data = [
|
|
||||||
'email' => 'newemail@machine.local',
|
|
||||||
];
|
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
@ -48,9 +44,12 @@ class UpdateUserControllerTest extends ApiControllerTestCase
|
|||||||
*/
|
*/
|
||||||
public function users_can_see_their_private_information()
|
public function users_can_see_their_private_information()
|
||||||
{
|
{
|
||||||
$this->actor = User::find(2);
|
$response = $this->send(
|
||||||
|
$this->request('PATCH', '/api/users/2', [
|
||||||
$response = $this->callWith([], ['id' => 2]);
|
'authenticatedAs' => 2,
|
||||||
|
'json' => [],
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
// Test for successful response and that the email is included in the response
|
// Test for successful response and that the email is included in the response
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
@ -62,9 +61,12 @@ class UpdateUserControllerTest extends ApiControllerTestCase
|
|||||||
*/
|
*/
|
||||||
public function users_can_not_see_other_users_private_information()
|
public function users_can_not_see_other_users_private_information()
|
||||||
{
|
{
|
||||||
$this->actor = User::find(2);
|
$response = $this->send(
|
||||||
|
$this->request('PATCH', '/api/users/1', [
|
||||||
$response = $this->callWith([], ['id' => 1]);
|
'authenticatedAs' => 2,
|
||||||
|
'json' => [],
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
// Make sure sensitive information is not made public
|
// Make sure sensitive information is not made public
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
Loading…
x
Reference in New Issue
Block a user