prbaron
prbaron

Reputation: 867

Auth::attempt fails with PHPUnit TESTING

I have a really strange behavior and I do not see where it could come from. I built an authentication system with Laravel documentation. When I save a User and try to login from the application, it works fine. However when I do the same thing in my AuthControllerTest, the Auth::attempt fails.

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

Thank you

Upvotes: 0

Views: 1218

Answers (1)

Flavio
Flavio

Reputation: 398

Remember that the password is encrypted, so you need to get the password directly from the User instance, like:

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

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

Upvotes: 3

Related Questions