You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The new Getting Started guide of Rails 8 has a chapter on Adding Authentication (with the new v8 authentication generator). What is missing are instructions (or pointers to other docs) for users on how to write tests for controller actions that require authentication. There is no such information in Securing Rails Applications, either.
Expected behavior
When I finish the Getting Started guide, I'm ready to write tests for my first app (possibly by using my local "finished" Getting Started code as the starting point), including any such tests that require authentication.
Actual behavior
When I finish the Getting Started guide, I have no ready-to-use information at hand for "creating tests that require authentication".
Here's an example change for how I fixed tests that I manually added (before chapter 11) while going through the new Rails Getting Started guide and that subsequently broke after adding authentication in chapter 11:
diff --git test/controllers/products_controller_test.rb test/controllers/products_controller_test.rb
index 8ff3369..49cd4e2 100644
--- test/controllers/products_controller_test.rb+++ test/controllers/products_controller_test.rb@@ -3,6 +3,11 @@
require "test_helper"
class ProductsControllerTest < ActionDispatch::IntegrationTest
+ setup do+ @user = users(:one)+ sign_in @user+ end+
test "should get index" do
get products_url
assert_response :success
@@ -48,4 +53,8 @@ class ProductsControllerTest < ActionDispatch::IntegrationTest
end
assert_redirected_to products_url
end
++ private def sign_in(user, password: "password")+ post session_url, params: { email_address: user.email_address, password: }+ end
end
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 8-0-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
Background
The new Getting Started guide of Rails 8 has a chapter on Adding Authentication (with the new v8 authentication generator). What is missing are instructions (or pointers to other docs) for users on how to write tests for controller actions that require authentication. There is no such information in Securing Rails Applications, either.
Expected behavior
When I finish the Getting Started guide, I'm ready to write tests for my first app (possibly by using my local "finished" Getting Started code as the starting point), including any such tests that require authentication.
Actual behavior
When I finish the Getting Started guide, I have no ready-to-use information at hand for "creating tests that require authentication".
What helped me was the answer to the StackOverflow question How to test Controller with sign in user on Rails 8 authentication from Nov 2024.
Example
Here's an example change for how I fixed tests that I manually added (before chapter 11) while going through the new Rails Getting Started guide and that subsequently broke after adding authentication in chapter 11:
Related
The text was updated successfully, but these errors were encountered: