1
0

rails_helper.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # frozen_string_literal: true
  2. ENV['RAILS_ENV'] ||= 'test'
  3. unless ENV['DISABLE_SIMPLECOV'] == 'true'
  4. require 'simplecov'
  5. SimpleCov.start 'rails' do
  6. if ENV['CI']
  7. require 'simplecov-lcov'
  8. formatter SimpleCov::Formatter::LcovFormatter
  9. formatter.config.report_with_single_file = true
  10. else
  11. formatter SimpleCov::Formatter::HTMLFormatter
  12. end
  13. enable_coverage :branch
  14. add_filter 'lib/linter'
  15. add_group 'Libraries', 'lib'
  16. add_group 'Policies', 'app/policies'
  17. add_group 'Presenters', 'app/presenters'
  18. add_group 'Search', 'app/chewy'
  19. add_group 'Serializers', 'app/serializers'
  20. add_group 'Services', 'app/services'
  21. add_group 'Validators', 'app/validators'
  22. end
  23. end
  24. # This needs to be defined before Rails is initialized
  25. STREAMING_PORT = ENV.fetch('TEST_STREAMING_PORT', '4020')
  26. ENV['STREAMING_API_BASE_URL'] = "http://localhost:#{STREAMING_PORT}"
  27. require_relative '../config/environment'
  28. abort('The Rails environment is running in production mode!') if Rails.env.production?
  29. require 'spec_helper'
  30. require 'rspec/rails'
  31. require 'webmock/rspec'
  32. require 'paperclip/matchers'
  33. require 'capybara/rspec'
  34. require 'chewy/rspec'
  35. require 'email_spec/rspec'
  36. require 'pundit/rspec'
  37. require 'test_prof/recipes/rspec/before_all'
  38. Rails.root.glob('spec/support/**/*.rb').each { |f| require f }
  39. ActiveRecord::Migration.maintain_test_schema!
  40. WebMock.disable_net_connect!(
  41. allow_localhost: true,
  42. allow: Chewy.settings[:host]
  43. )
  44. Sidekiq.logger = nil
  45. DatabaseCleaner.strategy = [:deletion]
  46. Chewy.settings[:enabled] = false
  47. Devise::Test::ControllerHelpers.module_eval do
  48. alias_method :original_sign_in, :sign_in
  49. def sign_in(resource, _deprecated = nil, scope: nil)
  50. original_sign_in(resource, scope: scope)
  51. SessionActivation.deactivate warden.cookies.signed['_session_id']
  52. warden.cookies.signed['_session_id'] = {
  53. value: resource.activate_session(warden.request),
  54. expires: 1.year.from_now,
  55. httponly: true,
  56. }
  57. end
  58. end
  59. RSpec.configure do |config|
  60. # By default, skip specs that need full JS browser
  61. config.filter_run_excluding :js
  62. # By default, skip specs that need elastic search server
  63. config.filter_run_excluding :search
  64. # By default, skip specs that need the streaming server
  65. config.filter_run_excluding :streaming
  66. config.fixture_paths = [
  67. Rails.root.join('spec', 'fixtures'),
  68. ]
  69. config.use_transactional_fixtures = true
  70. config.order = 'random'
  71. config.infer_spec_type_from_file_location!
  72. config.filter_rails_from_backtrace!
  73. # Set type to `cli` for all CLI specs
  74. config.define_derived_metadata(file_path: Regexp.new('spec/lib/mastodon/cli')) do |metadata|
  75. metadata[:type] = :cli
  76. end
  77. # Set `search` metadata true for all specs in spec/search/
  78. config.define_derived_metadata(file_path: Regexp.new('spec/search/*')) do |metadata|
  79. metadata[:search] = true
  80. end
  81. config.include Devise::Test::ControllerHelpers, type: :controller
  82. config.include Devise::Test::ControllerHelpers, type: :helper
  83. config.include Devise::Test::ControllerHelpers, type: :view
  84. config.include Devise::Test::IntegrationHelpers, type: :system
  85. config.include Devise::Test::IntegrationHelpers, type: :request
  86. config.include ActionMailer::TestHelper
  87. config.include Paperclip::Shoulda::Matchers
  88. config.include ActiveSupport::Testing::TimeHelpers
  89. config.include Chewy::Rspec::Helpers
  90. config.include Redisable
  91. config.include DomainHelpers
  92. config.include ThreadingHelpers
  93. config.include SignedRequestHelpers, type: :request
  94. config.include CommandLineHelpers, type: :cli
  95. config.include SystemHelpers, type: :system
  96. config.around(:each, use_transactional_tests: false) do |example|
  97. self.use_transactional_tests = false
  98. example.run
  99. self.use_transactional_tests = true
  100. end
  101. config.around do |example|
  102. if example.metadata[:inline_jobs] == true
  103. Sidekiq::Testing.inline!
  104. else
  105. Sidekiq::Testing.fake!
  106. end
  107. example.run
  108. end
  109. config.around(:each, type: :search) do |example|
  110. Chewy.settings[:enabled] = true
  111. example.run
  112. Chewy.settings[:enabled] = false
  113. end
  114. config.before :each, type: :cli do
  115. stub_reset_connection_pools
  116. end
  117. config.before do |example|
  118. allow(Resolv::DNS).to receive(:open).and_raise('Real DNS queries are disabled, stub Resolv::DNS as needed') unless example.metadata[:type] == :system
  119. end
  120. config.before do |example|
  121. unless example.metadata[:attachment_processing]
  122. # rubocop:disable RSpec/AnyInstance
  123. allow_any_instance_of(Paperclip::Attachment).to receive(:post_process).and_return(true)
  124. allow_any_instance_of(Paperclip::MediaTypeSpoofDetector).to receive(:spoofed?).and_return(false)
  125. # rubocop:enable RSpec/AnyInstance
  126. end
  127. end
  128. config.before :each, type: :request do
  129. # Use https and configured hostname in request spec requests
  130. integration_session.https!
  131. host! Rails.configuration.x.local_domain
  132. end
  133. config.before :each, type: :system do
  134. # Align with capybara config so that rails helpers called from rspec use matching host
  135. host! 'localhost:3000'
  136. end
  137. config.after do
  138. Rails.cache.clear
  139. redis.del(redis.keys)
  140. end
  141. # Assign types based on dir name for non-inferred types
  142. config.define_derived_metadata(file_path: %r{/spec/}) do |metadata|
  143. unless metadata.key?(:type)
  144. match = metadata[:location].match(%r{/spec/([^/]+)/})
  145. metadata[:type] = match[1].singularize.to_sym
  146. end
  147. end
  148. end
  149. RSpec::Sidekiq.configure do |config|
  150. config.warn_when_jobs_not_processed_by_sidekiq = false
  151. end
  152. RSpec::Matchers.define_negated_matcher :not_change, :change
  153. RSpec::Matchers.define_negated_matcher :not_eq, :eq
  154. RSpec::Matchers.define_negated_matcher :not_include, :include
  155. def request_fixture(name)
  156. Rails.root.join('spec', 'fixtures', 'requests', name).read
  157. end
  158. def attachment_fixture(name)
  159. Rails.root.join('spec', 'fixtures', 'files', name).open
  160. end
  161. def stub_reset_connection_pools
  162. # TODO: Is there a better way to correctly run specs without stubbing this?
  163. # (Avoids reset_connection_pools! in test env)
  164. allow(ActiveRecord::Base).to receive(:establish_connection)
  165. allow(RedisConnection).to receive(:establish_pool)
  166. end