我在我的应用程序中使用 Doorkeeper gem 作为 Oauth2 提供程序。我正在根据我的要求为 config/initializers/doorkeeper.rb 文件中的配置编写测试用例。我的 config/initializers/doorkeeper.rb 文件如下所示。我正在验证 resource_owner 取决于 session[:user_id] 是否存在或重定向到登录页面以验证 resource_owner。此代码在开发环境中执行良好并获取 session[:user_id] 值,但在测试环境中却没有。
Doorkeeper.configure do
# Change the ORM that doorkeeper will use.
# Currently supported options are :active_record, :mongoid2, :mongoid3, :mongo_mapper
orm :active_record
# if no scope was requested, this will be the default
default_scopes :public
# other available scopes
optional_scopes :admin, :write
# This block will be called to check whether the resource owner is authenticated or not.
resource_owner_authenticator do #|routes|
#fail "Please configure doorkeeper resource_owner_authenticator block located in #{__FILE__}"
# Put your resource owner authentication logic here.
# Example implementation:
# User.find_by_id(session[:user_id]) || redirect_to(new_user_session_url
@user = User.find_by_id(session[:user_id]) || redirect_to(login_url(return_to: request.fullpath))
end
end
我在测试用例中使用 oauth2 客户端来模拟对我的提供者应用程序中的 oauth2 提供者授权端点的请求,并将客户端连接适配器设置为 Rack。
@client = OAuth2::Client.new(@application.uid, @application.secret, :site => "http://127.0.0.1:3000") do |b|
b.request :url_encoded
b.adapter :rack, Rails.application
end
有没有其他方法可以模拟门卫配置的请求和编写测试用例,或者我在这里犯了什么错误?