From 38901d7b5ec820f33b434e672cec64e02a8b4988 Mon Sep 17 00:00:00 2001 From: Hamed Asghari Date: Sun, 18 Aug 2024 15:31:26 -0600 Subject: [PATCH] test: Use default settings of Rails version under test --- .github/workflows/tests.yml | 4 +- .gitignore | 5 +- Appraisals | 10 +++ Gemfile.lock | 10 +-- Rakefile | 11 +-- bin/setup | 4 +- clearance.gemspec | 3 +- db/schema.rb | 28 ------- gemfiles/rails_6.1.gemfile | 4 + gemfiles/rails_7.0.gemfile | 4 + spec/dummy/Rakefile | 6 ++ spec/dummy/app/assets/config/manifest.js | 0 spec/dummy/application.rb | 40 ---------- spec/dummy/config.ru | 6 ++ spec/dummy/config/application.rb | 13 ++++ spec/dummy/config/boot.rb | 5 ++ spec/dummy/config/environment.rb | 5 ++ spec/dummy/config/environments/test.rb | 31 ++++++++ .../20110111224543_create_clearance_users.rb | 5 +- spec/dummy/db/schema.rb | 25 +++++++ spec/{factories.rb => factories/users.rb} | 0 spec/requests/csrf_rotation_spec.rb | 6 +- spec/spec_helper.rb | 14 +--- spec/support/cookies.rb | 74 ------------------- 24 files changed, 134 insertions(+), 179 deletions(-) delete mode 100644 db/schema.rb create mode 100644 spec/dummy/Rakefile create mode 100644 spec/dummy/app/assets/config/manifest.js delete mode 100644 spec/dummy/application.rb create mode 100644 spec/dummy/config.ru create mode 100644 spec/dummy/config/application.rb create mode 100644 spec/dummy/config/boot.rb create mode 100644 spec/dummy/config/environment.rb create mode 100644 spec/dummy/config/environments/test.rb rename {db => spec/dummy/db}/migrate/20110111224543_create_clearance_users.rb (65%) create mode 100644 spec/dummy/db/schema.rb rename spec/{factories.rb => factories/users.rb} (100%) delete mode 100644 spec/support/cookies.rb diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index cc168fddc..40c30a20a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -44,8 +44,8 @@ jobs: - name: "Reset app database" run: | - bundle exec rake dummy:db:drop - bundle exec rake dummy:db:setup + bundle exec rake db:drop + bundle exec rake db:setup - name: "Run tests" run: bundle exec rake diff --git a/.gitignore b/.gitignore index e6d58a954..a9de87411 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,10 @@ *.swp *~ .bundle -db/*.sqlite3 +.idea +.tool-versions +spec/dummy/db/*.sqlite3* +spec/dummy/log gemfiles/*.lock gemfiles/vendor/ log/*.log diff --git a/Appraisals b/Appraisals index 983aa3da2..dd0ff5647 100644 --- a/Appraisals +++ b/Appraisals @@ -1,10 +1,20 @@ appraise "rails_6.1" do gem "railties", "~> 6.1.0" gem "net-smtp", require: false # not bundled in ruby 3.1 + # The following gems will not be bundled with Ruby 3.4 + gem "base64", require: false + gem "bigdecimal", require: false + gem "drb", require: false + gem "mutex_m", require: false end appraise "rails_7.0" do gem "railties", "~> 7.0.0" + # The following gems will not be bundled with Ruby 3.4 + gem "base64", require: false + gem "bigdecimal", require: false + gem "drb", require: false + gem "mutex_m", require: false end appraise "rails_7.1" do diff --git a/Gemfile.lock b/Gemfile.lock index 17c83db0c..900f1463f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -146,7 +146,7 @@ GEM method_source (1.1.0) mini_mime (1.1.5) mini_portile2 (2.8.7) - minitest (5.24.1) + minitest (5.25.1) net-imap (0.4.14) date net-protocol @@ -159,7 +159,7 @@ GEM nokogiri (1.16.7) mini_portile2 (~> 2.8.2) racc (~> 1.4) - parallel (1.26.2) + parallel (1.26.3) parser (3.3.4.2) ast (~> 2.4.1) racc @@ -214,7 +214,7 @@ GEM rspec-mocks (3.13.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) - rspec-rails (6.1.3) + rspec-rails (6.1.4) actionpack (>= 6.1) activesupport (>= 6.1) railties (>= 6.1) @@ -234,11 +234,11 @@ GEM rubocop-ast (>= 1.31.1, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.32.0) + rubocop-ast (1.32.1) parser (>= 3.3.1.0) ruby-progressbar (1.13.0) securerandom (0.3.1) - shoulda-matchers (6.3.1) + shoulda-matchers (6.4.0) activesupport (>= 5.2.0) smart_properties (1.17.0) sqlite3 (1.7.3) diff --git a/Rakefile b/Rakefile index bbf731b9e..1055b16a2 100644 --- a/Rakefile +++ b/Rakefile @@ -1,15 +1,12 @@ -require "rubygems" require "bundler/setup" + +APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__) +load "rails/tasks/engine.rake" + require "bundler/gem_tasks" -require "rake" require "rspec/core/rake_task" -namespace :dummy do - require_relative "spec/dummy/application" - Dummy::Application.load_tasks -end - desc "Run specs other than spec/acceptance" RSpec::Core::RakeTask.new("spec") do |task| task.exclude_pattern = "spec/acceptance/**/*_spec.rb" diff --git a/bin/setup b/bin/setup index 63cb3481e..166e8554d 100755 --- a/bin/setup +++ b/bin/setup @@ -12,5 +12,5 @@ if [ -z "$CI" ]; then fi # Set up database for the application that Clearance tests against -RAILS_ENV=test bundle exec rake dummy:db:drop -RAILS_ENV=test bundle exec rake dummy:db:setup +RAILS_ENV=test bundle exec rake db:drop +RAILS_ENV=test bundle exec rake db:setup diff --git a/clearance.gemspec b/clearance.gemspec index e91d8c12e..bd989fbf6 100644 --- a/clearance.gemspec +++ b/clearance.gemspec @@ -1,5 +1,4 @@ -$LOAD_PATH.push File.expand_path('../lib', __FILE__) -require 'clearance/version' +require_relative 'lib/clearance/version' Gem::Specification.new do |s| s.add_dependency 'bcrypt', '>= 3.1.1' diff --git a/db/schema.rb b/db/schema.rb deleted file mode 100644 index ed0146752..000000000 --- a/db/schema.rb +++ /dev/null @@ -1,28 +0,0 @@ -# encoding: UTF-8 -# This file is auto-generated from the current state of the database. Instead -# of editing this file, please use the migrations feature of Active Record to -# incrementally modify your database, and then regenerate this schema definition. -# -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). -# -# It's strongly recommended that you check this file into your version control system. - -ActiveRecord::Schema.define(version: 20110111224543) do - - create_table "users", force: true do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "email", null: false - t.string "encrypted_password", limit: 128, null: false - t.string "confirmation_token", limit: 128 - t.string "remember_token", limit: 128, null: false - end - - add_index "users", ["email"], name: "index_users_on_email" - add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true - add_index "users", ["remember_token"], name: "index_users_on_remember_token", unique: true -end diff --git a/gemfiles/rails_6.1.gemfile b/gemfiles/rails_6.1.gemfile index fa52260cf..a61e39f81 100644 --- a/gemfiles/rails_6.1.gemfile +++ b/gemfiles/rails_6.1.gemfile @@ -18,5 +18,9 @@ gem "sqlite3", "~> 1.7" gem "timecop" gem "railties", "~> 6.1.0" gem "net-smtp", require: false +gem "base64", require: false +gem "bigdecimal", require: false +gem "drb", require: false +gem "mutex_m", require: false gemspec path: "../" diff --git a/gemfiles/rails_7.0.gemfile b/gemfiles/rails_7.0.gemfile index 5b6bc25d9..f603939bc 100644 --- a/gemfiles/rails_7.0.gemfile +++ b/gemfiles/rails_7.0.gemfile @@ -17,5 +17,9 @@ gem "shoulda-matchers" gem "sqlite3", "~> 1.7" gem "timecop" gem "railties", "~> 7.0.0" +gem "base64", require: false +gem "bigdecimal", require: false +gem "drb", require: false +gem "mutex_m", require: false gemspec path: "../" diff --git a/spec/dummy/Rakefile b/spec/dummy/Rakefile new file mode 100644 index 000000000..9a5ea7383 --- /dev/null +++ b/spec/dummy/Rakefile @@ -0,0 +1,6 @@ +# Add your own tasks in files placed in lib/tasks ending in .rake, +# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. + +require_relative "config/application" + +Rails.application.load_tasks diff --git a/spec/dummy/app/assets/config/manifest.js b/spec/dummy/app/assets/config/manifest.js new file mode 100644 index 000000000..e69de29bb diff --git a/spec/dummy/application.rb b/spec/dummy/application.rb deleted file mode 100644 index 591748661..000000000 --- a/spec/dummy/application.rb +++ /dev/null @@ -1,40 +0,0 @@ -require "rails/all" - -require "clearance" - -module Dummy - APP_ROOT = File.expand_path("..", __FILE__).freeze - - class Application < Rails::Application - config.action_controller.perform_caching = false - config.action_mailer.default_url_options = { host: "dummy.example.com" } - config.action_mailer.delivery_method = :test - if Rails.version.match?(/(6.1|7.0)/) - config.active_record.legacy_connection_handling = false - end - - if Rails.gem_version >= Gem::Version.new("7.1") - config.active_support.cache_format_version = 7.0 - end - - config.active_support.deprecation = :stderr - config.eager_load = false - - config.paths["app/controllers"] << "#{APP_ROOT}/app/controllers" - config.paths["app/models"] << "#{APP_ROOT}/app/models" - config.paths["app/views"] << "#{APP_ROOT}/app/views" - config.paths["config/database"] = "#{APP_ROOT}/config/database.yml" - config.paths["log"] = "tmp/log/development.log" - config.paths.add "config/routes.rb", with: "#{APP_ROOT}/config/routes.rb" - - config.middleware.use Clearance::BackDoor - - def require_environment! - initialize! - end - - def initialize!(&block) - super unless @initialized - end - end -end diff --git a/spec/dummy/config.ru b/spec/dummy/config.ru new file mode 100644 index 000000000..4a3c09a68 --- /dev/null +++ b/spec/dummy/config.ru @@ -0,0 +1,6 @@ +# This file is used by Rack-based servers to start the application. + +require_relative "config/environment" + +run Rails.application +Rails.application.load_server diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb new file mode 100644 index 000000000..e384d724c --- /dev/null +++ b/spec/dummy/config/application.rb @@ -0,0 +1,13 @@ +require_relative "boot" + +require "rails/all" + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(*Rails.groups) + +module Dummy + class Application < Rails::Application + config.load_defaults Rails::VERSION::STRING.to_f + end +end diff --git a/spec/dummy/config/boot.rb b/spec/dummy/config/boot.rb new file mode 100644 index 000000000..116591a4e --- /dev/null +++ b/spec/dummy/config/boot.rb @@ -0,0 +1,5 @@ +# Set up gems listed in the Gemfile. +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../../Gemfile", __dir__) + +require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"]) +$LOAD_PATH.unshift File.expand_path("../../../lib", __dir__) diff --git a/spec/dummy/config/environment.rb b/spec/dummy/config/environment.rb new file mode 100644 index 000000000..cac531577 --- /dev/null +++ b/spec/dummy/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require_relative "application" + +# Initialize the Rails application. +Rails.application.initialize! diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb new file mode 100644 index 000000000..e61ca8af4 --- /dev/null +++ b/spec/dummy/config/environments/test.rb @@ -0,0 +1,31 @@ +require "active_support/core_ext/integer/time" + +Rails.application.configure do + config.enable_reloading = false + + config.eager_load = ENV["CI"].present? + + config.public_file_server.headers = { "Cache-Control" => "public, max-age=#{1.hour.to_i}" } + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + config.cache_store = :null_store + + config.action_dispatch.show_exceptions = :rescuable + + config.action_controller.allow_forgery_protection = false + + config.action_mailer.perform_caching = false + config.action_mailer.delivery_method = :test + + config.action_mailer.default_url_options = { host: "www.example.com" } + + config.active_support.deprecation = :stderr + config.active_support.disallowed_deprecation = :raise + config.active_support.disallowed_deprecation_warnings = [] + + config.factory_bot.definition_file_paths = [File.expand_path('../../../factories', __dir__)] + + config.middleware.use Clearance::BackDoor +end diff --git a/db/migrate/20110111224543_create_clearance_users.rb b/spec/dummy/db/migrate/20110111224543_create_clearance_users.rb similarity index 65% rename from db/migrate/20110111224543_create_clearance_users.rb rename to spec/dummy/db/migrate/20110111224543_create_clearance_users.rb index 75e22bfee..bd83cb9fb 100644 --- a/db/migrate/20110111224543_create_clearance_users.rb +++ b/spec/dummy/db/migrate/20110111224543_create_clearance_users.rb @@ -1,4 +1,4 @@ -class CreateClearanceUsers < ActiveRecord::Migration +class CreateClearanceUsers < ActiveRecord::Migration[Rails::VERSION::STRING.to_f] def self.up create_table :users do |t| t.timestamps null: false @@ -9,7 +9,8 @@ def self.up end add_index :users, :email - add_index :users, :remember_token + add_index :users, :confirmation_token, unique: true + add_index :users, :remember_token, unique: true end def self.down diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb new file mode 100644 index 000000000..840b02408 --- /dev/null +++ b/spec/dummy/db/schema.rb @@ -0,0 +1,25 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.2].define(version: 2011_01_11_224543) do + create_table "users", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "email", null: false + t.string "encrypted_password", limit: 128, null: false + t.string "confirmation_token", limit: 128 + t.string "remember_token", limit: 128, null: false + t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true + t.index ["email"], name: "index_users_on_email" + t.index ["remember_token"], name: "index_users_on_remember_token", unique: true + end +end diff --git a/spec/factories.rb b/spec/factories/users.rb similarity index 100% rename from spec/factories.rb rename to spec/factories/users.rb diff --git a/spec/requests/csrf_rotation_spec.rb b/spec/requests/csrf_rotation_spec.rb index 2119c8d67..ba03d652e 100644 --- a/spec/requests/csrf_rotation_spec.rb +++ b/spec/requests/csrf_rotation_spec.rb @@ -16,7 +16,7 @@ original_token = csrf_token post session_path, params: { - session: session_params(user, "password"), + authenticity_token: csrf_token, session: { email: user.email, password: "password" } } expect(csrf_token).not_to eq original_token @@ -28,8 +28,4 @@ def csrf_token session[:_csrf_token] end - - def session_params(user, password) - { email: user.email, password: password, authenticity_token: csrf_token } - end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 191cb880a..320534a73 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,18 +1,10 @@ ENV["RAILS_ENV"] ||= "test" +require_relative "dummy/config/environment" -require "rails/all" -require "dummy/application" - -require "clearance/rspec" -require "factory_bot_rails" -require "rails-controller-testing" require "rspec/rails" -require "shoulda-matchers" -require "timecop" - -Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } +require "clearance/rspec" -Dummy::Application.initialize! +Dir[File.expand_path("spec/support/**/*.rb")].each { |f| require f } RSpec.configure do |config| config.include FactoryBot::Syntax::Methods diff --git a/spec/support/cookies.rb b/spec/support/cookies.rb deleted file mode 100644 index fd608b9d5..000000000 --- a/spec/support/cookies.rb +++ /dev/null @@ -1,74 +0,0 @@ -RSpec::Matchers.define :set_cookie do |name, expected_value, expected_expires_at| - failure_message do - "Expected #{expectation} got #{result}" - end - - match do |subject| - @headers = subject - @expected_name = name - @expected_value = expected_value - @expected_expires_at = expected_expires_at - extract_cookies - find_expected_cookie - parse_expiration - parse_value - parse_path - ensure_cookie_set - ensure_expiration_correct - ensure_path_is_correct - end - - def ensure_cookie_set - expect(@value).to eq @expected_value - end - - def ensure_expiration_correct - expect(@expires_at).not_to be_nil - expect(@expires_at).to be_within(100).of(@expected_expires_at) - end - - def ensure_path_is_correct - expect(@path).to eq '/' - end - - def expectation - "a cookie named #{@expected_name} with value #{@expected_value.inspect} expiring at #{@expected_expires_at.inspect}" - end - - def extract_cookies - @cookie_headers = @headers["Set-Cookie"] || @headers["set-cookie"] || [] - @cookie_headers = [@cookie_headers] if @cookie_headers.respond_to?(:to_str) - end - - def find_expected_cookie - @cookie = @cookie_headers.detect do |header| - header =~ /^#{@expected_name}=[^;]*(;|$)/ - end - end - - def parse_expiration - if @cookie && result = @cookie.match(/; expires=(.*?)(;|$)/) - @expires_at = Time.parse(result[1]) - end - end - - def parse_path - if @cookie && result = @cookie.match(/; path=(.*?)(;|$)/) - @path = result[1] - end - end - - def parse_value - if @cookie && result = @cookie.match(/=(.*?)(?:;|$)/) - @value = result[1] - end - end - - def result - if @cookie - @cookie - else - @cookie_headers.join("; ") - end - end -end