2017-10-29 09:19:02 +08:00
|
|
|
<?php
|
|
|
|
|
2018-08-17 15:25:08 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
2019-04-25 13:01:39 +08:00
|
|
|
use Event;
|
2017-11-30 10:02:29 +08:00
|
|
|
use App\Models\User;
|
2019-03-22 21:40:12 +08:00
|
|
|
use App\Models\Player;
|
2017-10-29 09:19:02 +08:00
|
|
|
use App\Services\Facades\Option;
|
2018-08-17 15:25:08 +08:00
|
|
|
use Illuminate\Support\Facades\Schema;
|
2017-10-29 09:19:02 +08:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
|
|
|
class MiddlewareTest extends TestCase
|
|
|
|
{
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
2019-04-25 13:01:39 +08:00
|
|
|
public function testAuthenticate()
|
2017-10-29 09:19:02 +08:00
|
|
|
{
|
2018-07-13 16:05:20 +08:00
|
|
|
$this->get('/user')->assertRedirect('auth/login');
|
2019-04-25 13:01:39 +08:00
|
|
|
$this->actAs('normal')->assertAuthenticated();
|
2017-10-29 09:19:02 +08:00
|
|
|
}
|
|
|
|
|
2018-08-17 12:32:44 +08:00
|
|
|
public function testCheckUserVerified()
|
|
|
|
{
|
|
|
|
$unverified = factory(User::class)->create(['verified' => false]);
|
|
|
|
|
|
|
|
option(['require_verification' => false]);
|
|
|
|
$this->actingAs($unverified)
|
|
|
|
->get('/skinlib/upload')
|
|
|
|
->assertSuccessful();
|
|
|
|
|
|
|
|
option(['require_verification' => true]);
|
|
|
|
$this->actingAs($unverified)
|
|
|
|
->get('/skinlib/upload')
|
|
|
|
->assertStatus(403)
|
|
|
|
->assertSee(trans('auth.check.verified'));
|
|
|
|
|
|
|
|
$this->actAs('normal')
|
|
|
|
->get('/skinlib/upload')
|
|
|
|
->assertSuccessful();
|
|
|
|
}
|
|
|
|
|
2017-10-29 09:19:02 +08:00
|
|
|
public function testCheckAdministrator()
|
|
|
|
{
|
|
|
|
// Without logged in
|
2018-07-13 16:05:20 +08:00
|
|
|
$this->get('/admin')->assertRedirect('/auth/login');
|
2017-10-29 09:19:02 +08:00
|
|
|
|
|
|
|
// Normal user
|
|
|
|
$this->actAs('normal')
|
|
|
|
->get('/admin')
|
2018-07-13 16:05:20 +08:00
|
|
|
->assertStatus(403);
|
2017-10-29 09:19:02 +08:00
|
|
|
|
|
|
|
// Admin
|
|
|
|
$this->actAs('admin')
|
2018-07-13 16:05:20 +08:00
|
|
|
->get('/admin')
|
|
|
|
->assertSuccessful();
|
2017-10-29 09:19:02 +08:00
|
|
|
|
|
|
|
// Super admin
|
|
|
|
$this->actAs('superAdmin')
|
2018-07-13 16:05:20 +08:00
|
|
|
->get('/admin')
|
|
|
|
->assertSuccessful();
|
2017-10-29 09:19:02 +08:00
|
|
|
}
|
|
|
|
|
2018-08-21 09:05:29 +08:00
|
|
|
public function testCheckSuperAdmin()
|
|
|
|
{
|
|
|
|
// Admin
|
|
|
|
$this->actAs('admin')
|
|
|
|
->get('/admin/plugins/manage')
|
|
|
|
->assertForbidden();
|
|
|
|
|
|
|
|
// Super admin
|
|
|
|
$this->actAs('superAdmin')
|
|
|
|
->get('/admin/plugins/manage')
|
|
|
|
->assertSuccessful();
|
|
|
|
}
|
|
|
|
|
2017-10-29 09:19:02 +08:00
|
|
|
public function testCheckInstallation()
|
|
|
|
{
|
2018-07-13 16:05:20 +08:00
|
|
|
$this->get('/setup')->assertSee('Already installed');
|
2017-10-29 09:19:02 +08:00
|
|
|
|
|
|
|
$tables = [
|
2019-03-14 23:55:49 +08:00
|
|
|
'user_closet', 'migrations', 'options', 'players', 'textures', 'users',
|
2017-10-29 09:19:02 +08:00
|
|
|
];
|
|
|
|
array_walk($tables, function ($table) {
|
|
|
|
Schema::dropIfExists($table);
|
|
|
|
});
|
2018-07-13 16:05:20 +08:00
|
|
|
$this->get('/setup')->assertSee(trans(
|
2017-10-29 09:19:02 +08:00
|
|
|
'setup.wizard.welcome.text',
|
|
|
|
['version' => config('app.version')]
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckPlayerExist()
|
|
|
|
{
|
2019-05-01 10:38:50 +08:00
|
|
|
Event::fake();
|
|
|
|
|
2018-07-13 16:05:20 +08:00
|
|
|
$this->getJson('/nope.json')
|
|
|
|
->assertStatus(404)
|
2018-08-14 01:00:02 +08:00
|
|
|
->assertSee(trans('general.unexistent-player'));
|
2017-10-29 09:19:02 +08:00
|
|
|
|
|
|
|
$this->get('/skin/nope.png')
|
2018-07-13 16:05:20 +08:00
|
|
|
->assertStatus(404)
|
2018-08-14 01:00:02 +08:00
|
|
|
->assertSee(trans('general.unexistent-player'));
|
2017-10-29 09:19:02 +08:00
|
|
|
|
2018-07-22 16:42:58 +08:00
|
|
|
Option::set('return_204_when_notfound', true);
|
|
|
|
$this->getJson('/nope.json')->assertStatus(204);
|
2017-10-29 09:19:02 +08:00
|
|
|
|
2018-08-17 15:25:08 +08:00
|
|
|
$player = factory(\App\Models\Player::class)->create();
|
2019-03-13 13:16:51 +08:00
|
|
|
$this->getJson("/{$player->name}.json")
|
|
|
|
->assertJson(['username' => $player->name]); // Default is CSL API
|
2017-10-29 09:19:02 +08:00
|
|
|
|
2019-03-13 13:16:51 +08:00
|
|
|
$this->getJson("/{$player->name}.json");
|
2019-05-01 10:38:50 +08:00
|
|
|
Event::assertDispatched(\App\Events\CheckPlayerExists::class);
|
2017-11-15 14:00:11 +08:00
|
|
|
|
|
|
|
$player = factory(\App\Models\Player::class)->create();
|
2018-07-16 10:22:19 +08:00
|
|
|
$user = $player->user;
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2019-04-24 13:10:03 +08:00
|
|
|
->postJson('/user/player/rename/-1', ['name' => 'name'])
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('general.unexistent-player'),
|
2017-11-15 14:00:11 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckPlayerOwner()
|
|
|
|
{
|
|
|
|
$other_user = factory(\App\Models\User::class)->create();
|
|
|
|
$player = factory(\App\Models\Player::class)->create();
|
2018-07-16 10:22:19 +08:00
|
|
|
$owner = $player->user;
|
2017-11-15 14:00:11 +08:00
|
|
|
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($other_user)
|
2018-07-13 16:05:20 +08:00
|
|
|
->get('/user/player')
|
|
|
|
->assertSuccessful();
|
2017-11-15 14:00:11 +08:00
|
|
|
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($other_user)
|
2019-04-24 13:10:03 +08:00
|
|
|
->postJson('/user/player/rename/'.$player->pid)
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('admin.players.no-permission'),
|
2017-11-15 14:00:11 +08:00
|
|
|
]);
|
2017-10-29 09:19:02 +08:00
|
|
|
}
|
|
|
|
|
2019-04-25 13:01:39 +08:00
|
|
|
public function testEnsureEmailFilled()
|
|
|
|
{
|
|
|
|
$noEmailUser = factory(User::class)->create(['email' => '']);
|
|
|
|
$this->actingAs($noEmailUser)->get('/user')->assertRedirect('/auth/bind');
|
|
|
|
|
|
|
|
$normalUser = factory(User::class)->create();
|
|
|
|
$this->actingAs($normalUser)->get('/auth/bind')->assertRedirect('/user');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFireUserAuthenticated()
|
|
|
|
{
|
|
|
|
Event::fake();
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
$this->actingAs($user)->get('/user');
|
|
|
|
Event::assertDispatched(\App\Events\UserAuthenticated::class, function ($event) use ($user) {
|
|
|
|
$this->assertEquals($user->uid, $event->user->uid);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-29 09:19:02 +08:00
|
|
|
public function testRedirectIfAuthenticated()
|
|
|
|
{
|
2018-07-13 16:05:20 +08:00
|
|
|
$this->get('/auth/login')
|
|
|
|
->assertViewIs('auth.login')
|
2018-08-14 01:00:02 +08:00
|
|
|
->assertDontSee(trans('general.user-center'));
|
2017-10-29 09:19:02 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
$this->actingAs(factory(User::class)->create())
|
2018-07-13 16:05:20 +08:00
|
|
|
->get('/auth/login')
|
|
|
|
->assertRedirect('/user');
|
2017-10-29 09:19:02 +08:00
|
|
|
}
|
2019-03-22 21:40:12 +08:00
|
|
|
|
2019-04-25 13:01:39 +08:00
|
|
|
public function testRejectBannedUser()
|
|
|
|
{
|
|
|
|
$user = factory(User::class, 'banned')->create();
|
|
|
|
$this->actingAs($user)->get('/user')->assertForbidden();
|
|
|
|
$this->get('/user', ['accept' => 'application/json'])
|
|
|
|
->assertForbidden()
|
|
|
|
->assertJson(['code' => -1, 'message' => trans('auth.check.banned')]);
|
|
|
|
}
|
|
|
|
|
2019-03-22 21:40:12 +08:00
|
|
|
public function testRequireBindPlayer()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)->get('/user')->assertViewIs('user.index');
|
2019-03-22 21:40:12 +08:00
|
|
|
$this->get('/user/player/bind')->assertRedirect('/user');
|
|
|
|
|
|
|
|
option(['single_player' => true]);
|
|
|
|
|
|
|
|
$this->getJson('/user/player/list')->assertHeader('content-type', 'application/json');
|
|
|
|
|
|
|
|
$this->get('/user/player/bind')->assertViewIs('user.bind');
|
|
|
|
$this->get('/user')->assertRedirect('/user/player/bind');
|
|
|
|
|
|
|
|
factory(Player::class)->create(['uid' => $user->uid]);
|
|
|
|
$this->get('/user')->assertViewIs('user.index');
|
|
|
|
$this->get('/user/player/bind')->assertRedirect('/user');
|
|
|
|
}
|
2019-04-22 21:09:36 +08:00
|
|
|
|
|
|
|
public function testForbiddenIE()
|
|
|
|
{
|
|
|
|
$this->get('/', ['user-agent' => 'MSIE'])->assertSee(trans('errors.http.ie'));
|
|
|
|
$this->get('/', ['user-agent' => 'Trident'])->assertSee(trans('errors.http.ie'));
|
|
|
|
}
|
2017-10-29 09:19:02 +08:00
|
|
|
}
|