fix tests of AuthController

This commit is contained in:
Pig Fang 2018-07-13 15:13:35 +08:00
parent e28f01abf3
commit aa68641eba

View File

@ -14,7 +14,7 @@ class AuthControllerTest extends TestCase
public function testLogin()
{
$this->visit('/auth/login')->see('Log in');
$this->get('/auth/login')->assertSee('Log in');
}
public function testHandleLogin()
@ -31,45 +31,45 @@ class AuthControllerTest extends TestCase
);
// Should return a warning if `identification` is empty
$this->post(
$this->postJson(
'/auth/login', [], [
'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => trans('auth.identification')])
]);
// Should return a warning if `password` is empty
$this->post(
$this->postJson(
'/auth/login', [
'identification' => $user->email
], [
'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'password'])
]);
// Should return a warning if length of `password` is lower than 6
$this->post(
$this->postJson(
'/auth/login', [
'identification' => $user->email,
'password' => '123'
], [
'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 6])
]);
// Should return a warning if length of `password` is greater than 32
$this->post(
$this->postJson(
'/auth/login', [
'identification' => $user->email,
'password' => str_random(80)
], [
'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 32])
]);
@ -77,11 +77,11 @@ class AuthControllerTest extends TestCase
$this->flushSession();
// Logging in should be failed if password is wrong
$this->post(
$this->postJson(
'/auth/login', [
'identification' => $user->email,
'password' => 'wrong-password'
])->seeJson(
])->assertJson(
[
'errno' => 1,
'msg' => trans('auth.validation.password'),
@ -97,12 +97,12 @@ class AuthControllerTest extends TestCase
'login_fails' => 4,
'phrase' => 'a'
]
)->post(
)->postJson(
'/auth/login', [
'identification' => $user->email,
'password' => '12345678',
'captcha' => 'b'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('auth.validation.captcha')
]);
@ -110,11 +110,11 @@ class AuthControllerTest extends TestCase
$this->flushSession();
// Should return a warning if user isn't existed
$this->post(
$this->postJson(
'/auth/login', [
'identification' => 'nope@nope.net',
'password' => '12345678'
])->seeJson([
])->assertJson([
'errno' => 2,
'msg' => trans('auth.validation.user')
]);
@ -122,10 +122,10 @@ class AuthControllerTest extends TestCase
$this->flushSession();
// Should clean the `login_fails` session if logged in successfully
$this->withSession(['login_fails' => 1])->post('/auth/login', [
$this->withSession(['login_fails' => 1])->postJson('/auth/login', [
'identification' => $user->email,
'password' => '12345678'
])->seeJson(
])->assertJson(
[
'errno' => 0,
'msg' => trans('auth.login.success'),
@ -136,19 +136,19 @@ class AuthControllerTest extends TestCase
$this->flushSession();
// Logged in should be in success if logged in with player name
$this->post(
$this->postJson(
'/auth/login', [
'identification' => $player->player_name,
'password' => '12345678'
]
)->seeJson(
)->assertJson(
[
'errno' => 0,
'msg' => trans('auth.login.success'),
'token' => $user->getToken()
]
)->seeCookie('uid', $user->uid)
->seeCookie('token', $user->getToken())
)->assertCookie('uid', $user->uid)
->assertCookie('token', $user->getToken())
->assertSessionHasAll(
[
'uid' => $user->uid,
@ -166,7 +166,7 @@ class AuthControllerTest extends TestCase
'uid' => $user->uid,
'token' => $user->getToken()
]
)->post('/auth/logout')->seeJson(
)->postJson('/auth/logout')->assertJson(
[
'errno' => 0,
'msg' => trans('auth.logout.success')
@ -174,8 +174,8 @@ class AuthControllerTest extends TestCase
)->assertSessionMissing(['uid', 'token']);
$this->flushSession();
$this->post('/auth/logout')
->seeJson([
$this->postJson('/auth/logout')
->assertJson([
'errno' => 1,
'msg' => trans('auth.logout.fail')
]);
@ -183,10 +183,10 @@ class AuthControllerTest extends TestCase
public function testRegister()
{
$this->visit('/auth/register')->see('Register');
$this->get('/auth/register')->assertSee('Register');
option(['user_can_register' => false]);
$this->visit('/auth/register')->see(trans('auth.register.close'));
$this->get('/auth/register')->assertSee(trans('auth.register.close'));
}
public function testHandleRegister()
@ -195,10 +195,10 @@ class AuthControllerTest extends TestCase
// Should return a warning if `captcha` is wrong
$this->withSession(['phrase' => 'a'])
->post(
->postJson(
'/auth/register', [], [
'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('auth.validation.captcha')
]);
@ -206,43 +206,43 @@ class AuthControllerTest extends TestCase
// Once we have sent session in the last assertion,
// we don't need to send it again until we flush it.
// Should return a warning if `email` is empty
$this->post(
$this->postJson(
'/auth/register',
['captcha' => 'a'],
['X-Requested-With' => 'XMLHttpRequest']
)->seeJson([
)->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'email'])
]);
// Should return a warning if `email` is invalid
$this->post(
$this->postJson(
'/auth/register',
[
'email' => 'not_an_email',
'captcha' => 'a'
],
['X-Requested-With' => 'XMLHttpRequest']
)->seeJson([
)->assertJson([
'errno' => 1,
'msg' => trans('validation.email', ['attribute' => 'email'])
]);
// Should return a warning if `password` is empty
$this->post(
$this->postJson(
'/auth/register',
[
'email' => 'a@b.c',
'captcha' => 'a'
],
['X-Requested-With' => 'XMLHttpRequest']
)->seeJson([
)->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'password'])
]);
// Should return a warning if length of `password` is lower than 8
$this->post(
$this->postJson(
'/auth/register',
[
'email' => 'a@b.c',
@ -250,13 +250,13 @@ class AuthControllerTest extends TestCase
'captcha' => 'a'
],
['X-Requested-With' => 'XMLHttpRequest']
)->seeJson([
)->assertJson([
'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 8])
]);
// Should return a warning if length of `password` is greater than 32
$this->post(
$this->postJson(
'/auth/register',
[
'email' => 'a@b.c',
@ -264,13 +264,13 @@ class AuthControllerTest extends TestCase
'captcha' => 'a'
],
['X-Requested-With' => 'XMLHttpRequest']
)->seeJson([
)->assertJson([
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 32])
]);
// Should return a warning if `nickname` is empty
$this->post(
$this->postJson(
'/auth/register',
[
'email' => 'a@b.c',
@ -278,13 +278,13 @@ class AuthControllerTest extends TestCase
'captcha' => 'a'
],
['X-Requested-With' => 'XMLHttpRequest']
)->seeJson([
)->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'nickname'])
]);
// Should return a warning if `nickname` is invalid
$this->post(
$this->postJson(
'/auth/register',
[
'email' => 'a@b.c',
@ -293,13 +293,13 @@ class AuthControllerTest extends TestCase
'captcha' => 'a'
],
['X-Requested-With' => 'XMLHttpRequest']
)->seeJson([
)->assertJson([
'errno' => 1,
'msg' => trans('validation.no_special_chars', ['attribute' => 'nickname'])
]);
// Should return a warning if `nickname` is too long
$this->post(
$this->postJson(
'/auth/register',
[
'email' => 'a@b.c',
@ -308,14 +308,14 @@ class AuthControllerTest extends TestCase
'captcha' => 'a'
],
['X-Requested-With' => 'XMLHttpRequest']
)->seeJson([
)->assertJson([
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'nickname', 'max' => 255])
]);
// Should be forbidden if registering is closed
Option::set('user_can_register', false);
$this->post(
$this->postJson(
'/auth/register',
[
'email' => 'a@b.c',
@ -324,7 +324,7 @@ class AuthControllerTest extends TestCase
'captcha' => 'a'
],
['X-Requested-With' => 'XMLHttpRequest']
)->seeJson([
)->assertJson([
'errno' => 7,
'msg' => trans('auth.register.close')
]);
@ -334,7 +334,7 @@ class AuthControllerTest extends TestCase
// Should be forbidden if registering's count current IP is over
Option::set('regs_per_ip', -1);
$this->post(
$this->postJson(
'/auth/register',
[
'email' => 'a@b.c',
@ -342,7 +342,7 @@ class AuthControllerTest extends TestCase
'nickname' => 'nickname',
'captcha' => 'a'
]
)->seeJson([
)->assertJson([
'errno' => 7,
'msg' => trans('auth.register.max', ['regs' => option('regs_per_ip')])
]);
@ -351,7 +351,7 @@ class AuthControllerTest extends TestCase
// Should return a warning if using a duplicated email
$existedUser = factory(User::class)->create();
$this->post(
$this->postJson(
'/auth/register',
[
'email' => $existedUser->email,
@ -359,13 +359,13 @@ class AuthControllerTest extends TestCase
'nickname' => 'nickname',
'captcha' => 'a'
]
)->seeJson([
)->assertJson([
'errno' => 5,
'msg' => trans('auth.register.registered')
]);
// Database should be updated if succeeded
$response = $this->post(
$response = $this->postJson(
'/auth/register',
[
'email' => 'a@b.c',
@ -375,14 +375,14 @@ class AuthControllerTest extends TestCase
]
);
$newUser = User::where('email', 'a@b.c')->first();
$response->seeJson([
$response->assertJson([
'errno' => 0,
'msg' => trans('auth.register.success'),
'token' => $newUser->getToken()
])->seeCookie('uid', $newUser->uid)
->seeCookie('token', $newUser->getToken());
])->assertCookie('uid', $newUser->uid)
->assertCookie('token', $newUser->getToken());
$this->assertTrue($newUser->verifyPassword('12345678'));
$this->seeInDatabase('users', [
$this->assertDatabaseHas('users', [
'email' => 'a@b.c',
'nickname' => 'nickname',
'score' => option('user_initial_score'),
@ -393,36 +393,36 @@ class AuthControllerTest extends TestCase
public function testForgot()
{
$this->visit('/auth/forgot')->see('Forgot Password');
$this->get('/auth/forgot')->assertSee('Forgot Password');
config(['mail.host' => '']);
$this->visit('/auth/forgot')->see(trans('auth.forgot.close'));
$this->get('/auth/forgot')->assertSee(trans('auth.forgot.close'));
}
public function testHandleForgot()
{
// Should return a warning if `captcha` is wrong
$this->withSession(['phrase' => 'a'])->post('/auth/forgot', [
$this->withSession(['phrase' => 'a'])->postJson('/auth/forgot', [
'captcha' => 'b'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('auth.validation.captcha')
]);
// Should be forbidden if "forgot password" is closed
config(['mail.host' => '']);
$this->withSession(['phrase' => 'a'])->post('/auth/forgot', [
$this->withSession(['phrase' => 'a'])->postJson('/auth/forgot', [
'captcha' => 'a'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('auth.forgot.close')
]);
config(['mail.host' => 'localhost']);
// Should be forbidden if sending email frequently
$this->withSession(['last_mail_time' => time()])->post('/auth/forgot', [
$this->withSession(['last_mail_time' => time()])->postJson('/auth/forgot', [
'captcha' => 'a'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('auth.forgot.frequent-mail')
]);
@ -430,10 +430,10 @@ class AuthControllerTest extends TestCase
// Should return a warning if user is not existed
$this->flushSession();
$user = factory(User::class)->create();
$this->withSession(['phrase' => 'a'])->post('/auth/forgot', [
$this->withSession(['phrase' => 'a'])->postJson('/auth/forgot', [
'email' => 'nope@nope.net',
'captcha' => 'a'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('auth.forgot.unregistered')
]);
@ -473,10 +473,10 @@ class AuthControllerTest extends TestCase
return true;
})
);
$this->post('/auth/forgot', [
$this->postJson('/auth/forgot', [
'email' => $user->email,
'captcha' => 'a'
])->seeJson([
])->assertJson([
'errno' => 0,
'msg' => trans('auth.mail.success')
])->assertSessionHas('last_mail_time');
@ -487,10 +487,10 @@ class AuthControllerTest extends TestCase
->andThrow(new \Mockery\Exception('A fake exception.'));
$this->flushSession();
$this->withSession(['phrase' => 'a'])
->post('/auth/forgot', [
->postJson('/auth/forgot', [
'email' => $user->email,
'captcha' => 'a'
])->seeJson([
])->assertJson([
'errno' => 2,
'msg' => trans('auth.mail.failed', ['msg' => 'A fake exception.'])
]);
@ -501,35 +501,29 @@ class AuthControllerTest extends TestCase
$user = factory(User::class)->create();
// Should be redirected if `uid` or `token` is empty
$this->visit('/auth/reset')
->seePageIs('/auth/login')
->see(trans('auth.check.anonymous'));
$this->get('/auth/reset')
->assertRedirect('/auth/login');
// Should be redirected if `uid` is invalid
$this->visit('/auth/reset?uid=-1&token=nothing')
->seePageIs('/auth/forgot')
->see(trans('auth.reset.invalid'));
$this->get('/auth/reset?uid=-1&token=nothing')
->assertRedirect('/auth/forgot');
// Should be redirected if `token` is invalid
$this->visit('/auth/reset?uid=' . $user->uid . '&token=nothing')
->seePageIs('/auth/forgot')
->see(trans('auth.reset.invalid'));
$this->get('/auth/reset?uid=' . $user->uid . '&token=nothing')
->assertRedirect('/auth/forgot');
// Should be redirected if expired
$token = base64_encode(
$user->getToken().substr(time() - 60 * 60 * 2, 4, 6).str_random(16)
);
$this->visit('/auth/reset?uid=' . $user->uid . '&token=' . $token)
->seePageIs('/auth/forgot')
->see(trans('auth.reset.expired'));
$this->get('/auth/reset?uid=' . $user->uid . '&token=' . $token)
->assertRedirect('/auth/forgot');
// Success
$token = base64_encode(
$user->getToken().substr(time(), 4, 6).str_random(16)
);
$uri = $this->visit('/auth/reset?uid=' . $user->uid . '&token=' . $token)
->currentUri;
$this->assertContains('/auth/reset', $uri);
$this->get('/auth/reset?uid=' . $user->uid . '&token=' . $token);
}
public function testHandleReset()
@ -537,64 +531,64 @@ class AuthControllerTest extends TestCase
$user = factory(User::class)->create();
// Should return a warning if `uid` is empty
$this->post('/auth/reset', [], [
$this->postJson('/auth/reset', [], [
'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'uid'])
]);
// Should return a warning if `uid` is not an integer
$this->post('/auth/reset', [
$this->postJson('/auth/reset', [
'uid' => 'string'
], [
'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'uid'])
]);
// Should return a warning if `password` is empty
$this->post(
$this->postJson(
'/auth/reset', [
'uid' => $user->uid
], [
'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'password'])
]);
// Should return a warning if `password` is too short
$this->post(
$this->postJson(
'/auth/reset', [
'uid' => $user->uid,
'password' => '123'
], [
'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('validation.min.string', ['attribute' => 'password', 'min' => 8])
]);
// Should return a warning if `password` is too long
$this->post(
$this->postJson(
'/auth/reset', [
'uid' => $user->uid,
'password' => str_random(33)
], [
'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('validation.max.string', ['attribute' => 'password', 'max' => 32])
]);
// Should be forbidden if `token` is missing
$this->post(
$this->postJson(
'/auth/reset', [
'uid' => $user->uid,
'password' => '12345678'
], ['X-Requested-With' => 'XMLHttpRequest'])->seeJson([
], ['X-Requested-With' => 'XMLHttpRequest'])->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'token'])
]);
@ -603,12 +597,12 @@ class AuthControllerTest extends TestCase
$token = base64_encode(
$user->getToken().substr(time() - 60 * 60 * 2, 4, 6).str_random(16)
);
$this->post(
$this->postJson(
'/auth/reset', [
'uid' => $user->uid,
'password' => '12345678',
'token' => $token
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('auth.reset.expired')
]);
@ -617,23 +611,23 @@ class AuthControllerTest extends TestCase
$token = base64_encode(
$user->getToken().substr(time(), 4, 6).str_random(16)
);
$this->post(
$this->postJson(
'/auth/reset', [
'uid' => -1,
'password' => '12345678',
'token' => $token
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('auth.reset.invalid')
]);
// Should be forbidden if `token` is invalid
$this->post(
$this->postJson(
'/auth/reset', [
'uid' => $user->uid,
'password' => '12345678',
'token' => 'invalid'
])->seeJson([
])->assertJson([
'errno' => 1,
'msg' => trans('auth.reset.invalid')
]);
@ -642,12 +636,12 @@ class AuthControllerTest extends TestCase
$token = base64_encode(
$user->getToken().substr(time(), 4, 6).str_random(16)
);
$this->post(
$this->postJson(
'/auth/reset', [
'uid' => $user->uid,
'password' => '12345678',
'token' => $token
])->seeJson([
])->assertJson([
'errno' => 0,
'msg' => trans('auth.reset.success')
]);
@ -665,8 +659,8 @@ class AuthControllerTest extends TestCase
}
$this->get('/auth/captcha')
->assertResponseStatus(200)
->seeHeader('Content-Type', 'image/png')
->assertSuccessful()
->assertHeader('Content-Type', 'image/png')
->assertSessionHas('phrase');
}
}