gpt4 book ai didi

ruby-on-rails - rails/RSpec : reset_session not changing Set-Cookie HTTP header value during integration tests

转载 作者:行者123 更新时间:2023-12-02 10:49:18 33 4
gpt4 key购买 nike

我正在编写一个集成测试,以确保我的网络应用程序不易受到 session 固定的影响。

我已经手动验证了 reset_session 实际上在身份验证逻辑中触发,并且当我使用网络浏览器登录时,cookie 确实发生了变化(因此,我不容易受到 session 的影响)不再固定),但我无法让我的 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 无法按预期工作的已知问题?我可以做什么来编写一个测试来验证 session 固定不是问题?

最佳答案

所以我最终确实解决了这个问题。

我试图直接编辑响应 header 来测试 cookie,但我想这不是一个好的方法。

无论如何,在 Rails 2.x 的集成测试中,您可以使用 cookies 哈希。测试结果如下:

  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)

关于ruby-on-rails - rails/RSpec : reset_session not changing Set-Cookie HTTP header value during integration tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4764126/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com