From 159a24f452a8f639cfefe313b455d3eb2812bb49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 3 Dec 2022 14:45:11 +0100 Subject: [PATCH 1/2] Don't create unnecessary tenants in tenant tests While creating the "venus" tenant for every test makes the code cleaner, it also makes the tests much slower, so we aren't doing so in tests where we don't use this tenant. --- spec/system/multitenancy_spec.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/spec/system/multitenancy_spec.rb b/spec/system/multitenancy_spec.rb index 513a4489e..188764ad9 100644 --- a/spec/system/multitenancy_spec.rb +++ b/spec/system/multitenancy_spec.rb @@ -1,12 +1,10 @@ require "rails_helper" describe "Multitenancy", :seed_tenants do - before do - create(:tenant, schema: "mars") - create(:tenant, schema: "venus") - end + before { create(:tenant, schema: "mars") } scenario "Disabled features", :no_js do + create(:tenant, schema: "venus") Tenant.switch("mars") { Setting["process.debates"] = true } Tenant.switch("venus") { Setting["process.debates"] = nil } @@ -22,6 +20,7 @@ describe "Multitenancy", :seed_tenants do end scenario "Content is different for differents tenants" do + create(:tenant, schema: "venus") Tenant.switch("mars") { create(:poll, name: "Human rights for Martians?") } with_subdomain("mars") do @@ -69,6 +68,7 @@ describe "Multitenancy", :seed_tenants do end scenario "Creating content in one tenant doesn't affect other tenants" do + create(:tenant, schema: "venus") Tenant.switch("mars") { login_as(create(:user)) } with_subdomain("mars") do @@ -96,6 +96,7 @@ describe "Multitenancy", :seed_tenants do end scenario "Users from another tenant cannot vote" do + create(:tenant, schema: "venus") Tenant.switch("mars") { create(:proposal, title: "Earth invasion") } Tenant.switch("venus") { login_as(create(:user)) } @@ -138,6 +139,7 @@ describe "Multitenancy", :seed_tenants do end scenario "Users from another tenant can't sign in" do + create(:tenant, schema: "venus") Tenant.switch("mars") { create(:user, email: "marty@consul.dev", password: "20151021") } with_subdomain("mars") do From 94f78c0a5500874417ec1a12a6d4108d60f22478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 3 Dec 2022 14:53:07 +0100 Subject: [PATCH 2/2] Respond with not found on missing tenants Just like we respond with "not found" for any other record. This improves the user experience because with the "Not found" error message people realize the URL is wrong instead of thinking that they broke the application. --- config/application.rb | 1 + spec/system/multitenancy_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/config/application.rb b/config/application.rb index 82c552a05..554a525dc 100644 --- a/config/application.rb +++ b/config/application.rb @@ -48,6 +48,7 @@ module Consul # Handle custom exceptions config.action_dispatch.rescue_responses["FeatureFlags::FeatureDisabled"] = :forbidden + config.action_dispatch.rescue_responses["Apartment::TenantNotFound"] = :not_found # Store uploaded files on the local file system (see config/storage.yml for options). config.active_storage.service = :local diff --git a/spec/system/multitenancy_spec.rb b/spec/system/multitenancy_spec.rb index 188764ad9..bc92dc60e 100644 --- a/spec/system/multitenancy_spec.rb +++ b/spec/system/multitenancy_spec.rb @@ -173,4 +173,12 @@ describe "Multitenancy", :seed_tenants do expect(page).not_to have_css "html.tenant-public" end end + + scenario "Shows the not found page when accessing a non-existing tenant", :show_exceptions do + with_subdomain("jupiter") do + visit root_path + + expect(page).to have_title "Not found" + end + end end