rails_helper.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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::Testing.inline!
  22. Sidekiq.logger = nil
  23. # System tests config
  24. DatabaseCleaner.strategy = [:deletion]
  25. Devise::Test::ControllerHelpers.module_eval do
  26. alias_method :original_sign_in, :sign_in
  27. def sign_in(resource, _deprecated = nil, scope: nil)
  28. original_sign_in(resource, scope: scope)
  29. SessionActivation.deactivate warden.cookies.signed['_session_id']
  30. warden.cookies.signed['_session_id'] = {
  31. value: resource.activate_session(warden.request),
  32. expires: 1.year.from_now,
  33. httponly: true,
  34. }
  35. end
  36. end
  37. RSpec.configure do |config|
  38. # This is set before running spec:system, see lib/tasks/tests.rake
  39. config.filter_run_excluding type: lambda { |type|
  40. case type
  41. when :system
  42. !RUN_SYSTEM_SPECS
  43. end
  44. }
  45. # By default, skip the elastic search integration specs
  46. config.filter_run_excluding search: true
  47. config.fixture_paths = [
  48. Rails.root.join('spec', 'fixtures'),
  49. ]
  50. config.use_transactional_fixtures = true
  51. config.order = 'random'
  52. config.infer_spec_type_from_file_location!
  53. config.filter_rails_from_backtrace!
  54. # Set type to `cli` for all CLI specs
  55. config.define_derived_metadata(file_path: Regexp.new('spec/lib/mastodon/cli')) do |metadata|
  56. metadata[:type] = :cli
  57. end
  58. # Set `search` metadata true for all specs in spec/search/
  59. config.define_derived_metadata(file_path: Regexp.new('spec/search/*')) do |metadata|
  60. metadata[:search] = true
  61. end
  62. config.include Devise::Test::ControllerHelpers, type: :controller
  63. config.include Devise::Test::ControllerHelpers, type: :helper
  64. config.include Devise::Test::ControllerHelpers, type: :view
  65. config.include Devise::Test::IntegrationHelpers, type: :feature
  66. config.include Devise::Test::IntegrationHelpers, type: :request
  67. config.include Paperclip::Shoulda::Matchers
  68. config.include ActiveSupport::Testing::TimeHelpers
  69. config.include Chewy::Rspec::Helpers
  70. config.include Redisable
  71. config.include SignedRequestHelpers, type: :request
  72. config.around(:each, use_transactional_tests: false) do |example|
  73. self.use_transactional_tests = false
  74. example.run
  75. self.use_transactional_tests = true
  76. end
  77. config.around(:each, :sidekiq_fake) do |example|
  78. Sidekiq::Testing.fake! do
  79. example.run
  80. Sidekiq::Worker.clear_all
  81. end
  82. end
  83. config.before :each, type: :cli do
  84. stub_stdout
  85. stub_reset_connection_pools
  86. end
  87. config.before :each, type: :feature do
  88. Capybara.current_driver = :rack_test
  89. end
  90. config.around :each, type: :system do |example|
  91. driven_by :selenium, using: :headless_chrome, screen_size: [1600, 1200]
  92. # The streaming server needs access to the database
  93. # but with use_transactional_tests every transaction
  94. # is rolled-back, so the streaming server never sees the data
  95. # So we disable this feature for system tests, and use DatabaseCleaner to clean
  96. # the database tables between each test
  97. self.use_transactional_tests = false
  98. DatabaseCleaner.cleaning do
  99. example.run
  100. end
  101. self.use_transactional_tests = true
  102. end
  103. config.before do |example|
  104. unless example.metadata[:paperclip_processing]
  105. allow_any_instance_of(Paperclip::Attachment).to receive(:post_process).and_return(true) # rubocop:disable RSpec/AnyInstance
  106. end
  107. end
  108. config.after do
  109. Rails.cache.clear
  110. redis.del(redis.keys)
  111. end
  112. # Assign types based on dir name for non-inferred types
  113. config.define_derived_metadata(file_path: %r{/spec/}) do |metadata|
  114. unless metadata.key?(:type)
  115. match = metadata[:location].match(%r{/spec/([^/]+)/})
  116. metadata[:type] = match[1].singularize.to_sym
  117. end
  118. end
  119. end
  120. RSpec::Sidekiq.configure do |config|
  121. config.warn_when_jobs_not_processed_by_sidekiq = false
  122. end
  123. RSpec::Matchers.define_negated_matcher :not_change, :change
  124. def request_fixture(name)
  125. Rails.root.join('spec', 'fixtures', 'requests', name).read
  126. end
  127. def attachment_fixture(name)
  128. Rails.root.join('spec', 'fixtures', 'files', name).open
  129. end
  130. def stub_stdout
  131. # TODO: Is there a bettery way to:
  132. # - Avoid CLI command output being printed out
  133. # - Allow rspec to assert things against STDOUT
  134. # - Avoid disabling stdout for other desirable output (deprecation warnings, for example)
  135. allow($stdout).to receive(:write)
  136. end
  137. def stub_reset_connection_pools
  138. # TODO: Is there a better way to correctly run specs without stubbing this?
  139. # (Avoids reset_connection_pools! in test env)
  140. allow(ActiveRecord::Base).to receive(:establish_connection)
  141. allow(RedisConfiguration).to receive(:establish_pool)
  142. end