简体   繁体   English

使用RSpec for API进行设备登录测试

[英]Devise Signin Testing with RSpec for API

      authenticate_with_http_token do |token, options|
    auth_key = AuthKey.find_by(authentication_token: token)
    if auth_key.present?
      if auth_key.token_valid?
        auth_key.touch
        sign_in(:user, auth_key.user, store: false, bypass: false) unless current_user.present?
      else
        render json: { message: t('invalid_otp_access'), errors: [t('token_expired')] }, status: 401 and return
      end
    else
      render json: { message: t('invalid_access_message'), errors: [t('invalid_access')] }, status: 401 and return
    end
  end

i need to write spec for the above code, in my controller i am using current_user. 我需要为上述代码编写规范,在我的控制器中,我使用的是current_user。

My controller looks like below. 我的控制器如下所示。

  def index
schedules = params[:type] == "upcoming" ? :upcoming : :past
schedules = current_user.audit_schedules.send(schedules)
if schedules.present?
  paginate json: schedules, per_page:10, root: false, each_serializer: Api::V1::MyAuditSerializer
else
  render json: { message: t('.no_audits_scheduled'), errors: [] }
end
end

and i am trying to test my index with passing valid token and params 我正在尝试通过传递有效的令牌和参数来测试索引

context "with invalid attributes" do
  it "It will return list of audits" do
    request.headers["Authorization"] = "Token #{auth_key.authentication_token}"
    @request.env["devise.mapping"] = Devise.mappings[:user]
    get :index, { params: { type: "upcoming" } }
    expect(response.body).to eq 200
  end
end

the above spec returning body like 以上规格返回的身体像

<html><body>You are being <a href=\"http://test.host/users/sign_in\">redirected</a>.</body></html>

And in my spec helper i included devise helpers like 在我的规范助手中,我包括了一些设计助手,例如

  config.include Devise::TestHelpers, type: :controller

If i remove that helper current_user is always nil. 如果我删除该助手,current_user始终为nil。 if i add that helper it is redirecting like above, please let me know what i missed and how can i test those spec. 如果我添加了该助手,它会像上面那样重定向,请让我知道我错过了什么以及如何测试这些规格。

I think you want user_signed_in? 我认为您想要user_signed_in? vs. current_user.present? current_user.present? . This doesn't fix the problem. 这不能解决问题。

You're sure user is not null? 您确定用户不为空吗? and that the user has been confirmed if you're using confirmable? 并确认您是否正在使用用户确认用户?

Digging through the code, I see this: if options[:bypass] warden.session_serializer.store(resource, scope) elsif warden.user(scope) == resource && !options.delete(:force) # Do nothing. User already signed in and we are not forcing it. true else warden.set_user(resource, options.merge!(scope: scope)) end 仔细阅读代码,我会看到: if options[:bypass] warden.session_serializer.store(resource, scope) elsif warden.user(scope) == resource && !options.delete(:force) # Do nothing. User already signed in and we are not forcing it. true else warden.set_user(resource, options.merge!(scope: scope)) end if options[:bypass] warden.session_serializer.store(resource, scope) elsif warden.user(scope) == resource && !options.delete(:force) # Do nothing. User already signed in and we are not forcing it. true else warden.set_user(resource, options.merge!(scope: scope)) end if options[:bypass] warden.session_serializer.store(resource, scope) elsif warden.user(scope) == resource && !options.delete(:force) # Do nothing. User already signed in and we are not forcing it. true else warden.set_user(resource, options.merge!(scope: scope)) end source: https://github.com/hassox/warden/blob/906edf86c6c31be917a921097031b89361d022e8/lib/warden/proxy.rb if options[:bypass] warden.session_serializer.store(resource, scope) elsif warden.user(scope) == resource && !options.delete(:force) # Do nothing. User already signed in and we are not forcing it. true else warden.set_user(resource, options.merge!(scope: scope)) end来源: https : //github.com/hassox/warden/blob/906edf86c6c31be917a921097031b89361d022e8/lib/warden/proxy.rb

You can try adding :force which should force the setting of the user. 您可以尝试添加:force ,以强制设置用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM