From 0e443d360299f8e8656f8a1b5b2eae6fd8c85301 Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Wed, 10 Dec 2025 17:26:53 +0000 Subject: [PATCH] Use initializer to configure tenant mode --- app/models/concerns/multi_tenant.rb | 6 +++++- config/environments/test.rb | 3 +++ config/initializers/multi_tenant.rb | 3 +++ test/controllers/signups_controller_test.rb | 2 +- test/models/account/multi_tenant_test.rb | 22 +++++++++++++++++++++ test/test_helper.rb | 2 -- test/test_helpers/session_test_helper.rb | 8 ++++---- 7 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 config/initializers/multi_tenant.rb create mode 100644 test/models/account/multi_tenant_test.rb diff --git a/app/models/concerns/multi_tenant.rb b/app/models/concerns/multi_tenant.rb index 4a9c194f2..28c16b2c4 100644 --- a/app/models/concerns/multi_tenant.rb +++ b/app/models/concerns/multi_tenant.rb @@ -1,9 +1,13 @@ module MultiTenant extend ActiveSupport::Concern + included do + cattr_accessor :multi_tenant, default: false + end + class_methods do def accepting_signups? - ENV.fetch("MULTI_TENANT", "false") == "true" || Account.none? + multi_tenant || Account.none? end end end diff --git a/config/environments/test.rb b/config/environments/test.rb index 4be27d242..0bfb2e0f0 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -67,4 +67,7 @@ Rails.application.configure do # Load test helpers config.autoload_paths += %w[ test/test_helpers ] + + # Enable multi-tenant mode for tests + config.x.multi_tenant = true end diff --git a/config/initializers/multi_tenant.rb b/config/initializers/multi_tenant.rb new file mode 100644 index 000000000..0b581ef9d --- /dev/null +++ b/config/initializers/multi_tenant.rb @@ -0,0 +1,3 @@ +Rails.application.config.after_initialize do + Account.multi_tenant = ENV["MULTI_TENANT"] == "true" || Rails.configuration.x.multi_tenant == true +end diff --git a/test/controllers/signups_controller_test.rb b/test/controllers/signups_controller_test.rb index 230064515..d5cbef51f 100644 --- a/test/controllers/signups_controller_test.rb +++ b/test/controllers/signups_controller_test.rb @@ -67,7 +67,7 @@ class SignupsControllerTest < ActionDispatch::IntegrationTest test "redirects to session#new when single_tenant and user exists" do users(:david) - in_single_tenant_mode do + with_multi_tenant_mode(false) do untenanted do get new_signup_path diff --git a/test/models/account/multi_tenant_test.rb b/test/models/account/multi_tenant_test.rb new file mode 100644 index 000000000..fe5ebde00 --- /dev/null +++ b/test/models/account/multi_tenant_test.rb @@ -0,0 +1,22 @@ +require "test_helper" + +class MultiTenantTest < ActiveSupport::TestCase + test "accepting_signups? is true when multi_tenant is enabled" do + with_multi_tenant_mode(true) do + assert Account.accepting_signups? + end + end + + test "accepting_signups? is false when multi_tenant is disabled and accounts exist" do + with_multi_tenant_mode(false) do + assert_not Account.accepting_signups? + end + end + + test "accepting_signups? is true when multi_tenant is disabled but no accounts exist" do + with_multi_tenant_mode(false) do + Account.delete_all + assert Account.accepting_signups? + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 36738664e..12ab491ed 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,6 +1,4 @@ ENV["RAILS_ENV"] ||= "test" -ENV["MULTI_TENANT"] ||= "true" - require_relative "../config/environment" require "rails/test_help" diff --git a/test/test_helpers/session_test_helper.rb b/test/test_helpers/session_test_helper.rb index b7f369036..57d697f75 100644 --- a/test/test_helpers/session_test_helper.rb +++ b/test/test_helpers/session_test_helper.rb @@ -58,11 +58,11 @@ module SessionTestHelper integration_session.default_url_options[:script_name] = original_script_name end - def in_single_tenant_mode - previous_multi_tenant = ENV["MULTI_TENANT"] - ENV["MULTI_TENANT"] = "false" + def with_multi_tenant_mode(enabled) + previous = Account.multi_tenant + Account.multi_tenant = enabled yield ensure - ENV["MULTI_TENANT"] = previous_multi_tenant + Account.multi_tenant = previous end end