blessing-skin-server/tests/MiddlewareTest.php

204 lines
6.2 KiB
PHP
Raw Normal View History

<?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;
use App\Services\Facades\Option;
2018-08-17 15:25:08 +08:00
use Illuminate\Support\Facades\Schema;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class MiddlewareTest extends TestCase
{
use DatabaseTransactions;
2019-04-25 13:01:39 +08:00
public function testAuthenticate()
{
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();
}
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();
}
public function testCheckAdministrator()
{
// Without logged in
2018-07-13 16:05:20 +08:00
$this->get('/admin')->assertRedirect('/auth/login');
// Normal user
$this->actAs('normal')
->get('/admin')
2018-07-13 16:05:20 +08:00
->assertStatus(403);
// Admin
$this->actAs('admin')
2018-07-13 16:05:20 +08:00
->get('/admin')
->assertSuccessful();
// Super admin
$this->actAs('superAdmin')
2018-07-13 16:05:20 +08:00
->get('/admin')
->assertSuccessful();
}
public function testCheckSuperAdmin()
{
// Admin
$this->actAs('admin')
->get('/admin/plugins/manage')
->assertForbidden();
// Super admin
$this->actAs('superAdmin')
->get('/admin/plugins/manage')
->assertSuccessful();
}
public function testCheckInstallation()
{
2018-07-13 16:05:20 +08:00
$this->get('/setup')->assertSee('Already installed');
$tables = [
2019-03-14 23:55:49 +08:00
'user_closet', '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(
'setup.wizard.welcome.text',
['version' => config('app.version')]
));
}
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'));
$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'));
Option::set('return_204_when_notfound', true);
$this->getJson('/nope.json')->assertStatus(204);
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
$this->expectsEvents(\App\Events\CheckPlayerExists::class);
2019-03-13 13:16:51 +08:00
$this->getJson("/{$player->name}.json");
$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'),
]);
}
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;
2019-04-04 09:50:48 +08:00
$this->actingAs($other_user)
2018-07-13 16:05:20 +08:00
->get('/user/player')
->assertSuccessful();
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'),
]);
}
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;
});
}
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'));
$this->actingAs(factory(User::class)->create())
2018-07-13 16:05:20 +08:00
->get('/auth/login')
->assertRedirect('/user');
}
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'));
}
}