From 453ab414a3fabe1ee79d47b135073f8790dfc59c Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 8 Mar 2023 09:08:06 -0300 Subject: [PATCH] Reenable tests with mongoid on Rails 5 and up Devise hasn't been tested with Mongoid since Rails version 5, only 4.x was still running those tests. This enables the tests again on all currently supported Rails versions, with their respective mongoid supported versions. There were a couple of minor tweaks to make it happen, namely: * The way we were dropping the session before doesn't work in later versions so I changed back to calling `purge!` which appears to work fine. We used to call `Mongoid.purge!` but that changed in #4686. * Some of the configs in the Rails test app were setting Active Record values when outside of the AR ORM tests, updated those to make sure they are not set when running mongoid ORM tests. * The validations added to the shared admin code in tests were only checking for Rails version 5.1, but we need to use the same check for AR 5.1 that is used in code, otherwise it will try to use methods not available in mongoid there. I also had to lock the sqlite3 gem on Rails 6 versions to 1.5.x, due to it dropping support to Ruby 2.6 on v1.6+ now, and those Rails 6 versions need to support that Ruby version. Finally, I locked mongoid to v7.5+ but not allowing v8+ yet, there seems to be some change to dirty attributes that I need to look into which is making a handful of tests fail. For now, this should hopefully get us to green as step 1. --- .github/workflows/test.yml | 16 +--------------- CHANGELOG.md | 1 + Gemfile | 7 +++---- Gemfile.lock | 8 ++++++++ gemfiles/Gemfile-rails-4-1 | 2 +- gemfiles/Gemfile-rails-4-2 | 2 +- gemfiles/Gemfile-rails-5-0 | 7 +++---- gemfiles/Gemfile-rails-5-1 | 4 ++++ gemfiles/Gemfile-rails-5-2 | 4 ++++ gemfiles/Gemfile-rails-6-0 | 6 +++++- gemfiles/Gemfile-rails-6-1 | 6 +++++- gemfiles/Gemfile-rails-main | 4 ++++ test/orm/mongoid.rb | 2 +- test/rails_app/config/application.rb | 26 ++++++++++++++------------ test/rails_app/lib/shared_admin.rb | 2 +- 15 files changed, 56 insertions(+), 41 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 207f5703fc..0c94592080 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -42,8 +42,6 @@ jobs: ruby: '2.2' - gemfile: Gemfile ruby: '2.1' - - gemfile: Gemfile - env: DEVISE_ORM=mongoid - gemfile: gemfiles/Gemfile-rails-main ruby: '2.6' - gemfile: gemfiles/Gemfile-rails-main @@ -56,8 +54,6 @@ jobs: ruby: '2.2' - gemfile: gemfiles/Gemfile-rails-main ruby: '2.1' - - gemfile: gemfiles/Gemfile-rails-main - env: DEVISE_ORM=mongoid - gemfile: gemfiles/Gemfile-rails-6-1 ruby: '2.4' - gemfile: gemfiles/Gemfile-rails-6-1 @@ -66,8 +62,6 @@ jobs: ruby: '2.2' - gemfile: gemfiles/Gemfile-rails-6-1 ruby: '2.1' - - gemfile: gemfiles/Gemfile-rails-6-1 - env: DEVISE_ORM=mongoid - gemfile: gemfiles/Gemfile-rails-6-0 ruby: '3.2' - gemfile: gemfiles/Gemfile-rails-6-0 @@ -80,8 +74,6 @@ jobs: ruby: '2.2' - gemfile: gemfiles/Gemfile-rails-6-0 ruby: '2.1' - - gemfile: gemfiles/Gemfile-rails-6-0 - env: DEVISE_ORM=mongoid - gemfile: gemfiles/Gemfile-rails-5-2 ruby: '3.2' - gemfile: gemfiles/Gemfile-rails-5-2 @@ -94,8 +86,6 @@ jobs: ruby: '2.2' - gemfile: gemfiles/Gemfile-rails-5-2 ruby: '2.1' - - gemfile: gemfiles/Gemfile-rails-5-2 - env: DEVISE_ORM=mongoid - gemfile: gemfiles/Gemfile-rails-5-1 ruby: '3.2' - gemfile: gemfiles/Gemfile-rails-5-1 @@ -106,8 +96,6 @@ jobs: ruby: '2.7' - gemfile: gemfiles/Gemfile-rails-5-1 ruby: '2.1' - - gemfile: gemfiles/Gemfile-rails-5-1 - env: DEVISE_ORM=mongoid - gemfile: gemfiles/Gemfile-rails-5-0 ruby: '3.2' - gemfile: gemfiles/Gemfile-rails-5-0 @@ -118,8 +106,6 @@ jobs: ruby: '2.7' - gemfile: gemfiles/Gemfile-rails-5-0 ruby: '2.1' - - gemfile: gemfiles/Gemfile-rails-5-0 - env: DEVISE_ORM=mongoid - gemfile: gemfiles/Gemfile-rails-4-2 ruby: '3.2' - gemfile: gemfiles/Gemfile-rails-4-2 @@ -164,6 +150,6 @@ jobs: bundler-cache: true # runs bundle install and caches installed gems automatically bundler: ${{ env.BUNDLER_VERSION || 'default' }} rubygems: ${{ env.RUBYGEMS_VERSION || 'latest' }} - - uses: supercharge/mongodb-github-action@1.3.0 + - uses: supercharge/mongodb-github-action@1.9.0 if: ${{ matrix.env == 'DEVISE_ORM=mongoid' }} - run: bundle exec rake diff --git a/CHANGELOG.md b/CHANGELOG.md index 22b1b76358..a4705f134f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * enhancements * Allow resource class scopes to override the global configuration for `sign_in_after_reset_password` behaviour. [#5429](https://github.com/heartcombo/devise/pull/5429) [@mattr](https://github.com/mattr) + * Reenable Mongoid test suite across all Rails 5+ versions, to ensure we continue supporting it. * bug fixes * Fix frozen string exception in validatable. [#5563](https://github.com/heartcombo/devise/pull/5563) [#5465](https://github.com/heartcombo/devise/pull/5465) [@mameier](https://github.com/mameier) diff --git a/Gemfile b/Gemfile index 1a4ec8235a..d3750442f7 100644 --- a/Gemfile +++ b/Gemfile @@ -32,7 +32,6 @@ end # gem "jruby-openssl" # end -# TODO: -# group :mongoid do -# gem "mongoid", "~> 4.0.0" -# end +group :mongoid do + gem "mongoid", "~> 7.5" +end diff --git a/Gemfile.lock b/Gemfile.lock index bffcb2cdc1..015da6c75e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -86,6 +86,7 @@ GEM minitest (>= 5.1) tzinfo (~> 2.0) bcrypt (3.1.18) + bson (4.15.0) builder (3.2.4) concurrent-ruby (1.2.2) crass (1.0.6) @@ -115,6 +116,12 @@ GEM mini_portile2 (2.8.1) minitest (5.18.0) mocha (1.16.1) + mongo (2.18.2) + bson (>= 4.14.1, < 5.0.0) + mongoid (7.5.2) + activemodel (>= 5.1, < 7.1, != 7.0.0) + mongo (>= 2.10.5, < 3.0.0) + ruby2_keywords (~> 0.0.5) multi_xml (0.6.0) net-imap (0.3.4) date @@ -224,6 +231,7 @@ PLATFORMS DEPENDENCIES devise! mocha (~> 1.1) + mongoid (~> 7.5) omniauth omniauth-facebook omniauth-oauth2 diff --git a/gemfiles/Gemfile-rails-4-1 b/gemfiles/Gemfile-rails-4-1 index 6f0b77e83d..cf00b284da 100644 --- a/gemfiles/Gemfile-rails-4-1 +++ b/gemfiles/Gemfile-rails-4-1 @@ -36,7 +36,7 @@ platforms :ruby do end group :mongoid do - gem "mongoid", "~> 4.0" + gem "mongoid", "~> 5.0" end if RUBY_VERSION < "2.3.0" diff --git a/gemfiles/Gemfile-rails-4-2 b/gemfiles/Gemfile-rails-4-2 index 43367157fd..fd3c1dd8ef 100644 --- a/gemfiles/Gemfile-rails-4-2 +++ b/gemfiles/Gemfile-rails-4-2 @@ -35,7 +35,7 @@ platforms :ruby do end group :mongoid do - gem "mongoid", "~> 4.0" + gem "mongoid", "~> 5.0" end if RUBY_VERSION < "2.3.0" diff --git a/gemfiles/Gemfile-rails-5-0 b/gemfiles/Gemfile-rails-5-0 index 382cf3c5d3..f26d92b3d9 100644 --- a/gemfiles/Gemfile-rails-5-0 +++ b/gemfiles/Gemfile-rails-5-0 @@ -25,10 +25,9 @@ platforms :ruby do gem "sqlite3", "~> 1.3.6" end -# TODO: -# group :mongoid do -# gem "mongoid", "~> 4.0.0" -# end +group :mongoid do + gem "mongoid", "~> 6.0" +end if RUBY_VERSION < "2.3.0" # We're getting version 2.2.0 which doesn't play nice with Ruby 2.2, using diff --git a/gemfiles/Gemfile-rails-5-1 b/gemfiles/Gemfile-rails-5-1 index 24fe1d5e98..149871f6ca 100644 --- a/gemfiles/Gemfile-rails-5-1 +++ b/gemfiles/Gemfile-rails-5-1 @@ -23,6 +23,10 @@ platforms :ruby do gem "sqlite3", "~> 1.3.6" end +group :mongoid do + gem "mongoid", "~> 6.0" +end + if RUBY_VERSION < "2.3.0" # We're getting version 2.2.0 which doesn't play nice with Ruby 2.2, using # `Object.deprecate_constant` which isn't available. diff --git a/gemfiles/Gemfile-rails-5-2 b/gemfiles/Gemfile-rails-5-2 index 5dc267def1..de8d0bd77f 100644 --- a/gemfiles/Gemfile-rails-5-2 +++ b/gemfiles/Gemfile-rails-5-2 @@ -22,3 +22,7 @@ end platforms :ruby do gem "sqlite3", "~> 1.3.6" end + +group :mongoid do + gem "mongoid", "~> 6.0" +end diff --git a/gemfiles/Gemfile-rails-6-0 b/gemfiles/Gemfile-rails-6-0 index b59c4d3d76..b276668a52 100644 --- a/gemfiles/Gemfile-rails-6-0 +++ b/gemfiles/Gemfile-rails-6-0 @@ -21,5 +21,9 @@ group :test do end platforms :ruby do - gem "sqlite3", "~> 1.4" + gem "sqlite3", "~> 1.5.4" +end + +group :mongoid do + gem "mongoid", "~> 7.5" end diff --git a/gemfiles/Gemfile-rails-6-1 b/gemfiles/Gemfile-rails-6-1 index a4a81e4149..27627b1d4d 100644 --- a/gemfiles/Gemfile-rails-6-1 +++ b/gemfiles/Gemfile-rails-6-1 @@ -27,5 +27,9 @@ group :test do end platforms :ruby do - gem "sqlite3", "~> 1.4" + gem "sqlite3", "~> 1.5.4" +end + +group :mongoid do + gem "mongoid", "~> 7.5" end diff --git a/gemfiles/Gemfile-rails-main b/gemfiles/Gemfile-rails-main index c9bea6f2b8..857631a462 100644 --- a/gemfiles/Gemfile-rails-main +++ b/gemfiles/Gemfile-rails-main @@ -23,3 +23,7 @@ end platforms :ruby do gem "sqlite3", "~> 1.4" end + +group :mongoid do + gem "mongoid", "~> 7.5" +end diff --git a/test/orm/mongoid.rb b/test/orm/mongoid.rb index d31bc4d212..5b8807f1fb 100644 --- a/test/orm/mongoid.rb +++ b/test/orm/mongoid.rb @@ -10,6 +10,6 @@ class ActiveSupport::TestCase setup do - Mongoid.default_session.drop + Mongoid::Config.purge! end end diff --git a/test/rails_app/config/application.rb b/test/rails_app/config/application.rb index 1f4fbddba1..ba475fb357 100644 --- a/test/rails_app/config/application.rb +++ b/test/rails_app/config/application.rb @@ -33,25 +33,27 @@ class Application < Rails::Application # config.assets.enabled = false config.action_mailer.default_url_options = { host: "localhost", port: 3000 } - rails_version = Gem::Version.new(Rails.version) - if DEVISE_ORM == :active_record && - rails_version >= Gem::Version.new('4.2.0') && - rails_version < Gem::Version.new('5.1.0') - config.active_record.raise_in_transactional_callbacks = true - end # This was used to break devise in some situations config.to_prepare do Devise::SessionsController.layout "application" end - # Remove the first check once Rails 5.0 support is removed. - if Devise::Test.rails52_and_up? && !Devise::Test.rails6_and_up? - Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true - end + if DEVISE_ORM == :active_record + rails_version = Gem::Version.new(Rails.version) + + if rails_version >= Gem::Version.new('4.2.0') && rails_version < Gem::Version.new('5.1.0') + config.active_record.raise_in_transactional_callbacks = true + end + + # Remove the first check once Rails 5.0 support is removed. + if Devise::Test.rails52_and_up? && !Devise::Test.rails6_and_up? + config.active_record.sqlite3.represent_boolean_as_integer = true + end - if Devise::Test.rails70? - config.active_record.legacy_connection_handling = false + if Devise::Test.rails70? + config.active_record.legacy_connection_handling = false + end end end end diff --git a/test/rails_app/lib/shared_admin.rb b/test/rails_app/lib/shared_admin.rb index 3e6362a78d..ba37cfc857 100644 --- a/test/rails_app/lib/shared_admin.rb +++ b/test/rails_app/lib/shared_admin.rb @@ -10,7 +10,7 @@ module SharedAdmin allow_unconfirmed_access_for: 2.weeks, reconfirmable: true validates_length_of :reset_password_token, minimum: 3, allow_blank: true - if Devise::Test.rails51? + if Devise.activerecord51? validates_uniqueness_of :email, allow_blank: true, if: :will_save_change_to_email? else validates_uniqueness_of :email, allow_blank: true, if: :email_changed?