Rspec 使用令牌和SimpleTokenAuthentication登录用户的测试失败,因为SimpleTokenAuthentication未登录用户
轨道5.2 宝石: 我有以下控制器:Rspec 使用令牌和SimpleTokenAuthentication登录用户的测试失败,因为SimpleTokenAuthentication未登录用户,rspec,ruby-on-rails-5.2,http-token-authentication,Rspec,Ruby On Rails 5.2,Http Token Authentication,轨道5.2 宝石: 我有以下控制器: class RegistrationsController < ApplicationController acts_as_token_authentication_handler_for User, only: [:start] def start binding.pry if user_signed_in? redirect_to edit_user_path(current_user) e
class RegistrationsController < ApplicationController
acts_as_token_authentication_handler_for User, only: [:start]
def start
binding.pry
if user_signed_in?
redirect_to edit_user_path(current_user)
else
redirect_to new_user_session_path
end
end
end
以下是我得到的错误:
Failure/Error: expect(response).to redirect_to(edit_user_path(new_user))
Expected response to be a <3XX: redirect>, but was a <500: Internal Server Error>
失败/错误:预期(响应)。重定向到(编辑用户路径(新用户))
预期响应为a,但为a
似乎使用acts_as_token_authentication_handler_的登录过程出现了一些问题,但我无法理解
感谢您的帮助。最后,我没有使用SimpleTokenAuthentication,而是使用了我自己的代码,如下所示:
class RegistrationsController < ApplicationController
before_action :authenticate_user_from_token!, only: [:start]
def preview
end
def start
authorize :registration
if user_signed_in?
redirect_to edit_user_path(current_user)
else
raise Pundit::NotAuthorizedError
end
end
def calendar
authorize :registration
end
def confirmation
authorize :registration
current_user.register
end
private
def authenticate_user_from_token!
sign_out current_user if user_signed_in?
if user && Devise.secure_compare(user.authentication_token, params[:user_token])
sign_in user
@current_user = user
renew_authentication_token
end
end
def user
@user ||= User.find_by(email: params[:user_email])
end
def renew_authentication_token
current_user.renew_authentication_token!
end
end
类注册控制器
它可能与RegistrationController#start
操作中的绑定.pry
有关吗?另外,在哪里定义了用户登录?
方法?还有,它看起来像什么?谢谢。我认为问题在于SimpleTokenAuthentication不起作用。所以我自己对认证进行了编码,现在一切都正常了。
class RegistrationsController < ApplicationController
before_action :authenticate_user_from_token!, only: [:start]
def preview
end
def start
authorize :registration
if user_signed_in?
redirect_to edit_user_path(current_user)
else
raise Pundit::NotAuthorizedError
end
end
def calendar
authorize :registration
end
def confirmation
authorize :registration
current_user.register
end
private
def authenticate_user_from_token!
sign_out current_user if user_signed_in?
if user && Devise.secure_compare(user.authentication_token, params[:user_token])
sign_in user
@current_user = user
renew_authentication_token
end
end
def user
@user ||= User.find_by(email: params[:user_email])
end
def renew_authentication_token
current_user.renew_authentication_token!
end
end