我有一个Cucumber场景,其中包含以下步骤:
Given /^I have logged in$/ do
visit root_url
fill_in 'username', with: 'testuser'
fill_in 'password', with: 'testpass'
click_button 'Log In'
end
如果我运行我的场景&在我的Rails日志中,我可以确认1)登录页面加载,2)当提交登录详细信息时,它们被接受并且所需的用户信息正确存储在Rails的session
对象中,然后3) 302重定向到登录后页面后会话数据丢失(因此403):
Started GET "/" for 127.0.0.1 at 2012-04-18 16:09:41 +0100
Processing by HomeController#index as HTML
Rendered home/index.html.erb within layouts/application (4.6ms)
Completed 200 OK in 9ms (Views: 8.3ms)
Started POST "/" for 127.0.0.1 at 2012-04-18 16:09:41 +0100
Processing by HomeController#index as HTML
Parameters: {"utf8"=>"✓", "is_submitted"=>"true", "username"=>"testuser", "password"=>"[FILTERED]", "commit"=>"Log In"}
DEBUG - login success
DEBUG - session: {"UserId"=>"19fd75c8-0e80-4832-94af-6a93ee74bf46", "Username"=>"testuser", "Password"=>"d68579bfdac2321d05f19042d8dbc49b9dd611c8", "Name"=>"Active User", "Active"=>true}
Redirected to http://www.example.com/app
Completed 302 Found in 3ms
Started GET "/app" for 127.0.0.1 at 2012-04-18 16:09:41 +0100
Processing by HomeController#app as HTML
DEBUG - session: {}
Redirected to http://www.example.com/
Completed 403 Forbidden in 1ms
正如您在第三个请求中看到的那样,会话对象为空(即使我们知道它在上一个操作中已正确设置)。似乎会话数据不会在重定向后的请求中持久存在,但我无法弄清楚为什么(我花了好几个小时!)。有什么想法吗?
答案 0 :(得分:1)
所以这是一个batshit ...如果我的应用程序控制器中的auth逻辑执行此操作(nb user
是一个哈希),它不工作...:< / p>
session.merge! user
但是这个 工作:
session['UserId'] = user['UserId']
session.merge! user
事实上,这也有效:
session[:make_sessions_work_in_capybara_ffs] = true
session.merge! user
因此,似乎在会话中设置密钥(任何密钥)会使Capybara识别/持久化会话cookie。出于某种原因。