fix tests of UserController

This commit is contained in:
Pig Fang 2018-07-13 16:24:19 +08:00
parent d1442902b2
commit 68d3fb1eb4

View File

@ -23,13 +23,13 @@ class UserControllerTest extends TestCase
$players_count = option('score_per_player') / option('user_initial_score'); $players_count = option('score_per_player') / option('user_initial_score');
$this->actAs($user) $this->actAs($user)
->visit('/user') ->get('/user')
->assertViewHas('user') ->assertViewHas('user')
->assertViewHas('statistics') ->assertViewHas('statistics')
->see(1 / $players_count * 100) // Players ->assertSee((string) (1 / $players_count * 100)) // Players
->see(0) // Storage ->assertSee('0') // Storage
->see(bs_announcement()) ->assertSee(bs_announcement())
->see($user->score); ->assertSee((string) $user->score);
} }
public function testSign() public function testSign()
@ -39,8 +39,8 @@ class UserControllerTest extends TestCase
// Success // Success
$this->actAs($user) $this->actAs($user)
->post('/user/sign') ->postJson('/user/sign')
->seeJson([ ->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.sign-success', ['score' => 50]), 'msg' => trans('user.sign-success', ['score' => 50]),
'score' => option('user_initial_score') + 50, 'score' => option('user_initial_score') + 50,
@ -53,8 +53,8 @@ class UserControllerTest extends TestCase
]); ]);
// Remaining time is greater than 0 // Remaining time is greater than 0
$this->post('/user/sign') $this->postJson('/user/sign')
->seeJson([ ->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans( 'msg' => trans(
'user.cant-sign-until', 'user.cant-sign-until',
@ -76,8 +76,8 @@ class UserControllerTest extends TestCase
$diff = round($diff / 60); $diff = round($diff / 60);
$unit = 'min'; $unit = 'min';
} }
$this->post('/user/sign') $this->postJson('/user/sign')
->seeJson([ ->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans( 'msg' => trans(
'user.cant-sign-until', 'user.cant-sign-until',
@ -90,15 +90,15 @@ class UserControllerTest extends TestCase
$user->last_sign_at = \Carbon\Carbon::today()->toDateTimeString(); $user->last_sign_at = \Carbon\Carbon::today()->toDateTimeString();
$user->save(); $user->save();
$this->post('/user/sign') $this->postJson('/user/sign')
->seeJson([ ->assertJson([
'errno' => 0 'errno' => 0
]); ]);
} }
public function testProfile() public function testProfile()
{ {
$this->visit('/user/profile') $this->get('/user/profile')
->assertViewHas('user'); ->assertViewHas('user');
} }
@ -109,283 +109,283 @@ class UserControllerTest extends TestCase
// Invalid action // Invalid action
$this->actAs($user) $this->actAs($user)
->post('/user/profile') ->postJson('/user/profile')
->seeJson([ ->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('general.illegal-parameters') 'msg' => trans('general.illegal-parameters')
]); ]);
// Change nickname without `new_nickname` field // Change nickname without `new_nickname` field
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'nickname' 'action' => 'nickname'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'new nickname']) 'msg' => trans('validation.required', ['attribute' => 'new nickname'])
]); ]);
// Invalid nickname // Invalid nickname
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'nickname', 'action' => 'nickname',
'new_nickname' => '\\' 'new_nickname' => '\\'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.no_special_chars', ['attribute' => 'new nickname']) 'msg' => trans('validation.no_special_chars', ['attribute' => 'new nickname'])
]); ]);
// Too long nickname // Too long nickname
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'nickname', 'action' => 'nickname',
'new_nickname' => str_random(256) 'new_nickname' => str_random(256)
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'new nickname', 'max' => 255]) 'msg' => trans('validation.max.string', ['attribute' => 'new nickname', 'max' => 255])
]); ]);
// Change nickname successfully // Change nickname successfully
$this->expectsEvents(Events\UserProfileUpdated::class); $this->expectsEvents(Events\UserProfileUpdated::class);
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'nickname', 'action' => 'nickname',
'new_nickname' => 'nickname' 'new_nickname' => 'nickname'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.profile.nickname.success', ['nickname' => 'nickname']) 'msg' => trans('user.profile.nickname.success', ['nickname' => 'nickname'])
]); ]);
$this->assertEquals('nickname', User::find($user->uid)->nickname); $this->assertEquals('nickname', User::find($user->uid)->nickname);
// Change password without `current_password` field // Change password without `current_password` field
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'password' 'action' => 'password'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'current password']) 'msg' => trans('validation.required', ['attribute' => 'current password'])
]); ]);
// Too short current password // Too short current password
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'password', 'action' => 'password',
'current_password' => '1', 'current_password' => '1',
'new_password' => '12345678' 'new_password' => '12345678'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'current password', 'min' => 6]) 'msg' => trans('validation.min.string', ['attribute' => 'current password', 'min' => 6])
]); ]);
// Too long current password // Too long current password
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'password', 'action' => 'password',
'current_password' => str_random(33), 'current_password' => str_random(33),
'new_password' => '12345678' 'new_password' => '12345678'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'current password', 'max' => 32]) 'msg' => trans('validation.max.string', ['attribute' => 'current password', 'max' => 32])
]); ]);
// Too short new password // Too short new password
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'password', 'action' => 'password',
'current_password' => '12345678', 'current_password' => '12345678',
'new_password' => '1' 'new_password' => '1'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'new password', 'min' => 8]) 'msg' => trans('validation.min.string', ['attribute' => 'new password', 'min' => 8])
]); ]);
// Too long new password // Too long new password
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'password', 'action' => 'password',
'current_password' => '12345678', 'current_password' => '12345678',
'new_password' => str_random(33) 'new_password' => str_random(33)
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'new password', 'max' => 32]) 'msg' => trans('validation.max.string', ['attribute' => 'new password', 'max' => 32])
]); ]);
// Wrong old password // Wrong old password
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'password', 'action' => 'password',
'current_password' => '1234567', 'current_password' => '1234567',
'new_password' => '87654321' 'new_password' => '87654321'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('user.profile.password.wrong-password') 'msg' => trans('user.profile.password.wrong-password')
]); ]);
// Change password successfully // Change password successfully
$this->expectsEvents(Events\EncryptUserPassword::class); $this->expectsEvents(Events\EncryptUserPassword::class);
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'password', 'action' => 'password',
'current_password' => '12345678', 'current_password' => '12345678',
'new_password' => '87654321' 'new_password' => '87654321'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.profile.password.success') 'msg' => trans('user.profile.password.success')
]); ]);
$this->assertTrue(User::find($user->uid)->verifyPassword('87654321')); $this->assertTrue(User::find($user->uid)->verifyPassword('87654321'));
// After changed password, user should re-login. // After changed password, user should re-login.
$this->visit('/user')->seePageIs('/auth/login'); $this->get('/user')->assertRedirect('/auth/login');
$user = User::find($user->uid); $user = User::find($user->uid);
// Change email without `new_email` field // Change email without `new_email` field
$this->actAs($user) $this->actAs($user)
->post( ->postJson(
'/user/profile', '/user/profile',
['action' => 'email'], ['action' => 'email'],
['X-Requested-With' => 'XMLHttpRequest']) ['X-Requested-With' => 'XMLHttpRequest'])
->seeJson([ ->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'new email']) 'msg' => trans('validation.required', ['attribute' => 'new email'])
]); ]);
// Invalid email // Invalid email
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'email', 'action' => 'email',
'new_email' => 'not_an_email' 'new_email' => 'not_an_email'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.email', ['attribute' => 'new email']) 'msg' => trans('validation.email', ['attribute' => 'new email'])
]); ]);
// Too short current password // Too short current password
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'email', 'action' => 'email',
'new_email' => 'a@b.c', 'new_email' => 'a@b.c',
'password' => '1' 'password' => '1'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 6]) 'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 6])
]); ]);
// Too long current password // Too long current password
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'email', 'action' => 'email',
'new_email' => 'a@b.c', 'new_email' => 'a@b.c',
'password' => str_random(33) 'password' => str_random(33)
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 32]) 'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 32])
]); ]);
// Use a duplicated email // Use a duplicated email
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'email', 'action' => 'email',
'new_email' => $user->email, 'new_email' => $user->email,
'password' => '87654321' 'password' => '87654321'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('user.profile.email.existed') 'msg' => trans('user.profile.email.existed')
]); ]);
// Wrong password // Wrong password
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'email', 'action' => 'email',
'new_email' => 'a@b.c', 'new_email' => 'a@b.c',
'password' => '7654321' 'password' => '7654321'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('user.profile.email.wrong-password') 'msg' => trans('user.profile.email.wrong-password')
]); ]);
// Change email successfully // Change email successfully
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'email', 'action' => 'email',
'new_email' => 'a@b.c', 'new_email' => 'a@b.c',
'password' => '87654321' 'password' => '87654321'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.profile.email.success') 'msg' => trans('user.profile.email.success')
]); ]);
$this->assertEquals('a@b.c', User::find($user->uid)->email); $this->assertEquals('a@b.c', User::find($user->uid)->email);
// After changed email, user should re-login. // After changed email, user should re-login.
$this->visit('/user')->seePageIs('/auth/login'); $this->get('/user')->assertRedirect('/auth/login');
$user = User::find($user->uid); $user = User::find($user->uid);
// Delete account without `password` field // Delete account without `password` field
$this->actAs($user) $this->actAs($user)
->post( ->postJson(
'/user/profile', '/user/profile',
['action' => 'delete'], ['action' => 'delete'],
['X-Requested-With' => 'XMLHttpRequest']) ['X-Requested-With' => 'XMLHttpRequest'])
->seeJson([ ->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'password']) 'msg' => trans('validation.required', ['attribute' => 'password'])
]); ]);
// Too short current password // Too short current password
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'delete', 'action' => 'delete',
'password' => '1' 'password' => '1'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 6]) 'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 6])
]); ]);
// Too long current password // Too long current password
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'delete', 'action' => 'delete',
'password' => str_random(33) 'password' => str_random(33)
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 32]) 'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 32])
]); ]);
// Wrong password // Wrong password
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'delete', 'action' => 'delete',
'password' => '7654321' 'password' => '7654321'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('user.profile.delete.wrong-password') 'msg' => trans('user.profile.delete.wrong-password')
]); ]);
// Delete account successfully // Delete account successfully
$this->post('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'delete', 'action' => 'delete',
'password' => '87654321' 'password' => '87654321'
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.profile.delete.success') 'msg' => trans('user.profile.delete.success')
])->seeCookie('uid', '') ])->assertCookie('uid', '')
->seeCookie('token', ''); ->assertCookie('token', '');
$this->assertNull(User::find($user->uid)); $this->assertNull(User::find($user->uid));
} }
@ -397,52 +397,52 @@ class UserControllerTest extends TestCase
// Without `tid` field // Without `tid` field
$this->actAs($user) $this->actAs($user)
->post('/user/profile/avatar', [], [ ->postJson('/user/profile/avatar', [], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
]) ])
->seeJson([ ->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid']) 'msg' => trans('validation.required', ['attribute' => 'tid'])
]); ]);
// TID is not a integer // TID is not a integer
$this->actAs($user) $this->actAs($user)
->post('/user/profile/avatar', [ ->postJson('/user/profile/avatar', [
'tid' => 'string' 'tid' => 'string'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
]) ])
->seeJson([ ->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid']) 'msg' => trans('validation.integer', ['attribute' => 'tid'])
]); ]);
// Texture cannot be found // Texture cannot be found
$this->actAs($user) $this->actAs($user)
->post('/user/profile/avatar', [ ->postJson('/user/profile/avatar', [
'tid' => 0 'tid' => 0
]) ])
->seeJson([ ->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('skinlib.non-existent') 'msg' => trans('skinlib.non-existent')
]); ]);
// Use cape // Use cape
$this->actAs($user) $this->actAs($user)
->post('/user/profile/avatar', [ ->postJson('/user/profile/avatar', [
'tid' => $cape->tid 'tid' => $cape->tid
]) ])
->seeJson([ ->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('user.profile.avatar.wrong-type') 'msg' => trans('user.profile.avatar.wrong-type')
]); ]);
// Success // Success
$this->actAs($user) $this->actAs($user)
->post('/user/profile/avatar', [ ->postJson('/user/profile/avatar', [
'tid' => $steve->tid 'tid' => $steve->tid
]) ])
->seeJson([ ->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.profile.avatar.success') 'msg' => trans('user.profile.avatar.success')
]); ]);