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