【发布时间】:2013-12-01 04:46:08
【问题描述】:
既然 Devise 已经从数据库中删除了 :confirmation_token,我如何在 rspec 中返回设计确认令牌?
我正在尝试通过使用确认令牌手动访问 user_confirmation 路径来测试可确认模块。我怎样才能做到这一点?
【问题讨论】:
标签: ruby-on-rails rspec devise
既然 Devise 已经从数据库中删除了 :confirmation_token,我如何在 rspec 中返回设计确认令牌?
我正在尝试通过使用确认令牌手动访问 user_confirmation 路径来测试可确认模块。我怎样才能做到这一点?
【问题讨论】:
标签: ruby-on-rails rspec devise
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)
背景:正如this Platformatec blog post“在数据库中存储摘要令牌”中所述,Devise 3.1+ 向用户发送“原始”确认令牌,并将加密版本保存在数据库中。当用户点击原始链接时,原始令牌在数据库中搜索之前被重新加密。请参阅此源代码中的send_confirmation_instructions 和generate_confirmation_token:
https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb
此代码生成一对新的原始和加密令牌,用新的加密版本更新数据库中的最后一个用户,然后visits 原始版本。令牌应该匹配并且帐户应该被确认。
虽然不是在最初的问题中,但我也想测试设计确认邮件中的正确链接,但我认为这是不可能的:当我创建用户时,确认电子邮件会立即发送,所以电子邮件的body 包含原始原始令牌,而不是此处生成的新令牌。
【讨论】:
确认流程已更改。现在,确认流程使用 User.confirm_by_token。你可以这样做:
old_token = User.last.confirmation_token
new_token = Devise.token_generator.digest(User, :confirmation_token, old_token)
User.last.update_attribute(:confirmation_token, new_token)
visit user_confirmation_url(confirmation_token: old_token)
【讨论】: