A platform for community discussion. Free, open, simple.
DEV: Improve multisite testing (#14884)
David Taylor13fdc979a8e0
diff --git a/config/initializers/200-first_middlewares.rb b/config/initializers/200-first_middlewares.rbindex ad383d6137b..c8e4951bd1a 100644--- a/config/initializers/200-first_middlewares.rb+++ b/config/initializers/200-first_middlewares.rb@@ -20,7 +20,18 @@ if Rails.env != 'development' || ENV['TRACK_REQUESTS']endend-if Rails.configuration.multisite+if Rails.env.test?+ # In test mode we can't insert/remove middlewares+ # Therefore we insert a small helper which effectively switches the multisite+ # middleware on/off based on the Rails.configuration.multisite value+ class TestMultisiteMiddleware < RailsMultisite::Middleware+ def call(env)+ return @app.call(env) if !Rails.configuration.multisite+ super(env)+ end+ end+ Rails.configuration.middleware.unshift TestMultisiteMiddleware, RailsMultisite::DiscoursePatches.config+elsif Rails.configuration.multisiteassets_hostnames = GlobalSetting.cdn_hostnamesif assets_hostnames.empty?diff --git a/spec/integration/multisite_spec.rb b/spec/integration/multisite_spec.rbindex d369617850b..d1026bd9809 100644--- a/spec/integration/multisite_spec.rb+++ b/spec/integration/multisite_spec.rb@@ -2,62 +2,33 @@require 'rails_helper'-describe 'multisite', type: :multisite do- class DBNameMiddleware- def initialize(app, config = {})- @app = app- end-- def call(env)- # note current_db is already being ruined on boot cause its not multisite- [200, {}, [RailsMultisite::ConnectionManagement.current_hostname]]- end- end-- let :session do- stack = ActionDispatch::MiddlewareStack.new- stack.use RailsMultisite::Middleware, RailsMultisite::DiscoursePatches.config- stack.use DBNameMiddleware-- routes = ActionDispatch::Routing::RouteSet.new- stack.build(routes)- end-+describe 'multisite', type: [:multisite, :request] doit "should always allow /srv/status through" do- headers = {- "HTTP_HOST" => "unknown.com",- "REQUEST_METHOD" => "GET",- "PATH_INFO" => "/srv/status",- "rack.input" => StringIO.new- }+ get "http://unknown.com/srv/status"+ expect(response.status).to eq(200)+ expect(request.env["HTTP_HOST"]).to eq("test.localhost") # Rewritten by EnforceHostname middleware+ end- code, _, body = session.call(headers)- expect(code).to eq(200)- expect(body.join).to eq("test.localhost")+ it "should 404 for unknown domains" do+ get "http://unknown.com/about.json"+ expect(response.status).to eq(404)end- it "should 404 on unknown routes" do- headers = {- "HTTP_HOST" => "unknown.com",- "REQUEST_METHOD" => "GET",- "PATH_INFO" => "/topics",- "rack.input" => StringIO.new- }+ it "should hit correct site otherwise" do+ site_1_url = Fabricate(:topic, title: "Site 1 Topic Title", user: Discourse.system_user).relative_url- code, _ = session.call(headers)- expect(code).to eq(404)- end+ test_multisite_connection('second') do+ site_2_url = Fabricate(:topic, title: "Site 2 Topic Title", user: Discourse.system_user).relative_url- it "should hit correct site elsewise" do- headers = {- "HTTP_HOST" => "test2.localhost",- "REQUEST_METHOD" => "GET",- "PATH_INFO" => "/topics",- "rack.input" => StringIO.new- }+ get "http://test.localhost/#{site_1_url}.json"+ expect(request.env["RAILS_MULTISITE_HOST"]).to eq("test.localhost")+ expect(response.status).to eq(200)+ expect(response.parsed_body["title"]).to eq("Site 1 Topic Title")- code, _, body = session.call(headers)- expect(code).to eq(200)- expect(body.join).to eq("test2.localhost")+ get "http://test2.localhost/#{site_2_url}.json"+ expect(response.status).to eq(200)+ expect(request.env["RAILS_MULTISITE_HOST"]).to eq("test2.localhost")+ expect(response.parsed_body["title"]).to eq("Site 2 Topic Title")+ endendenddiff --git a/spec/lib/backup_restore/uploads_restorer_spec.rb b/spec/lib/backup_restore/uploads_restorer_spec.rbindex 2963e381e08..cd3079e1fa8 100644--- a/spec/lib/backup_restore/uploads_restorer_spec.rb+++ b/spec/lib/backup_restore/uploads_restorer_spec.rb@@ -99,7 +99,7 @@ describe BackupRestore::UploadsRestorer dolet!(:multisite) { { name: "multisite", value: true } }let!(:no_multisite) { { name: "multisite", value: false } }let!(:source_db_name) { { name: "db_name", value: "foo" } }- let!(:base_url) { { name: "base_url", value: "https://www.example.com/forum" } }+ let!(:base_url) { { name: "base_url", value: "https://test.localhost/forum" } }let!(:no_cdn_url) { { name: "cdn_url", value: nil } }let!(:cdn_url) { { name: "cdn_url", value: "https://some-cdn.example.com" } }let(:target_site_name) { target_site_type == multisite ? "second" : "default" }@@ -187,7 +187,7 @@ describe BackupRestore::UploadsRestorer doexpect_remap(target_site_name: target_site_name,metadata: [source_site_type, base_url],- from: "https://www.example.com/forum",+ from: "https://test.localhost/forum",to: "http://localhost")enddiff --git a/spec/rails_helper.rb b/spec/rails_helper.rbindex 1a68ce59bb7..18c9956b053 100644--- a/spec/rails_helper.rb+++ b/spec/rails_helper.rb@@ -291,6 +291,10 @@ RSpec.configure do |config|DB.test_transaction = ActiveRecord::Base.connection.current_transactionend+ # Match the request hostname to the value in `database.yml`+ config.before(:all, type: [:request, :multisite]) { host! "test.localhost" }+ config.before(:each, type: [:request, :multisite]) { host! "test.localhost" }+config.before(:each, type: :multisite) doRails.configuration.multisite = true # rubocop:disable Discourse/NoDirectMultisiteManipulation