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;
|
2019-09-06 23:53:47 +08:00
|
|
|
use Illuminate\Filesystem\Filesystem;
|
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();
|
2019-05-22 10:13:01 +08:00
|
|
|
|
|
|
|
$user = factory(User::class)->create(['verified' => false]);
|
|
|
|
$this->actingAs($user)->get('/user/oauth/manage')->assertForbidden();
|
|
|
|
$this->getJson('/oauth/clients')->assertForbidden();
|
|
|
|
$user->verified = true;
|
|
|
|
$user->save();
|
|
|
|
$this->getJson('/oauth/clients')->assertSuccessful();
|
2018-08-17 12:32:44 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-09-06 23:53:47 +08:00
|
|
|
$this->mock(Filesystem::class, function ($mock) {
|
|
|
|
$mock->shouldReceive('exists')
|
|
|
|
->with(storage_path('install.lock'))
|
|
|
|
->twice()
|
|
|
|
->andReturn(false);
|
|
|
|
$mock->shouldReceive('exists')
|
|
|
|
->with(base_path('.env'))
|
|
|
|
->andReturn(true);
|
2017-10-29 09:19:02 +08:00
|
|
|
});
|
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-09-06 23:53:47 +08:00
|
|
|
|
|
|
|
$this->actAs('superAdmin');
|
|
|
|
$this->mock(Filesystem::class, function ($mock) {
|
|
|
|
$mock->shouldReceive('exists')
|
|
|
|
->with(storage_path('install.lock'))
|
|
|
|
->andReturn(true);
|
|
|
|
});
|
|
|
|
config(['app.version' => '100.0.0']);
|
|
|
|
$this->get('/setup/update')->assertSee(trans('setup.updates.welcome.title'));
|
2017-10-29 09:19:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-05-19 13:49:44 +08:00
|
|
|
|
2019-04-25 13:01:39 +08:00
|
|
|
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-09-06 23:53:47 +08:00
|
|
|
public function testRedirectToSetup()
|
|
|
|
{
|
|
|
|
$current = config('app.version');
|
|
|
|
config(['app.version' => '100.0.0']);
|
|
|
|
$this->get('/')->assertStatus(503);
|
|
|
|
$this->actAs('superAdmin')->get('/')->assertRedirect('/setup/update');
|
|
|
|
config(['app.version' => $current]);
|
|
|
|
|
|
|
|
$this->mock(Filesystem::class, function ($mock) {
|
|
|
|
$mock->shouldReceive('exists')
|
|
|
|
->with(storage_path('install.lock'))
|
|
|
|
->andReturn(true, false, false);
|
|
|
|
|
|
|
|
$mock->shouldReceive('exists')
|
|
|
|
->with(base_path('.env'))
|
|
|
|
->andReturn(true);
|
|
|
|
});
|
|
|
|
$this->get('/')->assertViewIs('index');
|
|
|
|
$this->get('/setup')->assertViewIs('setup.wizard.welcome');
|
|
|
|
$this->get('/')->assertRedirect('/setup');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|