Add tests for middleware and HomeController

This commit is contained in:
Pig Fang 2017-10-29 09:19:02 +08:00
parent 71438446fe
commit 2a895e1b88
11 changed files with 277 additions and 29 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
.env
.sass-cache
coverage
.idea/
vendor/*
plugins/*
storage/textures/*

View File

@ -36,7 +36,7 @@ class Handler extends ExceptionHandler
*/
public function report(Exception $e)
{
return parent::report($e);
parent::report($e);
}
/**
@ -56,7 +56,7 @@ class Handler extends ExceptionHandler
abort(403, 'Method not allowed.');
}
if ($e instanceof PrettyPageException && PHP_SAPI != "cli") {
if ($e instanceof PrettyPageException) {
return $e->showErrorPage();
}
@ -84,19 +84,21 @@ class Handler extends ExceptionHandler
* Render an exception using Whoops.
*
* @param \Exception $e
* @param int $code
* @param array $headers
* @return \Illuminate\Http\Response
*/
protected function renderExceptionWithWhoops(Exception $e)
protected function renderExceptionWithWhoops(Exception $e, $code = 200, $headers = [])
{
$whoops = new \Whoops\Run;
$handler = ($_SERVER['REQUEST_METHOD'] == "GET") ?
$handler = (request()->isMethod('GET')) ?
new \Whoops\Handler\PrettyPageHandler : new \Whoops\Handler\PlainTextHandler;
$whoops->pushHandler($handler);
return new \Illuminate\Http\Response(
$whoops->handleException($e),
$e->getStatusCode(),
$e->getHeaders()
$code,
$headers
);
}
@ -108,7 +110,7 @@ class Handler extends ExceptionHandler
*/
protected function renderExceptionInBrief(Exception $e)
{
if ($_SERVER['REQUEST_METHOD'] == "GET" && !app('request')->ajax()) {
if (request()->isMethod('GET') && !request()->ajax()) {
return response()->view('errors.exception', ['message' => $e->getMessage()]);
} else {
return $e->getMessage();

View File

@ -18,10 +18,10 @@ class CheckPlayerExist
$player_name = urldecode($matches[1]);
$responses = Event::fire(new CheckPlayerExists($player_name));
$responses = event(new CheckPlayerExists($player_name));
foreach ($responses as $r) {
if ($r) return $next($request);
if ($r) return $next($request); // @codeCoverageIgnore
}
if (!Player::where('player_name', $player_name)->get()->isEmpty())
@ -34,7 +34,7 @@ class CheckPlayerExist
'msg' => 'Player Not Found.'
])->header('Cache-Control', 'public, max-age='.option('cache_expire_time'));
} else {
abort(404, trans('general.unexistent-player'));
return abort(404, trans('general.unexistent-player'));
}
}

View File

@ -12,7 +12,7 @@ class RedirectIfAuthenticated
{
if (session()->has('uid')) {
if (session('token') != app('users')->get(session('uid'))->getToken()) {
Session::put('msg', trans('auth.check.token'));
Session::put('msg', trans('auth.check.token')); // @codeCoverageIgnore
} else {
return redirect('user');
}

View File

@ -0,0 +1,21 @@
<?php
use App\Models\Player;
$factory->define(Player::class, function (Faker\Generator $faker) {
return [
'uid' => factory(App\Models\User::class)->create()->uid,
'player_name' => $faker->firstName,
'preference' => 'default',
'last_modified' => $faker->dateTime
];
});
$factory->defineAs(Player::class, 'slim', function (Faker\Generator $faker) {
return [
'uid' => factory(App\Models\User::class)->create()->uid,
'player_name' => $faker->firstName,
'preference' => 'slim',
'last_modified' => $faker->dateTime
];
});

View File

@ -0,0 +1,59 @@
<?php
use App\Models\User;
$factory->define(User::class, function (Faker\Generator $faker) {
return [
'email' => $faker->email,
'nickname' => $faker->name,
'score' => 1000,
'avatar' => 0,
'password' => app('cipher')->hash(str_random(10), config('secure.salt')),
'ip' => '127.0.0.1',
'permission' => 0,
'last_sign_at' => $faker->dateTime,
'register_at' => $faker->dateTime
];
});
$factory->defineAs(User::class, 'admin', function (Faker\Generator $faker) {
return [
'email' => $faker->email,
'nickname' => $faker->name,
'score' => 1000,
'avatar' => 0,
'password' => app('cipher')->hash(str_random(10), config('secure.salt')),
'ip' => '127.0.0.1',
'permission' => 1,
'last_sign_at' => $faker->dateTime,
'register_at' => $faker->dateTime
];
});
$factory->defineAs(User::class, 'superAdmin', function (Faker\Generator $faker) {
return [
'email' => $faker->email,
'nickname' => $faker->name,
'score' => 1000,
'avatar' => 0,
'password' => app('cipher')->hash(str_random(10), config('secure.salt')),
'ip' => '127.0.0.1',
'permission' => 2,
'last_sign_at' => $faker->dateTime,
'register_at' => $faker->dateTime
];
});
$factory->defineAs(User::class, 'banned', function (Faker\Generator $faker) {
return [
'email' => $faker->email,
'nickname' => $faker->name,
'score' => 1000,
'avatar' => 0,
'password' => app('cipher')->hash(str_random(10), config('secure.salt')),
'ip' => '127.0.0.1',
'permission' => -1,
'last_sign_at' => $faker->dateTime,
'register_at' => $faker->dateTime
];
});

View File

@ -23,6 +23,7 @@
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_DATABASE" value="blessing_test"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>

View File

@ -0,0 +1,23 @@
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HomeControllerTest extends TestCase
{
public function testIndex()
{
$this->get('/')
->see(option('site_name'))
->see(option('site_description'))
->assertViewHas('home_pic_url', option('home_pic_url'));
$this->visit('/')->click('Log In')->seePageIs('/auth/login');
$this->visit('/')->click('#btn-register')->seePageIs('/auth/register');
// Nav bar
$this->visit('/')->click('Homepage')->seePageIs('/');
$this->visit('/')->click('Skin Library')->seePageIs('/skinlib');
}
}

141
tests/MiddlewareTest.php Normal file
View File

@ -0,0 +1,141 @@
<?php
use App\Services\Facades\Option;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class MiddlewareTest extends TestCase
{
use DatabaseTransactions;
public function testCheckAuthenticated()
{
// Not logged in
$this->visit('/user')->seePageIs('/auth/login');
// Normal user
$this->actAs('normal')
->visit('/user')
->seePageIs('/user')
->assertResponseStatus(200);
// Banned User
$this->actAs('banned')
->get('/user') // Do not use `visit` method here.
->see('banned')
->dontSee('User Center')
->assertResponseStatus(403);
// Binding email
$noEmailUser = factory(App\Models\User::class)->create(['email' => '']);
$this->withSession([
'uid' => $noEmailUser->uid,
'token' => $noEmailUser->getToken()
])->visit('/user')->see('Bind')->dontSee('User Center');
// Without token
$this->withSession([
'uid' => 0
])->visit('/user')->seePageIs('/auth/login');
// Without invalid token
$this->withSession([
'uid' => 0,
'token' => 'invalid'
])->visit('/user')->seePageIs('/auth/login');
}
public function testCheckAdministrator()
{
// Without logged in
$this->get('/admin')->assertRedirectedTo('/auth/login');
// Normal user
$this->actAs('normal')
->get('/admin')
->assertResponseStatus(403);
// Admin
$this->actAs('admin')
->visit('/admin')
->seePageIs('/admin')
->assertResponseStatus(200);
// Super admin
$this->actAs('superAdmin')
->visit('/admin')
->seePageIs('/admin')
->assertResponseStatus(200);
}
public function testCheckInstallation()
{
$this->visit('/setup')->see('Already installed');
$tables = [
'closets', 'migrations', 'options', 'players', 'textures', 'users'
];
array_walk($tables, function ($table) {
Schema::dropIfExists($table);
});
$this->visit('/setup')->see(trans(
'setup.wizard.welcome.text',
['version' => config('app.version')]
));
}
public function testCheckPlayerExist()
{
$this->get('/nope.json')
->assertResponseStatus(404)
->see('Un-existent player');
$this->get('/skin/nope.png')
->assertResponseStatus(404)
->see('Un-existent player');
Option::set('return_200_when_notfound', true);
$this->get('/nope.json')
->assertResponseStatus(200)
->seeJson([
'player_name' => 'nope',
'errno' => 404,
'msg' => 'Player Not Found.'
]);
$player = factory(App\Models\Player::class)->create();
$this->get("/{$player->player_name}.json")
->seeJson(['username' => $player->player_name]); // Default is CSL API
$this->expectsEvents(\App\Events\CheckPlayerExists::class);
$this->get("/{$player->player_name}.json");
}
public function testRedirectIfAuthenticated()
{
$this->visit('/auth/login')
->seePageIs('/auth/login')
->dontSee('User Center');
$user = factory(\App\Models\User::class)->create();
$this->withSession(['uid' => $user->uid])
->visit('/auth/login')
->see('Invalid token');
$this->withSession(['uid' => $user->uid, 'token' => 'nothing'])
->visit('/auth/login')
->seePageIs('/auth/login')
->see(trans('auth.check.token'));
$this->actAs('normal')
->visit('/auth/login')
->seePageIs('/user');
}
public function testRedirectIfUrlEndsWithSlash()
{
$this->visit('/auth/login/')->seePageIs('/auth/login');
}
}

View File

@ -20,6 +20,24 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
Artisan::call('migrate');
return $app;
}
/**
* @param \App\Models\User|string $role
* @return $this
*/
public function actAs($role)
{
if (is_string($role)) {
if ($role == 'normal') {
$role = factory(\App\Models\User::class)->create();
} else {
$role = factory(\App\Models\User::class, $role)->create();
}
}
return $this->withSession(['uid' => $role->uid, 'token' => $role->getToken()]);
}
}

View File

@ -1,18 +0,0 @@
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UtilsTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}