Jer*_*nch 5 rspec ruby-on-rails devise
既然Devise已从数据库中删除了:confirmation_token,我该如何在rspec中返回设计确认令牌?
我试图通过手动访问带有确认令牌的user_confirmation路径来测试可确认模块.我怎样才能做到这一点?
Mar*_*rry 10
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)
Run Code Online (Sandbox Code Playgroud)
背景:正如Platformatec博客文章 "在数据库中存储消化的令牌"中所述,Devise 3.1+向用户发送"原始"确认令牌,并将加密版本保存在数据库中.当用户单击原始链接时,在搜索数据库之前会重新加密原始令牌.见send_confirmation_instructions及generate_confirmation_token此源代码:
https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb
此代码生成一对新的原始和加密令牌,使用新的加密版本更新数据库中的最后一个用户,然后visit是原始版本.令牌应匹配,并应确认帐户.
虽然不在原始问题中,但我也想测试Devise确认邮件中的正确链接,但我不认为这是可能的:当我创建用户时,确认电子邮件会立即发送,因此电子邮件的正文包含原始的原始令牌,而不是这里生成的新令牌.
小智 5
确认流程已更改.现在,确认流程使用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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3698 次 |
| 最近记录: |