mirror of
https://github.com/flarum/framework.git
synced 2025-02-17 02:02:47 +08:00
Improve auth API tests
This commit is contained in:
parent
526b7c588e
commit
f2ac56cb9c
|
@ -16,17 +16,20 @@ class Login extends Base
|
|||
*/
|
||||
protected function run()
|
||||
{
|
||||
$identifier = $this->input('identifier');
|
||||
$identification = $this->input('identification');
|
||||
$password = $this->input('password');
|
||||
$field = filter_var($identifier, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
|
||||
$credentials = [$field => $identifier, 'password' => $password];
|
||||
$field = filter_var($identification, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
|
||||
$credentials = [$field => $identification, 'password' => $password];
|
||||
|
||||
if (! Auth::attempt($credentials)) {
|
||||
if (! Auth::attempt($credentials, true)) {
|
||||
return $this->respondWithError('invalidLogin', 401);
|
||||
}
|
||||
|
||||
$token = Auth::user()->getRememberToken();
|
||||
$user = Auth::user();
|
||||
|
||||
return Response::json(compact('token'));
|
||||
return Response::json([
|
||||
'token' => $user->getRememberToken(),
|
||||
'userId' => $user->id
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,9 +12,9 @@ class ApiHelper extends \Codeception\Module
|
|||
return Factory::create('Flarum\Core\Users\User', $data);
|
||||
}
|
||||
|
||||
public function login($identifier, $password)
|
||||
public function login($identification, $password)
|
||||
{
|
||||
$this->getModule('REST')->sendPOST('/api/auth/login', ['identifier' => $identifier, 'password' => $password]);
|
||||
$this->getModule('REST')->sendPOST('/api/auth/login', ['identification' => $identification, 'password' => $password]);
|
||||
|
||||
$response = json_decode($this->getModule('REST')->grabResponse(), true);
|
||||
if ($response && is_array($response) && isset($response['token'])) {
|
||||
|
|
|
@ -16,11 +16,15 @@ class AuthCest
|
|||
'password' => 'pass7word'
|
||||
]);
|
||||
|
||||
$token = $I->login('foo@bar.com', 'pass7word');
|
||||
$I->login('foo@bar.com', 'pass7word');
|
||||
$I->seeResponseCodeIs(200);
|
||||
$I->seeResponseIsJson();
|
||||
|
||||
$loggedIn = User::where('remember_token', $token)->first();
|
||||
$token = $I->grabDataFromJsonResponse('token');
|
||||
$userId = $I->grabDataFromJsonResponse('userId');
|
||||
$I->assertNotEmpty($token);
|
||||
|
||||
$loggedIn = User::where('remember_token', $token)->where('id', $userId)->first();
|
||||
$I->assertEquals($user->id, $loggedIn->id);
|
||||
}
|
||||
|
||||
|
@ -33,11 +37,15 @@ class AuthCest
|
|||
'password' => 'pass7word'
|
||||
]);
|
||||
|
||||
$token = $I->login('tobscure', 'pass7word');
|
||||
$I->login('tobscure', 'pass7word');
|
||||
$I->seeResponseCodeIs(200);
|
||||
$I->seeResponseIsJson();
|
||||
|
||||
$loggedIn = User::where('remember_token', $token)->first();
|
||||
$token = $I->grabDataFromJsonResponse('token');
|
||||
$userId = $I->grabDataFromJsonResponse('userId');
|
||||
$I->assertNotEmpty($token);
|
||||
|
||||
$loggedIn = User::where('remember_token', $token)->where('id', $userId)->first();
|
||||
$I->assertEquals($user->id, $loggedIn->id);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user