blessing-skin-server/tests/MiddlewareTest.php

195 lines
5.8 KiB
PHP
Raw Normal View History

<?php
2017-11-30 10:02:29 +08:00
use App\Models\User;
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
2018-07-13 16:05:20 +08:00
$this->get('/user')->assertRedirect('auth/login');
// Normal user
$this->actAs('normal')
2018-07-13 16:05:20 +08:00
->get('/user')
->assertViewIs('user.index')
->assertSuccessful();
// Banned User
$this->actAs('banned')
2018-07-13 16:05:20 +08:00
->get('/user')
->assertSee('banned')
->assertDontSee('User Center')
->assertStatus(403);
// Binding email
$noEmailUser = factory(App\Models\User::class)->create(['email' => '']);
$this->withSession([
'uid' => $noEmailUser->uid,
'token' => $noEmailUser->getToken()
2018-07-13 16:05:20 +08:00
])->get('/user')->assertSee('Bind')->assertDontSee('User Center');
2017-11-30 10:02:29 +08:00
$this->actAs($noEmailUser)
->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();
$this->actAs($noEmailUser)
->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
$this->actAs($noEmailUser)
->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);
// Without token
$this->withSession([
'uid' => 0
2018-07-13 16:05:20 +08:00
])->get('/user')->assertRedirect('/auth/login');
// Without invalid token
2017-11-30 10:02:29 +08:00
$user = factory(User::class)->create();
$this->withSession([
2017-11-30 10:02:29 +08:00
'uid' => $user->uid,
'token' => 'invalid'
2018-07-13 16:05:20 +08:00
])->get('/user')->assertRedirect('/auth/login');
}
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 testCheckInstallation()
{
2018-07-13 16:05:20 +08:00
$this->get('/setup')->assertSee('Already installed');
$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(
'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)
->assertSee('Un-existent player');
$this->get('/skin/nope.png')
2018-07-13 16:05:20 +08:00
->assertStatus(404)
->assertSee('Un-existent player');
Option::set('return_200_when_notfound', true);
2018-07-13 16:05:20 +08:00
$this->getJson('/nope.json')
->assertSuccessful()
->assertJson([
'player_name' => 'nope',
'errno' => 404,
'msg' => 'Player Not Found.'
]);
$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
$this->expectsEvents(\App\Events\CheckPlayerExists::class);
2018-07-13 16:05:20 +08:00
$this->getJson("/{$player->player_name}.json");
$player = factory(\App\Models\Player::class)->create();
$user = \App\Models\User::find($player->uid);
$this->actAs($user)
2018-07-13 16:05:20 +08:00
->postJson('/user/player/rename', [
'pid' => -1,
'new_player_name' => 'name'
2018-07-13 16:05:20 +08:00
])->assertJson([
'errno' => 1,
'msg' => trans('general.unexistent-player')
]);
$this->actAs($user)
2018-07-13 16:05:20 +08:00
->postJson('/user/player/rename', [
'pid' => $player->pid,
'new_player_name' => 'name'
2018-07-13 16:05:20 +08:00
])->assertJson([
'errno' => 0
]);
}
public function testCheckPlayerOwner()
{
$other_user = factory(\App\Models\User::class)->create();
$player = factory(\App\Models\Player::class)->create();
$owner = \App\Models\User::find($player->uid);
$this->actAs($other_user)
2018-07-13 16:05:20 +08:00
->get('/user/player')
->assertSuccessful();
$this->actAs($other_user)
2018-07-13 16:05:20 +08:00
->postJson('/user/player/rename', [
'pid' => $player->pid
2018-07-13 16:05:20 +08:00
])->assertJson([
'errno' => 1,
'msg' => trans('admin.players.no-permission')
]);
$this->actAs($owner)
2018-07-13 16:05:20 +08:00
->postJson('/user/player/rename', [
'pid' => $player->pid,
'new_player_name' => 'name'
2018-07-13 16:05:20 +08:00
])->assertJson([
'errno' => 0
]);
}
public function testRedirectIfAuthenticated()
{
2018-07-13 16:05:20 +08:00
$this->get('/auth/login')
->assertViewIs('auth.login')
->assertDontSee('User Center');
$user = factory(\App\Models\User::class)->create();
$this->withSession(['uid' => $user->uid])
2018-07-13 16:05:20 +08:00
->get('/auth/login')
->assertRedirect('/auth/login');
$this->withSession(['uid' => $user->uid, 'token' => 'nothing'])
2018-07-13 16:05:20 +08:00
->get('/auth/login')
->assertRedirect('/auth/login');
$this->actAs('normal')
2018-07-13 16:05:20 +08:00
->get('/auth/login')
->assertRedirect('/user');
}
}