Skip to content

Commit 8009769

Browse files
gobijandhh
andauthored
Add login_as(user) testing helper when generating authentication (rails#53708)
* Add a `SessionTestHelper` module with `sign_in_as(user)` and `sign_out` test helpers when running `rails g authentication`. Simplifies authentication in integration tests. * Style --------- Co-authored-by: David Heinemeier Hansson <[email protected]>
1 parent ab6cb5a commit 8009769

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

railties/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Add a `SessionTestHelper` module with `sign_in_as(user)` and `sign_out` test helpers when
2+
running `rails g authentication`. Simplifies authentication in integration tests.
3+
4+
*Bijan Rahnema*
5+
16
* Rate limit password resets in authentication generator
27

38
This helps mitigate abuse from attackers spamming the password reset form.

railties/lib/rails/generators/rails/authentication/authentication_generator.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def create_authentication_files
2727
template "app/views/passwords_mailer/reset.text.erb"
2828

2929
template "test/mailers/previews/passwords_mailer_preview.rb"
30+
template "test/helpers/session_test_helper.rb"
3031
end
3132

3233
def configure_application_controller
@@ -52,6 +53,11 @@ def add_migrations
5253
generate "migration", "CreateSessions", "user:references ip_address:string user_agent:string", "--force"
5354
end
5455

56+
def configure_test_helper
57+
inject_into_file "test/test_helper.rb", "require_relative \"helpers/session_test_helper\"\n", after: "require \"rails/test_help\"\n"
58+
inject_into_class "test/test_helper.rb", "TestCase", " include SessionTestHelper\n"
59+
end
60+
5561
hook_for :test_framework
5662
end
5763
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module SessionTestHelper
2+
def sign_in_as(user)
3+
Current.session = user.sessions.create!
4+
5+
ActionDispatch::TestRequest.create.cookie_jar.tap do |cookie_jar|
6+
cookie_jar.signed[:session_id] = Current.session.id
7+
cookies[:session_id] = cookie_jar[:session_id]
8+
end
9+
end
10+
11+
def sign_out
12+
Current.session&.destroy!
13+
cookies.delete(:session_id)
14+
end
15+
end

railties/test/generators/authentication_generator_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def setup
1313
class ApplicationController < ActionController::Base
1414
end
1515
RUBY
16+
FileUtils.mkdir_p("#{destination_root}/test")
17+
File.write("#{destination_root}/test/test_helper.rb", <<~RUBY)
18+
require "rails/test_help"
19+
module ActiveSupport
20+
class TestCase
21+
end
22+
end
23+
RUBY
1624

1725
copy_gemfile
1826

@@ -52,6 +60,11 @@ def test_authentication_generator
5260

5361
assert_file "test/models/user_test.rb"
5462
assert_file "test/fixtures/users.yml"
63+
64+
assert_file "test/test_helper.rb" do |content|
65+
assert_match(/session_test_helper/, content)
66+
assert_match(/SessionTestHelper/, content)
67+
end
5568
end
5669

5770
def test_authentication_generator_without_bcrypt_in_gemfile
@@ -96,6 +109,11 @@ def test_authentication_generator_with_api_flag
96109

97110
assert_file "test/models/user_test.rb"
98111
assert_file "test/fixtures/users.yml"
112+
113+
assert_file "test/test_helper.rb" do |content|
114+
assert_match(/session_test_helper/, content)
115+
assert_match(/SessionTestHelper/, content)
116+
end
99117
end
100118

101119
def test_model_test_is_skipped_if_test_framework_is_given

0 commit comments

Comments
 (0)