我正在尝试为我们的登录过程进行集成测试,我们已经覆盖会话创建过程,如下所示:
module UsersDashboard
class SessionsController < Devise::SessionsController
protect_from_forgery :except => [:create, :mfa_callback, :authenticate]
def test
session[:test] = 'doodoo'
self.resource = warden.authenticate!(auth_options)
render text: 'hi'
end
# Used for User Dashboard login
def create
current_user = warden.authenticate!(auth_options)
resource = current_user
current_user.update_attribute(:mfa_authenticated, false)
# some code here
session[:channel] = 'some value'
p "session channel in create: #{session[:channel]}, #{session.class}"
redirect_url = 'some value here'
redirect_to redirect_url
end
end
这是我的测试代码:
require 'test_helper'
include Warden::Test::Helpers
class DashboardOkTest < ActionDispatch::IntegrationTest
test "Dashboard sign in and visiting various pages inside dashboard " do
post_via_redirect '/users_dashboard/sessions/mani'
p session[:test]
mani = users(:mani)
post_via_redirect '/users/sign_in', 'user[email]' => mani.email, 'user[password]' => '123456'
assert_response :success
assert_equal '/mfa/index', path
assert_select '#choose-mfa-method', 'Choose Your Acceptto Multi-Factor Auth Method:'
login_as mani, scope: :user
p "session: #{session[:channel]}, #{session.inspect}"
assert request.query_parameters.has_key?(:channel)
tfa = Tfa.find_by_channel(request.query_parameters[:channel])
assert tfa
tfa.update_attribute(:status, 'approved')
get "/users_dashboard/sessions/authenticate?channel=#{tfa.channel}"
end
end
正如你在输出中看到的那样:
"doodoo"
"session channel in create: 123, ActionDispatch::Request::Session"
"session: , #<ActionDispatch::Request::Session:0x7fabecaa9458 not yet loaded>"
"session channel is nil"
会话[:频道]无效,会话尚未加载!有趣的是,我已经在会话控制器中创建了一个测试方法,你可以看到,如果我们在调用self.resource = warden.authenticate之前设置session [:test]!(auth_options)会话工作并打印'doodoo',但它不是如果我们在self.resource = warden.authenticate!(auth_options)之前设置session [:channel],它仍然不起作用。
看起来设计有一些在创建调用后使会话无效的包装器。所有这些只是在测试中发生,而会话和其他一切都在web中的开发服务器中工作。
知道如何在create方法中设置测试会话吗?