【问题标题】:How to test validation of current password in Laravel 5.8如何在 Laravel 5.8 中测试当前密码的验证
【发布时间】:2019-09-05 05:15:29
【问题描述】:

我正在尝试测试当前密码是否与数据库中的相同。

我的简化控制器:

class ChangePasswordController extends Controller
{
    public function update(Request $request, User $user)
    {
        $this->validate($request, [
            'current_password' => ['required', new CurrentPassword()],
            'password' => 'required|string|min:6|confirmed'
        ]);

        $user->update([
            'password' => bcrypt($request->password)
        ]);

    }
}

在我的自定义 CurrentPassword 规则中,我正在检查这样的哈希:

class CurrentPassword implements Rule
{

    public function passes($attribute, $value)
    {
        $check = Hash::check($value, auth()->user()->password);
        dump($check);
        return $check;
    }


    public function message()
    {
        return 'Current password is incorrect.';
    }
}

我对自定义规则的测试是:

/** @test */
public function an_authenticated_user_may_change_own_password()
{
    $this->withoutExceptionHandling();

    $user = factory(User::class)->create([
        'password' => '1234'
    ]);

    $this->actingAs($user)->patch("/profile/{$user->id}/password", [
        'current_password' => '1234',
        'password' => 'mynewpassword',
        'password_confirmation' => 'mynewpassword'
    ]);

    $this->assertTrue(Hash::check('mynewpassword', $user->fresh()->password));
}    

不幸的是,我遇到了一个错误:

1) Tests\Feature\UpdatePasswordTest::an_authenticated_user_may_change_own_password Illuminate\Validation\ValidationException:给定的数据无效。

我不明白为什么会这样。当我运行这个测试时,我的 dump($check); 返回 false。我的 $value 是 '1234' 并且 auth()->user()->password 也返回 '1234'。也许有人知道我做错了什么。

这个测试正在变绿:

 /** @test */
    public function current_password_must_be_valid()
    {
        $user = factory(User::class)->create([
            'password' => '1234'
        ]);

        $this->actingAs($user)->patch("/profile/{$user->id}/password", [
            'current_password' => '12345',
            'password' => 'mynewpassword',
            'password_confirmation' => 'mynewpassword'
        ])->assertSessionHasErrors('current_password');

    }

【问题讨论】:

    标签: laravel tdd


    【解决方案1】:

    您也应该在您的工厂中对您的密码进行哈希处理,否则 Eloquent 会将其以明文形式存储(这就是 auth()->user()->password 返回 '1234' 的原因)

    public function current_password_must_be_valid()
    {
        $user = factory(User::class)->create([
            'password' => Hash::make('1234'); // remember to import the Hash facade
        ]);
    
        ...
    }
    

    【讨论】:

    • 我已经更新了我的测试,但我还是变红了。 "$2y$04$C8lenwCy6SwJqFsuEcw0LOQpo/jeqGOCkgys2i/NUWzsaBuE3kVTW" "$2y$04$C8lenwCy6SwJqFsuEcw0LOQpo/jeqGOCkgys2i/NUWzsaBuE3kVTW" false。这是我在自定义规则中的转储($value // auth()->user()->password // Hash::check)。
    • Hash::check 将检查纯文本,因此 $value 应该是纯文本。在您的情况下,您似乎对其进行了哈希处理(因此它将哈希哈希)所以,为了确定,这应该在您的自定义规则中通过:Hash::check('1234', auth()->user()->password),对吧?
    • 你是对的。 $value 也被散列,这就是为什么测试是红色的。谢谢!
    猜你喜欢
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 2018-11-27
    • 2017-04-27
    • 2019-10-02
    • 2017-03-14
    相关资源
    最近更新 更多