acts_as_tenant 0.4.2 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +124 -25
- data/Rakefile +5 -3
- data/lib/acts_as_tenant/configuration.rb +6 -19
- data/lib/acts_as_tenant/controller_extensions/filter.rb +13 -0
- data/lib/acts_as_tenant/controller_extensions/subdomain.rb +20 -0
- data/lib/acts_as_tenant/controller_extensions/subdomain_or_domain.rb +20 -0
- data/lib/acts_as_tenant/controller_extensions.rb +10 -59
- data/lib/acts_as_tenant/errors.rb +3 -4
- data/lib/acts_as_tenant/model_extensions.rb +59 -136
- data/lib/acts_as_tenant/sidekiq.rb +16 -8
- data/lib/acts_as_tenant/tenant_helper.rb +7 -0
- data/lib/acts_as_tenant/test_tenant_middleware.rb +15 -0
- data/lib/acts_as_tenant/version.rb +1 -1
- data/lib/acts_as_tenant.rb +112 -14
- data/spec/acts_as_tenant/configuration_spec.rb +8 -19
- data/spec/acts_as_tenant/sidekiq_spec.rb +14 -19
- data/spec/{acts_as_tenant/tenant_by_filter_spec.rb → controllers/filter_spec.rb} +7 -12
- data/spec/controllers/subdomain_or_domain_spec.rb +55 -0
- data/spec/controllers/subdomain_spec.rb +49 -0
- data/spec/dummy/.ruby-version +1 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/config/manifest.js +2 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +2 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/javascript/packs/application.js +15 -0
- data/spec/dummy/app/jobs/application_job.rb +7 -0
- data/spec/dummy/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/app/mailers/user_mailer.rb +5 -0
- data/spec/dummy/app/models/account.rb +4 -0
- data/spec/dummy/app/models/aliased_task.rb +4 -0
- data/spec/dummy/app/models/article.rb +3 -0
- data/spec/dummy/app/models/comment.rb +5 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/models/custom_counter_cache_task.rb +4 -0
- data/spec/dummy/app/models/custom_foreign_key_task.rb +4 -0
- data/spec/dummy/app/models/custom_primary_key_task.rb +4 -0
- data/spec/dummy/app/models/global_project.rb +6 -0
- data/spec/dummy/app/models/global_project_with_conditions.rb +6 -0
- data/spec/dummy/app/models/global_project_with_if.rb +6 -0
- data/spec/dummy/app/models/manager.rb +4 -0
- data/spec/dummy/app/models/polymorphic_tenant_comment.rb +5 -0
- data/spec/dummy/app/models/project.rb +8 -0
- data/spec/dummy/app/models/task.rb +7 -0
- data/spec/dummy/app/models/unique_task.rb +5 -0
- data/spec/dummy/app/models/unscoped_model.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +33 -0
- data/spec/dummy/config/application.rb +19 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/cable.yml +10 -0
- data/spec/dummy/config/database.yml +19 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +62 -0
- data/spec/dummy/config/environments/production.rb +112 -0
- data/spec/dummy/config/environments/test.rb +49 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/dummy/config/initializers/assets.rb +12 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/content_security_policy.rb +28 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +40 -0
- data/spec/dummy/config/puma.rb +38 -0
- data/spec/dummy/config/routes.rb +4 -0
- data/spec/dummy/config/spring.rb +6 -0
- data/spec/dummy/config/storage.yml +34 -0
- data/spec/dummy/config.ru +5 -0
- data/spec/dummy/db/schema.rb +84 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/tmp/development_secret.txt +1 -0
- data/spec/fixtures/accounts.yml +10 -0
- data/spec/fixtures/custom_primary_key_tasks.yml +2 -0
- data/spec/fixtures/global_projects.yml +13 -0
- data/spec/fixtures/projects.yml +10 -0
- data/spec/helpers/tenant_helper_spec.rb +16 -0
- data/spec/middlewares/test_tenant_middleware_spec.rb +86 -0
- data/spec/models/model_extensions_spec.rb +413 -0
- data/spec/spec_helper.rb +9 -13
- metadata +205 -41
- data/.gitignore +0 -7
- data/.travis.yml +0 -4
- data/CHANGELOG.md +0 -108
- data/Gemfile +0 -4
- data/_config.yml +0 -1
- data/acts_as_tenant.gemspec +0 -32
- data/docs/blog_post.md +0 -67
- data/rails/init.rb +0 -2
- data/spec/active_record_helper.rb +0 -22
- data/spec/active_record_models.rb +0 -116
- data/spec/acts_as_tenant/model_extensions_spec.rb +0 -420
- data/spec/acts_as_tenant/tenant_by_subdomain_or_domain.rb +0 -46
- data/spec/acts_as_tenant/tenant_by_subdomain_spec.rb +0 -32
- data/spec/database.yml +0 -3
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
f1e8981c98a3cd825ba410f2080160547945e8c380645e0dfaeba509fa55291dcd4e317d6c2ba08bc94a4110b6c567987a66f616f67e3b5f62e11a4b216820e6
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ActionView::Base, type: :helper do
|
4
|
+
it "responds ot current_tenant" do
|
5
|
+
expect(helper).to respond_to(:current_tenant)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns nil if no tenant set" do
|
9
|
+
expect(helper.current_tenant).to be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns the current tenant" do
|
13
|
+
ActsAsTenant.current_tenant = accounts(:foo)
|
14
|
+
expect(helper.current_tenant).to eq(accounts(:foo))
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "acts_as_tenant/test_tenant_middleware"
|
3
|
+
|
4
|
+
class TestRackApp1
|
5
|
+
def call(_env)
|
6
|
+
ActsAsTenant.current_tenant = Account.first
|
7
|
+
TestReceiver.assert_current_id(ActsAsTenant.current_tenant.id)
|
8
|
+
ActsAsTenant.current_tenant = nil
|
9
|
+
[200, {}, ["OK"]]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestRackApp2
|
14
|
+
def call(_env)
|
15
|
+
TestReceiver.assert_current_id(ActsAsTenant.current_tenant.try(:id))
|
16
|
+
[200, {}, ["OK"]]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class TestReceiver
|
21
|
+
def self.assert_current_id(id)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe ActsAsTenant::TestTenantMiddleware do
|
26
|
+
fixtures :accounts
|
27
|
+
after { ActsAsTenant.test_tenant = nil }
|
28
|
+
|
29
|
+
subject { request.get("/some/path") }
|
30
|
+
|
31
|
+
let(:middleware) { described_class.new(app) }
|
32
|
+
let(:request) { Rack::MockRequest.new(middleware) }
|
33
|
+
|
34
|
+
let!(:account1) { accounts(:foo) }
|
35
|
+
let!(:account2) { accounts(:bar) }
|
36
|
+
|
37
|
+
context "when test_tenant is nil before processing" do
|
38
|
+
context "that switches tenancies" do
|
39
|
+
let(:app) { TestRackApp1.new }
|
40
|
+
|
41
|
+
it "should remain nil after processing" do
|
42
|
+
expect(ActsAsTenant.current_tenant).to be_nil
|
43
|
+
expect(TestReceiver).to receive(:assert_current_id).with(account1.id)
|
44
|
+
expect(subject.status).to eq 200
|
45
|
+
expect(ActsAsTenant.current_tenant).to be_nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "that does not switch tenancies" do
|
50
|
+
let(:app) { TestRackApp2.new }
|
51
|
+
|
52
|
+
it "should remain nil after processing" do
|
53
|
+
expect(ActsAsTenant.current_tenant).to be_nil
|
54
|
+
expect(TestReceiver).to receive(:assert_current_id).with(nil)
|
55
|
+
expect(subject.status).to eq 200
|
56
|
+
expect(ActsAsTenant.current_tenant).to be_nil
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when test_tenant is assigned before processing" do
|
62
|
+
before { ActsAsTenant.test_tenant = account2 }
|
63
|
+
|
64
|
+
context "that switches tenancies" do
|
65
|
+
let(:app) { TestRackApp1.new }
|
66
|
+
|
67
|
+
it "should remain assigned after processing" do
|
68
|
+
expect(ActsAsTenant.current_tenant).to eq account2
|
69
|
+
expect(TestReceiver).to receive(:assert_current_id).with(account1.id)
|
70
|
+
expect(subject.status).to eq 200
|
71
|
+
expect(ActsAsTenant.current_tenant).to eq account2
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "that does not switch tenancies" do
|
76
|
+
let(:app) { TestRackApp2.new }
|
77
|
+
|
78
|
+
it "should remain assigned after processing" do
|
79
|
+
expect(ActsAsTenant.current_tenant).to eq account2
|
80
|
+
expect(TestReceiver).to receive(:assert_current_id).with(nil)
|
81
|
+
expect(subject.status).to eq 200
|
82
|
+
expect(ActsAsTenant.current_tenant).to eq account2
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,413 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ActsAsTenant do
|
4
|
+
let(:account) { accounts(:foo) }
|
5
|
+
|
6
|
+
it "can set the current tenant" do
|
7
|
+
ActsAsTenant.current_tenant = :foo
|
8
|
+
expect(ActsAsTenant.current_tenant).to eq(:foo)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "is_scoped_as_tenant should return the correct value when true" do
|
12
|
+
expect(Project.respond_to?(:scoped_by_tenant?)).to eq(true)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "is_scoped_as_tenant should return the correct value when false" do
|
16
|
+
expect(UnscopedModel.respond_to?(:scoped_by_tenant?)).to eq(false)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "tenant_id should be immutable, if already set" do
|
20
|
+
project = account.projects.create!(name: "bar")
|
21
|
+
expect { project.account_id = account.id + 1 }.to raise_error(ActsAsTenant::Errors::TenantIsImmutable)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "setting tenant_id to the same value should not error" do
|
25
|
+
project = account.projects.create!(name: "bar")
|
26
|
+
expect { project.account_id = account.id }.not_to raise_error
|
27
|
+
end
|
28
|
+
|
29
|
+
it "setting tenant_id to a string with same to_i value should not error" do
|
30
|
+
project = account.projects.create!(name: "bar")
|
31
|
+
expect { project.account_id = account.id.to_s }.not_to raise_error
|
32
|
+
end
|
33
|
+
|
34
|
+
it "tenant_id should be mutable, if not already set" do
|
35
|
+
project = projects(:without_account)
|
36
|
+
expect(project.account_id).to be_nil
|
37
|
+
expect { project.account = account }.not_to raise_error
|
38
|
+
end
|
39
|
+
|
40
|
+
it "tenant_id should auto populate after initialization" do
|
41
|
+
ActsAsTenant.current_tenant = account
|
42
|
+
expect(Project.new.account_id).to eq(account.id)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "handles custom foreign_key on tenant model" do
|
46
|
+
ActsAsTenant.current_tenant = account
|
47
|
+
custom_foreign_key_task = CustomForeignKeyTask.create!(name: "foo")
|
48
|
+
expect(custom_foreign_key_task.account).to eq(account)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "handles custom primary_key on tenant model" do
|
52
|
+
ActsAsTenant.current_tenant = account
|
53
|
+
custom_primary_key_task = CustomPrimaryKeyTask.create!
|
54
|
+
expect(custom_primary_key_task.account).to eq(account)
|
55
|
+
expect(CustomPrimaryKeyTask.count).to eq(1)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should correctly increment and decrement the tenants counter_cache column" do
|
59
|
+
ActsAsTenant.current_tenant = account
|
60
|
+
project = CustomCounterCacheTask.create!(name: "bar")
|
61
|
+
expect(account.reload.projects_count).to eq(1)
|
62
|
+
project.destroy
|
63
|
+
expect(account.reload.projects_count).to eq(0)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "does not cache account association" do
|
67
|
+
project = account.projects.first
|
68
|
+
ActsAsTenant.current_tenant = account
|
69
|
+
expect(project.account.name).to eq(account.name)
|
70
|
+
account.update!(name: "Acme")
|
71
|
+
expect(project.account.name).to eq("Acme")
|
72
|
+
end
|
73
|
+
|
74
|
+
it "Querying the tenant from a scoped model without a tenant set" do
|
75
|
+
expect(projects(:foo).account).to_not be_nil
|
76
|
+
end
|
77
|
+
|
78
|
+
it "Querying the tenant from a scoped model with a tenant set" do
|
79
|
+
ActsAsTenant.current_tenant = account
|
80
|
+
expect(projects(:foo).account).to eq(accounts(:foo))
|
81
|
+
expect(projects(:bar).account).to eq(accounts(:bar))
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "scoping models" do
|
85
|
+
it "should scope Project.all to the current tenant if set" do
|
86
|
+
ActsAsTenant.current_tenant = account
|
87
|
+
expect(Project.count).to eq(account.projects.count)
|
88
|
+
expect(Project.all).to eq(account.projects)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should allow unscoping" do
|
92
|
+
ActsAsTenant.current_tenant = account
|
93
|
+
expect(Project.unscoped.count).to be > account.projects.count
|
94
|
+
end
|
95
|
+
|
96
|
+
it "returns nothing with unsaved tenant" do
|
97
|
+
ActsAsTenant.current_tenant = Account.new
|
98
|
+
expect(Project.all.count).to eq(0)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "A tenant model with global records" do
|
103
|
+
before do
|
104
|
+
ActsAsTenant.current_tenant = account
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should return global and tenant projects" do
|
108
|
+
expect(GlobalProject.count).to eq(GlobalProject.unscoped.where(account: [nil, account]).count)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "returns global records with unsaved tenant" do
|
112
|
+
ActsAsTenant.current_tenant = Account.new
|
113
|
+
expect(GlobalProject.all.count).to eq(GlobalProject.unscoped.where(account: [nil]).count)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should add the model to ActsAsTenant.models_with_global_records" do
|
117
|
+
expect(ActsAsTenant.models_with_global_records.include?(GlobalProject)).to be_truthy
|
118
|
+
expect(ActsAsTenant.models_with_global_records.include?(Project)).to be_falsy
|
119
|
+
end
|
120
|
+
|
121
|
+
context "should validate tenant records against global & tenant records" do
|
122
|
+
it "global records are valid" do
|
123
|
+
expect(global_projects(:global).valid?).to be(true)
|
124
|
+
end
|
125
|
+
|
126
|
+
it "allows separate global and tenant records" do
|
127
|
+
expect(GlobalProject.new(name: "foo new").valid?).to be(true)
|
128
|
+
end
|
129
|
+
|
130
|
+
it "is valid if tenant is different" do
|
131
|
+
ActsAsTenant.current_tenant = accounts(:bar)
|
132
|
+
|
133
|
+
expect(GlobalProject.new(name: "global foo").valid?).to be(true)
|
134
|
+
end
|
135
|
+
|
136
|
+
it "is invalid with duplicate tenant records" do
|
137
|
+
expect(GlobalProject.new(name: "global foo").valid?).to be(false)
|
138
|
+
end
|
139
|
+
|
140
|
+
it "is invalid if tenant record conflicts with global record" do
|
141
|
+
expect(GlobalProject.new(name: "global").valid?).to be(false)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
context "should validate global records against global & tenant records" do
|
146
|
+
before do
|
147
|
+
ActsAsTenant.current_tenant = nil
|
148
|
+
end
|
149
|
+
|
150
|
+
it "is invalid if global record conflicts with tenant record" do
|
151
|
+
expect(GlobalProject.new(name: "global foo").valid?).to be(false)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context "with conditions in args" do
|
156
|
+
it "respects conditions" do
|
157
|
+
expect(GlobalProjectWithConditions.new(name: "foo").valid?).to be(false)
|
158
|
+
expect(GlobalProjectWithConditions.new(name: "global foo").valid?).to be(true)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
context "with if in args" do
|
163
|
+
it "respects if" do
|
164
|
+
expect(GlobalProjectWithIf.new(name: "foo").valid?).to be(false)
|
165
|
+
expect(GlobalProjectWithIf.new(name: "global foo").valid?).to be(true)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
# Associations
|
171
|
+
context "Associations should be correctly scoped by current tenant" do
|
172
|
+
before do
|
173
|
+
@project = account.projects.create!(name: "foobar")
|
174
|
+
|
175
|
+
# the next line should normally be (nearly) impossible: a task assigned to a tenant project,
|
176
|
+
# but the task has no tenant assigned
|
177
|
+
@task1 = Task.create!(name: "no_tenant", project: @project)
|
178
|
+
|
179
|
+
ActsAsTenant.current_tenant = account
|
180
|
+
@task2 = @project.tasks.create!(name: "baz")
|
181
|
+
|
182
|
+
@project.reload
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should correctly set the tenant on the task created with current_tenant set" do
|
186
|
+
expect(@task2.account).to eq(account)
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should filter out the non-tenant task from the project" do
|
190
|
+
expect(@project.tasks.length).to eq(1)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
it "associations can only be made with in-scope objects" do
|
195
|
+
project1 = accounts(:bar).projects.create!(name: "inaccessible_project")
|
196
|
+
ActsAsTenant.current_tenant = account
|
197
|
+
|
198
|
+
project2 = Project.create!(name: "accessible_project")
|
199
|
+
task = project2.tasks.create!(name: "bar")
|
200
|
+
|
201
|
+
expect(task.update(project_id: project1.id)).to eq(false)
|
202
|
+
end
|
203
|
+
|
204
|
+
it "can create and save an AaT-enabled child without it having a parent" do
|
205
|
+
ActsAsTenant.current_tenant = account
|
206
|
+
expect(Task.new(name: "bar").valid?).to eq(true)
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should be possible to use aliased associations" do
|
210
|
+
expect(AliasedTask.create(name: "foo", project_alias: @project2).valid?).to eq(true)
|
211
|
+
end
|
212
|
+
|
213
|
+
describe "It should be possible to use associations with foreign_key from polymorphic" do
|
214
|
+
it "tenanted objects have a polymorphic association" do
|
215
|
+
ActsAsTenant.current_tenant = account
|
216
|
+
expect { Comment.create!(commentable: account.projects.first) }.not_to raise_error
|
217
|
+
end
|
218
|
+
|
219
|
+
context "tenant is polymorphic" do
|
220
|
+
before do
|
221
|
+
@project = Project.create!(name: "polymorphic project")
|
222
|
+
ActsAsTenant.current_tenant = @project
|
223
|
+
@comment = PolymorphicTenantComment.new(account: account)
|
224
|
+
end
|
225
|
+
|
226
|
+
it "populates commentable_type with the current tenant" do
|
227
|
+
expect(@comment.polymorphic_tenant_commentable_id).to eql(@project.id)
|
228
|
+
expect(@comment.polymorphic_tenant_commentable_type).to eql(@project.class.to_s)
|
229
|
+
end
|
230
|
+
|
231
|
+
context "with another type of tenant, same id" do
|
232
|
+
before do
|
233
|
+
@comment.save!
|
234
|
+
@article = Article.create!(id: @project.id, title: "article title")
|
235
|
+
@comment_on_article = @article.polymorphic_tenant_comments.create!
|
236
|
+
end
|
237
|
+
|
238
|
+
it "correctly scopes to the current tenant type" do
|
239
|
+
expect(@comment_on_article).to be_persisted
|
240
|
+
expect(@comment).to be_persisted
|
241
|
+
expect(PolymorphicTenantComment.count).to eql(1)
|
242
|
+
expect(PolymorphicTenantComment.all.first.attributes).to eql(@comment.attributes)
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
# Additional default_scopes
|
249
|
+
it "should apply both the tenant scope and the user defined default_scope, including :order" do
|
250
|
+
project1 = Project.create!(name: "inaccessible")
|
251
|
+
Task.create!(name: "no_tenant", project: project1)
|
252
|
+
|
253
|
+
ActsAsTenant.current_tenant = account
|
254
|
+
project2 = Project.create!(name: "accessible")
|
255
|
+
task2 = project2.tasks.create!(name: "bar")
|
256
|
+
task3 = project2.tasks.create!(name: "baz")
|
257
|
+
task4 = project2.tasks.create!(name: "foo")
|
258
|
+
project2.tasks.create!(name: "foobar", completed: true)
|
259
|
+
|
260
|
+
tasks = Task.all
|
261
|
+
|
262
|
+
expect(tasks.length).to eq(3)
|
263
|
+
expect(tasks).to eq([task2, task3, task4])
|
264
|
+
end
|
265
|
+
|
266
|
+
# Validates_uniqueness
|
267
|
+
context "When using validates_uniqueness_to_tenant in a aat model" do
|
268
|
+
before do
|
269
|
+
@name = "existing_name"
|
270
|
+
ActsAsTenant.current_tenant = account
|
271
|
+
Project.create!(name: @name)
|
272
|
+
end
|
273
|
+
|
274
|
+
it "should not be possible to create a duplicate within the same tenant" do
|
275
|
+
expect(Project.new(name: @name).valid?).to eq(false)
|
276
|
+
end
|
277
|
+
|
278
|
+
it "should be possible to create a duplicate in another tenant" do
|
279
|
+
ActsAsTenant.current_tenant = accounts(:bar)
|
280
|
+
expect(Project.create(name: @name).valid?).to eq(true)
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
it "handles user defined scopes" do
|
285
|
+
UniqueTask.create!(name: "foo", user_defined_scope: "unique_scope")
|
286
|
+
expect(UniqueTask.create(name: "foo", user_defined_scope: "another_scope")).to be_valid
|
287
|
+
expect(UniqueTask.create(name: "foo", user_defined_scope: "unique_scope")).not_to be_valid
|
288
|
+
end
|
289
|
+
|
290
|
+
context "When using validates_uniqueness_of in a NON-aat model" do
|
291
|
+
it "should not be possible to create duplicates" do
|
292
|
+
UnscopedModel.create!(name: "foo")
|
293
|
+
expect(UnscopedModel.create(name: "foo").valid?).to eq(false)
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
# ::with_tenant
|
298
|
+
describe "::with_tenant" do
|
299
|
+
it "should set current_tenant to the specified tenant inside the block" do
|
300
|
+
ActsAsTenant.with_tenant(account) do
|
301
|
+
expect(ActsAsTenant.current_tenant).to eq(account)
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
it "should reset current_tenant to the previous tenant once exiting the block" do
|
306
|
+
ActsAsTenant.current_tenant = account
|
307
|
+
ActsAsTenant.with_tenant(accounts(:bar)) {}
|
308
|
+
expect(ActsAsTenant.current_tenant).to eq(account)
|
309
|
+
end
|
310
|
+
|
311
|
+
it "should return the value of the block" do
|
312
|
+
ActsAsTenant.current_tenant = account
|
313
|
+
value = ActsAsTenant.with_tenant(accounts(:bar)) { "something" }
|
314
|
+
expect(value).to eq "something"
|
315
|
+
end
|
316
|
+
|
317
|
+
it "should raise an error when no block is provided" do
|
318
|
+
expect { ActsAsTenant.with_tenant(nil) }.to raise_error(ArgumentError, /block required/)
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
describe "::without_tenant" do
|
323
|
+
it "should set current_tenant to nil inside the block" do
|
324
|
+
ActsAsTenant.without_tenant do
|
325
|
+
expect(ActsAsTenant.current_tenant).to be_nil
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
it "should set current_tenant to nil even if default_tenant is set" do
|
330
|
+
old_default_tenant = ActsAsTenant.default_tenant
|
331
|
+
ActsAsTenant.default_tenant = Account.create!(name: "foo")
|
332
|
+
ActsAsTenant.without_tenant do
|
333
|
+
expect(ActsAsTenant.current_tenant).to be_nil
|
334
|
+
end
|
335
|
+
ensure
|
336
|
+
ActsAsTenant.default_tenant = old_default_tenant
|
337
|
+
end
|
338
|
+
|
339
|
+
it "should reset current_tenant to the previous tenant once exiting the block" do
|
340
|
+
ActsAsTenant.current_tenant = account
|
341
|
+
ActsAsTenant.without_tenant {}
|
342
|
+
expect(ActsAsTenant.current_tenant).to eq(account)
|
343
|
+
end
|
344
|
+
|
345
|
+
it "should return the value of the block" do
|
346
|
+
value = ActsAsTenant.without_tenant { "something" }
|
347
|
+
expect(value).to eq "something"
|
348
|
+
end
|
349
|
+
|
350
|
+
it "should raise an error when no block is provided" do
|
351
|
+
expect { ActsAsTenant.without_tenant }.to raise_error(ArgumentError, /block required/)
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
# Tenant required
|
356
|
+
context "tenant required" do
|
357
|
+
before do
|
358
|
+
account.projects.create!(name: "foobar")
|
359
|
+
allow(ActsAsTenant.configuration).to receive_messages(require_tenant: true)
|
360
|
+
end
|
361
|
+
|
362
|
+
it "should raise an error when no tenant is provided" do
|
363
|
+
expect { Project.all }.to raise_error(ActsAsTenant::Errors::NoTenantSet)
|
364
|
+
end
|
365
|
+
|
366
|
+
it "should not raise an error when no tenant is provided" do
|
367
|
+
expect { ActsAsTenant.without_tenant { Project.all } }.to_not raise_error
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
context "no tenant required" do
|
372
|
+
it "should not raise an error when no tenant is provided" do
|
373
|
+
expect { Project.all }.to_not raise_error
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
describe "ActsAsTenant.default_tenant=" do
|
378
|
+
after(:each) do
|
379
|
+
ActsAsTenant.default_tenant = nil
|
380
|
+
end
|
381
|
+
|
382
|
+
it "provides current_tenant" do
|
383
|
+
ActsAsTenant.default_tenant = account
|
384
|
+
expect(ActsAsTenant.current_tenant).to eq(account)
|
385
|
+
end
|
386
|
+
|
387
|
+
it "can be overridden by assignment" do
|
388
|
+
ActsAsTenant.default_tenant = account
|
389
|
+
ActsAsTenant.current_tenant = accounts(:bar)
|
390
|
+
expect(ActsAsTenant.current_tenant).to eq(accounts(:bar))
|
391
|
+
end
|
392
|
+
|
393
|
+
it "can be overridden by with_tenant" do
|
394
|
+
ActsAsTenant.default_tenant = account
|
395
|
+
ActsAsTenant.with_tenant accounts(:bar) do
|
396
|
+
expect(ActsAsTenant.current_tenant).to eq(accounts(:bar))
|
397
|
+
end
|
398
|
+
expect(ActsAsTenant.current_tenant).to eq(account)
|
399
|
+
end
|
400
|
+
|
401
|
+
it "doesn't override existing current_tenant" do
|
402
|
+
ActsAsTenant.current_tenant = accounts(:bar)
|
403
|
+
ActsAsTenant.default_tenant = account
|
404
|
+
expect(ActsAsTenant.current_tenant).to eq(accounts(:bar))
|
405
|
+
end
|
406
|
+
|
407
|
+
it "survives request resets" do
|
408
|
+
ActsAsTenant.default_tenant = account
|
409
|
+
RequestStore.clear!
|
410
|
+
expect(ActsAsTenant.current_tenant).to eq(account)
|
411
|
+
end
|
412
|
+
end
|
413
|
+
end
|