Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / actionpack / test / activerecord / active_record_store_test.rb
1 require 'active_record_unit'
2
3 class ActiveRecordStoreTest < ActionController::IntegrationTest
4 DispatcherApp = ActionController::Dispatcher.new
5 SessionApp = ActiveRecord::SessionStore.new(DispatcherApp,
6 :key => '_session_id')
7 SessionAppWithFixation = ActiveRecord::SessionStore.new(DispatcherApp,
8 :key => '_session_id', :cookie_only => false)
9
10 class TestController < ActionController::Base
11 def no_session_access
12 head :ok
13 end
14
15 def set_session_value
16 session[:foo] = params[:foo] || "bar"
17 head :ok
18 end
19
20 def get_session_value
21 render :text => "foo: #{session[:foo].inspect}"
22 end
23
24 def get_session_id
25 session[:foo]
26 render :text => "#{request.session_options[:id]}"
27 end
28
29 def call_reset_session
30 session[:bar]
31 reset_session
32 session[:bar] = "baz"
33 head :ok
34 end
35
36 def rescue_action(e) raise end
37 end
38
39 def setup
40 ActiveRecord::SessionStore.session_class.create_table!
41 @integration_session = open_session(SessionApp)
42 end
43
44 def teardown
45 ActiveRecord::SessionStore.session_class.drop_table!
46 end
47
48 def test_setting_and_getting_session_value
49 with_test_route_set do
50 get '/set_session_value'
51 assert_response :success
52 assert cookies['_session_id']
53
54 get '/get_session_value'
55 assert_response :success
56 assert_equal 'foo: "bar"', response.body
57
58 get '/set_session_value', :foo => "baz"
59 assert_response :success
60 assert cookies['_session_id']
61
62 get '/get_session_value'
63 assert_response :success
64 assert_equal 'foo: "baz"', response.body
65 end
66 end
67
68 def test_getting_nil_session_value
69 with_test_route_set do
70 get '/get_session_value'
71 assert_response :success
72 assert_equal 'foo: nil', response.body
73 end
74 end
75
76 def test_setting_session_value_after_session_reset
77 with_test_route_set do
78 get '/set_session_value'
79 assert_response :success
80 assert cookies['_session_id']
81 session_id = cookies['_session_id']
82
83 get '/call_reset_session'
84 assert_response :success
85 assert_not_equal [], headers['Set-Cookie']
86
87 get '/get_session_value'
88 assert_response :success
89 assert_equal 'foo: nil', response.body
90
91 get '/get_session_id'
92 assert_response :success
93 assert_not_equal session_id, response.body
94 end
95 end
96
97 def test_getting_session_id
98 with_test_route_set do
99 get '/set_session_value'
100 assert_response :success
101 assert cookies['_session_id']
102 session_id = cookies['_session_id']
103
104 get '/get_session_id'
105 assert_response :success
106 assert_equal session_id, response.body
107 end
108 end
109
110 def test_prevents_session_fixation
111 with_test_route_set do
112 get '/set_session_value'
113 assert_response :success
114 assert cookies['_session_id']
115
116 get '/get_session_value'
117 assert_response :success
118 assert_equal 'foo: "bar"', response.body
119 session_id = cookies['_session_id']
120 assert session_id
121
122 reset!
123
124 get '/set_session_value', :_session_id => session_id, :foo => "baz"
125 assert_response :success
126 assert_equal nil, cookies['_session_id']
127
128 get '/get_session_value', :_session_id => session_id
129 assert_response :success
130 assert_equal 'foo: nil', response.body
131 assert_equal nil, cookies['_session_id']
132 end
133 end
134
135 def test_allows_session_fixation
136 @integration_session = open_session(SessionAppWithFixation)
137
138 with_test_route_set do
139 get '/set_session_value'
140 assert_response :success
141 assert cookies['_session_id']
142
143 get '/get_session_value'
144 assert_response :success
145 assert_equal 'foo: "bar"', response.body
146 session_id = cookies['_session_id']
147 assert session_id
148
149 reset!
150 @integration_session = open_session(SessionAppWithFixation)
151
152 get '/set_session_value', :_session_id => session_id, :foo => "baz"
153 assert_response :success
154 assert_equal session_id, cookies['_session_id']
155
156 get '/get_session_value', :_session_id => session_id
157 assert_response :success
158 assert_equal 'foo: "baz"', response.body
159 assert_equal session_id, cookies['_session_id']
160 end
161 end
162
163 private
164 def with_test_route_set
165 with_routing do |set|
166 set.draw do |map|
167 map.with_options :controller => "active_record_store_test/test" do |c|
168 c.connect "/:action"
169 end
170 end
171 yield
172 end
173 end
174 end