rails_helper.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 'Serializers', 'app/serializers'
  19. add_group 'Services', 'app/services'
  20. add_group 'Validators', 'app/validators'
  21. end
  22. end
  23. # This needs to be defined before Rails is initialized
  24. STREAMING_PORT = ENV.fetch('TEST_STREAMING_PORT', '4020')
  25. ENV['STREAMING_API_BASE_URL'] = "http://localhost:#{STREAMING_PORT}"
  26. require File.expand_path('../config/environment', __dir__)
  27. abort('The Rails environment is running in production mode!') if Rails.env.production?
  28. require 'spec_helper'
  29. require 'rspec/rails'
  30. require 'webmock/rspec'
  31. require 'paperclip/matchers'
  32. require 'capybara/rspec'
  33. require 'chewy/rspec'
  34. require 'email_spec/rspec'
  35. require 'test_prof/recipes/rspec/before_all'
  36. Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
  37. ActiveRecord::Migration.maintain_test_schema!
  38. WebMock.disable_net_connect!(
  39. allow_localhost: true,
  40. allow: Chewy.settings[:host]
  41. )
  42. Sidekiq.logger = nil
  43. DatabaseCleaner.strategy = [:deletion]
  44. Devise::Test::ControllerHelpers.module_eval do
  45. alias_method :original_sign_in, :sign_in
  46. def sign_in(resource, _deprecated = nil, scope: nil)
  47. original_sign_in(resource, scope: scope)
  48. SessionActivation.deactivate warden.cookies.signed['_session_id']
  49. warden.cookies.signed['_session_id'] = {
  50. value: resource.activate_session(warden.request),
  51. expires: 1.year.from_now,
  52. httponly: true,
  53. }
  54. end
  55. end
  56. RSpec.configure do |config|
  57. # By default, skip specs that need full JS browser
  58. config.filter_run_excluding :js
  59. # By default, skip specs that need elastic search server
  60. config.filter_run_excluding :search
  61. # By default, skip specs that need the streaming server
  62. config.filter_run_excluding :streaming
  63. config.fixture_paths = [
  64. Rails.root.join('spec', 'fixtures'),
  65. ]
  66. config.use_transactional_fixtures = true
  67. config.order = 'random'
  68. config.infer_spec_type_from_file_location!
  69. config.filter_rails_from_backtrace!
  70. # Set type to `cli` for all CLI specs
  71. config.define_derived_metadata(file_path: Regexp.new('spec/lib/mastodon/cli')) do |metadata|
  72. metadata[:type] = :cli
  73. end
  74. # Set `search` metadata true for all specs in spec/search/
  75. config.define_derived_metadata(file_path: Regexp.new('spec/search/*')) do |metadata|
  76. metadata[:search] = true
  77. end
  78. config.include Devise::Test::ControllerHelpers, type: :controller
  79. config.include Devise::Test::ControllerHelpers, type: :helper
  80. config.include Devise::Test::ControllerHelpers, type: :view
  81. config.include Devise::Test::IntegrationHelpers, type: :system
  82. config.include Devise::Test::IntegrationHelpers, type: :request
  83. config.include ActionMailer::TestHelper
  84. config.include Paperclip::Shoulda::Matchers
  85. config.include ActiveSupport::Testing::TimeHelpers
  86. config.include Chewy::Rspec::Helpers
  87. config.include Redisable
  88. config.include ThreadingHelpers
  89. config.include SignedRequestHelpers, type: :request
  90. config.include CommandLineHelpers, type: :cli
  91. config.around(:each, use_transactional_tests: false) do |example|
  92. self.use_transactional_tests = false
  93. example.run
  94. self.use_transactional_tests = true
  95. end
  96. config.around do |example|
  97. if example.metadata[:sidekiq_inline] == true
  98. Sidekiq::Testing.inline!
  99. else
  100. Sidekiq::Testing.fake!
  101. end
  102. example.run
  103. end
  104. config.before :each, type: :cli do
  105. stub_reset_connection_pools
  106. end
  107. config.before do |example|
  108. allow(Resolv::DNS).to receive(:open).and_raise('Real DNS queries are disabled, stub Resolv::DNS as needed') unless example.metadata[:type] == :system
  109. end
  110. config.before do |example|
  111. unless example.metadata[:paperclip_processing]
  112. allow_any_instance_of(Paperclip::Attachment).to receive(:post_process).and_return(true) # rubocop:disable RSpec/AnyInstance
  113. end
  114. end
  115. config.after do
  116. Rails.cache.clear
  117. redis.del(redis.keys)
  118. end
  119. # Assign types based on dir name for non-inferred types
  120. config.define_derived_metadata(file_path: %r{/spec/}) do |metadata|
  121. unless metadata.key?(:type)
  122. match = metadata[:location].match(%r{/spec/([^/]+)/})
  123. metadata[:type] = match[1].singularize.to_sym
  124. end
  125. end
  126. end
  127. RSpec::Sidekiq.configure do |config|
  128. config.warn_when_jobs_not_processed_by_sidekiq = false
  129. end
  130. RSpec::Matchers.define_negated_matcher :not_change, :change
  131. RSpec::Matchers.define_negated_matcher :not_include, :include
  132. def request_fixture(name)
  133. Rails.root.join('spec', 'fixtures', 'requests', name).read
  134. end
  135. def attachment_fixture(name)
  136. Rails.root.join('spec', 'fixtures', 'files', name).open
  137. end
  138. def stub_reset_connection_pools
  139. # TODO: Is there a better way to correctly run specs without stubbing this?
  140. # (Avoids reset_connection_pools! in test env)
  141. allow(ActiveRecord::Base).to receive(:establish_connection)
  142. allow(RedisConfiguration).to receive(:establish_pool)
  143. end