Skip to content

Commit d517758

Browse files
authored
Merge pull request #221 from docsend/change_test_setup_to_mirror_cookie_store_tests
Use a test setup similar to `actiondispatch`'s Cookie Store tests
2 parents 33983f5 + afc0f5a commit d517758

File tree

8 files changed

+106
-50
lines changed

8 files changed

+106
-50
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@ jobs:
77
strategy:
88
fail-fast: false
99
matrix:
10-
ruby: ['2.7', '3.0', '3.1', '3.2', '3.3']
11-
rails: ['7.0', '7.1', '7.2', '8.0', 'edge']
12-
exclude:
13-
- ruby: '2.7'
14-
rails: '7.2'
10+
ruby: ['3.1', '3.2', '3.3']
11+
rails: ['7.1', '7.2', '8.0', 'edge']
12+
include:
1513
- ruby: '2.7'
16-
rails: '8.0'
17-
- ruby: '2.7'
18-
rails: 'edge'
14+
rails: '7.1'
1915
- ruby: '3.0'
20-
rails: '7.2'
21-
- ruby: '3.0'
22-
rails: '8.0'
23-
- ruby: '3.0'
24-
rails: 'edge'
16+
rails: '7.1'
17+
exclude:
2518
- ruby: '3.1'
2619
rails: '8.0'
2720
- ruby: '3.1'

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Unreleased
2+
3+
* Drop Rails 7.0 support.
4+
15
## 2.2.0
26

37
* Drop dependency on `multi_json`.

activerecord-session_store.gemspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
66
s.version = ActiveRecord::SessionStore::VERSION
77
s.summary = 'An Action Dispatch session store backed by an Active Record class.'
88

9-
s.required_ruby_version = '>= 2.5.0'
9+
s.required_ruby_version = '>= 2.7.0'
1010
s.license = 'MIT'
1111

1212
s.author = 'David Heinemeier Hansson'
@@ -23,9 +23,9 @@ Gem::Specification.new do |s|
2323
s.extra_rdoc_files = %w( README.md )
2424
s.rdoc_options.concat ['--main', 'README.md']
2525

26-
s.add_dependency('activerecord', '>= 7.0')
27-
s.add_dependency('actionpack', '>= 7.0')
28-
s.add_dependency('railties', '>= 7.0')
26+
s.add_dependency('activerecord', '>= 7.1')
27+
s.add_dependency('actionpack', '>= 7.1')
28+
s.add_dependency('railties', '>= 7.1')
2929
s.add_dependency('rack', '>= 2.0.8', '< 4')
3030
s.add_dependency('cgi', '>= 0.3.6')
3131
end

gemfiles/rails_6.1.gemfile

Lines changed: 0 additions & 9 deletions
This file was deleted.

gemfiles/rails_7.0.gemfile

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/action_controller_test.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ def test_prevents_session_fixation
197197
end
198198

199199
def test_allows_session_fixation
200-
with_test_route_set(:cookie_only => false) do
200+
session_options(cookie_only: false)
201+
202+
with_test_route_set do
201203
get '/set_session_value'
202204
assert_response :success
203205
assert cookies['_session_id']
@@ -238,7 +240,9 @@ def test_incoming_invalid_session_id_via_cookie_should_be_ignored
238240
end
239241

240242
def test_incoming_invalid_session_id_via_parameter_should_be_ignored
241-
with_test_route_set(:cookie_only => false) do
243+
session_options(cookie_only: false)
244+
245+
with_test_route_set do
242246
open_session do |sess|
243247
sess.get '/set_session_value', :params => { :_session_id => 'INVALID' }
244248
new_session_id = sess.cookies['_session_id']
@@ -252,7 +256,9 @@ def test_incoming_invalid_session_id_via_parameter_should_be_ignored
252256
end
253257

254258
def test_session_store_with_all_domains
255-
with_test_route_set(:domain => :all) do
259+
session_options(domain: :all)
260+
261+
with_test_route_set do
256262
get '/set_session_value'
257263
assert_response :success
258264
end

test/helper.rb

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def config
4444
class ActionDispatch::IntegrationTest < ActiveSupport::TestCase
4545
include ActionDispatch::SharedRoutes
4646

47-
def self.build_app(routes, options)
47+
def self.build_app(routes = nil)
4848
RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new) do |middleware|
4949
middleware.use ActionDispatch::DebugExceptions
5050
middleware.use ActionDispatch::ActionableExceptions
@@ -53,16 +53,25 @@ def self.build_app(routes, options)
5353
middleware.use ActionDispatch::Flash
5454
middleware.use Rack::MethodOverride
5555
middleware.use Rack::Head
56-
middleware.use ActionDispatch::Session::ActiveRecordStore, options.reverse_merge(key: "_session_id")
5756
yield(middleware) if block_given?
5857
end
5958
end
6059

61-
self.app = build_app(nil, {})
60+
self.app = build_app
6261

6362
private
6463

65-
def with_test_route_set(options = {})
64+
def session_options(options = {})
65+
(@session_options ||= {key: "_session_id"}).merge!(options)
66+
end
67+
68+
def app
69+
@app ||= self.class.build_app do |middleware|
70+
middleware.use ActionDispatch::Session::ActiveRecordStore, session_options
71+
end
72+
end
73+
74+
def with_test_route_set
6675
controller_namespace = self.class.to_s.underscore
6776
actions = %w[set_session_value get_session_value call_reset_session renew get_session_id]
6877

@@ -71,14 +80,7 @@ def with_test_route_set(options = {})
7180
actions.each { |action| get action, controller: "#{controller_namespace}/test" }
7281
end
7382

74-
old_app = self.class.app
75-
begin
76-
self.class.app = self.class.build_app(set, options)
77-
78-
yield
79-
ensure
80-
self.class.app = old_app
81-
end
83+
yield
8284
end
8385
end
8486

@@ -89,6 +91,13 @@ def with_store(class_name)
8991
ensure
9092
ActionDispatch::Session::ActiveRecordStore.session_class = session_class
9193
end
94+
95+
# Patch in support for with_routing for integration tests, which was introduced in Rails 7.2
96+
if !defined?(ActionDispatch::Assertions::RoutingAssertions::WithIntegrationRouting)
97+
require_relative 'with_integration_routing_patch'
98+
99+
include WithIntegrationRoutingPatch
100+
end
92101
end
93102

94103
ActiveSupport::TestCase.test_order = :random
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Bring with_routing method support for integration tests back to Rails 7.1.
2+
# We can remove this when we drop support for Rails 7.1.
3+
# See: https://github.com/rails/rails/pull/49819
4+
module WithIntegrationRoutingPatch # :nodoc:
5+
extend ActiveSupport::Concern
6+
7+
module ClassMethods
8+
def with_routing(&block)
9+
old_routes = nil
10+
old_integration_session = nil
11+
12+
setup do
13+
old_routes = app.routes
14+
old_integration_session = integration_session
15+
create_routes(&block)
16+
end
17+
18+
teardown do
19+
reset_routes(old_routes, old_integration_session)
20+
end
21+
end
22+
end
23+
24+
def with_routing(&block)
25+
old_routes = app.routes
26+
old_integration_session = integration_session
27+
create_routes(&block)
28+
ensure
29+
reset_routes(old_routes, old_integration_session)
30+
end
31+
32+
private
33+
34+
def create_routes
35+
app = self.app
36+
routes = ActionDispatch::Routing::RouteSet.new
37+
rack_app = app.config.middleware.build(routes)
38+
https = integration_session.https?
39+
host = integration_session.host
40+
41+
app.instance_variable_set(:@routes, routes)
42+
app.instance_variable_set(:@app, rack_app)
43+
@integration_session = Class.new(ActionDispatch::Integration::Session) do
44+
include app.routes.url_helpers
45+
include app.routes.mounted_helpers
46+
end.new(app)
47+
@integration_session.https! https
48+
@integration_session.host! host
49+
@routes = routes
50+
51+
yield routes
52+
end
53+
54+
def reset_routes(old_routes, old_integration_session)
55+
old_rack_app = app.config.middleware.build(old_routes)
56+
57+
app.instance_variable_set(:@routes, old_routes)
58+
app.instance_variable_set(:@app, old_rack_app)
59+
@integration_session = old_integration_session
60+
@routes = old_routes
61+
end
62+
end

0 commit comments

Comments
 (0)