解密Laravel密码重置令牌

Den*_*ebe 7 php unit-testing laravel laravel-5.3 laravel-5.4

我正在编写一个测试,确保我的应用程序的密码重置功能正常工作.密码重置系统是使用该php artisan make:auth命令创建的.为了使测试通过我需要自动化的GET请求来/password/reset/{$token}这里$token是存储在值password_resets表.Laravel像这样存储令牌:

$2y$10$9grKb3c6.Toiv0kjUWbCUeT8Q8D.Fg2gZ/xDLGQUAkmdyHigmRkNW

但是当Laravel将密码重置电子邮件发送给用户时,重置令牌在电子邮件中如下所示:

382aa64567ecd05a774c2e4ebb199d3340a1424300707053354c749c10487594.

/password/reset/$2y$10$9grKb3c6.Toiv0kjUWbCUeT8Q8D.Fg2gZ/xDLGQUAkmdyHigmRkNW由于重置令牌中的正斜杠,我的GET请求失败.(紧跟'g2gZ'之后)

我尝试使用辅助功能,decrypt()但没有运气.

如何转换从password_resets表中提取的密码重置令牌以匹配Laravel发送给用户的内容?

不确定这是否相关,但我确实将我的应用程序从5.3升级到5.4.

pin*_*eke 11

您可以从用于传递给Notification的assertSentTo方法的其他检查的闭包中获取令牌,因为它$token是标准ResetPassword通知的公共属性.

在你的测试中:

Notification::fake();

$this->postJson('api/user/reset', ['email' => $user->email])
    ->assertStatus(200);

$token = '';

Notification::assertSentTo(
    $this->user,
    \Illuminate\Auth\Notifications\ResetPassword::class,
    function ($notification, $channels) use (&$token) {
        $token = $notification->token;

        return true;
    });

$this->postJson('api/user/resetting', [
    'email' => $user->email,
    'token' => $token,
    'password' => '87538753',
    'password_confirmation' => '87538753'
])
    ->assertStatus(200);
Run Code Online (Sandbox Code Playgroud)

  • @pinguinjkeke 你能解释一下你为什么使用 Notification::fake(); 为什么我们在这里使用通知......? (2认同)
  • @usama Laravel 通知通道也可用于发送 Mailables。一段时间以来,标准密码重置邮件作为通知发送。我正在使用 ```Notification::fake()``` 用于测试目的并且调用假通知不会发送真实的通知,只是在测试中存储有关它的信息。检查此链接:https://laravel.com/docs/5.4/mocking#notification-fake (2认同)
  • 迄今为止我见过的最干净的解决方案。谢谢一堆。 (2认同)