Make it possible to disable tenants

Note we could use `acts_as_paranoid` with the `without_default_scope`
option, but we aren't doing so because it isn't possible to consider
deleted records in uniqueness validations with the paranoia gem [1].
I've added tests for these cases so we don't accidentally add
`acts_as_paranoid` in the future.

Also note we're extracting a `RowComponent` because, when
enabling/disabling a tenant, we're also enabling/disabling the link
pointing to its URL, and so we need to update the URL column after the
AJAX call.

[1] See issues 285 and 319 in https://github.com/rubysherpas/paranoia/
This commit is contained in:
Javi Martín
2022-12-19 16:32:31 +01:00
parent 0dac6ead77
commit 25435b0297
18 changed files with 204 additions and 11 deletions

View File

@@ -181,6 +181,8 @@ describe Abilities::Administrator do
it { should be_able_to :create, Tenant }
it { should be_able_to :read, Tenant }
it { should be_able_to :update, Tenant }
it { should be_able_to :hide, Tenant }
it { should be_able_to :restore, Tenant }
it { should_not be_able_to :destroy, Tenant }
context "administrators from other tenants" do
@@ -193,6 +195,8 @@ describe Abilities::Administrator do
it { should_not be_able_to :read, Tenant }
it { should_not be_able_to :update, Tenant }
it { should_not be_able_to :destroy, Tenant }
it { should_not be_able_to :hide, Tenant }
it { should_not be_able_to :restore, Tenant }
end
end
end

View File

@@ -117,6 +117,37 @@ describe Tenant do
expect(Tenant.resolve_host("www.consul.dev")).to eq "www.consul.dev"
end
it "raises an exception when accessing a hidden tenant using a subdomain" do
insert(:tenant, schema: "saturn", hidden_at: Time.current)
expect { Tenant.resolve_host("saturn.consul.dev") }.to raise_exception(Apartment::TenantNotFound)
end
it "raises an exception when accessing a hidden tenant using a domain" do
insert(:tenant, :domain, schema: "consul.dev", hidden_at: Time.current)
expect { Tenant.resolve_host("consul.dev") }.to raise_exception(Apartment::TenantNotFound)
end
it "raises an exception when accessing a hidden tenant using a domain starting with www" do
insert(:tenant, :domain, schema: "www.consul.dev", hidden_at: Time.current)
expect { Tenant.resolve_host("www.consul.dev") }.to raise_exception(Apartment::TenantNotFound)
end
it "raises an exception when accessing a hidden tenant with a domain and another tenant resolves to the same domain" do
insert(:tenant, :domain, schema: "saturn.consul.dev", hidden_at: Time.current)
insert(:tenant, schema: "saturn")
expect { Tenant.resolve_host("saturn.consul.dev") }.to raise_exception(Apartment::TenantNotFound)
end
it "ignores hidden tenants with nil as their schema" do
insert(:tenant, schema: nil, hidden_at: Time.current)
expect(Tenant.resolve_host("consul.dev")).to be nil
end
context "multitenancy disabled" do
before { allow(Rails.application.config).to receive(:multitenancy).and_return(false) }
@@ -318,6 +349,11 @@ describe Tenant do
expect(build(:tenant, schema: "subdomainx")).not_to be_valid
end
it "is not valid with the schema of an already existing hidden record" do
insert(:tenant, schema: "subdomainx", hidden_at: Time.current)
expect(build(:tenant, schema: "subdomainx")).not_to be_valid
end
it "is not valid with an excluded subdomain" do
%w[mail public shared_extensions www].each do |subdomain|
tenant.schema = subdomain
@@ -345,6 +381,11 @@ describe Tenant do
expect(build(:tenant, name: "Name X")).not_to be_valid
end
it "is not valid with the name of an already existing hidden record" do
insert(:tenant, name: "Name X", hidden_at: Time.current)
expect(build(:tenant, name: "Name X")).not_to be_valid
end
context "Domain schema type" do
before { tenant.schema_type = :domain }