现在 Devise 已从数据库中删除了 :confirmation_token,我如何在 rspec 中返回 devise 确认令牌?
我试图通过使用确认令牌手动访问 user_confirmation 路径来测试可确认模块。我怎样才能实现这个目标?
莱昂纳多·平托(Leonardo Pinto)的非常有用的答案略有不同:
# Generate new raw/encrypted confirmation token pair and update database.
# This lets us visit the new "raw" path to confirm the user.
raw_confirmation_token, db_confirmation_token =
Devise.token_generator.generate(User, :confirmation_token)
User.last.update_attribute(:confirmation_token, db_confirmation_token)
visit user_confirmation_url(confirmation_token: raw_confirmation_token)
背景:如中所述这篇 Platformatec 博客文章 http://blog.plataformatec.com.br/2013/08/devise-3-1-now-with-more-secure-defaults/在“将摘要令牌存储在数据库中”下,Devise 3.1+ 向用户发送“原始”确认令牌,并将加密版本保存在数据库中。当用户单击原始链接时,原始令牌会在数据库中搜索之前重新加密。看send_confirmation_instructions
and generate_confirmation_token
在此源代码中:
https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb
此代码生成一对新的原始令牌和加密令牌,用新的加密版本更新数据库中的最后一个用户,然后visit
是原始版本。令牌应匹配并且帐户应得到确认。
虽然不在最初的问题中,但我也想测试 Devise 确认邮件中的正确链接,但我认为这是不可能的:当我创建用户时,确认电子邮件会立即发送,因此电子邮件的正文包含原始原始令牌,而不是此处生成的新令牌。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)