【问题标题】:Auth::attempt fails with PHPUnit TESTINGAuth::attempt 因 PHPUnit TESTING 而失败
【发布时间】:2015-03-27 19:18:13
【问题描述】:

我有一个非常奇怪的行为,我不知道它可能来自哪里。我使用 Laravel 文档构建了一个身份验证系统。当我保存用户并尝试从应用程序登录时,它工作正常。但是,当我在 AuthControllerTest 中执行相同操作时,Auth::attempt 会失败。

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'          => 'testing@testing.com',
            'password'       => 'testing'
        ]);

        $credentials = [
            'email'    => 'testing@testing.com',
            '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'    => 'testing@testing.com',
            'password' => 'testing'
        ]);
        $this->getJson('login', 'POST', [
            'email'    => 'testing@testing.com',
            'password' => 'test'
        ]);
        $this->assertResponseStatus(401);
    }

    /** @test */
    public function it_should_log_out_user()
    {
        $user = User::create([
            'email'    => 'testing@testing.com',
            '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();
    }
}

谢谢

【问题讨论】:

  • 哈希::make($password)

标签: php laravel laravel-4 phpunit


【解决方案1】:

记住密码是加密的,所以需要直接从User实例中获取密码,比如:

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

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

【讨论】:

  • 谢谢!我假设密码是由框架散列的,因为我们将“密码”键传递给它,并且我们将数据保存到用户表中......你拯救了我的夜晚!
猜你喜欢
  • 1970-01-01
  • 2017-04-28
  • 2014-10-11
  • 1970-01-01
  • 2012-11-21
  • 2014-03-13
  • 2014-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多