Don't create schemas in model tests unless needed
Creating a schema takes about 3-4 seconds on my machine, so omitting the callbacks makes tests much faster. To do so, we're using the `insert!` method added in Rails 6.0, which inserts a record without executing callbacks or validations. To make the tests look consistent, we're adding a FactoryBot strategy which uses `insert!` instead of `create!`. Note this strategy is useless in most cases because it doesn't work when models have translatable attributes or associations. However, IMHO it's worth it even if we only use it for tenants. We could also use `Tenant.insert!` instead, but then we would have to add all the mandatory attributes, and in this case the code is clearer if we only add the attributes we need for the test.
This commit is contained in:
@@ -183,17 +183,16 @@ describe Abilities::Administrator do
|
||||
it { should be_able_to :update, Tenant }
|
||||
it { should_not be_able_to :destroy, Tenant }
|
||||
|
||||
it "does not allow administrators from other tenants to manage tenants " do
|
||||
create(:tenant, schema: "subsidiary")
|
||||
|
||||
Tenant.switch("subsidiary") do
|
||||
admin = create(:administrator).user
|
||||
|
||||
expect(admin).not_to be_able_to :create, Tenant
|
||||
expect(admin).not_to be_able_to :read, Tenant
|
||||
expect(admin).not_to be_able_to :update, Tenant
|
||||
expect(admin).not_to be_able_to :destroy, Tenant
|
||||
context "administrators from other tenants" do
|
||||
before do
|
||||
insert(:tenant, schema: "subsidiary")
|
||||
allow(Tenant).to receive(:current_schema).and_return("subsidiary")
|
||||
end
|
||||
|
||||
it { should_not be_able_to :create, Tenant }
|
||||
it { should_not be_able_to :read, Tenant }
|
||||
it { should_not be_able_to :update, Tenant }
|
||||
it { should_not be_able_to :destroy, Tenant }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user