0

有没有办法知道 laravel fortify 在用户注册时如何生成令牌(通过发送带有令牌的电子邮件验证)以及用户重置密码时(使用重置密码链接发送的令牌)。我想打印/回显那些在单元测试期间使用它。

4

1 回答 1

0

我使用赛普拉斯执行前端测试,也需要这个。就我而言,我定义了 cypress 可以访问的特定路由以检索令牌。在控制器内部,我这样做:

public function show (Request $request) {
        $broker = Password::broker(config('fortify.passwords'));

        $token = $broker->createToken($request->user());

        return response()->json($token);
    }

这只是返回令牌。

我可以像这样运行测试:

it('Should be possible to access the password reset page', () => {
    cy.createUserAndLogin().then(user => {
      cy.resetToken().then(token => {
        cy.logout()

        // Visit the password reset page:
        cy.visit('/reset-password/' + token + '?email=' + user.email)

        // Fill the form:
        cy.get('[name=password]').type('A completely new Password 1')
        cy.get('[name=password_confirmation]').type('A completely new Password 1')
        cy.get('button.primary').click()

        // I should now be able to login:
        cy.visit('/login')
        cy.get('[name=email]').type(user.email)
        cy.get('[name=password]').type('A completely new Password 1')
        cy.get('#loginform').submit()

        cy.location('pathname').should('eq', '/spa')
      })
    })
  })

cy.resetToken().then(token => {})使用 cypress support 命令对服务器进行实际调用并返回令牌。

当然,诀窍就在cy.visit('/reset-password/' + token + '?email=' + user.email)眼前。您可以在前端测试系统中执行类似的操作。

于 2021-05-06T18:38:29.573 回答