2017-10-29 09:19:02 +08:00
|
|
|
<?php
|
|
|
|
|
2018-08-17 15:25:08 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
2019-02-18 17:15:14 +08:00
|
|
|
use DB;
|
2017-11-30 10:02:29 +08:00
|
|
|
use App\Models\User;
|
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\WithoutMiddleware;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
|
|
|
class MiddlewareTest extends TestCase
|
|
|
|
{
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
|
|
|
public function testCheckAuthenticated()
|
|
|
|
{
|
|
|
|
// Not logged in
|
2018-07-13 16:05:20 +08:00
|
|
|
$this->get('/user')->assertRedirect('auth/login');
|
2018-07-20 14:42:43 +08:00
|
|
|
$this->assertGuest();
|
2017-10-29 09:19:02 +08:00
|
|
|
|
|
|
|
// Normal user
|
|
|
|
$this->actAs('normal')
|
2018-07-20 14:42:43 +08:00
|
|
|
->assertAuthenticated();
|
2017-10-29 09:19:02 +08:00
|
|
|
|
|
|
|
// Banned User
|
|
|
|
$this->actAs('banned')
|
2018-07-13 16:05:20 +08:00
|
|
|
->get('/user')
|
|
|
|
->assertSee('banned')
|
|
|
|
->assertStatus(403);
|
2017-10-29 09:19:02 +08:00
|
|
|
|
|
|
|
// Binding email
|
2018-08-17 15:25:08 +08:00
|
|
|
$noEmailUser = factory(\App\Models\User::class)->create(['email' => '']);
|
2018-07-20 14:42:43 +08:00
|
|
|
$this->actingAs($noEmailUser)
|
|
|
|
->get('/user')
|
|
|
|
->assertSee('Bind')
|
|
|
|
->assertDontSee('User Center');
|
2017-10-29 09:19:02 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
$this->actingAs($noEmailUser)
|
2017-11-30 10:02:29 +08:00
|
|
|
->get('/user?email=email')
|
2018-07-13 16:05:20 +08:00
|
|
|
->assertSee('Bind');
|
2017-11-30 10:02:29 +08:00
|
|
|
|
|
|
|
$other = factory(User::class)->create();
|
2018-07-20 14:42:43 +08:00
|
|
|
$this->actingAs($noEmailUser)
|
2017-11-30 10:02:29 +08:00
|
|
|
->get('/user?email='.$other->email)
|
2018-07-13 16:05:20 +08:00
|
|
|
->assertSee(trans('auth.bind.registered'));
|
2017-11-30 10:02:29 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
$this->actingAs($noEmailUser)
|
2017-11-30 10:02:29 +08:00
|
|
|
->get('/user?email=a@b.c')
|
2018-07-13 16:05:20 +08:00
|
|
|
->assertSee('User Center');
|
2017-11-30 10:02:29 +08:00
|
|
|
$this->assertEquals('a@b.c', User::find($noEmailUser->uid)->email);
|
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 = [
|
|
|
|
'closets', 'migrations', 'options', 'players', 'textures', 'users'
|
|
|
|
];
|
|
|
|
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')]
|
|
|
|
));
|
2019-02-18 17:15:14 +08:00
|
|
|
|
|
|
|
DB::shouldReceive('connection')->once()->andThrow(new \Exception('fake exception'));
|
|
|
|
DB::shouldReceive('disconnect')->andReturnSelf();
|
|
|
|
$this->get('/setup')->assertSee(trans(
|
|
|
|
'setup.wizard.welcome.text',
|
|
|
|
['version' => config('app.version')]
|
|
|
|
));
|
2017-10-29 09:19:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckPlayerExist()
|
|
|
|
{
|
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();
|
2018-07-13 16:05:20 +08:00
|
|
|
$this->getJson("/{$player->player_name}.json")
|
|
|
|
->assertJson(['username' => $player->player_name]); // Default is CSL API
|
2017-10-29 09:19:02 +08:00
|
|
|
|
|
|
|
$this->expectsEvents(\App\Events\CheckPlayerExists::class);
|
2018-07-13 16:05:20 +08:00
|
|
|
$this->getJson("/{$player->player_name}.json");
|
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;
|
2017-11-15 14:00:11 +08:00
|
|
|
$this->actAs($user)
|
2018-07-13 16:05:20 +08:00
|
|
|
->postJson('/user/player/rename', [
|
2017-11-15 14:00:11 +08:00
|
|
|
'pid' => -1,
|
|
|
|
'new_player_name' => 'name'
|
2018-07-13 16:05:20 +08:00
|
|
|
])->assertJson([
|
2017-11-15 14:00:11 +08:00
|
|
|
'errno' => 1,
|
|
|
|
'msg' => trans('general.unexistent-player')
|
|
|
|
]);
|
|
|
|
$this->actAs($user)
|
2018-07-13 16:05:20 +08:00
|
|
|
->postJson('/user/player/rename', [
|
2017-11-15 14:00:11 +08:00
|
|
|
'pid' => $player->pid,
|
|
|
|
'new_player_name' => 'name'
|
2018-07-13 16:05:20 +08:00
|
|
|
])->assertJson([
|
2017-11-15 14:00:11 +08:00
|
|
|
'errno' => 0
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
$this->actAs($other_user)
|
2018-07-13 16:05:20 +08:00
|
|
|
->get('/user/player')
|
|
|
|
->assertSuccessful();
|
2017-11-15 14:00:11 +08:00
|
|
|
|
|
|
|
$this->actAs($other_user)
|
2018-07-13 16:05:20 +08:00
|
|
|
->postJson('/user/player/rename', [
|
2017-11-15 14:00:11 +08:00
|
|
|
'pid' => $player->pid
|
2018-07-13 16:05:20 +08:00
|
|
|
])->assertJson([
|
2017-11-15 14:00:11 +08:00
|
|
|
'errno' => 1,
|
|
|
|
'msg' => trans('admin.players.no-permission')
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->actAs($owner)
|
2018-07-13 16:05:20 +08:00
|
|
|
->postJson('/user/player/rename', [
|
2017-11-15 14:00:11 +08:00
|
|
|
'pid' => $player->pid,
|
|
|
|
'new_player_name' => 'name'
|
2018-07-13 16:05:20 +08:00
|
|
|
])->assertJson([
|
2017-11-15 14:00:11 +08:00
|
|
|
'errno' => 0
|
|
|
|
]);
|
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
|
|
|
}
|
|
|
|
}
|