acts_as_tenant 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +40 -4
  3. data/lib/acts_as_tenant/errors.rb +0 -3
  4. data/lib/acts_as_tenant/model_extensions.rb +16 -6
  5. data/lib/acts_as_tenant/version.rb +1 -1
  6. data/lib/acts_as_tenant.rb +19 -1
  7. metadata +4 -170
  8. data/spec/acts_as_tenant/configuration_spec.rb +0 -17
  9. data/spec/acts_as_tenant/sidekiq_spec.rb +0 -58
  10. data/spec/controllers/filter_spec.rb +0 -28
  11. data/spec/controllers/subdomain_or_domain_spec.rb +0 -55
  12. data/spec/controllers/subdomain_spec.rb +0 -49
  13. data/spec/dummy/.ruby-version +0 -1
  14. data/spec/dummy/Rakefile +0 -6
  15. data/spec/dummy/app/assets/config/manifest.js +0 -2
  16. data/spec/dummy/app/assets/images/.keep +0 -0
  17. data/spec/dummy/app/assets/stylesheets/application.css +0 -15
  18. data/spec/dummy/app/channels/application_cable/channel.rb +0 -4
  19. data/spec/dummy/app/channels/application_cable/connection.rb +0 -4
  20. data/spec/dummy/app/controllers/application_controller.rb +0 -2
  21. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  22. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  23. data/spec/dummy/app/javascript/packs/application.js +0 -15
  24. data/spec/dummy/app/jobs/application_job.rb +0 -7
  25. data/spec/dummy/app/mailers/application_mailer.rb +0 -4
  26. data/spec/dummy/app/mailers/user_mailer.rb +0 -5
  27. data/spec/dummy/app/models/account.rb +0 -4
  28. data/spec/dummy/app/models/aliased_task.rb +0 -4
  29. data/spec/dummy/app/models/article.rb +0 -3
  30. data/spec/dummy/app/models/comment.rb +0 -5
  31. data/spec/dummy/app/models/concerns/.keep +0 -0
  32. data/spec/dummy/app/models/custom_counter_cache_task.rb +0 -4
  33. data/spec/dummy/app/models/custom_foreign_key_task.rb +0 -4
  34. data/spec/dummy/app/models/custom_primary_key_task.rb +0 -4
  35. data/spec/dummy/app/models/global_project.rb +0 -6
  36. data/spec/dummy/app/models/global_project_with_conditions.rb +0 -6
  37. data/spec/dummy/app/models/global_project_with_if.rb +0 -6
  38. data/spec/dummy/app/models/manager.rb +0 -4
  39. data/spec/dummy/app/models/polymorphic_tenant_comment.rb +0 -5
  40. data/spec/dummy/app/models/project.rb +0 -8
  41. data/spec/dummy/app/models/task.rb +0 -7
  42. data/spec/dummy/app/models/unique_task.rb +0 -5
  43. data/spec/dummy/app/models/unscoped_model.rb +0 -3
  44. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  45. data/spec/dummy/app/views/layouts/mailer.html.erb +0 -13
  46. data/spec/dummy/app/views/layouts/mailer.text.erb +0 -1
  47. data/spec/dummy/bin/rails +0 -4
  48. data/spec/dummy/bin/rake +0 -4
  49. data/spec/dummy/bin/setup +0 -33
  50. data/spec/dummy/config/application.rb +0 -19
  51. data/spec/dummy/config/boot.rb +0 -5
  52. data/spec/dummy/config/cable.yml +0 -10
  53. data/spec/dummy/config/database.yml +0 -19
  54. data/spec/dummy/config/environment.rb +0 -5
  55. data/spec/dummy/config/environments/development.rb +0 -62
  56. data/spec/dummy/config/environments/production.rb +0 -112
  57. data/spec/dummy/config/environments/test.rb +0 -49
  58. data/spec/dummy/config/initializers/application_controller_renderer.rb +0 -8
  59. data/spec/dummy/config/initializers/assets.rb +0 -12
  60. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  61. data/spec/dummy/config/initializers/content_security_policy.rb +0 -28
  62. data/spec/dummy/config/initializers/cookies_serializer.rb +0 -5
  63. data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  64. data/spec/dummy/config/initializers/inflections.rb +0 -16
  65. data/spec/dummy/config/initializers/mime_types.rb +0 -4
  66. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  67. data/spec/dummy/config/locales/en.yml +0 -40
  68. data/spec/dummy/config/puma.rb +0 -38
  69. data/spec/dummy/config/routes.rb +0 -4
  70. data/spec/dummy/config/spring.rb +0 -6
  71. data/spec/dummy/config/storage.yml +0 -34
  72. data/spec/dummy/config.ru +0 -5
  73. data/spec/dummy/db/schema.rb +0 -84
  74. data/spec/dummy/lib/assets/.keep +0 -0
  75. data/spec/dummy/log/.keep +0 -0
  76. data/spec/dummy/public/404.html +0 -67
  77. data/spec/dummy/public/422.html +0 -67
  78. data/spec/dummy/public/500.html +0 -66
  79. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  80. data/spec/dummy/public/apple-touch-icon.png +0 -0
  81. data/spec/dummy/public/favicon.ico +0 -0
  82. data/spec/dummy/tmp/development_secret.txt +0 -1
  83. data/spec/fixtures/accounts.yml +0 -10
  84. data/spec/fixtures/custom_primary_key_tasks.yml +0 -2
  85. data/spec/fixtures/global_projects.yml +0 -13
  86. data/spec/fixtures/projects.yml +0 -10
  87. data/spec/helpers/tenant_helper_spec.rb +0 -16
  88. data/spec/middlewares/test_tenant_middleware_spec.rb +0 -86
  89. data/spec/models/model_extensions_spec.rb +0 -413
  90. data/spec/spec_helper.rb +0 -19
@@ -1,413 +0,0 @@
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
data/spec/spec_helper.rb DELETED
@@ -1,19 +0,0 @@
1
- # Configure Rails Environment
2
- ENV["RAILS_ENV"] = "test"
3
-
4
- require_relative "../spec/dummy/config/environment"
5
- ActiveRecord::Migrator.migrations_paths = [File.expand_path("../spec/dummy/db/migrate", __dir__)]
6
- ActiveRecord::Migration.maintain_test_schema!
7
-
8
- require "rspec/rails"
9
-
10
- RSpec.configure do |config|
11
- config.after(:each) do
12
- ActsAsTenant.current_tenant = nil
13
- end
14
-
15
- config.fixture_path = "spec/fixtures"
16
- config.global_fixtures = :all
17
- config.use_transactional_fixtures = true
18
- config.infer_base_class_for_anonymous_controllers = true
19
- end