From 79d84c0139203f8c1107f83e38bb14eeee37539c Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Thu, 4 Feb 2016 10:21:52 -0300 Subject: [PATCH] Setup multi-tenancy before loading the user's session --- app/controllers/application_controller.rb | 5 ----- config/application.rb | 3 +++ lib/noosfero/multi_tenancy.rb | 12 ++++++++++++ test/functional/application_controller_test.rb | 24 ------------------------ test/integration/multi_tenancy_test.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 test/integration/multi_tenancy_test.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4abaacb..b226fae 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,7 +3,6 @@ require 'noosfero/multi_tenancy' class ApplicationController < ActionController::Base protect_from_forgery - before_filter :setup_multitenancy before_filter :detect_stuff_by_domain before_filter :init_noosfero_plugins before_filter :allow_cross_domain_access @@ -105,10 +104,6 @@ class ApplicationController < ActionController::Base super || form_authenticity_token == request.headers['X-XSRF-TOKEN'] end - def setup_multitenancy - Noosfero::MultiTenancy.setup!(request.host) - end - def boxes_editor? false end diff --git a/config/application.rb b/config/application.rb index 8dcb35f..8394adf 100644 --- a/config/application.rb +++ b/config/application.rb @@ -15,6 +15,9 @@ module Noosfero require 'noosfero/plugin' + require 'noosfero/multi_tenancy' + config.middleware.use Noosfero::MultiTenancy::Middleware + config.action_controller.include_all_helpers = false # Settings in config/environments/* take precedence over those specified here. diff --git a/lib/noosfero/multi_tenancy.rb b/lib/noosfero/multi_tenancy.rb index ee829e9..823eaa8 100644 --- a/lib/noosfero/multi_tenancy.rb +++ b/lib/noosfero/multi_tenancy.rb @@ -22,6 +22,18 @@ module Noosfero end end + class Middleware + def initialize(app) + @app = app + end + + def call(env) + request = Rack::Request.new(env) + Noosfero::MultiTenancy.setup!(request.host) + @app.call(env) + end + end + private def self.load_map diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index 5a4d504..1032ba2 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -478,30 +478,6 @@ class ApplicationControllerTest < ActionController::TestCase assert_response :forbidden end - if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL' - - should 'change postgresql schema' do - uses_host 'schema1.com' - Noosfero::MultiTenancy.expects(:on?).returns(true) - Noosfero::MultiTenancy.expects(:mapping).returns({ 'schema1.com' => 'schema1' }).at_least_once - exception = assert_raise(ActiveRecord::StatementInvalid) { get :index } - - # we have switched to a new database schema; depending on the PostgreSQL - # version, we will receive either an error message because the schema - # does not exist, or an error saying that whatever table we need can't be - # found. - assert_match /(SET search_path TO schema1|PG::UndefinedTable)/, exception.message - end - - should 'not change postgresql schema if multitenancy is off' do - uses_host 'schema1.com' - Noosfero::MultiTenancy.stubs(:on?).returns(false) - Noosfero::MultiTenancy.stubs(:mapping).returns({ 'schema1.com' => 'schema1' }) - assert_nothing_raised(ActiveRecord::StatementInvalid) { get :index } - end - - end - should 'register search_term occurrence on find_by_contents' do controller = ApplicationController.new controller.stubs(:environment).returns(Environment.default) diff --git a/test/integration/multi_tenancy_test.rb b/test/integration/multi_tenancy_test.rb new file mode 100644 index 0000000..df9dc10 --- /dev/null +++ b/test/integration/multi_tenancy_test.rb @@ -0,0 +1,40 @@ +require_relative "../test_helper" + +class MultiTenancyTest < ActionDispatch::IntegrationTest + + should 'change postgresql schema' do + host! 'schema1.com' + Noosfero::MultiTenancy.expects(:on?).at_least_once.returns(true) + Noosfero::MultiTenancy.expects(:mapping).returns({ 'schema1.com' => 'schema1' }).at_least_once + exception = assert_raise(ActiveRecord::StatementInvalid) { get '/' } + + # we have switched to a new database schema; depending on the PostgreSQL + # version, we will receive either an error message because the schema + # does not exist, or an error saying that whatever table we need can't be + # found. + assert_match /(SET search_path TO schema1|PG::UndefinedTable)/, exception.message + end + + should 'not change postgresql schema if multitenancy is off' do + host! 'schema1.com' + Noosfero::MultiTenancy.stubs(:on?).returns(false) + Noosfero::MultiTenancy.stubs(:mapping).returns({ 'schema1.com' => 'schema1' }) + assert_nothing_raised(ActiveRecord::StatementInvalid) { get '/' } + end + + should 'find session from the correct database schema' do + Noosfero::MultiTenancy.expects(:on?).at_least_once.returns(true) + Noosfero::MultiTenancy.expects(:mapping).returns({ 'schema2.com' => 'public', 'schema1.com' => 'schema1' }).at_least_once + + user = create_user + session_obj = create(Session, user_id: user.id, session_id: 'some_id', data: {}) + person_identifier = user.person.identifier + + Noosfero::MultiTenancy.setup!('schema1.com') + host! 'schema2.com' + cookies[:_noosfero_session] = session_obj.session_id + assert_nothing_raised { get "/myprofile/#{person_identifier}" } + assert_equal 'public', ActiveRecord::Base.connection.schema_search_path + end + +end -- libgit2 0.21.2