fix tests of middlewares

This commit is contained in:
Pig Fang 2018-07-13 16:05:20 +08:00
parent f9774e9275
commit 4e6478c8f9

View File

@ -13,81 +13,79 @@ class MiddlewareTest extends TestCase
public function testCheckAuthenticated() public function testCheckAuthenticated()
{ {
// Not logged in // Not logged in
$this->visit('/user')->seePageIs('/auth/login'); $this->get('/user')->assertRedirect('auth/login');
// Normal user // Normal user
$this->actAs('normal') $this->actAs('normal')
->visit('/user') ->get('/user')
->seePageIs('/user') ->assertViewIs('user.index')
->assertResponseStatus(200); ->assertSuccessful();
// Banned User // Banned User
$this->actAs('banned') $this->actAs('banned')
->get('/user') // Do not use `visit` method here. ->get('/user')
->see('banned') ->assertSee('banned')
->dontSee('User Center') ->assertDontSee('User Center')
->assertResponseStatus(403); ->assertStatus(403);
// Binding email // Binding email
$noEmailUser = factory(App\Models\User::class)->create(['email' => '']); $noEmailUser = factory(App\Models\User::class)->create(['email' => '']);
$this->withSession([ $this->withSession([
'uid' => $noEmailUser->uid, 'uid' => $noEmailUser->uid,
'token' => $noEmailUser->getToken() 'token' => $noEmailUser->getToken()
])->visit('/user')->see('Bind')->dontSee('User Center'); ])->get('/user')->assertSee('Bind')->assertDontSee('User Center');
$this->actAs($noEmailUser) $this->actAs($noEmailUser)
->get('/user?email=email') ->get('/user?email=email')
->see('Bind'); ->assertSee('Bind');
$other = factory(User::class)->create(); $other = factory(User::class)->create();
$this->actAs($noEmailUser) $this->actAs($noEmailUser)
->get('/user?email='.$other->email) ->get('/user?email='.$other->email)
->see(trans('auth.bind.registered')); ->assertSee(trans('auth.bind.registered'));
$this->actAs($noEmailUser) $this->actAs($noEmailUser)
->get('/user?email=a@b.c') ->get('/user?email=a@b.c')
->see('User Center'); ->assertSee('User Center');
$this->assertEquals('a@b.c', User::find($noEmailUser->uid)->email); $this->assertEquals('a@b.c', User::find($noEmailUser->uid)->email);
// Without token // Without token
$this->withSession([ $this->withSession([
'uid' => 0 'uid' => 0
])->visit('/user')->seePageIs('/auth/login'); ])->get('/user')->assertRedirect('/auth/login');
// Without invalid token // Without invalid token
$user = factory(User::class)->create(); $user = factory(User::class)->create();
$this->withSession([ $this->withSession([
'uid' => $user->uid, 'uid' => $user->uid,
'token' => 'invalid' 'token' => 'invalid'
])->visit('/user')->seePageIs('/auth/login'); ])->get('/user')->assertRedirect('/auth/login');
} }
public function testCheckAdministrator() public function testCheckAdministrator()
{ {
// Without logged in // Without logged in
$this->get('/admin')->assertRedirectedTo('/auth/login'); $this->get('/admin')->assertRedirect('/auth/login');
// Normal user // Normal user
$this->actAs('normal') $this->actAs('normal')
->get('/admin') ->get('/admin')
->assertResponseStatus(403); ->assertStatus(403);
// Admin // Admin
$this->actAs('admin') $this->actAs('admin')
->visit('/admin') ->get('/admin')
->seePageIs('/admin') ->assertSuccessful();
->assertResponseStatus(200);
// Super admin // Super admin
$this->actAs('superAdmin') $this->actAs('superAdmin')
->visit('/admin') ->get('/admin')
->seePageIs('/admin') ->assertSuccessful();
->assertResponseStatus(200);
} }
public function testCheckInstallation() public function testCheckInstallation()
{ {
$this->visit('/setup')->see('Already installed'); $this->get('/setup')->assertSee('Already installed');
$tables = [ $tables = [
'closets', 'migrations', 'options', 'players', 'textures', 'users' 'closets', 'migrations', 'options', 'players', 'textures', 'users'
@ -95,7 +93,7 @@ class MiddlewareTest extends TestCase
array_walk($tables, function ($table) { array_walk($tables, function ($table) {
Schema::dropIfExists($table); Schema::dropIfExists($table);
}); });
$this->visit('/setup')->see(trans( $this->get('/setup')->assertSee(trans(
'setup.wizard.welcome.text', 'setup.wizard.welcome.text',
['version' => config('app.version')] ['version' => config('app.version')]
)); ));
@ -103,45 +101,45 @@ class MiddlewareTest extends TestCase
public function testCheckPlayerExist() public function testCheckPlayerExist()
{ {
$this->get('/nope.json') $this->getJson('/nope.json')
->assertResponseStatus(404) ->assertStatus(404)
->see('Un-existent player'); ->assertSee('Un-existent player');
$this->get('/skin/nope.png') $this->get('/skin/nope.png')
->assertResponseStatus(404) ->assertStatus(404)
->see('Un-existent player'); ->assertSee('Un-existent player');
Option::set('return_200_when_notfound', true); Option::set('return_200_when_notfound', true);
$this->get('/nope.json') $this->getJson('/nope.json')
->assertResponseStatus(200) ->assertSuccessful()
->seeJson([ ->assertJson([
'player_name' => 'nope', 'player_name' => 'nope',
'errno' => 404, 'errno' => 404,
'msg' => 'Player Not Found.' 'msg' => 'Player Not Found.'
]); ]);
$player = factory(App\Models\Player::class)->create(); $player = factory(App\Models\Player::class)->create();
$this->get("/{$player->player_name}.json") $this->getJson("/{$player->player_name}.json")
->seeJson(['username' => $player->player_name]); // Default is CSL API ->assertJson(['username' => $player->player_name]); // Default is CSL API
$this->expectsEvents(\App\Events\CheckPlayerExists::class); $this->expectsEvents(\App\Events\CheckPlayerExists::class);
$this->get("/{$player->player_name}.json"); $this->getJson("/{$player->player_name}.json");
$player = factory(\App\Models\Player::class)->create(); $player = factory(\App\Models\Player::class)->create();
$user = \App\Models\User::find($player->uid); $user = \App\Models\User::find($player->uid);
$this->actAs($user) $this->actAs($user)
->post('/user/player/rename', [ ->postJson('/user/player/rename', [
'pid' => -1, 'pid' => -1,
'new_player_name' => 'name' 'new_player_name' => 'name'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('general.unexistent-player') 'msg' => trans('general.unexistent-player')
]); ]);
$this->actAs($user) $this->actAs($user)
->post('/user/player/rename', [ ->postJson('/user/player/rename', [
'pid' => $player->pid, 'pid' => $player->pid,
'new_player_name' => 'name' 'new_player_name' => 'name'
])->seeJson([ ])->assertJson([
'errno' => 0 'errno' => 0
]); ]);
} }
@ -153,50 +151,44 @@ class MiddlewareTest extends TestCase
$owner = \App\Models\User::find($player->uid); $owner = \App\Models\User::find($player->uid);
$this->actAs($other_user) $this->actAs($other_user)
->visit('/user/player') ->get('/user/player')
->assertResponseStatus(200); ->assertSuccessful();
$this->actAs($other_user) $this->actAs($other_user)
->post('/user/player/rename', [ ->postJson('/user/player/rename', [
'pid' => $player->pid 'pid' => $player->pid
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('admin.players.no-permission') 'msg' => trans('admin.players.no-permission')
]); ]);
$this->actAs($owner) $this->actAs($owner)
->post('/user/player/rename', [ ->postJson('/user/player/rename', [
'pid' => $player->pid, 'pid' => $player->pid,
'new_player_name' => 'name' 'new_player_name' => 'name'
])->seeJson([ ])->assertJson([
'errno' => 0 'errno' => 0
]); ]);
} }
public function testRedirectIfAuthenticated() public function testRedirectIfAuthenticated()
{ {
$this->visit('/auth/login') $this->get('/auth/login')
->seePageIs('/auth/login') ->assertViewIs('auth.login')
->dontSee('User Center'); ->assertDontSee('User Center');
$user = factory(\App\Models\User::class)->create(); $user = factory(\App\Models\User::class)->create();
$this->withSession(['uid' => $user->uid]) $this->withSession(['uid' => $user->uid])
->visit('/auth/login') ->get('/auth/login')
->see('Invalid token'); ->assertRedirect('/auth/login');
$this->withSession(['uid' => $user->uid, 'token' => 'nothing']) $this->withSession(['uid' => $user->uid, 'token' => 'nothing'])
->visit('/auth/login') ->get('/auth/login')
->seePageIs('/auth/login') ->assertRedirect('/auth/login');
->see(trans('auth.check.token'));
$this->actAs('normal') $this->actAs('normal')
->visit('/auth/login') ->get('/auth/login')
->seePageIs('/user'); ->assertRedirect('/user');
}
public function testRedirectIfUrlEndsWithSlash()
{
$this->visit('/auth/login/')->seePageIs('/auth/login');
} }
} }