Improve auth API tests

This commit is contained in:
Toby Zerner 2015-01-23 15:24:38 +10:30
parent 526b7c588e
commit f2ac56cb9c
3 changed files with 23 additions and 12 deletions

View File

@ -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
]);
}
}

View File

@ -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'])) {

View File

@ -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);
}