summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorRenaud Chaput <renchap@gmail.com>2023-07-28 23:09:49 +0200
committerGitHub <noreply@github.com>2023-07-28 23:09:49 +0200
commit4d1b67f664e463f28ff45b8e125998ffcd2de50b (patch)
treeafe51e0b714338a3472b94f8d1d838c0bbc4859e /spec
parent8d5d707cc1b7ca5461f628bde1e59e0c2096a771 (diff)
Add end-to-end (system) tests (#25461)
Diffstat (limited to 'spec')
-rw-r--r--spec/rails_helper.rb48
-rw-r--r--spec/spec_helper.rb77
-rw-r--r--spec/support/stories/profile_stories.rb6
-rw-r--r--spec/system/new_statuses_spec.rb45
4 files changed, 173 insertions, 3 deletions
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index 2645f74e40b..0f1073630d9 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -1,6 +1,14 @@
# frozen_string_literal: true
ENV['RAILS_ENV'] ||= 'test'
+
+# This needs to be defined before Rails is initialized
+RUN_SYSTEM_SPECS = ENV.fetch('RUN_SYSTEM_SPECS', false)
+
+if RUN_SYSTEM_SPECS
+ STREAMING_PORT = ENV.fetch('TEST_STREAMING_PORT', '4020')
+ ENV['STREAMING_API_BASE_URL'] = "http://localhost:#{STREAMING_PORT}"
+end
require File.expand_path('../config/environment', __dir__)
abort('The Rails environment is running in production mode!') if Rails.env.production?
@@ -15,10 +23,14 @@ require 'chewy/rspec'
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
-WebMock.disable_net_connect!(allow: Chewy.settings[:host])
+WebMock.disable_net_connect!(allow: Chewy.settings[:host], allow_localhost: RUN_SYSTEM_SPECS)
Sidekiq::Testing.inline!
Sidekiq.logger = nil
+# System tests config
+DatabaseCleaner.strategy = [:deletion]
+streaming_server_manager = StreamingServerManager.new
+
Devise::Test::ControllerHelpers.module_eval do
alias_method :original_sign_in, :sign_in
@@ -56,6 +68,8 @@ module SignedRequestHelpers
end
RSpec.configure do |config|
+ # This is set before running spec:system, see lib/tasks/tests.rake
+ config.filter_run_excluding type: :system unless RUN_SYSTEM_SPECS
config.fixture_path = Rails.root.join('spec', 'fixtures')
config.use_transactional_fixtures = true
config.order = 'random'
@@ -83,8 +97,7 @@ RSpec.configure do |config|
end
config.before :each, type: :feature do
- https = ENV['LOCAL_HTTPS'] == 'true'
- Capybara.app_host = "http#{https ? 's' : ''}://#{ENV.fetch('LOCAL_DOMAIN')}"
+ Capybara.current_driver = :rack_test
end
config.before :each, type: :controller do
@@ -95,6 +108,35 @@ RSpec.configure do |config|
stub_jsonld_contexts!
end
+ config.before :suite do
+ if RUN_SYSTEM_SPECS
+ Webpacker.compile
+ streaming_server_manager.start(port: STREAMING_PORT)
+ end
+ end
+
+ config.after :suite do
+ streaming_server_manager.stop
+ end
+
+ config.around :each, type: :system do |example|
+ # driven_by :selenium, using: :chrome, screen_size: [1600, 1200]
+ driven_by :selenium, using: :headless_chrome, screen_size: [1600, 1200]
+
+ # The streaming server needs access to the database
+ # but with use_transactional_tests every transaction
+ # is rolled-back, so the streaming server never sees the data
+ # So we disable this feature for system tests, and use DatabaseCleaner to clean
+ # the database tables between each test
+ self.use_transactional_tests = false
+
+ DatabaseCleaner.cleaning do
+ example.run
+ end
+
+ self.use_transactional_tests = true
+ end
+
config.before(:each) do |example|
unless example.metadata[:paperclip_processing]
allow_any_instance_of(Paperclip::Attachment).to receive(:post_process).and_return(true) # rubocop:disable RSpec/AnyInstance
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 7b3af0f90bc..dcbcad48e6f 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -52,3 +52,80 @@ def expect_push_bulk_to_match(klass, matcher)
'args' => matcher,
}))
end
+
+class StreamingServerManager
+ @running_thread = nil
+
+ def initialize
+ at_exit { stop }
+ end
+
+ def start(port: 4020)
+ return if @running_thread
+
+ queue = Queue.new
+
+ @queue = queue
+
+ @running_thread = Thread.new do
+ Open3.popen2e(
+ {
+ 'REDIS_NAMESPACE' => ENV.fetch('REDIS_NAMESPACE'),
+ 'DB_NAME' => "#{ENV.fetch('DB_NAME', 'mastodon')}_test#{ENV.fetch('TEST_ENV_NUMBER', '')}",
+ 'RAILS_ENV' => ENV.fetch('RAILS_ENV', 'test'),
+ 'NODE_ENV' => ENV.fetch('STREAMING_NODE_ENV', 'development'),
+ 'PORT' => port.to_s,
+ },
+ 'node index.js', # must not call yarn here, otherwise it will fail because yarn does not send signals to its child process
+ chdir: Rails.root.join('streaming')
+ ) do |_stdin, stdout_err, process_thread|
+ status = :starting
+
+ # Spawn a thread to listen on streaming server output
+ output_thread = Thread.new do
+ stdout_err.each_line do |line|
+ Rails.logger.info "Streaming server: #{line}"
+
+ if status == :starting && line.match('Streaming API now listening on')
+ status = :started
+ @queue.enq 'started'
+ end
+ end
+ end
+
+ # And another thread to listen on commands from the main thread
+ loop do
+ msg = queue.pop
+
+ case msg
+ when 'stop'
+ # we need to properly stop the reading thread
+ output_thread.kill
+
+ # Then stop the node process
+ Process.kill('KILL', process_thread.pid)
+
+ # And we stop ourselves
+ @running_thread.kill
+ end
+ end
+ end
+ end
+
+ # wait for 10 seconds for the streaming server to start
+ Timeout.timeout(10) do
+ loop do
+ break if @queue.pop == 'started'
+ end
+ end
+ end
+
+ def stop
+ return unless @running_thread
+
+ @queue.enq 'stop'
+
+ # Wait for the thread to end
+ @running_thread.join
+ end
+end
diff --git a/spec/support/stories/profile_stories.rb b/spec/support/stories/profile_stories.rb
index de7ae17e633..2b345ddef10 100644
--- a/spec/support/stories/profile_stories.rb
+++ b/spec/support/stories/profile_stories.rb
@@ -9,6 +9,8 @@ module ProfileStories
email: email, password: password, confirmed_at: confirmed_at,
account: Fabricate(:account, username: 'bob')
)
+
+ Web::Setting.where(user: bob).first_or_initialize(user: bob).update!(data: { introductionVersion: 201812160442020 }) if finished_onboarding # rubocop:disable Style/NumericLiterals
end
def as_a_logged_in_user
@@ -42,4 +44,8 @@ module ProfileStories
def password
@password ||= 'password'
end
+
+ def finished_onboarding
+ @finished_onboarding || false
+ end
end
diff --git a/spec/system/new_statuses_spec.rb b/spec/system/new_statuses_spec.rb
new file mode 100644
index 00000000000..6faed6c808c
--- /dev/null
+++ b/spec/system/new_statuses_spec.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe 'NewStatuses' do
+ include ProfileStories
+
+ subject { page }
+
+ let(:email) { 'test@example.com' }
+ let(:password) { 'password' }
+ let(:confirmed_at) { Time.zone.now }
+ let(:finished_onboarding) { true }
+
+ before do
+ as_a_logged_in_user
+ visit root_path
+ end
+
+ it 'can be posted' do
+ expect(subject).to have_css('div.app-holder')
+
+ status_text = 'This is a new status!'
+
+ within('.compose-form') do
+ fill_in "What's on your mind?", with: status_text
+ click_on 'Publish!'
+ end
+
+ expect(subject).to have_selector('.status__content__text', text: status_text)
+ end
+
+ it 'can be posted again' do
+ expect(subject).to have_css('div.app-holder')
+
+ status_text = 'This is a second status!'
+
+ within('.compose-form') do
+ fill_in "What's on your mind?", with: status_text
+ click_on 'Publish!'
+ end
+
+ expect(subject).to have_selector('.status__content__text', text: status_text)
+ end
+end