RAILS/RSPEC:RESET_SESSION在集成测试期间不更改set-cookie http标头值
-
16-10-2019 - |
题
我正在编写集成测试,以确保我的WebApp不容易受到会话固定的影响。
我已经手动验证了 reset_session
实际上是在身份验证逻辑中启动,进一步是,当我使用Web浏览器登录时,cookie确实会发生变化(因此,我不再容易受到会话固定的影响),但是我无法获得我的RSPEC集成测试来成功成功验证这一点。
这是我的RSPEC集成测试。
require 'spec_helper'
describe "security" do
self.use_transactional_fixtures = false
append_after(:each) do
ALL_MODELS.each &:delete_all
end
describe "session fixation" do
it "should change the cookie session id after logging in" do
u = test_user :active_user => true,
:username => "nobody@example.com",
:password => "asdfasdf"
u.save!
https!
get_via_redirect "/login"
assert_response :success
cookie = response.header["Set-Cookie"].split(";").select{|x| x.match(/_session/)}[0].split("=")[1].strip
post_via_redirect "/login", "user[email]" => "nobody@example.com",
"user[password]" => "asdfasdf",
"user[remember_me]" => "1"
assert_response :success
path.should eql("/dashboard")
cookie.should_not eql(response.header["Set-Cookie"].split(";").select{|x| x.match(/_session/)}[0].split("=")[1].strip)
end
end
end
一切都起作用 除了 最后断言。 cookie不会改变。
RSPEC/Rails集成测试是否有任何已知问题 reset_session
无法正常工作?我该怎么做才能编写验证会话固定不是问题的测试?
解决方案
因此,我最终确实弄清楚了。
我试图直接编辑响应标头来测试cookie,但我想那不是幸福的方法。
无论如何,在与Rails 2.X的集成测试中,您可以使用cookie哈希。这是测试最终的样子:
u = test_user :active_user => true,
:username => "nobody@example.com",
:password => "asdfasdf"
u.save!
https!
get_via_redirect "/login"
assert_response :success
cookie = cookies['_session']
cookie.should be_present
path.should == "/login"
post_via_redirect "/login", "user[email]" => "nobody@example.com",
"user[password]" => "asdfasdf",
"user[remember_me]" => "1"
assert_response :success
path.should eql("/?login_success=1")
new_cookie = cookies['_session']
new_cookie.should be_present
cookie.should_not eql(new_cookie)
不隶属于 StackOverflow