1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
require 'test_helper'
class RedisStoreIntegrationTest < ::ActionDispatch::IntegrationTest
SessionKey = '_session_id'
SessionSecret = 'b3c631c314c0bbca50c1b2843150fe33'
test "reads the data" do
with_test_route_set do
get '/set_session_value'
assert_response :success
assert cookies['_session_id'].present?
get '/get_session_value'
assert_response :success
assert_equal response.body, 'foo: "bar"'
end
end
test "should get nil session value" do
with_test_route_set do
get '/get_session_value'
assert_response :success
assert_equal 'foo: nil', response.body
end
end
test "should delete the data after session reset" do
with_test_route_set do
get '/set_session_value'
assert_response :success
assert cookies['_session_id'].present?
session_cookie = cookies.send(:hash_for)['_session_id']
get '/call_reset_session'
assert_response :success
assert !headers['Set-Cookie'].blank?
cookies << session_cookie
get '/get_session_value'
assert_response :success
assert_equal 'foo: nil', response.body
end
end
test "should set a non-secure cookie by default" do
with_test_route_set do
https!
get '/set_session_value'
assert_response :success
cookie = cookies.instance_variable_get('@cookies').first
assert !cookie.secure?
end
end
test "should set a secure cookie when the 'secure' option is set" do
with_test_route_set(secure: true) do
https!
get '/set_session_value'
assert_response :success
cookie = cookies.instance_variable_get('@cookies').first
assert cookie.secure?
end
end
test "should set a signed cookie when the 'signed' option is set" do
with_test_route_set(signed: true) do
https!
get '/set_session_value'
assert_response :success
cookie = cookies.instance_variable_get('@cookies').first
assert_includes cookie.raw, '_session_id='
end
end
test "should set a http-only cookie by default" do
with_test_route_set do
get '/set_session_value'
assert_response :success
cookie = cookies.instance_variable_get('@cookies').first
options = cookie.instance_variable_get('@options')
assert options.key?('HttpOnly')
end
end
test "should set a non-http-only cookie when the 'httponlty' option is set to false" do
with_test_route_set(httponly: false) do
get '/set_session_value'
assert_response :success
cookie = cookies.instance_variable_get('@cookies').first
options = cookie.instance_variable_get('@options')
assert !options.key?('HttpOnly')
end
end
test "should not send cookies on write, not read" do
with_test_route_set do
get '/get_session_value'
assert_response :success
assert_equal 'foo: nil', response.body
assert cookies['_session_id'].nil?
end
end
test "should set session value after session reset" do
with_test_route_set do
get '/set_session_value'
assert_response :success
assert cookies['_session_id'].present?
session_id = cookies['_session_id']
get '/call_reset_session'
assert_response :success
assert !headers['Set-Cookie'].blank?
get '/get_session_value'
assert_response :success
assert_equal 'foo: nil', response.body
get '/get_session_id'
assert_response :success
assert(response.body != session_id)
end
end
test "should be able to read session id without accessing the session hash" do
with_test_route_set do
get '/set_session_value'
assert_response :success
assert cookies['_session_id'].present?
session_id = cookies['_session_id']
get '/get_session_id'
assert_response :success
assert_equal response.body, session_id
end
end
test "should auto-load unloaded class" do
with_test_route_set do
with_autoload_path "session_autoload_test" do
get '/set_serialized_session_value'
assert_response :success
assert cookies['_session_id'].present?
end
with_autoload_path "session_autoload_test" do
get '/get_session_id'
assert_response :success
end
with_autoload_path "session_autoload_test" do
get '/get_session_value'
assert_response :success
assert_equal response.body, 'foo: #<SessionAutoloadTest::Foo bar:"baz">'
end
end
end
test "should not resend the cookie again if session_id cookie is already exists" do
with_test_route_set do
get '/set_session_value'
assert_response :success
assert cookies['_session_id'].present?
get '/get_session_value'
assert_response :success
assert headers['Set-Cookie'].nil?
end
end
test "should prevent session fixation" do
with_test_route_set do
get '/get_session_value'
assert_response :success
assert_equal 'foo: nil', response.body
session_id = cookies['_session_id']
reset!
get '/set_session_value', headers: { _session_id: session_id }
assert_response :success
assert(cookies['_session_id'] != session_id)
end
end
test "should write the data with expiration time" do
with_test_route_set do
get '/set_session_value_with_expiry'
assert_response :success
get '/get_session_value'
assert_response :success
assert_equal response.body, 'foo: "bar"'
sleep 1
get '/get_session_value'
assert_response :success
assert_equal 'foo: nil', response.body
end
end
test "session store with explicit domain" do
with_test_route_set(:domain => "example.es") do
get '/set_session_value'
assert_match(/domain=example\.es/, headers['Set-Cookie'])
headers['Set-Cookie']
end
end
test "session store without domain" do
with_test_route_set do
get '/set_session_value'
assert_no_match(/domain\=/, headers['Set-Cookie'])
end
end
test "session store with nil domain" do
with_test_route_set(:domain => nil) do
get '/set_session_value'
assert_no_match(/domain\=/, headers['Set-Cookie'])
end
end
test "session store with all domains" do
with_test_route_set(:domain => :all) do
get '/set_session_value'
assert_match(/domain=\.example\.com/, headers['Set-Cookie'])
end
end
private
# from actionpack/test/abstract_unit.rb
class RoutedRackApp
attr_reader :routes
def initialize(routes, &blk)
@routes = routes
@stack = ActionDispatch::MiddlewareStack.new(&blk).build(@routes)
@secret = SecureRandom.hex
@key_generator = ActiveSupport::CachingKeyGenerator.new(
ActiveSupport::KeyGenerator.new(@secret, iterations: 2)
)
end
def call(env)
env[ActionDispatch::Cookies::GENERATOR_KEY] = @key_generator
env[ActionDispatch::Cookies::SIGNED_COOKIE_SALT] = SecureRandom.hex
if defined? ActionDispatch::Cookies::COOKIES_ROTATIONS
env[ActionDispatch::Cookies::COOKIES_ROTATIONS] = ActiveSupport::Messages::RotationConfiguration.new
end
@stack.call(env)
end
end
# from actionpack/test/abstract_unit.rb
def self.build_app(routes = nil)
RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new) do |middleware|
middleware.use ActionDispatch::DebugExceptions
middleware.use ActionDispatch::Callbacks
# middleware.use ActionDispatch::ParamsParser
middleware.use ActionDispatch::Cookies
middleware.use ActionDispatch::Flash
middleware.use Rack::Head
yield(middleware) if block_given?
end
end
# from actionpack/test/dispatch/session/cookie_store_test.rb
def with_test_route_set(options = {})
with_routing do |set|
set.draw do
get :no_session_access, to: 'test#no_session_access'
get :set_session_value, to: 'test#set_session_value'
get :set_session_value_with_expiry, to: 'test#set_session_value_with_expiry'
get :set_serialized_session_value, to: 'test#set_serialized_session_value'
get :get_session_value, to: 'test#get_session_value'
get :get_session_id, to: 'test#get_session_id'
get :call_reset_session, to: 'test#call_reset_session'
end
options = { :key => SessionKey }.merge!(options)
@app = self.class.build_app(set) do |middleware|
middleware.use ActionDispatch::Session::RedisStore, options
end
yield
end
end
end
|