rails_helper.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. RUN_SEARCH_SPECS = ENV.fetch('RUN_SEARCH_SPECS', false)
  6. if RUN_SYSTEM_SPECS
  7. STREAMING_PORT = ENV.fetch('TEST_STREAMING_PORT', '4020')
  8. ENV['STREAMING_API_BASE_URL'] = "http://localhost:#{STREAMING_PORT}"
  9. end
  10. if RUN_SEARCH_SPECS
  11. # Include any configuration or setups specific to search tests here
  12. end
  13. require File.expand_path('../config/environment', __dir__)
  14. abort('The Rails environment is running in production mode!') if Rails.env.production?
  15. require 'spec_helper'
  16. require 'rspec/rails'
  17. require 'webmock/rspec'
  18. require 'paperclip/matchers'
  19. require 'capybara/rspec'
  20. require 'chewy/rspec'
  21. Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
  22. ActiveRecord::Migration.maintain_test_schema!
  23. WebMock.disable_net_connect!(allow: Chewy.settings[:host], allow_localhost: RUN_SYSTEM_SPECS)
  24. Sidekiq::Testing.inline!
  25. Sidekiq.logger = nil
  26. # System tests config
  27. DatabaseCleaner.strategy = [:deletion]
  28. streaming_server_manager = StreamingServerManager.new
  29. search_data_manager = SearchDataManager.new
  30. Devise::Test::ControllerHelpers.module_eval do
  31. alias_method :original_sign_in, :sign_in
  32. def sign_in(resource, _deprecated = nil, scope: nil)
  33. original_sign_in(resource, scope: scope)
  34. SessionActivation.deactivate warden.cookies.signed['_session_id']
  35. warden.cookies.signed['_session_id'] = {
  36. value: resource.activate_session(warden.request),
  37. expires: 1.year.from_now,
  38. httponly: true,
  39. }
  40. end
  41. end
  42. RSpec.configure do |config|
  43. # This is set before running spec:system, see lib/tasks/tests.rake
  44. config.filter_run_excluding type: lambda { |type|
  45. case type
  46. when :system
  47. !RUN_SYSTEM_SPECS
  48. when :search
  49. !RUN_SEARCH_SPECS
  50. end
  51. }
  52. config.fixture_path = Rails.root.join('spec', 'fixtures')
  53. config.use_transactional_fixtures = true
  54. config.order = 'random'
  55. config.infer_spec_type_from_file_location!
  56. config.filter_rails_from_backtrace!
  57. config.define_derived_metadata(file_path: Regexp.new('spec/lib/mastodon/cli')) do |metadata|
  58. metadata[:type] = :cli
  59. end
  60. config.include Devise::Test::ControllerHelpers, type: :controller
  61. config.include Devise::Test::ControllerHelpers, type: :helper
  62. config.include Devise::Test::ControllerHelpers, type: :view
  63. config.include Devise::Test::IntegrationHelpers, type: :feature
  64. config.include Devise::Test::IntegrationHelpers, type: :request
  65. config.include Paperclip::Shoulda::Matchers
  66. config.include ActiveSupport::Testing::TimeHelpers
  67. config.include Chewy::Rspec::Helpers
  68. config.include Redisable
  69. config.include SignedRequestHelpers, type: :request
  70. config.around(:each, use_transactional_tests: false) do |example|
  71. self.use_transactional_tests = false
  72. example.run
  73. self.use_transactional_tests = true
  74. end
  75. config.before :each, type: :cli do
  76. stub_stdout
  77. stub_reset_connection_pools
  78. end
  79. config.before :each, type: :feature do
  80. Capybara.current_driver = :rack_test
  81. end
  82. config.before :suite do
  83. if RUN_SYSTEM_SPECS
  84. Webpacker.compile
  85. streaming_server_manager.start(port: STREAMING_PORT)
  86. end
  87. if RUN_SEARCH_SPECS
  88. Chewy.strategy(:urgent)
  89. search_data_manager.prepare_test_data
  90. end
  91. end
  92. config.after :suite do
  93. streaming_server_manager.stop
  94. search_data_manager.cleanup_test_data if RUN_SEARCH_SPECS
  95. end
  96. config.around :each, type: :system do |example|
  97. # driven_by :selenium, using: :chrome, screen_size: [1600, 1200]
  98. driven_by :selenium, using: :headless_chrome, screen_size: [1600, 1200]
  99. # The streaming server needs access to the database
  100. # but with use_transactional_tests every transaction
  101. # is rolled-back, so the streaming server never sees the data
  102. # So we disable this feature for system tests, and use DatabaseCleaner to clean
  103. # the database tables between each test
  104. self.use_transactional_tests = false
  105. DatabaseCleaner.cleaning do
  106. example.run
  107. end
  108. self.use_transactional_tests = true
  109. end
  110. config.around :each, type: :search do |example|
  111. search_data_manager.populate_indexes
  112. example.run
  113. search_data_manager.remove_indexes
  114. end
  115. config.before(:each) do |example|
  116. unless example.metadata[:paperclip_processing]
  117. allow_any_instance_of(Paperclip::Attachment).to receive(:post_process).and_return(true) # rubocop:disable RSpec/AnyInstance
  118. end
  119. end
  120. config.after :each do
  121. Rails.cache.clear
  122. redis.del(redis.keys)
  123. end
  124. # Assign types based on dir name for non-inferred types
  125. config.define_derived_metadata(file_path: %r{/spec/}) do |metadata|
  126. unless metadata.key?(:type)
  127. match = metadata[:location].match(%r{/spec/([^/]+)/})
  128. metadata[:type] = match[1].singularize.to_sym
  129. end
  130. end
  131. end
  132. RSpec::Sidekiq.configure do |config|
  133. config.warn_when_jobs_not_processed_by_sidekiq = false
  134. end
  135. RSpec::Matchers.define_negated_matcher :not_change, :change
  136. def request_fixture(name)
  137. Rails.root.join('spec', 'fixtures', 'requests', name).read
  138. end
  139. def attachment_fixture(name)
  140. Rails.root.join('spec', 'fixtures', 'files', name).open
  141. end
  142. def stub_stdout
  143. # TODO: Is there a bettery way to:
  144. # - Avoid CLI command output being printed out
  145. # - Allow rspec to assert things against STDOUT
  146. # - Avoid disabling stdout for other desirable output (deprecation warnings, for example)
  147. allow($stdout).to receive(:write)
  148. end
  149. def stub_reset_connection_pools
  150. # TODO: Is there a better way to correctly run specs without stubbing this?
  151. # (Avoids reset_connection_pools! in test env)
  152. allow(ActiveRecord::Base).to receive(:establish_connection)
  153. allow(RedisConfiguration).to receive(:establish_pool)
  154. end