mirror of
https://github.com/flarum/core.git
synced 2025-08-02 14:37:49 +02:00
Improve auth API tests
This commit is contained in:
@@ -16,17 +16,20 @@ class Login extends Base
|
|||||||
*/
|
*/
|
||||||
protected function run()
|
protected function run()
|
||||||
{
|
{
|
||||||
$identifier = $this->input('identifier');
|
$identification = $this->input('identification');
|
||||||
$password = $this->input('password');
|
$password = $this->input('password');
|
||||||
$field = filter_var($identifier, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
|
$field = filter_var($identification, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
|
||||||
$credentials = [$field => $identifier, 'password' => $password];
|
$credentials = [$field => $identification, 'password' => $password];
|
||||||
|
|
||||||
if (! Auth::attempt($credentials)) {
|
if (! Auth::attempt($credentials, true)) {
|
||||||
return $this->respondWithError('invalidLogin', 401);
|
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);
|
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);
|
$response = json_decode($this->getModule('REST')->grabResponse(), true);
|
||||||
if ($response && is_array($response) && isset($response['token'])) {
|
if ($response && is_array($response) && isset($response['token'])) {
|
||||||
|
@@ -16,11 +16,15 @@ class AuthCest
|
|||||||
'password' => 'pass7word'
|
'password' => 'pass7word'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$token = $I->login('foo@bar.com', 'pass7word');
|
$I->login('foo@bar.com', 'pass7word');
|
||||||
$I->seeResponseCodeIs(200);
|
$I->seeResponseCodeIs(200);
|
||||||
$I->seeResponseIsJson();
|
$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);
|
$I->assertEquals($user->id, $loggedIn->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,11 +37,15 @@ class AuthCest
|
|||||||
'password' => 'pass7word'
|
'password' => 'pass7word'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$token = $I->login('tobscure', 'pass7word');
|
$I->login('tobscure', 'pass7word');
|
||||||
$I->seeResponseCodeIs(200);
|
$I->seeResponseCodeIs(200);
|
||||||
$I->seeResponseIsJson();
|
$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);
|
$I->assertEquals($user->id, $loggedIn->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user