Move integration tests to separate directory

Again, we do all of this to prepare for creating "real" test suites for
each type of tests.
This commit is contained in:
Franz Liedke
2019-01-01 22:17:09 +01:00
parent ba16ebe61f
commit 4d10536d35
26 changed files with 30 additions and 30 deletions

View File

@ -0,0 +1,54 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\api\Controller;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Flarum\User\User;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
abstract class ApiControllerTestCase extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @var RequestHandlerInterface
*/
protected $controller;
/**
* @var null|User
*/
protected $actor = null;
protected function callWith(array $body = [], array $queryParams = []): ResponseInterface
{
if (! Arr::get($body, 'data') && Arr::isAssoc($body)) {
$body = ['data' => ['attributes' => $body]];
}
return $this->call(
$this->controller,
$this->actor,
$queryParams,
$body
);
}
protected function tearDown()
{
$this->actor = null;
parent::tearDown();
}
}

View File

@ -0,0 +1,82 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full 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\CreateDiscussionController;
use Flarum\Discussion\Discussion;
use Flarum\Post\Post;
use Illuminate\Support\Arr;
class CreateDiscussionControllerTest extends ApiControllerTestCase
{
protected $controller = CreateDiscussionController::class;
protected $data = [
'title' => 'test - too-obscure',
'content' => 'predetermined content for automated testing - too-obscure'
];
/**
* @test
*/
public function can_create_discussion()
{
$this->actor = $this->getAdminUser();
$response = $this->callWith($this->data);
$this->assertEquals(201, $response->getStatusCode());
/** @var Discussion $discussion */
$discussion = Discussion::where('title', $this->data['title'])->firstOrFail();
$data = json_decode($response->getBody()->getContents(), true);
$this->assertEquals($this->data['title'], $discussion->title);
$this->assertEquals($this->data['title'], array_get($data, 'data.attributes.title'));
}
/**
* @test
* @expectedException \Illuminate\Validation\ValidationException
* @expectedExceptionMessage The given data was invalid.
*/
public function cannot_create_discussion_without_content()
{
$this->actor = $this->getAdminUser();
$data = Arr::except($this->data, 'content');
$this->callWith($data);
}
/**
* @test
* @expectedException \Illuminate\Validation\ValidationException
* @expectedExceptionMessage The given data was invalid.
*/
public function cannot_create_discussion_without_title()
{
$this->actor = $this->getAdminUser();
$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

@ -0,0 +1,78 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full 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 Illuminate\Support\Str;
class CreateGroupControllerTest extends ApiControllerTestCase
{
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());
$data = json_decode($response->getBody()->getContents(), true);
$group = Group::where('icon', $this->data['icon'])->firstOrFail();
foreach ($this->data as $property => $value) {
$this->assertEquals($value, array_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
* @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();
}
}

View File

@ -0,0 +1,52 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full 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\CreatePostController;
use Flarum\Discussion\Discussion;
use Illuminate\Support\Arr;
class CreatePostControllerTest extends ApiControllerTestCase
{
protected $controller = CreatePostController::class;
protected $data = [
'content' => 'reply with predetermined content for automated testing - too-obscure'
];
/**
* @var Discussion
*/
protected $discussion;
protected function init()
{
$this->actor = $this->getNormalUser();
$this->discussion = Discussion::start(__CLASS__, $this->actor);
$this->discussion->save();
}
/**
* @test
*/
public function can_create_reply()
{
$body = [];
Arr::set($body, 'data.attributes', $this->data);
Arr::set($body, 'data.relationships.discussion.data.id', $this->discussion->id);
$response = $this->callWith($body);
$this->assertEquals(201, $response->getStatusCode());
}
}

View File

@ -0,0 +1,41 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full 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\CreateTokenController;
use Flarum\Http\AccessToken;
class CreateTokenControllerTest extends ApiControllerTestCase
{
protected $controller = CreateTokenController::class;
/**
* @test
*/
public function user_generates_token()
{
$user = $this->getNormalUser();
$response = $this->call($this->controller, null, [], [
'identification' => $user->username,
'password' => $this->userAttributes['password']
]);
$data = json_decode($response->getBody()->getContents(), true);
$this->assertEquals($user->id, $data['userId']);
$token = $data['token'];
$this->assertEquals($user->id, AccessToken::findOrFail($token)->user_id);
}
}

View File

@ -0,0 +1,99 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full 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\CreateUserController;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User;
use Illuminate\Support\Arr;
class CreateUserControllerTest extends ApiControllerTestCase
{
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, [
'isEmailConfirmed' => 1
]));
$this->assertEquals(201, $response->getStatusCode());
/** @var User $user */
$user = User::where('username', 'test')->firstOrFail();
$this->assertEquals(1, $user->is_email_confirmed);
}
/**
* @test
* @expectedException \Flarum\User\Exception\PermissionDeniedException
*/
public function disabling_sign_up_prevents_user_creation()
{
/** @var SettingsRepositoryInterface $settings */
$settings = app(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();
}
}

View File

@ -0,0 +1,40 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full 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\DeleteDiscussionController;
use Flarum\Discussion\Discussion;
class DeleteDiscussionControllerTest extends ApiControllerTestCase
{
protected $controller = DeleteDiscussionController::class;
protected $discussion;
protected function init()
{
$this->discussion = Discussion::start(__CLASS__, $this->getNormalUser());
$this->discussion->save();
}
/**
* @test
*/
public function admin_can_delete()
{
$this->actor = $this->getAdminUser();
$response = $this->callWith([], ['id' => $this->discussion->id]);
$this->assertEquals(204, $response->getStatusCode());
}
}

View File

@ -0,0 +1,50 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full 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\ListDiscussionsController;
use Flarum\Discussion\Discussion;
class ListDiscussionsControllerTest extends ApiControllerTestCase
{
protected $controller = ListDiscussionsController::class;
/**
* @test
*/
public function shows_index_for_guest()
{
$response = $this->callWith();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$this->assertEquals(Discussion::count(), count($data['data']));
}
/**
* @test
*/
public function can_search_for_author()
{
$user = $this->getNormalUser();
$response = $this->callWith([], [
'filter' => [
'q' => 'author:'.$user->username.' foo'
],
'include' => 'mostRelevantPost'
]);
$this->assertEquals(200, $response->getStatusCode());
}
}

View File

@ -0,0 +1,33 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full 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\ListGroupsController;
use Flarum\Group\Group;
class ListGroupsControllerTest extends ApiControllerTestCase
{
protected $controller = ListGroupsController::class;
/**
* @test
*/
public function shows_index_for_guest()
{
$response = $this->callWith();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$this->assertEquals(Group::count(), count($data['data']));
}
}

View File

@ -0,0 +1,40 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full 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\ListNotificationsController;
class ListNotificationsControllerTest extends ApiControllerTestCase
{
protected $controller = ListNotificationsController::class;
/**
* @test
* @expectedException \Flarum\User\Exception\PermissionDeniedException
*/
public function disallows_index_for_guest()
{
$this->callWith();
}
/**
* @test
*/
public function show_index_for_user()
{
$this->actor = $this->getNormalUser();
$response = $this->callWith();
$this->assertEquals(200, $response->getStatusCode());
}
}

View File

@ -0,0 +1,40 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full 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\ListUsersController;
class ListUsersControllerTest extends ApiControllerTestCase
{
protected $controller = ListUsersController::class;
/**
* @test
* @expectedException \Flarum\User\Exception\PermissionDeniedException
*/
public function disallows_index_for_guest()
{
$this->callWith();
}
/**
* @test
*/
public function shows_index_for_admin()
{
$this->actor = $this->getAdminUser();
$response = $this->callWith();
$this->assertEquals(200, $response->getStatusCode());
}
}

View File

@ -0,0 +1,86 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full 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\ShowDiscussionController;
use Flarum\Discussion\Discussion;
use Flarum\Tests\integration\ManagesContent;
class ShowDiscussionControllerTest extends ApiControllerTestCase
{
use ManagesContent;
protected $controller = ShowDiscussionController::class;
/**
* @var Discussion
*/
protected $discussion;
protected function init()
{
$this->discussion = Discussion::start(__CLASS__, $this->getNormalUser());
}
/**
* @test
*/
public function author_can_see_discussion()
{
$this->discussion->save();
$this->actor = $this->getNormalUser();
$response = $this->callWith([], ['id' => $this->discussion->id]);
$this->assertEquals(200, $response->getStatusCode());
}
/**
* @test
* @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function guest_cannot_see_empty_discussion()
{
$this->discussion->save();
$response = $this->callWith([], ['id' => $this->discussion->id]);
$this->assertEquals(200, $response->getStatusCode());
}
/**
* @test
*/
public function guest_can_see_discussion()
{
$this->discussion->save();
$this->addPostByNormalUser();
$response = $this->callWith([], ['id' => $this->discussion->id]);
$this->assertEquals(200, $response->getStatusCode());
}
/**
* @test
* @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function guests_cannot_see_private_discussion()
{
$this->discussion->is_private = true;
$this->discussion->save();
$this->callWith([], ['id' => $this->discussion->id]);
}
}

View File

@ -0,0 +1,62 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full 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\UpdateUserController;
class UpdateUserControllerTest extends ApiControllerTestCase
{
protected $controller = UpdateUserController::class;
protected $data = [
'email' => 'newemail@machine.local',
];
protected $userAttributes = [
'username' => 'timtom',
'password' => 'too-obscure',
'email' => 'timtom@machine.local',
'is_email_confirmed' => true,
];
/**
* @test
*/
public function users_can_see_their_private_information()
{
$this->actor = $this->getNormalUser();
$response = $this->callWith([], ['id' => $this->actor->id]);
// 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());
}
/**
* @test
*/
public function users_can_not_see_other_users_private_information()
{
$this->actor = $this->getNormalUser();
$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();
}
}