rails_helper.rb 4.8 KB

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