Skip to content

Commit 89fb9b3

Browse files
committed
Fix undefined method []= in tests
I found an entry in the Devise README which gave notice of exactly this error happening when the Warden middleware is initialized before the session middleware. The solution was to explicitly define the proper order in the test environment. See https://github.com/heartcombo/devise#testing and heartcombo/devise#4696.
1 parent 9a525dd commit 89fb9b3

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

config/application.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,23 @@ class Application < Rails::Application
2626
# Initialize configuration defaults for originally generated Rails version.
2727
config.load_defaults 6.1
2828

29-
# API Only application. This should remain at the top
30-
config.api_only = false # FIXME revert to true one day when the tests start working again
29+
# API Only application. This should remain near the top (below the load_defaults of course)
30+
# See also https://guides.rubyonrails.org/api_app.html#changing-an-existing-application for
31+
# some of what needs to be done for api_only apps.
32+
config.api_only = true
3133

34+
# See: https://github.com/rails/rails/blob/3c9d7a268f325f5cc6ab1ab49aed6f52e4c4f631/guides/source/api_app.md#using-session-middlewares.
3235
# Add cookie middleware (included by default in non api_only applications)
36+
# See the relevant section of https://guides.rubyonrails.org/v6.1/configuring.html#rails-general-configuration
3337
# This also configures session_options for use below
34-
config.session_store :cookie_store, key: '_interslice_session'
38+
config.session_store :cookie_store
3539

36-
# See: https://github.com/rails/rails/blob/3c9d7a268f325f5cc6ab1ab49aed6f52e4c4f631/guides/source/api_app.md#using-session-middlewares.
3740
# Required for all session management (regardless of session_store)
3841
config.middleware.use ActionDispatch::Cookies
39-
# config.session_options[:secure] = Rails.env.production? # TODO make sure that asserting this config in production.rb works with the following line.
42+
# Enable HTTPS-only session cookies in production
43+
config.session_options[:secure] = Rails.env.production?
4044
config.middleware.use config.session_store, config.session_options
4145

42-
# Add session middleware (included by default in non api_only applications)
43-
config.middleware.use ActionDispatch::Session::CookieStore
44-
# TODO not sure if it's helpful, from https://stackoverflow.com/a/61238872/7309070
45-
config.middleware.insert_after(ActionDispatch::Cookies, ActionDispatch::Session::CookieStore)
46-
4746
# Settings in config/environments/* take precedence over those specified here.
4847
# Application configuration can go into files in config/initializers
4948
# -- all .rb files in that directory are automatically loaded after loading

config/environments/test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,10 @@
6363

6464
# Annotate rendered view with file names.
6565
# config.action_view.annotate_rendered_view_with_filenames = true
66+
67+
# Solution obtained from https://github.com/heartcombo/devise#testing
68+
# See also https://github.com/heartcombo/devise/issues/4696
69+
# This is required because of how API mode re-orders the initialization of certain middlewares
70+
Rails.application.config.middleware.insert_before Warden::Manager, ActionDispatch::Cookies
71+
Rails.application.config.middleware.insert_before Warden::Manager, ActionDispatch::Session::CookieStore
6672
end

learning.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ Links:
7171
- [Issue on GitHub](https://github.com/rspec/rspec-rails/issues/2439).
7272
- [Someone else with the same issue](https://github.com/egiurleo/fixture-file-upload-test/commit/e2524d11220bb8169b42aaa5235d214ba8a1dd56).
7373

74+
## `Undefined method []=` after setting `config.api_only = true`
75+
76+
I was having trouble in all tests that used the Devise session helpers `sign_in` after enabling api_only for the application.
77+
After several days, I found [documentation on how to fix the issue](https://github.com/heartcombo/devise#testing), as well as
78+
a [GitHub ticket write-up about it](https://github.com/heartcombo/devise/issues/4696).
79+
The fix is just to re-order the insertion of middleware so that the `Warden::Manager` is inserted after
80+
`ActionDispatch::Cookies` and `ActionDispatch::Session::CookieStore`.
81+
7482
# Various hard to find Rails topics
7583

7684
[What does respond_to do?](https://api.rubyonrails.org/classes/ActionController/MimeResponds.html#method-i-respond_to)

0 commit comments

Comments
 (0)