1
0
mirror of https://github.com/flarum/core.git synced 2025-07-31 05:30:38 +02:00

Make integration tests independent

This creates a dedicated test suite for integration tests. All of them
can be run independently, and there is no order dependency - previously,
all integration tests needed the installer test to run first, and they
would fail if installation failed.

Now, the developer will have to set up a Flarum database to be used by
these tests. A setup script to make this simple will be added in the
next commit.

Small tradeoff: the installer is NOT tested in our test suite anymore,
only implicitly through the setup script. If we decide that this is a
problem, we can still set up separate, dedicated installer tests which
should probably test the web installer.
This commit is contained in:
Franz Liedke
2019-01-30 21:15:27 +01:00
parent 4d10536d35
commit cf746079ed
22 changed files with 416 additions and 419 deletions

View File

@@ -13,9 +13,12 @@ namespace Flarum\Tests\integration\api\Auth;
use Carbon\Carbon;
use Flarum\Api\ApiKey;
use Flarum\Api\Client;
use Flarum\Api\Controller\CreateGroupController;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Flarum\User\Guest;
use Flarum\User\User;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
@@ -28,6 +31,18 @@ class AuthenticateWithApiKeyTest extends TestCase
{
use RetrievesAuthorizedUsers;
public function setUp()
{
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
]);
}
protected function key(int $user_id = null): ApiKey
{
return ApiKey::unguarded(function () use ($user_id) {
@@ -45,9 +60,11 @@ class AuthenticateWithApiKeyTest extends TestCase
*/
public function cannot_authorize_without_key()
{
$this->call(
CreateGroupController::class
);
/** @var Client $api */
$api = $this->app()->getContainer()->make(Client::class);
$api->setErrorHandler(null);
$api->send(CreateGroupController::class, new Guest);
}
/**
@@ -79,7 +96,7 @@ class AuthenticateWithApiKeyTest extends TestCase
*/
public function personal_api_token_cannot_authenticate_as_anyone()
{
$user = $this->getNormalUser();
$user = User::find(2);
$key = $this->key($user->id);
@@ -105,7 +122,7 @@ class AuthenticateWithApiKeyTest extends TestCase
*/
public function personal_api_token_authenticates_user()
{
$user = $this->getNormalUser();
$user = User::find(2);
$key = $this->key($user->id);

View File

@@ -11,8 +11,10 @@
namespace Flarum\Tests\integration\api\Controller;
use Flarum\Api\Client;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Flarum\User\Guest;
use Flarum\User\User;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
@@ -46,9 +48,13 @@ abstract class ApiControllerTestCase extends TestCase
);
}
protected function tearDown()
public function call(string $controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface
{
$this->actor = null;
parent::tearDown();
/** @var Client $api */
$api = $this->app()->getContainer()->make(Client::class);
$api->setErrorHandler(null);
return $api->send($controller, $actor ?? new Guest, $queryParams, $body);
}
}

View File

@@ -13,7 +13,7 @@ namespace Flarum\Tests\integration\api\Controller;
use Flarum\Api\Controller\CreateDiscussionController;
use Flarum\Discussion\Discussion;
use Flarum\Post\Post;
use Flarum\User\User;
use Illuminate\Support\Arr;
class CreateDiscussionControllerTest extends ApiControllerTestCase
@@ -25,12 +25,31 @@ class CreateDiscussionControllerTest extends ApiControllerTestCase
'content' => 'predetermined content for automated testing - too-obscure'
];
public function setUp()
{
parent::setUp();
$this->prepareDatabase([
'discussions' => [],
'posts' => [],
'users' => [
$this->adminUser(),
],
'groups' => [
$this->adminGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
]);
}
/**
* @test
*/
public function can_create_discussion()
{
$this->actor = $this->getAdminUser();
$this->actor = User::find(1);
$response = $this->callWith($this->data);
@@ -51,7 +70,7 @@ class CreateDiscussionControllerTest extends ApiControllerTestCase
*/
public function cannot_create_discussion_without_content()
{
$this->actor = $this->getAdminUser();
$this->actor = User::find(1);
$data = Arr::except($this->data, 'content');
@@ -65,18 +84,10 @@ class CreateDiscussionControllerTest extends ApiControllerTestCase
*/
public function cannot_create_discussion_without_title()
{
$this->actor = $this->getAdminUser();
$this->actor = User::find(1);
$data = Arr::except($this->data, 'title');
$this->callWith($data);
}
public function tearDown()
{
Discussion::where('title', $this->data['title'])->delete();
// Prevent floodgate from kicking in.
Post::where('user_id', $this->getAdminUser()->id)->delete();
parent::tearDown();
}
}

View File

@@ -13,6 +13,7 @@ namespace Flarum\Tests\integration\api\Controller;
use Flarum\Api\Controller\CreateGroupController;
use Flarum\Group\Group;
use Flarum\User\User;
use Illuminate\Support\Str;
class CreateGroupControllerTest extends ApiControllerTestCase
@@ -26,6 +27,24 @@ class CreateGroupControllerTest extends ApiControllerTestCase
'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
* @expectedException \Illuminate\Validation\ValidationException
@@ -33,7 +52,7 @@ class CreateGroupControllerTest extends ApiControllerTestCase
*/
public function admin_cannot_create_group_without_data()
{
$this->actor = $this->getAdminUser();
$this->actor = User::find(1);
$this->callWith();
}
@@ -43,7 +62,7 @@ class CreateGroupControllerTest extends ApiControllerTestCase
*/
public function admin_can_create_group()
{
$this->actor = $this->getAdminUser();
$this->actor = User::find(1);
$response = $this->callWith($this->data);
@@ -65,14 +84,8 @@ class CreateGroupControllerTest extends ApiControllerTestCase
*/
public function unauthorized_user_cannot_create_group()
{
$this->actor = $this->getNormalUser();
$this->actor = User::find(2);
$this->callWith($this->data);
}
public function tearDown()
{
Group::where('icon', $this->data['icon'])->delete();
parent::tearDown();
}
}

View File

@@ -11,8 +11,9 @@
namespace Flarum\Tests\integration\api\Controller;
use Carbon\Carbon;
use Flarum\Api\Controller\CreatePostController;
use Flarum\Discussion\Discussion;
use Flarum\User\User;
use Illuminate\Support\Arr;
class CreatePostControllerTest extends ApiControllerTestCase
@@ -23,17 +24,28 @@ class CreatePostControllerTest extends ApiControllerTestCase
'content' => 'reply with predetermined content for automated testing - too-obscure'
];
/**
* @var Discussion
*/
protected $discussion;
protected function init()
public function setUp()
{
$this->actor = $this->getNormalUser();
$this->discussion = Discussion::start(__CLASS__, $this->actor);
parent::setUp();
$this->discussion->save();
$this->prepareDatabase([
'discussions' => [
['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2],
],
'posts' => [],
'users' => [
$this->normalUser(),
],
'groups' => [
$this->memberGroup(),
],
'group_user' => [
['user_id' => 2, 'group_id' => 3],
],
'group_permission' => [
['permission' => 'viewDiscussions', 'group_id' => 3],
]
]);
}
/**
@@ -41,9 +53,11 @@ 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', $this->discussion->id);
Arr::set($body, 'data.relationships.discussion.data.id', 1);
$response = $this->callWith($body);

View File

@@ -18,24 +18,33 @@ class CreateTokenControllerTest extends ApiControllerTestCase
{
protected $controller = CreateTokenController::class;
public function setUp()
{
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->normalUser(),
],
]);
}
/**
* @test
*/
public function user_generates_token()
{
$user = $this->getNormalUser();
$response = $this->call($this->controller, null, [], [
'identification' => $user->username,
'password' => $this->userAttributes['password']
'identification' => 'normal',
'password' => 'too-obscure'
]);
$data = json_decode($response->getBody()->getContents(), true);
$this->assertEquals($user->id, $data['userId']);
$this->assertEquals(2, $data['userId']);
$token = $data['token'];
$this->assertEquals($user->id, AccessToken::findOrFail($token)->user_id);
$this->assertEquals(2, AccessToken::findOrFail($token)->user_id);
}
}

View File

@@ -26,6 +26,23 @@ class CreateUserControllerTest extends ApiControllerTestCase
'email' => 'test@machine.local'
];
public function setUp()
{
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->adminUser(),
],
'groups' => [
$this->adminGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
]);
}
/**
* @test
* @expectedException \Illuminate\Validation\ValidationException
@@ -60,7 +77,7 @@ class CreateUserControllerTest extends ApiControllerTestCase
*/
public function admins_can_create_activated_users()
{
$this->actor = $this->getAdminUser();
$this->actor = User::find(1);
$response = $this->callWith(array_merge($this->data, [
'isEmailConfirmed' => 1
@@ -90,10 +107,4 @@ class CreateUserControllerTest extends ApiControllerTestCase
$settings->set('allow_sign_up', true);
}
}
public function tearDown()
{
User::where('username', $this->data['username'])->delete();
parent::tearDown();
}
}

View File

@@ -11,19 +11,34 @@
namespace Flarum\Tests\integration\api\Controller;
use Carbon\Carbon;
use Flarum\Api\Controller\DeleteDiscussionController;
use Flarum\Discussion\Discussion;
use Flarum\User\User;
class DeleteDiscussionControllerTest extends ApiControllerTestCase
{
protected $controller = DeleteDiscussionController::class;
protected $discussion;
protected function init()
public function setUp()
{
$this->discussion = Discussion::start(__CLASS__, $this->getNormalUser());
parent::setUp();
$this->discussion->save();
$this->prepareDatabase([
'discussions' => [
['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2],
],
'posts' => [],
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'groups' => [
$this->adminGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
]);
}
/**
@@ -31,9 +46,9 @@ class DeleteDiscussionControllerTest extends ApiControllerTestCase
*/
public function admin_can_delete()
{
$this->actor = $this->getAdminUser();
$this->actor = User::find(1);
$response = $this->callWith([], ['id' => $this->discussion->id]);
$response = $this->callWith([], ['id' => 1]);
$this->assertEquals(204, $response->getStatusCode());
}

View File

@@ -11,13 +11,38 @@
namespace Flarum\Tests\integration\api\Controller;
use Carbon\Carbon;
use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Discussion\Discussion;
use Flarum\User\User;
class ListDiscussionsControllerTest extends ApiControllerTestCase
{
protected $controller = ListDiscussionsController::class;
public function setUp()
{
parent::setUp();
$this->prepareDatabase([
'discussions' => [
['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 1, 'comment_count' => 1],
],
'posts' => [
['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>'],
],
'users' => [
$this->normalUser(),
],
'groups' => [
$this->memberGroup(),
$this->guestGroup(),
],
'group_permission' => [
['permission' => 'viewDiscussions', 'group_id' => 2],
]
]);
}
/**
* @test
*/
@@ -28,7 +53,7 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$this->assertEquals(Discussion::count(), count($data['data']));
$this->assertEquals(1, count($data['data']));
}
/**
@@ -36,7 +61,7 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase
*/
public function can_search_for_author()
{
$user = $this->getNormalUser();
$user = User::find(2);
$response = $this->callWith([], [
'filter' => [

View File

@@ -12,11 +12,23 @@
namespace Flarum\Tests\integration\api\Controller;
use Flarum\Api\Controller\ListNotificationsController;
use Flarum\User\User;
class ListNotificationsControllerTest extends ApiControllerTestCase
{
protected $controller = ListNotificationsController::class;
public function setUp()
{
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->normalUser(),
],
]);
}
/**
* @test
* @expectedException \Flarum\User\Exception\PermissionDeniedException
@@ -31,7 +43,7 @@ class ListNotificationsControllerTest extends ApiControllerTestCase
*/
public function show_index_for_user()
{
$this->actor = $this->getNormalUser();
$this->actor = User::find(2);
$response = $this->callWith();

View File

@@ -12,11 +12,29 @@
namespace Flarum\Tests\integration\api\Controller;
use Flarum\Api\Controller\ListUsersController;
use Flarum\User\User;
class ListUsersControllerTest extends ApiControllerTestCase
{
protected $controller = ListUsersController::class;
public function setUp()
{
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->adminUser(),
],
'groups' => [
$this->adminGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
]);
}
/**
* @test
* @expectedException \Flarum\User\Exception\PermissionDeniedException
@@ -31,7 +49,7 @@ class ListUsersControllerTest extends ApiControllerTestCase
*/
public function shows_index_for_admin()
{
$this->actor = $this->getAdminUser();
$this->actor = User::find(1);
$response = $this->callWith();

View File

@@ -11,14 +11,13 @@
namespace Flarum\Tests\integration\api\Controller;
use Carbon\Carbon;
use Flarum\Api\Controller\ShowDiscussionController;
use Flarum\Discussion\Discussion;
use Flarum\Tests\integration\ManagesContent;
use Flarum\User\User;
class ShowDiscussionControllerTest extends ApiControllerTestCase
{
use ManagesContent;
protected $controller = ShowDiscussionController::class;
/**
@@ -26,9 +25,34 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
*/
protected $discussion;
protected function init()
public function setUp()
{
$this->discussion = Discussion::start(__CLASS__, $this->getNormalUser());
parent::setUp();
$this->prepareDatabase([
'discussions' => [
['id' => 1, 'title' => 'Empty discussion', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => null, 'comment_count' => 0, 'is_private' => 0],
['id' => 2, 'title' => 'Discussion with post', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 1, 'comment_count' => 1, 'is_private' => 0],
['id' => 3, 'title' => 'Private discussion', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => null, 'comment_count' => 0, 'is_private' => 1],
],
'posts' => [
['id' => 1, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t><p>a normal reply - too-obscure</p></t>'],
],
'users' => [
$this->normalUser(),
],
'groups' => [
$this->guestGroup(),
$this->memberGroup(),
],
'group_user' => [
['user_id' => 2, 'group_id' => 3],
],
'group_permission' => [
['permission' => 'viewDiscussions', 'group_id' => 2],
['permission' => 'viewDiscussions', 'group_id' => 3],
]
]);
}
/**
@@ -36,11 +60,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
*/
public function author_can_see_discussion()
{
$this->discussion->save();
$this->actor = User::find(2);
$this->actor = $this->getNormalUser();
$response = $this->callWith([], ['id' => $this->discussion->id]);
$response = $this->callWith([], ['id' => 1]);
$this->assertEquals(200, $response->getStatusCode());
}
@@ -51,9 +73,7 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
*/
public function guest_cannot_see_empty_discussion()
{
$this->discussion->save();
$response = $this->callWith([], ['id' => $this->discussion->id]);
$response = $this->callWith([], ['id' => 1]);
$this->assertEquals(200, $response->getStatusCode());
}
@@ -63,11 +83,7 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
*/
public function guest_can_see_discussion()
{
$this->discussion->save();
$this->addPostByNormalUser();
$response = $this->callWith([], ['id' => $this->discussion->id]);
$response = $this->callWith([], ['id' => 2]);
$this->assertEquals(200, $response->getStatusCode());
}
@@ -78,9 +94,6 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
*/
public function guests_cannot_see_private_discussion()
{
$this->discussion->is_private = true;
$this->discussion->save();
$this->callWith([], ['id' => $this->discussion->id]);
$this->callWith([], ['id' => 3]);
}
}

View File

@@ -12,6 +12,7 @@
namespace Flarum\Tests\integration\api\Controller;
use Flarum\Api\Controller\UpdateUserController;
use Flarum\User\User;
class UpdateUserControllerTest extends ApiControllerTestCase
{
@@ -21,24 +22,41 @@ class UpdateUserControllerTest extends ApiControllerTestCase
'email' => 'newemail@machine.local',
];
protected $userAttributes = [
'username' => 'timtom',
'password' => 'too-obscure',
'email' => 'timtom@machine.local',
'is_email_confirmed' => true,
];
public function setUp()
{
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'groups' => [
$this->adminGroup(),
$this->memberGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
['user_id' => 2, 'group_id' => 3],
],
'group_permission' => [
['permission' => 'viewUserList', 'group_id' => 3],
]
]);
}
/**
* @test
*/
public function users_can_see_their_private_information()
{
$this->actor = $this->getNormalUser();
$response = $this->callWith([], ['id' => $this->actor->id]);
$this->actor = User::find(2);
$response = $this->callWith([], ['id' => 2]);
// Test for successful response and that the email is included in the response
$this->assertEquals(200, $response->getStatusCode());
$this->assertContains('timtom@machine.local', (string) $response->getBody());
$this->assertContains('normal@machine.local', (string) $response->getBody());
}
/**
@@ -46,17 +64,12 @@ class UpdateUserControllerTest extends ApiControllerTestCase
*/
public function users_can_not_see_other_users_private_information()
{
$this->actor = $this->getNormalUser();
$this->actor = User::find(2);
$response = $this->callWith([], ['id' => 1]);
// Make sure sensitive information is not made public
$this->assertEquals(200, $response->getStatusCode());
$this->assertNotContains('admin@example.com', (string) $response->getBody());
}
public function tearDown()
{
parent::tearDown();
$this->assertNotContains('admin@machine.local', (string) $response->getBody());
}
}