From 86e6732f78300f3569d6324e049372723ad08bd8 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 9 Nov 2017 15:40:34 +1100 Subject: [PATCH] FEATURE: update rails multisite always allow /srv/status through even if host does not match --- Gemfile | 2 +- Gemfile.lock | 4 +- config/initializers/014-rails_multisite.rb | 19 ++++++ spec/integration/multisite_spec.rb | 67 ++++++++++++++++++++++ 4 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 config/initializers/014-rails_multisite.rb create mode 100644 spec/integration/multisite_spec.rb diff --git a/Gemfile b/Gemfile index aafaf0e8051..291fac3d3c2 100644 --- a/Gemfile +++ b/Gemfile @@ -46,7 +46,7 @@ gem 'barber' gem 'message_bus' -gem 'rails_multisite', '~> 1.1.0.rc4' +gem 'rails_multisite' gem 'fast_xs' diff --git a/Gemfile.lock b/Gemfile.lock index 9451fef5ae5..c15ad160d78 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -271,7 +271,7 @@ GEM nokogiri (>= 1.6) rails-html-sanitizer (1.0.3) loofah (~> 2.0) - rails_multisite (1.1.0.rc4) + rails_multisite (1.1.1) activerecord (> 4.2, < 6) railties (> 4.2, < 6) railties (5.1.4) @@ -473,7 +473,7 @@ DEPENDENCIES r2 (~> 0.2.5) rack-mini-profiler rack-protection - rails_multisite (~> 1.1.0.rc4) + rails_multisite railties (~> 5.1) rake rb-fsevent diff --git a/config/initializers/014-rails_multisite.rb b/config/initializers/014-rails_multisite.rb new file mode 100644 index 00000000000..fba7ec79dab --- /dev/null +++ b/config/initializers/014-rails_multisite.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RailsMultisite::DiscoursePatches + def self.config + { + db_lookup: lambda do |env| + env["PATH_INFO"] == "/srv/status" ? "default" : nil + end + } + end +end + +if Rails.configuration.multisite + Rails.configuration.middleware.swap( + RailsMultisite::ConnectionManagement, + RailsMultisite::ConnectionManagement, + RailsMultisite::DiscoursePatches.config + ) +end diff --git a/spec/integration/multisite_spec.rb b/spec/integration/multisite_spec.rb new file mode 100644 index 00000000000..678052a0466 --- /dev/null +++ b/spec/integration/multisite_spec.rb @@ -0,0 +1,67 @@ +require 'rails_helper' + +describe '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 + RailsMultisite::ConnectionManagement.config_filename = "spec/fixtures/multisite/two_dbs.yml" + RailsMultisite::ConnectionManagement.load_settings! + + stack = ActionDispatch::MiddlewareStack.new + stack.use RailsMultisite::ConnectionManagement, RailsMultisite::DiscoursePatches.config + stack.use DBNameMiddleware + + routes = ActionDispatch::Routing::RouteSet.new + stack.build(routes) + end + + it "should always allow /srv/status through" do + headers = { + "HTTP_HOST" => "unknown.com", + "REQUEST_METHOD" => "GET", + "PATH_INFO" => "/srv/status", + "rack.input" => StringIO.new + } + + code, _, body = session.call(headers) + expect(code).to eq(200) + expect(body.join).to eq("test.localhost") + end + + it "should 404 on unknown routes" do + headers = { + "HTTP_HOST" => "unknown.com", + "REQUEST_METHOD" => "GET", + "PATH_INFO" => "/topics", + "rack.input" => StringIO.new + } + + code, _ = session.call(headers) + expect(code).to eq(404) + end + + it "should hit correct site elsewise" do + + headers = { + "HTTP_HOST" => "test2.localhost", + "REQUEST_METHOD" => "GET", + "PATH_INFO" => "/topics", + "rack.input" => StringIO.new + } + + code, _, body = session.call(headers) + expect(code).to eq(200) + expect(body.join).to eq("test2.localhost") + end + +end