acts_as_tenant 0.4.3 → 0.5.1

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