rails_helper.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ENV['RAILS_ENV'] ||= 'test'
  2. require File.expand_path('../../config/environment', __FILE__)
  3. abort("The Rails environment is running in production mode!") if Rails.env.production?
  4. require 'spec_helper'
  5. require 'rspec/rails'
  6. require 'webmock/rspec'
  7. require 'paperclip/matchers'
  8. require 'capybara/rspec'
  9. Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
  10. ActiveRecord::Migration.maintain_test_schema!
  11. WebMock.disable_net_connect!
  12. Redis.current = Redis::Namespace.new("mastodon_test#{ENV['TEST_ENV_NUMBER']}", redis: Redis.current)
  13. Sidekiq::Testing.inline!
  14. Sidekiq::Logging.logger = nil
  15. Devise::Test::ControllerHelpers.module_eval do
  16. alias_method :original_sign_in, :sign_in
  17. def sign_in(resource, _deprecated = nil, scope: nil)
  18. original_sign_in(resource, scope: scope)
  19. SessionActivation.deactivate warden.cookies.signed['_session_id']
  20. warden.cookies.signed['_session_id'] = {
  21. value: resource.activate_session(warden.request),
  22. expires: 1.year.from_now,
  23. httponly: true,
  24. }
  25. end
  26. end
  27. RSpec.configure do |config|
  28. config.fixture_path = "#{::Rails.root}/spec/fixtures"
  29. config.use_transactional_fixtures = true
  30. config.order = 'random'
  31. config.infer_spec_type_from_file_location!
  32. config.filter_rails_from_backtrace!
  33. config.include Devise::Test::ControllerHelpers, type: :controller
  34. config.include Devise::Test::ControllerHelpers, type: :view
  35. config.include Paperclip::Shoulda::Matchers
  36. config.include ActiveSupport::Testing::TimeHelpers
  37. config.before :each, type: :feature do
  38. https = ENV['LOCAL_HTTPS'] == 'true'
  39. Capybara.app_host = "http#{https ? 's' : ''}://#{ENV.fetch('LOCAL_DOMAIN')}"
  40. end
  41. config.before :each, type: :controller do
  42. stub_jsonld_contexts!
  43. end
  44. config.before :each, type: :service do
  45. stub_jsonld_contexts!
  46. end
  47. config.after :each do
  48. Rails.cache.clear
  49. keys = Redis.current.keys
  50. Redis.current.del(keys) if keys.any?
  51. end
  52. end
  53. RSpec::Sidekiq.configure do |config|
  54. config.warn_when_jobs_not_processed_by_sidekiq = false
  55. end
  56. def request_fixture(name)
  57. File.read(Rails.root.join('spec', 'fixtures', 'requests', name))
  58. end
  59. def attachment_fixture(name)
  60. File.open(Rails.root.join('spec', 'fixtures', 'files', name))
  61. end
  62. def stub_jsonld_contexts!
  63. stub_request(:get, 'https://www.w3.org/ns/activitystreams').to_return(request_fixture('json-ld.activitystreams.txt'))
  64. stub_request(:get, 'https://w3id.org/identity/v1').to_return(request_fixture('json-ld.identity.txt'))
  65. stub_request(:get, 'https://w3id.org/security/v1').to_return(request_fixture('json-ld.security.txt'))
  66. end