acts_as_tenant 0.4.4 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/README.md +158 -27
- 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 +2 -6
- data/lib/acts_as_tenant/model_extensions.rb +63 -155
- data/lib/acts_as_tenant/sidekiq.rb +11 -7
- 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 +130 -14
- metadata +46 -48
- data/.gitignore +0 -7
- data/.travis.yml +0 -4
- data/CHANGELOG.md +0 -119
- 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 -143
- data/spec/acts_as_tenant/configuration_spec.rb +0 -28
- data/spec/acts_as_tenant/model_extensions_spec.rb +0 -476
- data/spec/acts_as_tenant/sidekiq_spec.rb +0 -63
- data/spec/acts_as_tenant/tenant_by_filter_spec.rb +0 -33
- 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
- data/spec/spec_helper.rb +0 -23
@@ -1,476 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'active_record_models'
|
3
|
-
|
4
|
-
describe ActsAsTenant do
|
5
|
-
after { ActsAsTenant.current_tenant = nil }
|
6
|
-
|
7
|
-
# Setting and getting
|
8
|
-
describe 'Setting the current tenant' do
|
9
|
-
before { ActsAsTenant.current_tenant = :foo }
|
10
|
-
it { ActsAsTenant.current_tenant == :foo }
|
11
|
-
end
|
12
|
-
|
13
|
-
describe 'is_scoped_as_tenant should return the correct value when true' do
|
14
|
-
it {expect(Project.respond_to?(:scoped_by_tenant?)).to eq(true)}
|
15
|
-
end
|
16
|
-
|
17
|
-
describe 'is_scoped_as_tenant should return the correct value when false' do
|
18
|
-
it {expect(UnscopedModel.respond_to?(:scoped_by_tenant?)).to eq(false)}
|
19
|
-
end
|
20
|
-
|
21
|
-
describe 'tenant_id should be immutable, if already set' do
|
22
|
-
before do
|
23
|
-
@account = Account.create!(:name => 'foo')
|
24
|
-
@project = @account.projects.create!(:name => 'bar')
|
25
|
-
end
|
26
|
-
|
27
|
-
it { expect {@project.account_id = @account.id + 1}.to raise_error(ActsAsTenant::Errors::TenantIsImmutable) }
|
28
|
-
end
|
29
|
-
|
30
|
-
describe 'setting tenant_id to the same value should not error' do
|
31
|
-
before do
|
32
|
-
@account = Account.create!(:name => 'foo')
|
33
|
-
@project = @account.projects.create!(:name => 'bar')
|
34
|
-
end
|
35
|
-
|
36
|
-
it { expect {@project.account_id = @account.id}.not_to raise_error }
|
37
|
-
end
|
38
|
-
|
39
|
-
describe 'setting tenant_id to a string with same to_i value should not error' do
|
40
|
-
before do
|
41
|
-
@account = Account.create!(:name => 'foo')
|
42
|
-
@project = @account.projects.create!(:name => 'bar')
|
43
|
-
end
|
44
|
-
|
45
|
-
it { expect {@project.account_id = @account.id.to_s}.not_to raise_error }
|
46
|
-
end
|
47
|
-
|
48
|
-
describe 'tenant_id should be mutable, if not already set' do
|
49
|
-
before do
|
50
|
-
@account = Account.create!(:name => 'foo')
|
51
|
-
@project = Project.create!(:name => 'bar')
|
52
|
-
end
|
53
|
-
|
54
|
-
it { expect(@project.account_id).to be_nil }
|
55
|
-
it { expect { @project.account = @account }.not_to raise_error }
|
56
|
-
end
|
57
|
-
|
58
|
-
describe 'tenant_id should auto populate after initialization' do
|
59
|
-
before do
|
60
|
-
@account = Account.create!(:name => 'foo')
|
61
|
-
ActsAsTenant.current_tenant = @account
|
62
|
-
end
|
63
|
-
it {expect(Project.new.account_id).to eq(@account.id)}
|
64
|
-
end
|
65
|
-
|
66
|
-
describe 'Handles custom foreign_key on tenant model' do
|
67
|
-
before do
|
68
|
-
@account = Account.create!(:name => 'foo')
|
69
|
-
ActsAsTenant.current_tenant = @account
|
70
|
-
@custom_foreign_key_task = CustomForeignKeyTask.create!(:name => 'foo')
|
71
|
-
end
|
72
|
-
|
73
|
-
it { expect(@custom_foreign_key_task.account).to eq(@account) }
|
74
|
-
end
|
75
|
-
|
76
|
-
describe 'Handles custom primary_key on tenant model' do
|
77
|
-
before do
|
78
|
-
@account = Account.create!(:name => 'foo')
|
79
|
-
CustomPrimaryKeyTask.create!(name: 'bar')
|
80
|
-
ActsAsTenant.current_tenant = @account
|
81
|
-
@custom_primary_key_task = CustomPrimaryKeyTask.create!
|
82
|
-
end
|
83
|
-
|
84
|
-
it { expect(@custom_primary_key_task.account).to eq(@account) }
|
85
|
-
it { expect(CustomPrimaryKeyTask.count).to eq(1) }
|
86
|
-
end
|
87
|
-
|
88
|
-
# Scoping models
|
89
|
-
describe 'Project.all should be scoped to the current tenant if set' do
|
90
|
-
before do
|
91
|
-
@account1 = Account.create!(:name => 'foo')
|
92
|
-
@account2 = Account.create!(:name => 'bar')
|
93
|
-
|
94
|
-
@project1 = @account1.projects.create!(:name => 'foobar')
|
95
|
-
@project2 = @account2.projects.create!(:name => 'baz')
|
96
|
-
|
97
|
-
ActsAsTenant.current_tenant= @account1
|
98
|
-
@projects = Project.all
|
99
|
-
end
|
100
|
-
|
101
|
-
it { expect(@projects.length).to eq(1) }
|
102
|
-
it { expect(@projects).to eq([@project1]) }
|
103
|
-
end
|
104
|
-
|
105
|
-
describe 'Project.unscoped.all should return the unscoped value' do
|
106
|
-
before do
|
107
|
-
@account1 = Account.create!(:name => 'foo')
|
108
|
-
@account2 = Account.create!(:name => 'bar')
|
109
|
-
|
110
|
-
@project1 = @account1.projects.create!(:name => 'foobar')
|
111
|
-
@project2 = @account2.projects.create!(:name => 'baz')
|
112
|
-
|
113
|
-
ActsAsTenant.current_tenant= @account1
|
114
|
-
@projects = Project.unscoped
|
115
|
-
end
|
116
|
-
|
117
|
-
it { expect(@projects.count).to eq(2) }
|
118
|
-
end
|
119
|
-
|
120
|
-
describe 'Querying the tenant from a scoped model without a tenant set' do
|
121
|
-
before do
|
122
|
-
@project = Project.create!(:name => 'bar')
|
123
|
-
end
|
124
|
-
|
125
|
-
it { @project.account }
|
126
|
-
end
|
127
|
-
|
128
|
-
describe 'Querying the tenant from a scoped model with a tenant set' do
|
129
|
-
before do
|
130
|
-
@account = Account.create!(:name => 'foo')
|
131
|
-
@project = @account.projects.create!(:name => 'foobar')
|
132
|
-
ActsAsTenant.current_tenant= @account1
|
133
|
-
end
|
134
|
-
|
135
|
-
it { @project.account }
|
136
|
-
end
|
137
|
-
|
138
|
-
describe 'A tenant model with global records' do
|
139
|
-
before do
|
140
|
-
@account = Account.create!(:name => 'foo')
|
141
|
-
@project1 = GlobalProject.create!(:name => 'foobar global')
|
142
|
-
@project2 = GlobalProject.create!(:name => 'unaccessible project', :account => Account.create!)
|
143
|
-
ActsAsTenant.current_tenant = @account
|
144
|
-
@project3 = GlobalProject.create!(:name => 'foobar')
|
145
|
-
end
|
146
|
-
|
147
|
-
it 'should return two projects' do
|
148
|
-
expect(GlobalProject.all.count).to eq(2)
|
149
|
-
end
|
150
|
-
|
151
|
-
it 'should validate the project name against the global records too' do
|
152
|
-
expect(GlobalProject.new(:name => 'foobar').valid?).to be(false)
|
153
|
-
expect(GlobalProject.new(:name => 'foobar new').valid?).to be(true)
|
154
|
-
expect(GlobalProject.new(:name => 'foobar global').valid?).to be(false)
|
155
|
-
expect(@project1.valid?).to be(true)
|
156
|
-
end
|
157
|
-
|
158
|
-
it 'should add the model to ActsAsTenant.models_with_global_records' do
|
159
|
-
expect(ActsAsTenant.models_with_global_records.include?(GlobalProject)).to be(true)
|
160
|
-
expect(ActsAsTenant.models_with_global_records.include?(Project)).to be(false)
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
# Associations
|
165
|
-
describe 'Associations should be correctly scoped by current tenant' do
|
166
|
-
before do
|
167
|
-
@account = Account.create!(:name => 'foo')
|
168
|
-
@project = Project.create!(:name => 'foobar', :account => @account )
|
169
|
-
# the next line should normally be (nearly) impossible: a task assigned to a tenant project,
|
170
|
-
# but the task has no tenant assigned
|
171
|
-
@task1 = Task.create!(:name => 'no_tenant', :project => @project)
|
172
|
-
|
173
|
-
ActsAsTenant.current_tenant = @account
|
174
|
-
@task2 = @project.tasks.create!(:name => 'baz')
|
175
|
-
|
176
|
-
@project.reload
|
177
|
-
end
|
178
|
-
|
179
|
-
it 'should correctly set the tenant on the task created with current_tenant set' do
|
180
|
-
expect(@task2.account).to eq(@account)
|
181
|
-
end
|
182
|
-
|
183
|
-
it 'should filter out the non-tenant task from the project' do
|
184
|
-
expect(@project.tasks.length).to eq(1)
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
describe 'Associations can only be made with in-scope objects' do
|
189
|
-
before do
|
190
|
-
@account = Account.create!(:name => 'foo')
|
191
|
-
@project1 = Project.create!(:name => 'inaccessible_project', :account => Account.create!)
|
192
|
-
ActsAsTenant.current_tenant = @account
|
193
|
-
|
194
|
-
@project2 = Project.create!(:name => 'accessible_project')
|
195
|
-
@task = @project2.tasks.create!(:name => 'bar')
|
196
|
-
end
|
197
|
-
|
198
|
-
it { expect(@task.update_attributes(:project_id => @project1.id)).to eq(false) }
|
199
|
-
end
|
200
|
-
|
201
|
-
describe "Create and save an AaT-enabled child without it having a parent" do
|
202
|
-
before do
|
203
|
-
@account = Account.create!(:name => 'baz')
|
204
|
-
ActsAsTenant.current_tenant = @account
|
205
|
-
end
|
206
|
-
it { expect(Task.create(:name => 'bar').valid?).to eq(true) }
|
207
|
-
end
|
208
|
-
|
209
|
-
describe "It should be possible to use aliased associations" do
|
210
|
-
it { 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
|
-
context 'tenanted objects have a polymorphic association' do
|
215
|
-
before do
|
216
|
-
@account = Account.create!(name: 'foo')
|
217
|
-
ActsAsTenant.current_tenant = @account
|
218
|
-
@project = Project.create!(name: 'project', account: @account)
|
219
|
-
@comment = Comment.new commentable: @project, account: @account
|
220
|
-
end
|
221
|
-
|
222
|
-
it { expect(@comment.save!).to eq(true) }
|
223
|
-
end
|
224
|
-
|
225
|
-
context 'tenant is polymorphic' do
|
226
|
-
before do
|
227
|
-
@account = Account.create!(name: 'foo')
|
228
|
-
@project = Project.create!(name: 'polymorphic project')
|
229
|
-
ActsAsTenant.current_tenant = @project
|
230
|
-
@comment = PolymorphicTenantComment.new(account: @account)
|
231
|
-
end
|
232
|
-
|
233
|
-
it 'populates commentable_type with the current tenant' do
|
234
|
-
expect(@comment.polymorphic_tenant_commentable_id).to eql(@project.id)
|
235
|
-
expect(@comment.polymorphic_tenant_commentable_type).to eql(@project.class.to_s)
|
236
|
-
end
|
237
|
-
|
238
|
-
context 'with another type of tenant, same id' do
|
239
|
-
before do
|
240
|
-
@comment.save!
|
241
|
-
@article = Article.create!(id: @project.id, title: 'article title')
|
242
|
-
@comment_on_article = @article.polymorphic_tenant_comments.create!
|
243
|
-
end
|
244
|
-
|
245
|
-
it 'correctly scopes to the current tenant type' do
|
246
|
-
expect(@comment_on_article).to be_persisted
|
247
|
-
expect(@comment).to be_persisted
|
248
|
-
expect(PolymorphicTenantComment.count).to eql(1)
|
249
|
-
expect(PolymorphicTenantComment.all.first.attributes).to eql(@comment.attributes)
|
250
|
-
end
|
251
|
-
end
|
252
|
-
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
# Additional default_scopes
|
257
|
-
describe 'When dealing with a user defined default_scope' do
|
258
|
-
before do
|
259
|
-
@account = Account.create!(:name => 'foo')
|
260
|
-
@project1 = Project.create!(:name => 'inaccessible')
|
261
|
-
@task1 = Task.create!(:name => 'no_tenant', :project => @project1)
|
262
|
-
|
263
|
-
ActsAsTenant.current_tenant = @account
|
264
|
-
@project2 = Project.create!(:name => 'accessible')
|
265
|
-
@task2 = @project2.tasks.create!(:name => 'bar')
|
266
|
-
@task3 = @project2.tasks.create!(:name => 'baz')
|
267
|
-
@task4 = @project2.tasks.create!(:name => 'foo')
|
268
|
-
@task5 = @project2.tasks.create!(:name => 'foobar', :completed => true )
|
269
|
-
|
270
|
-
@tasks= Task.all
|
271
|
-
end
|
272
|
-
|
273
|
-
it 'should apply both the tenant scope and the user defined default_scope, including :order' do
|
274
|
-
expect(@tasks.length).to eq(3)
|
275
|
-
expect(@tasks).to eq([@task2, @task3, @task4])
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
# Validates_uniqueness
|
280
|
-
describe 'When using validates_uniqueness_to_tenant in a aat model' do
|
281
|
-
before do
|
282
|
-
account = Account.create!(:name => 'foo')
|
283
|
-
ActsAsTenant.current_tenant = account
|
284
|
-
Project.create!(:name => 'existing_name')
|
285
|
-
end
|
286
|
-
|
287
|
-
it 'should not be possible to create a duplicate within the same tenant' do
|
288
|
-
expect(Project.create(:name => 'existing_name').valid?).to eq(false)
|
289
|
-
end
|
290
|
-
|
291
|
-
it 'should be possible to create a duplicate outside the tenant scope' do
|
292
|
-
account = Account.create!(:name => 'baz')
|
293
|
-
ActsAsTenant.current_tenant = account
|
294
|
-
expect(Project.create(:name => 'bar').valid?).to eq(true)
|
295
|
-
end
|
296
|
-
end
|
297
|
-
|
298
|
-
describe 'Handles user defined scopes' do
|
299
|
-
before do
|
300
|
-
UniqueTask.create!(:name => 'foo', :user_defined_scope => 'unique_scope')
|
301
|
-
end
|
302
|
-
|
303
|
-
it { expect(UniqueTask.create(:name => 'foo', :user_defined_scope => 'another_scope')).to be_valid }
|
304
|
-
it { expect(UniqueTask.create(:name => 'foo', :user_defined_scope => 'unique_scope')).not_to be_valid }
|
305
|
-
end
|
306
|
-
|
307
|
-
describe 'When using validates_uniqueness_of in a NON-aat model' do
|
308
|
-
before do
|
309
|
-
UnscopedModel.create!(:name => 'foo')
|
310
|
-
end
|
311
|
-
it 'should not be possible to create duplicates' do
|
312
|
-
expect(UnscopedModel.create(:name => 'foo').valid?).to eq(false)
|
313
|
-
end
|
314
|
-
end
|
315
|
-
|
316
|
-
# ::with_tenant
|
317
|
-
describe "::with_tenant" do
|
318
|
-
it "should set current_tenant to the specified tenant inside the block" do
|
319
|
-
@account = Account.create!(:name => 'baz')
|
320
|
-
|
321
|
-
ActsAsTenant.with_tenant(@account) do
|
322
|
-
expect(ActsAsTenant.current_tenant).to eq(@account)
|
323
|
-
end
|
324
|
-
end
|
325
|
-
|
326
|
-
it "should reset current_tenant to the previous tenant once exiting the block" do
|
327
|
-
@account1 = Account.create!(:name => 'foo')
|
328
|
-
@account2 = Account.create!(:name => 'bar')
|
329
|
-
|
330
|
-
ActsAsTenant.current_tenant = @account1
|
331
|
-
ActsAsTenant.with_tenant @account2 do
|
332
|
-
|
333
|
-
end
|
334
|
-
|
335
|
-
expect(ActsAsTenant.current_tenant).to eq(@account1)
|
336
|
-
end
|
337
|
-
|
338
|
-
it "should return the value of the block" do
|
339
|
-
@account1 = Account.create!(:name => 'foo')
|
340
|
-
@account2 = Account.create!(:name => 'bar')
|
341
|
-
|
342
|
-
ActsAsTenant.current_tenant = @account1
|
343
|
-
value = ActsAsTenant.with_tenant @account2 do
|
344
|
-
"something"
|
345
|
-
end
|
346
|
-
|
347
|
-
expect(value).to eq "something"
|
348
|
-
end
|
349
|
-
|
350
|
-
it "should raise an error when no block is provided" do
|
351
|
-
expect { ActsAsTenant.with_tenant(nil) }.to raise_error(ArgumentError, /block required/)
|
352
|
-
end
|
353
|
-
end
|
354
|
-
|
355
|
-
describe "::without_tenant" do
|
356
|
-
it "should set current_tenant to nil inside the block" do
|
357
|
-
ActsAsTenant.without_tenant do
|
358
|
-
expect(ActsAsTenant.current_tenant).to be_nil
|
359
|
-
end
|
360
|
-
end
|
361
|
-
|
362
|
-
it "should set current_tenant to nil even if default_tenant is set" do
|
363
|
-
begin
|
364
|
-
old_default_tenant = ActsAsTenant.default_tenant
|
365
|
-
ActsAsTenant.default_tenant = Account.create!(name: 'foo')
|
366
|
-
ActsAsTenant.without_tenant do
|
367
|
-
expect(ActsAsTenant.current_tenant).to be_nil
|
368
|
-
end
|
369
|
-
ensure
|
370
|
-
ActsAsTenant.default_tenant = old_default_tenant
|
371
|
-
end
|
372
|
-
end
|
373
|
-
|
374
|
-
it "should reset current_tenant to the previous tenant once exiting the block" do
|
375
|
-
@account1 = Account.create!(:name => 'foo')
|
376
|
-
|
377
|
-
ActsAsTenant.current_tenant = @account1
|
378
|
-
ActsAsTenant.without_tenant do
|
379
|
-
end
|
380
|
-
|
381
|
-
expect(ActsAsTenant.current_tenant).to eq(@account1)
|
382
|
-
end
|
383
|
-
|
384
|
-
it "should return the value of the block" do
|
385
|
-
value = ActsAsTenant.without_tenant do
|
386
|
-
"something"
|
387
|
-
end
|
388
|
-
|
389
|
-
expect(value).to eq "something"
|
390
|
-
end
|
391
|
-
|
392
|
-
it "should raise an error when no block is provided" do
|
393
|
-
expect { ActsAsTenant.without_tenant }.to raise_error(ArgumentError, /block required/)
|
394
|
-
end
|
395
|
-
end
|
396
|
-
|
397
|
-
# Tenant required
|
398
|
-
context "tenant required" do
|
399
|
-
before do
|
400
|
-
@account1 = Account.create!(:name => 'foo')
|
401
|
-
@project1 = @account1.projects.create!(:name => 'foobar')
|
402
|
-
allow(ActsAsTenant.configuration).to receive_messages(require_tenant: true)
|
403
|
-
end
|
404
|
-
|
405
|
-
describe "raises exception if no tenant specified" do
|
406
|
-
it "should raise an error when no tenant is provided" do
|
407
|
-
expect { Project.all }.to raise_error(ActsAsTenant::Errors::NoTenantSet)
|
408
|
-
end
|
409
|
-
end
|
410
|
-
|
411
|
-
describe "does not raise exception when run in unscoped mode" do
|
412
|
-
it "should not raise an error when no tenant is provided" do
|
413
|
-
expect do
|
414
|
-
ActsAsTenant.without_tenant { Project.all }
|
415
|
-
end.to_not raise_error
|
416
|
-
end
|
417
|
-
end
|
418
|
-
end
|
419
|
-
|
420
|
-
context "no tenant required" do
|
421
|
-
describe "does not raise exception if no tenant specified" do
|
422
|
-
before do
|
423
|
-
@account1 = Account.create!(:name => 'foo')
|
424
|
-
@project1 = @account1.projects.create!(:name => 'foobar')
|
425
|
-
end
|
426
|
-
|
427
|
-
it "should not raise an error when no tenant is provided" do
|
428
|
-
expect { Project.all }.to_not raise_error
|
429
|
-
end
|
430
|
-
end
|
431
|
-
end
|
432
|
-
|
433
|
-
describe "ActsAsTenant.default_tenant=" do
|
434
|
-
before(:each) do
|
435
|
-
@account = Account.create!
|
436
|
-
end
|
437
|
-
|
438
|
-
after(:each) do
|
439
|
-
ActsAsTenant.default_tenant = nil
|
440
|
-
end
|
441
|
-
|
442
|
-
it "provides current_tenant" do
|
443
|
-
ActsAsTenant.default_tenant = @account
|
444
|
-
expect(ActsAsTenant.current_tenant).to eq(@account)
|
445
|
-
end
|
446
|
-
|
447
|
-
it "can be overridden by assignment" do
|
448
|
-
ActsAsTenant.default_tenant = @account
|
449
|
-
@account2 = Account.create!
|
450
|
-
ActsAsTenant.current_tenant = @account2
|
451
|
-
expect(ActsAsTenant.current_tenant).not_to eq(@account)
|
452
|
-
end
|
453
|
-
|
454
|
-
it "can be overridden by with_tenant" do
|
455
|
-
ActsAsTenant.default_tenant = @account
|
456
|
-
@account2 = Account.create!
|
457
|
-
ActsAsTenant.with_tenant @account2 do
|
458
|
-
expect(ActsAsTenant.current_tenant).to eq(@account2)
|
459
|
-
end
|
460
|
-
expect(ActsAsTenant.current_tenant).to eq(@account)
|
461
|
-
end
|
462
|
-
|
463
|
-
it "doesn't override existing current_tenant" do
|
464
|
-
@account2 = Account.create!
|
465
|
-
ActsAsTenant.current_tenant = @account2
|
466
|
-
ActsAsTenant.default_tenant = @account
|
467
|
-
expect(ActsAsTenant.current_tenant).to eq(@account2)
|
468
|
-
end
|
469
|
-
|
470
|
-
it "survives request resets" do
|
471
|
-
ActsAsTenant.default_tenant = @account
|
472
|
-
RequestStore.clear!
|
473
|
-
expect(ActsAsTenant.current_tenant).to eq(@account)
|
474
|
-
end
|
475
|
-
end
|
476
|
-
end
|
@@ -1,63 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'sidekiq'
|
3
|
-
require 'acts_as_tenant/sidekiq'
|
4
|
-
|
5
|
-
describe ActsAsTenant::Sidekiq do
|
6
|
-
after { ActsAsTenant.current_tenant = nil }
|
7
|
-
let(:account) { Account.new(id: 1234) }
|
8
|
-
let(:message) { { 'acts_as_tenant' => { 'class' => 'Account', 'id' => 1234 } } }
|
9
|
-
|
10
|
-
describe ActsAsTenant::Sidekiq::Client do
|
11
|
-
subject { ActsAsTenant::Sidekiq::Client.new }
|
12
|
-
|
13
|
-
it 'saves tenant if present' do
|
14
|
-
ActsAsTenant.current_tenant = account
|
15
|
-
|
16
|
-
msg = {}
|
17
|
-
subject.call(nil, msg, nil, nil) { }
|
18
|
-
expect(msg).to eq message
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'does not set tenant if not present' do
|
22
|
-
expect(ActsAsTenant.current_tenant).to be_nil
|
23
|
-
|
24
|
-
msg = {}
|
25
|
-
subject.call(nil, msg, nil, nil) { }
|
26
|
-
expect(msg).not_to eq message
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
describe ActsAsTenant::Sidekiq::Server do
|
31
|
-
subject { ActsAsTenant::Sidekiq::Server.new }
|
32
|
-
|
33
|
-
it 'restores tenant if tenant saved' do
|
34
|
-
expect(Account).to receive(:find).with(1234).once { account }
|
35
|
-
|
36
|
-
msg = message
|
37
|
-
subject.call(nil, msg, nil) do
|
38
|
-
expect(ActsAsTenant.current_tenant).to be_a_kind_of Account
|
39
|
-
end
|
40
|
-
expect(ActsAsTenant.current_tenant).to be_nil
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'runs without tenant if no tenant saved' do
|
44
|
-
expect(Account).not_to receive(:find)
|
45
|
-
|
46
|
-
msg = {}
|
47
|
-
subject.call(nil, msg, nil) do
|
48
|
-
expect(ActsAsTenant.current_tenant).to be_nil
|
49
|
-
end
|
50
|
-
expect(ActsAsTenant.current_tenant).to be_nil
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
describe 'Sidekiq configuration' do
|
55
|
-
describe 'client configuration' do
|
56
|
-
it 'includes ActsAsTenant client' do
|
57
|
-
expect(Sidekiq.client_middleware.exists?(ActsAsTenant::Sidekiq::Client)).to eq(true)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
# unable to test server configuration
|
62
|
-
end
|
63
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
#Setup test specific ApplicationController
|
4
|
-
class Account
|
5
|
-
attr_accessor :name
|
6
|
-
end
|
7
|
-
|
8
|
-
class ApplicationController2 < ActionController::Base
|
9
|
-
include Rails.application.routes.url_helpers
|
10
|
-
set_current_tenant_through_filter
|
11
|
-
before_action :your_method_that_finds_the_current_tenant
|
12
|
-
|
13
|
-
def your_method_that_finds_the_current_tenant
|
14
|
-
current_account = Account.new
|
15
|
-
current_account.name = 'account1'
|
16
|
-
set_current_tenant(current_account)
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
# Start testing
|
22
|
-
describe ApplicationController2, :type => :controller do
|
23
|
-
controller do
|
24
|
-
def index
|
25
|
-
render :plain => "custom called"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'Finds the correct tenant using the filter command' do
|
30
|
-
get :index
|
31
|
-
expect(ActsAsTenant.current_tenant.name).to eq 'account1'
|
32
|
-
end
|
33
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
#Setup test specific ApplicationController
|
4
|
-
class Account; end # this is so the spec will work in isolation
|
5
|
-
|
6
|
-
class ApplicationController < ActionController::Base
|
7
|
-
include Rails.application.routes.url_helpers
|
8
|
-
set_current_tenant_by_subdomain_or_domain
|
9
|
-
end
|
10
|
-
|
11
|
-
# Start testing
|
12
|
-
describe ApplicationController, :type => :controller do
|
13
|
-
controller do
|
14
|
-
def index
|
15
|
-
render :text => "custom called"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'Finds the correct tenant with a example1.com' do
|
20
|
-
@request.host = "example1.com"
|
21
|
-
expect(Account).to receive(:where).with({domain: 'example1.com'}) {['account1']}
|
22
|
-
get :index
|
23
|
-
expect(ActsAsTenant.current_tenant).to eq 'account1'
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'Finds the correct tenant with a subdomain.example.com' do
|
27
|
-
@request.host = "subdomain.example.com"
|
28
|
-
expect(Account).to receive(:where).with({subdomain: 'subdomain'}) {['account1']}
|
29
|
-
get :index
|
30
|
-
expect(ActsAsTenant.current_tenant).to eq "account1"
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'Finds the correct tenant with a www.subdomain.example.com' do
|
34
|
-
@request.host = "subdomain.example.com"
|
35
|
-
expect(Account).to receive(:where).with({subdomain: 'subdomain'}) {['account1']}
|
36
|
-
get :index
|
37
|
-
expect(ActsAsTenant.current_tenant).to eq "account1"
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'Ignores case when finding tenant by subdomain' do
|
41
|
-
@request.host = "SubDomain.example.com"
|
42
|
-
expect(Account).to receive(:where).with({subdomain: 'subdomain'}) {['account1']}
|
43
|
-
get :index
|
44
|
-
expect(ActsAsTenant.current_tenant).to eq "account1"
|
45
|
-
end
|
46
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
#Setup test specific ApplicationController
|
4
|
-
class Account; end # this is so the spec will work in isolation
|
5
|
-
|
6
|
-
class ApplicationController < ActionController::Base
|
7
|
-
include Rails.application.routes.url_helpers
|
8
|
-
set_current_tenant_by_subdomain
|
9
|
-
end
|
10
|
-
|
11
|
-
# Start testing
|
12
|
-
describe ApplicationController, :type => :controller do
|
13
|
-
controller do
|
14
|
-
def index
|
15
|
-
render :plain => "custom called"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'Finds the correct tenant with a subdomain.example.com' do
|
20
|
-
@request.host = "account1.example.com"
|
21
|
-
expect(Account).to receive(:where).with({subdomain: 'account1'}) {['account1']}
|
22
|
-
get :index
|
23
|
-
expect(ActsAsTenant.current_tenant).to eq 'account1'
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'Finds the correct tenant with a www.subdomain.example.com' do
|
27
|
-
@request.host = "www.account1.example.com"
|
28
|
-
expect(Account).to receive(:where).with({subdomain: 'account1'}) {['account1']}
|
29
|
-
get :index
|
30
|
-
expect(ActsAsTenant.current_tenant).to eq 'account1'
|
31
|
-
end
|
32
|
-
end
|
data/spec/database.yml
DELETED
data/spec/spec_helper.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
-
|
4
|
-
require "active_record_helper"
|
5
|
-
|
6
|
-
require 'rspec/rails'
|
7
|
-
require 'acts_as_tenant'
|
8
|
-
|
9
|
-
RSpec.configure do |config|
|
10
|
-
config.after(:each) do
|
11
|
-
ActsAsTenant.current_tenant = nil
|
12
|
-
end
|
13
|
-
|
14
|
-
config.infer_base_class_for_anonymous_controllers = true
|
15
|
-
end
|
16
|
-
|
17
|
-
# Setup a test app
|
18
|
-
module Rollcall
|
19
|
-
class Application < Rails::Application; end
|
20
|
-
end
|
21
|
-
|
22
|
-
Rollcall::Application.config.secret_token = '1234567890123456789012345678901234567890'
|
23
|
-
Rollcall::Application.config.secret_key_base = '1234567890123456789012345678901234567890'
|