From b57a8d3bc22d3ee20b90660ec620f1bc66dfd9e6 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 23 Jan 2015 15:24:38 +1030 Subject: [PATCH] Improve auth API tests --- src/Flarum/Api/Actions/Auth/Login.php | 15 +++++++++------ tests/_support/ApiHelper.php | 4 ++-- tests/api/AuthCest.php | 16 ++++++++++++---- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Flarum/Api/Actions/Auth/Login.php b/src/Flarum/Api/Actions/Auth/Login.php index 3473b670d..e68c0ab0e 100644 --- a/src/Flarum/Api/Actions/Auth/Login.php +++ b/src/Flarum/Api/Actions/Auth/Login.php @@ -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 + ]); } } diff --git a/tests/_support/ApiHelper.php b/tests/_support/ApiHelper.php index 722db46ef..1381f6d8a 100644 --- a/tests/_support/ApiHelper.php +++ b/tests/_support/ApiHelper.php @@ -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'])) { diff --git a/tests/api/AuthCest.php b/tests/api/AuthCest.php index c9179239f..a99b7b748 100644 --- a/tests/api/AuthCest.php +++ b/tests/api/AuthCest.php @@ -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); }