2015-01-28 3 views
0

정말 이상한 행동이 있는데 어디에서 왔는지 알 수 없습니다. 나는 Laravel 문서로 인증 시스템을 구축했다. 사용자를 저장하고 응용 프로그램에서 로그인을 시도하면 정상적으로 작동합니다. 그러나 AuthControllerTest에서 동일한 작업을 수행하면 Auth :: 시도가 실패합니다. 당신은 사용자 인스턴스에서 직접 비밀번호를 발급받을 필요가 있으므로PHPUnit에서 Auth :: 시도가 실패합니다.

AuthControllerTest.php

<?php 
use tests\helpers\Factory; 

class AuthControllerTest extends ApiTester 
{ 
    use Factory; 

    public function setUp() 
    { 
     parent::setUp(); 
     Artisan::call('migrate'); 
     Route::enableFilters(); 
     Session::start(); 
    } 

/* 
This test fails 
I have a 401 instead of a 200 HTTP response code 
*/ 
    /** @test */ 
    public function it_should_log_in_user() 
    { 
     User::create([ 
      'email'   => '[email protected]', 
      'password'  => 'testing' 
     ]); 

     $credentials = [ 
      'email' => '[email protected]', 
      'password' => 'testing' 
     ]; 
    //dd(User::count() // I have 1 record 
     $this->getJson('login', 'POST', $credentials); 

     $this->assertResponseOk(); 
    } 

    /** @test */ 
    public function it_should_throws_exception_if_login_fails() 
    { 
     User::create([ 
      'email' => '[email protected]', 
      'password' => 'testing' 
     ]); 
     $this->getJson('login', 'POST', [ 
      'email' => '[email protected]', 
      'password' => 'test' 
     ]); 
     $this->assertResponseStatus(401); 
    } 

    /** @test */ 
    public function it_should_log_out_user() 
    { 
     $user = User::create([ 
      'email' => '[email protected]', 
      'password' => 'password' 
     ]); 
     $this->be($user); 

     $this->getJson('logout', 'POST'); 
     $this->assertResponseStatus(204); 
    } 

    /** 
    * Generate Alert mock 
    * @return array 
    */ 
    protected function getStub() 
    { 
     return [ 
     ]; 
    } 
} 

AuthController.php

<?php 


use Arato\Transformers\UserTransformer; 
use controllers\ApiController; 

class AuthController extends ApiController 
{ 
    protected $userTransformer; 

    function __construct(UserTransformer $userTransformer) 
    { 
     $this->userTransformer = $userTransformer; 
    } 

    public function login() 
    { 
     $rules = [ 
      'email' => ['required', 'email'], 
      'password' => ['required', 'alphaNum'] 
     ]; 

     $validator = Validator::make(Input::all(), $rules); 
     if ($validator->fails()) { 
      return $this->respondUnauthorized(); 
     } 

     $userData = [ 
      'email' => Input::get('email'), 
      'password' => Input::get('password') 
     ]; 

     if (Auth::attempt($userData)) { 
      return $this->respond([ 
       'data' => $this->userTransformer->transform(Auth::user()) 
      ]); 
     } else { 
      return $this->respondUnauthorized(); 
     } 
    } 

    public function logout() 
    { 
     Auth::logout(); 

     return $this->respondNoContent(); 
    } 
} 

는, 당신이

+0

해시 :: make ($ password) – gxela

답변

2

비밀번호가 암호화되어 기억 감사 like :

 $user = User::create([ 
      'email'   => '[email protected]', 
      'password'  => Hash::make('testing') 
     ]); 

     $credentials = [ 
      'email' => $user->email 
      'password' => $user->password 
     ]; 
+0

고마워! 우리가 암호 키를 전달하고 사용자 테이블에 데이터를 저장했기 때문에 암호가 프레임 워크에 의해 해시되었다는 가정을했습니다 ... 내 밤을 저장했습니다! – prbaron

+0

당신을 환영합니다;) – Flavio

관련 문제