1
0
mirror of https://github.com/flarum/core.git synced 2025-08-11 10:55:47 +02:00

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,141 @@
<?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;
use Flarum\Database\DatabaseMigrationRepository;
use Flarum\Database\Migrator;
use Flarum\Foundation\Application;
use Flarum\Foundation\InstalledSite;
use Flarum\Foundation\SiteInterface;
use Flarum\Foundation\UninstalledSite;
use Flarum\Http\Server;
use Flarum\Install\Console\DataProviderInterface;
use Flarum\Install\DatabaseConfig;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Connectors\MySqlConnector;
use Illuminate\Database\MySqlConnection;
use Illuminate\Filesystem\Filesystem;
trait CreatesForum
{
/**
* @var Server
*/
protected $http;
/**
* @var SiteInterface
*/
protected $site;
/**
* @var Application
*/
protected $app;
/**
* @var DataProviderInterface
*/
protected $configuration;
/**
* Make the test set up Flarum as an installed app.
*
* @var bool
*/
protected $isInstalled = true;
protected function createsSite()
{
$paths = [
'base' => __DIR__.'/tmp',
'public' => __DIR__.'/tmp/public',
'storage' => __DIR__.'/tmp/storage',
];
if ($this->isInstalled) {
$this->site = new InstalledSite($paths, $this->getFlarumConfig());
} else {
$this->site = new UninstalledSite($paths);
}
}
protected function createsHttpForum()
{
$this->app = $this->site->bootApp();
}
protected function getDatabaseConfiguration(): DatabaseConfig
{
return new DatabaseConfig(
'mysql',
env('DB_HOST', 'localhost'),
3306,
env('DB_DATABASE', 'flarum'),
env('DB_USERNAME', 'root'),
env('DB_PASSWORD', ''),
env('DB_PREFIX', '')
);
}
protected function refreshApplication()
{
$this->seedsDatabase();
$this->createsSite();
$this->createsHttpForum();
}
protected function teardownApplication()
{
/** @var ConnectionInterface $connection */
$connection = app(ConnectionInterface::class);
$connection->rollBack();
}
protected function getFlarumConfig()
{
return [
'debug' => true,
'database' => $this->getDatabaseConfiguration()->toArray(),
'url' => 'http://flarum.local',
'paths' => [
'api' => 'api',
'admin' => 'admin',
],
];
}
protected function seedsDatabase()
{
if (! $this->isInstalled) {
return;
}
$dbConfig = $this->getDatabaseConfiguration()->toArray();
$pdo = (new MySqlConnector)->connect($dbConfig);
$db = new MySqlConnection($pdo, $dbConfig['database'], $dbConfig['prefix'], $dbConfig);
$repository = new DatabaseMigrationRepository($db, 'migrations');
$migrator = new Migrator($repository, $db, new Filesystem);
if (! $migrator->getRepository()->repositoryExists()) {
$migrator->getRepository()->createRepository();
}
$migrator->run(__DIR__.'/../../../migrations');
$db->beginTransaction();
}
}

View File

@@ -0,0 +1,30 @@
<?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;
use Flarum\Api\Client;
use Flarum\User\Guest;
use Flarum\User\User;
use Psr\Http\Message\ResponseInterface;
trait MakesApiRequests
{
public function call(string $controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface
{
/** @var Client $api */
$api = app(Client::class);
$api->setErrorHandler(null);
return $api->send($controller, $actor ?? new Guest, $queryParams, $body);
}
}

View File

@@ -0,0 +1,45 @@
<?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;
use Flarum\Post\CommentPost;
use Flarum\Post\Event\Posted;
trait ManagesContent
{
use RetrievesAuthorizedUsers;
protected function addPostByNormalUser(): CommentPost
{
$actor = $this->getNormalUser();
$post = CommentPost::reply(
$this->discussion->id,
'a normal reply - too-obscure',
$actor->id,
'127.0.0.1'
);
$post->save();
if (! $this->discussion->firstPost) {
$this->discussion->setFirstPost($post);
$this->discussion->setLastPost($post);
$this->discussion->save();
event(new Posted($post, $actor));
}
return $post;
}
}

View File

@@ -0,0 +1,37 @@
<?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;
use Flarum\User\User;
trait RetrievesAuthorizedUsers
{
protected $userAttributes = [
'username' => 'normal',
'password' => 'too-obscure',
'email' => 'normal@machine.local'
];
public function getAdminUser(): User
{
return User::find(1);
}
public function getNormalUser(): User
{
return User::unguarded(function () {
return User::firstOrCreate([
'username' => $this->userAttributes['username']
], $this->userAttributes);
});
}
}

View File

@@ -0,0 +1,31 @@
<?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;
use PHPUnit\Framework\TestCase as Test;
abstract class TestCase extends Test
{
use CreatesForum,
MakesApiRequests;
public function setUp()
{
$this->refreshApplication();
$this->init();
}
protected function init()
{
// .. allows implementation by children without the need to call the parent.
}
}

View File

@@ -0,0 +1,150 @@
<?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\Auth;
use Carbon\Carbon;
use Flarum\Api\ApiKey;
use Flarum\Api\Controller\CreateGroupController;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Stratigility\MiddlewarePipe;
class AuthenticateWithApiKeyTest extends TestCase
{
use RetrievesAuthorizedUsers;
protected function key(int $user_id = null): ApiKey
{
return ApiKey::unguarded(function () use ($user_id) {
return ApiKey::query()->firstOrCreate([
'key' => str_random(),
'user_id' => $user_id,
'created_at' => Carbon::now()
]);
});
}
/**
* @test
* @expectedException \Flarum\User\Exception\PermissionDeniedException
*/
public function cannot_authorize_without_key()
{
$this->call(
CreateGroupController::class
);
}
/**
* @test
*/
public function master_token_can_authenticate_as_anyone()
{
$key = $this->key();
$request = ServerRequestFactory::fromGlobals()
->withAddedHeader('Authorization', "Token {$key->key}; userId=1");
$pipe = $this->injectAuthorizationPipeline();
$response = $pipe->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(1, $response->getHeader('X-Authenticated-As')[0]);
$key = $key->refresh();
$this->assertNotNull($key->last_activity_at);
$key->delete();
}
/**
* @test
*/
public function personal_api_token_cannot_authenticate_as_anyone()
{
$user = $this->getNormalUser();
$key = $this->key($user->id);
$request = ServerRequestFactory::fromGlobals()
->withAddedHeader('Authorization', "Token {$key->key}; userId=1");
$pipe = $this->injectAuthorizationPipeline();
$response = $pipe->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals($user->id, $response->getHeader('X-Authenticated-As')[0]);
$key = $key->refresh();
$this->assertNotNull($key->last_activity_at);
$key->delete();
}
/**
* @test
*/
public function personal_api_token_authenticates_user()
{
$user = $this->getNormalUser();
$key = $this->key($user->id);
$request = ServerRequestFactory::fromGlobals()
->withAddedHeader('Authorization', "Token {$key->key}");
$pipe = $this->injectAuthorizationPipeline();
$response = $pipe->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals($user->id, $response->getHeader('X-Authenticated-As')[0]);
$key = $key->refresh();
$this->assertNotNull($key->last_activity_at);
$key->delete();
}
protected function injectAuthorizationPipeline(): MiddlewarePipe
{
app()->resolving('flarum.api.middleware', function ($pipeline) {
$pipeline->pipe(new class implements MiddlewareInterface {
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
if ($actor = $request->getAttribute('actor')) {
return new Response\EmptyResponse(200, [
'X-Authenticated-As' => $actor->id
]);
}
}
});
});
$pipe = app('flarum.api.middleware');
return $pipe;
}
}

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

View File

@@ -0,0 +1,75 @@
<?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\Install;
use Flarum\Install\AdminUser;
use Flarum\Install\Installation;
use Flarum\Tests\Test\TestCase;
use Illuminate\Database\Connectors\ConnectionFactory;
class DefaultInstallationTest extends TestCase
{
protected $isInstalled = false;
/**
* @test
*/
public function allows_forum_installation()
{
if (file_exists(base_path('config.php'))) {
unlink(base_path('config.php'));
}
/** @var Installation $installation */
$installation = app(Installation::class);
$installation
->debugMode(true)
->baseUrl('http://flarum.local')
->databaseConfig($this->getDatabaseConfiguration())
->adminUser($this->getAdmin())
->settings($this->getSettings())
->build()->run();
$this->assertFileExists(base_path('config.php'));
$this->assertEquals(
$this->getDatabase()->table('users')->find(1)->username,
'admin'
);
}
private function getDatabase()
{
$factory = new ConnectionFactory(app());
return $factory->make($this->getDatabaseConfiguration()->toArray());
}
private function getAdmin(): AdminUser
{
return new AdminUser(
'admin',
'password',
'admin@example.com'
);
}
private function getSettings()
{
return [
'forum_title' => 'Development Forum',
'mail_driver' => 'log',
'welcome_title' => 'Welcome to Development Forum',
];
}
}

View File

View File

@@ -0,0 +1 @@
{}