ros-apartment 2.3.0 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/.rubocop-linter.yml +22 -0
- data/.pryrc +5 -3
- data/.rubocop.yml +17 -0
- data/.rubocop_todo.yml +29 -0
- data/.story_branch.yml +4 -0
- data/.travis.yml +12 -11
- data/Appraisals +18 -18
- data/Gemfile +4 -1
- data/README.md +33 -5
- data/Rakefile +2 -0
- data/apartment.gemspec +6 -6
- data/gemfiles/rails_4_2.gemfile +12 -10
- data/gemfiles/rails_5_0.gemfile +11 -9
- data/gemfiles/rails_5_1.gemfile +11 -9
- data/gemfiles/rails_5_2.gemfile +10 -8
- data/gemfiles/rails_6_0.gemfile +11 -9
- data/gemfiles/rails_master.gemfile +11 -9
- data/lib/apartment/adapters/abstract_adapter.rb +34 -34
- data/lib/apartment/adapters/abstract_jdbc_adapter.rb +4 -3
- data/lib/apartment/adapters/jdbc_mysql_adapter.rb +3 -3
- data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +13 -11
- data/lib/apartment/adapters/mysql2_adapter.rb +10 -9
- data/lib/apartment/adapters/postgis_adapter.rb +3 -2
- data/lib/apartment/adapters/postgresql_adapter.rb +28 -25
- data/lib/apartment/adapters/sqlite3_adapter.rb +16 -8
- data/lib/apartment/console.rb +28 -3
- data/lib/apartment/custom_console.rb +26 -0
- data/lib/apartment/deprecation.rb +2 -1
- data/lib/apartment/elevators/domain.rb +4 -3
- data/lib/apartment/elevators/first_subdomain.rb +3 -2
- data/lib/apartment/elevators/generic.rb +4 -3
- data/lib/apartment/elevators/host.rb +6 -1
- data/lib/apartment/elevators/host_hash.rb +6 -2
- data/lib/apartment/elevators/subdomain.rb +9 -5
- data/lib/apartment/migrator.rb +4 -3
- data/lib/apartment/railtie.rb +13 -5
- data/lib/apartment/reloader.rb +2 -1
- data/lib/apartment/tasks/enhancements.rb +4 -6
- data/lib/apartment/tenant.rb +3 -4
- data/lib/apartment/version.rb +3 -1
- data/lib/apartment.rb +15 -9
- data/lib/generators/apartment/install/install_generator.rb +4 -3
- data/lib/generators/apartment/install/templates/apartment.rb +3 -2
- data/lib/tasks/apartment.rake +19 -18
- data/spec/adapters/jdbc_mysql_adapter_spec.rb +5 -4
- data/spec/adapters/jdbc_postgresql_adapter_spec.rb +10 -12
- data/spec/adapters/mysql2_adapter_spec.rb +15 -13
- data/spec/adapters/postgresql_adapter_spec.rb +22 -20
- data/spec/adapters/sqlite3_adapter_spec.rb +41 -23
- data/spec/apartment_spec.rb +4 -2
- data/spec/dummy/app/controllers/application_controller.rb +4 -3
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/application_record.rb +6 -0
- data/spec/dummy/app/models/company.rb +4 -2
- data/spec/dummy/app/models/user.rb +4 -2
- data/spec/dummy/config/application.rb +11 -9
- data/spec/dummy/config/boot.rb +4 -2
- data/spec/dummy/config/environment.rb +3 -1
- data/spec/dummy/config/environments/development.rb +2 -1
- data/spec/dummy/config/environments/production.rb +3 -1
- data/spec/dummy/config/environments/test.rb +3 -1
- data/spec/dummy/config/initializers/apartment.rb +4 -2
- data/spec/dummy/config/initializers/backtrace_silencers.rb +1 -0
- data/spec/dummy/config/initializers/inflections.rb +1 -0
- data/spec/dummy/config/initializers/mime_types.rb +1 -0
- data/spec/dummy/config/initializers/secret_token.rb +2 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -1
- data/spec/dummy/config/routes.rb +3 -1
- data/spec/dummy/config.ru +3 -1
- data/spec/dummy/db/seeds/import.rb +3 -1
- data/spec/dummy/script/rails +4 -2
- data/spec/dummy_engine/config/initializers/apartment.rb +4 -3
- data/spec/dummy_engine/lib/dummy_engine/engine.rb +2 -0
- data/spec/dummy_engine/lib/dummy_engine/version.rb +3 -1
- data/spec/dummy_engine/lib/dummy_engine.rb +3 -1
- data/spec/dummy_engine/test/dummy/config/application.rb +4 -2
- data/spec/dummy_engine/test/dummy/config/boot.rb +4 -2
- data/spec/dummy_engine/test/dummy/config/environment.rb +3 -1
- data/spec/dummy_engine/test/dummy/config/environments/development.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/environments/production.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/environments/test.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +1 -0
- data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +3 -1
- data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +1 -0
- data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +1 -0
- data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/routes.rb +2 -0
- data/spec/dummy_engine/test/dummy/config.ru +3 -1
- data/spec/examples/connection_adapter_examples.rb +15 -13
- data/spec/examples/generic_adapter_custom_configuration_example.rb +21 -23
- data/spec/examples/generic_adapter_examples.rb +40 -39
- data/spec/examples/schema_adapter_examples.rb +93 -88
- data/spec/integration/apartment_rake_integration_spec.rb +24 -24
- data/spec/integration/query_caching_spec.rb +10 -8
- data/spec/integration/use_within_an_engine_spec.rb +5 -5
- data/spec/schemas/v1.rb +2 -3
- data/spec/schemas/v2.rb +22 -24
- data/spec/schemas/v3.rb +26 -28
- data/spec/spec_helper.rb +8 -6
- data/spec/support/apartment_helpers.rb +9 -5
- data/spec/support/capybara_sessions.rb +5 -5
- data/spec/support/config.rb +5 -2
- data/spec/support/contexts.rb +10 -8
- data/spec/support/requirements.rb +19 -6
- data/spec/support/setup.rb +5 -5
- data/spec/tasks/apartment_rake_spec.rb +30 -35
- data/spec/tenant_spec.rb +48 -44
- data/spec/unit/config_spec.rb +26 -27
- data/spec/unit/elevators/domain_spec.rb +9 -8
- data/spec/unit/elevators/first_subdomain_spec.rb +14 -12
- data/spec/unit/elevators/generic_spec.rb +16 -15
- data/spec/unit/elevators/host_hash_spec.rb +9 -8
- data/spec/unit/elevators/host_spec.rb +26 -26
- data/spec/unit/elevators/subdomain_spec.rb +24 -23
- data/spec/unit/migrator_spec.rb +18 -17
- data/spec/unit/reloader_spec.rb +8 -8
- metadata +40 -22
@@ -1,10 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'rake'
|
3
5
|
require 'apartment/migrator'
|
4
6
|
require 'apartment/tenant'
|
5
7
|
|
6
|
-
describe
|
7
|
-
|
8
|
+
describe 'apartment rake tasks' do
|
8
9
|
before do
|
9
10
|
@rake = Rake::Application.new
|
10
11
|
Rake.application = @rake
|
@@ -20,110 +21,104 @@ describe "apartment rake tasks" do
|
|
20
21
|
|
21
22
|
after do
|
22
23
|
Rake.application = nil
|
23
|
-
ENV['VERSION'] = nil
|
24
|
+
ENV['VERSION'] = nil # linux users reported env variable carrying on between tests
|
24
25
|
end
|
25
26
|
|
26
27
|
after(:all) do
|
27
28
|
Apartment::Test.load_schema
|
28
29
|
end
|
29
30
|
|
30
|
-
let(:version){ '1234' }
|
31
|
+
let(:version) { '1234' }
|
31
32
|
|
32
33
|
context 'database migration' do
|
33
|
-
|
34
|
-
let(:
|
35
|
-
let(:tenant_count){ tenant_names.length }
|
34
|
+
let(:tenant_names) { 3.times.map { Apartment::Test.next_db } }
|
35
|
+
let(:tenant_count) { tenant_names.length }
|
36
36
|
|
37
37
|
before do
|
38
38
|
allow(Apartment).to receive(:tenant_names).and_return tenant_names
|
39
39
|
end
|
40
40
|
|
41
|
-
describe
|
41
|
+
describe 'apartment:migrate' do
|
42
42
|
before do
|
43
|
-
allow(ActiveRecord::Migrator).to receive(:migrate)
|
43
|
+
allow(ActiveRecord::Migrator).to receive(:migrate) # don't care about this
|
44
44
|
end
|
45
45
|
|
46
|
-
it
|
46
|
+
it 'should migrate public and all multi-tenant dbs' do
|
47
47
|
expect(Apartment::Migrator).to receive(:migrate).exactly(tenant_count).times
|
48
48
|
@rake['apartment:migrate'].invoke
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
describe
|
53
|
-
|
54
|
-
context "without a version" do
|
52
|
+
describe 'apartment:migrate:up' do
|
53
|
+
context 'without a version' do
|
55
54
|
before do
|
56
55
|
ENV['VERSION'] = nil
|
57
56
|
end
|
58
57
|
|
59
|
-
it
|
60
|
-
expect
|
58
|
+
it 'requires a version to migrate to' do
|
59
|
+
expect do
|
61
60
|
@rake['apartment:migrate:up'].invoke
|
62
|
-
|
61
|
+
end.to raise_error('VERSION is required')
|
63
62
|
end
|
64
63
|
end
|
65
64
|
|
66
|
-
context
|
67
|
-
|
65
|
+
context 'with version' do
|
68
66
|
before do
|
69
67
|
ENV['VERSION'] = version
|
70
68
|
end
|
71
69
|
|
72
|
-
it
|
70
|
+
it 'migrates up to a specific version' do
|
73
71
|
expect(Apartment::Migrator).to receive(:run).with(:up, anything, version.to_i).exactly(tenant_count).times
|
74
72
|
@rake['apartment:migrate:up'].invoke
|
75
73
|
end
|
76
74
|
end
|
77
75
|
end
|
78
76
|
|
79
|
-
describe
|
80
|
-
|
81
|
-
context "without a version" do
|
77
|
+
describe 'apartment:migrate:down' do
|
78
|
+
context 'without a version' do
|
82
79
|
before do
|
83
80
|
ENV['VERSION'] = nil
|
84
81
|
end
|
85
82
|
|
86
|
-
it
|
87
|
-
expect
|
83
|
+
it 'requires a version to migrate to' do
|
84
|
+
expect do
|
88
85
|
@rake['apartment:migrate:down'].invoke
|
89
|
-
|
86
|
+
end.to raise_error('VERSION is required')
|
90
87
|
end
|
91
88
|
end
|
92
89
|
|
93
|
-
context
|
94
|
-
|
90
|
+
context 'with version' do
|
95
91
|
before do
|
96
92
|
ENV['VERSION'] = version
|
97
93
|
end
|
98
94
|
|
99
|
-
it
|
95
|
+
it 'migrates up to a specific version' do
|
100
96
|
expect(Apartment::Migrator).to receive(:run).with(:down, anything, version.to_i).exactly(tenant_count).times
|
101
97
|
@rake['apartment:migrate:down'].invoke
|
102
98
|
end
|
103
99
|
end
|
104
100
|
end
|
105
101
|
|
106
|
-
describe
|
107
|
-
let(:step){ '3' }
|
102
|
+
describe 'apartment:rollback' do
|
103
|
+
let(:step) { '3' }
|
108
104
|
|
109
|
-
it
|
105
|
+
it 'should rollback dbs' do
|
110
106
|
expect(Apartment::Migrator).to receive(:rollback).exactly(tenant_count).times
|
111
107
|
@rake['apartment:rollback'].invoke
|
112
108
|
end
|
113
109
|
|
114
|
-
it
|
110
|
+
it 'should rollback dbs STEP amt' do
|
115
111
|
expect(Apartment::Migrator).to receive(:rollback).with(anything, step.to_i).exactly(tenant_count).times
|
116
112
|
ENV['STEP'] = step
|
117
113
|
@rake['apartment:rollback'].invoke
|
118
114
|
end
|
119
115
|
end
|
120
116
|
|
121
|
-
describe
|
122
|
-
it
|
117
|
+
describe 'apartment:drop' do
|
118
|
+
it 'should migrate public and all multi-tenant dbs' do
|
123
119
|
expect(Apartment::Tenant).to receive(:drop).exactly(tenant_count).times
|
124
120
|
@rake['apartment:drop'].invoke
|
125
121
|
end
|
126
122
|
end
|
127
|
-
|
128
123
|
end
|
129
124
|
end
|
data/spec/tenant_spec.rb
CHANGED
@@ -1,24 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Apartment::Tenant do
|
4
|
-
context
|
5
|
-
|
6
|
+
context 'using mysql', database: :mysql do
|
6
7
|
before { subject.reload!(config) }
|
7
8
|
|
8
|
-
describe
|
9
|
-
it
|
9
|
+
describe '#adapter' do
|
10
|
+
it 'should load mysql adapter' do
|
10
11
|
subject.adapter
|
11
12
|
expect(Apartment::Adapters::Mysql2Adapter).to be_a(Class)
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
15
|
-
# TODO this doesn't belong here, but there aren't integration tests currently for mysql
|
16
|
+
# TODO: this doesn't belong here, but there aren't integration tests currently for mysql
|
16
17
|
# where to put???
|
17
|
-
describe
|
18
|
+
describe 'exception recovery', type: :request do
|
18
19
|
before do
|
19
20
|
subject.create db1
|
20
21
|
end
|
21
|
-
after{ subject.drop db1 }
|
22
|
+
after { subject.drop db1 }
|
22
23
|
|
23
24
|
# it "should recover from incorrect database" do
|
24
25
|
# session = Capybara::Session.new(:rack_test, Capybara.app)
|
@@ -30,9 +31,9 @@ describe Apartment::Tenant do
|
|
30
31
|
# end
|
31
32
|
end
|
32
33
|
|
33
|
-
# TODO re-organize these tests
|
34
|
-
context
|
35
|
-
describe
|
34
|
+
# TODO: re-organize these tests
|
35
|
+
context 'with prefix and schemas' do
|
36
|
+
describe '#create' do
|
36
37
|
before do
|
37
38
|
Apartment.configure do |config|
|
38
39
|
config.prepend_environment = true
|
@@ -42,35 +43,41 @@ describe Apartment::Tenant do
|
|
42
43
|
subject.reload!(config)
|
43
44
|
end
|
44
45
|
|
45
|
-
after
|
46
|
+
after do
|
47
|
+
begin
|
48
|
+
subject.drop 'db_with_prefix'
|
49
|
+
rescue StandardError => _e
|
50
|
+
nil
|
51
|
+
end
|
52
|
+
end
|
46
53
|
|
47
|
-
it
|
48
|
-
subject.create
|
54
|
+
it 'should create a new database' do
|
55
|
+
subject.create 'db_with_prefix'
|
49
56
|
end
|
50
57
|
end
|
51
58
|
end
|
52
59
|
end
|
53
60
|
|
54
|
-
context
|
61
|
+
context 'using postgresql', database: :postgresql do
|
55
62
|
before do
|
56
63
|
Apartment.use_schemas = true
|
57
64
|
subject.reload!(config)
|
58
65
|
end
|
59
66
|
|
60
|
-
describe
|
61
|
-
it
|
67
|
+
describe '#adapter' do
|
68
|
+
it 'should load postgresql adapter' do
|
62
69
|
expect(subject.adapter).to be_a(Apartment::Adapters::PostgresqlSchemaAdapter)
|
63
70
|
end
|
64
71
|
|
65
|
-
it
|
72
|
+
it 'raises exception with invalid adapter specified' do
|
66
73
|
subject.reload!(config.merge(adapter: 'unknown'))
|
67
74
|
|
68
|
-
expect
|
75
|
+
expect do
|
69
76
|
Apartment::Tenant.adapter
|
70
|
-
|
77
|
+
end.to raise_error(RuntimeError)
|
71
78
|
end
|
72
79
|
|
73
|
-
context
|
80
|
+
context 'threadsafety' do
|
74
81
|
before { subject.create db1 }
|
75
82
|
after { subject.drop db1 }
|
76
83
|
|
@@ -83,8 +90,8 @@ describe Apartment::Tenant do
|
|
83
90
|
end
|
84
91
|
end
|
85
92
|
|
86
|
-
# TODO above spec are also with use_schemas=true
|
87
|
-
context
|
93
|
+
# TODO: above spec are also with use_schemas=true
|
94
|
+
context 'with schemas' do
|
88
95
|
before do
|
89
96
|
Apartment.configure do |config|
|
90
97
|
config.excluded_models = []
|
@@ -94,30 +101,28 @@ describe Apartment::Tenant do
|
|
94
101
|
subject.create db1
|
95
102
|
end
|
96
103
|
|
97
|
-
after{ subject.drop db1 }
|
104
|
+
after { subject.drop db1 }
|
98
105
|
|
99
|
-
describe
|
100
|
-
it
|
106
|
+
describe '#create' do
|
107
|
+
it 'should seed data' do
|
101
108
|
subject.switch! db1
|
102
109
|
expect(User.count).to be > 0
|
103
110
|
end
|
104
111
|
end
|
105
112
|
|
106
|
-
describe
|
113
|
+
describe '#switch!' do
|
114
|
+
let(:x) { rand(3) }
|
107
115
|
|
108
|
-
|
116
|
+
context 'creating models' do
|
117
|
+
before { subject.create db2 }
|
118
|
+
after { subject.drop db2 }
|
109
119
|
|
110
|
-
|
111
|
-
|
112
|
-
before{ subject.create db2 }
|
113
|
-
after{ subject.drop db2 }
|
114
|
-
|
115
|
-
it "should create a model instance in the current schema" do
|
120
|
+
it 'should create a model instance in the current schema' do
|
116
121
|
subject.switch! db2
|
117
|
-
db2_count = User.count + x.times{ User.create }
|
122
|
+
db2_count = User.count + x.times { User.create }
|
118
123
|
|
119
124
|
subject.switch! db1
|
120
|
-
db_count = User.count + x.times{ User.create }
|
125
|
+
db_count = User.count + x.times { User.create }
|
121
126
|
|
122
127
|
subject.switch! db2
|
123
128
|
expect(User.count).to eq(db2_count)
|
@@ -127,11 +132,10 @@ describe Apartment::Tenant do
|
|
127
132
|
end
|
128
133
|
end
|
129
134
|
|
130
|
-
context
|
131
|
-
|
135
|
+
context 'with excluded models' do
|
132
136
|
before do
|
133
137
|
Apartment.configure do |config|
|
134
|
-
config.excluded_models = [
|
138
|
+
config.excluded_models = ['Company']
|
135
139
|
end
|
136
140
|
subject.init
|
137
141
|
end
|
@@ -144,12 +148,12 @@ describe Apartment::Tenant do
|
|
144
148
|
end
|
145
149
|
end
|
146
150
|
|
147
|
-
it
|
151
|
+
it 'should create excluded models in public schema' do
|
148
152
|
subject.reset # ensure we're on public schema
|
149
|
-
count = Company.count + x.times{ Company.create }
|
153
|
+
count = Company.count + x.times { Company.create }
|
150
154
|
|
151
155
|
subject.switch! db1
|
152
|
-
x.times{ Company.create }
|
156
|
+
x.times { Company.create }
|
153
157
|
expect(Company.count).to eq(count + x)
|
154
158
|
subject.reset
|
155
159
|
expect(Company.count).to eq(count + x)
|
@@ -158,7 +162,7 @@ describe Apartment::Tenant do
|
|
158
162
|
end
|
159
163
|
end
|
160
164
|
|
161
|
-
context
|
165
|
+
context 'seed paths' do
|
162
166
|
before do
|
163
167
|
Apartment.configure do |config|
|
164
168
|
config.excluded_models = []
|
@@ -167,7 +171,7 @@ describe Apartment::Tenant do
|
|
167
171
|
end
|
168
172
|
end
|
169
173
|
|
170
|
-
after{ subject.drop db1 }
|
174
|
+
after { subject.drop db1 }
|
171
175
|
|
172
176
|
it 'should seed from default path' do
|
173
177
|
subject.create db1
|
@@ -178,7 +182,7 @@ describe Apartment::Tenant do
|
|
178
182
|
|
179
183
|
it 'should seed from custom path' do
|
180
184
|
Apartment.configure do |config|
|
181
|
-
config.seed_data_file =
|
185
|
+
config.seed_data_file = Rails.root.join('db', 'seeds', 'import.rb')
|
182
186
|
end
|
183
187
|
subject.create db1
|
184
188
|
subject.switch! db1
|
data/spec/unit/config_spec.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Apartment do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
let(:excluded_models){ ["Company"] }
|
8
|
-
let(:seed_data_file_path){ "#{Rails.root}/db/seeds/import.rb" }
|
6
|
+
describe '#config' do
|
7
|
+
let(:excluded_models) { ['Company'] }
|
8
|
+
let(:seed_data_file_path) { Rails.root.join('db', 'seeds', 'import.rb') }
|
9
9
|
|
10
10
|
def tenant_names_from_array(names)
|
11
11
|
names.each_with_object({}) do |tenant, hash|
|
@@ -13,21 +13,21 @@ describe Apartment do
|
|
13
13
|
end.with_indifferent_access
|
14
14
|
end
|
15
15
|
|
16
|
-
it
|
16
|
+
it 'should yield the Apartment object' do
|
17
17
|
Apartment.configure do |config|
|
18
18
|
config.excluded_models = []
|
19
19
|
expect(config).to eq(Apartment)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
it
|
23
|
+
it 'should set excluded models' do
|
24
24
|
Apartment.configure do |config|
|
25
25
|
config.excluded_models = excluded_models
|
26
26
|
end
|
27
27
|
expect(Apartment.excluded_models).to eq(excluded_models)
|
28
28
|
end
|
29
29
|
|
30
|
-
it
|
30
|
+
it 'should set use_schemas' do
|
31
31
|
Apartment.configure do |config|
|
32
32
|
config.excluded_models = []
|
33
33
|
config.use_schemas = false
|
@@ -35,14 +35,14 @@ describe Apartment do
|
|
35
35
|
expect(Apartment.use_schemas).to be false
|
36
36
|
end
|
37
37
|
|
38
|
-
it
|
38
|
+
it 'should set seed_data_file' do
|
39
39
|
Apartment.configure do |config|
|
40
40
|
config.seed_data_file = seed_data_file_path
|
41
41
|
end
|
42
42
|
expect(Apartment.seed_data_file).to eq(seed_data_file_path)
|
43
43
|
end
|
44
44
|
|
45
|
-
it
|
45
|
+
it 'should set seed_after_create' do
|
46
46
|
Apartment.configure do |config|
|
47
47
|
config.excluded_models = []
|
48
48
|
config.seed_after_create = true
|
@@ -50,7 +50,7 @@ describe Apartment do
|
|
50
50
|
expect(Apartment.seed_after_create).to be true
|
51
51
|
end
|
52
52
|
|
53
|
-
context
|
53
|
+
context 'databases' do
|
54
54
|
let(:users_conf_hash) { { port: 5444 } }
|
55
55
|
|
56
56
|
before do
|
@@ -59,54 +59,53 @@ describe Apartment do
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
-
context
|
63
|
-
let(:tenant_names) { [
|
62
|
+
context 'tenant_names as string array' do
|
63
|
+
let(:tenant_names) { %w[users companies] }
|
64
64
|
|
65
|
-
it
|
65
|
+
it 'should return object if it doesnt respond_to call' do
|
66
66
|
expect(Apartment.tenant_names).to eq(tenant_names_from_array(tenant_names).keys)
|
67
67
|
end
|
68
68
|
|
69
|
-
it
|
69
|
+
it 'should set tenants_with_config' do
|
70
70
|
expect(Apartment.tenants_with_config).to eq(tenant_names_from_array(tenant_names))
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
-
context
|
75
|
-
let(:tenant_names) {
|
74
|
+
context 'tenant_names as proc returning an array' do
|
75
|
+
let(:tenant_names) { -> { %w[users companies] } }
|
76
76
|
|
77
|
-
it
|
77
|
+
it 'should return object if it doesnt respond_to call' do
|
78
78
|
expect(Apartment.tenant_names).to eq(tenant_names_from_array(tenant_names.call).keys)
|
79
79
|
end
|
80
80
|
|
81
|
-
it
|
81
|
+
it 'should set tenants_with_config' do
|
82
82
|
expect(Apartment.tenants_with_config).to eq(tenant_names_from_array(tenant_names.call))
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
-
context
|
86
|
+
context 'tenant_names as Hash' do
|
87
87
|
let(:tenant_names) { { users: users_conf_hash }.with_indifferent_access }
|
88
88
|
|
89
|
-
it
|
89
|
+
it 'should return object if it doesnt respond_to call' do
|
90
90
|
expect(Apartment.tenant_names).to eq(tenant_names.keys)
|
91
91
|
end
|
92
92
|
|
93
|
-
it
|
93
|
+
it 'should set tenants_with_config' do
|
94
94
|
expect(Apartment.tenants_with_config).to eq(tenant_names)
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
|
-
context
|
99
|
-
let(:tenant_names) {
|
98
|
+
context 'tenant_names as proc returning a Hash' do
|
99
|
+
let(:tenant_names) { -> { { users: users_conf_hash }.with_indifferent_access } }
|
100
100
|
|
101
|
-
it
|
101
|
+
it 'should return object if it doesnt respond_to call' do
|
102
102
|
expect(Apartment.tenant_names).to eq(tenant_names.call.keys)
|
103
103
|
end
|
104
104
|
|
105
|
-
it
|
105
|
+
it 'should set tenants_with_config' do
|
106
106
|
expect(Apartment.tenants_with_config).to eq(tenant_names.call)
|
107
107
|
end
|
108
108
|
end
|
109
109
|
end
|
110
|
-
|
111
110
|
end
|
112
111
|
end
|
@@ -1,29 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'apartment/elevators/domain'
|
3
5
|
|
4
6
|
describe Apartment::Elevators::Domain do
|
7
|
+
subject(:elevator) { described_class.new(proc {}) }
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
describe "#parse_tenant_name" do
|
9
|
-
it "parses the host for a domain name" do
|
9
|
+
describe '#parse_tenant_name' do
|
10
|
+
it 'parses the host for a domain name' do
|
10
11
|
request = ActionDispatch::Request.new('HTTP_HOST' => 'example.com')
|
11
12
|
expect(elevator.parse_tenant_name(request)).to eq('example')
|
12
13
|
end
|
13
14
|
|
14
|
-
it
|
15
|
+
it 'ignores a www prefix and domain suffix' do
|
15
16
|
request = ActionDispatch::Request.new('HTTP_HOST' => 'www.example.bc.ca')
|
16
17
|
expect(elevator.parse_tenant_name(request)).to eq('example')
|
17
18
|
end
|
18
19
|
|
19
|
-
it
|
20
|
+
it 'returns nil if there is no host' do
|
20
21
|
request = ActionDispatch::Request.new('HTTP_HOST' => '')
|
21
22
|
expect(elevator.parse_tenant_name(request)).to be_nil
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
25
|
-
describe
|
26
|
-
it
|
26
|
+
describe '#call' do
|
27
|
+
it 'switches to the proper tenant' do
|
27
28
|
expect(Apartment::Tenant).to receive(:switch).with('example')
|
28
29
|
|
29
30
|
elevator.call('HTTP_HOST' => 'www.example.com')
|
@@ -1,24 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'apartment/elevators/first_subdomain'
|
3
5
|
|
4
6
|
describe Apartment::Elevators::FirstSubdomain do
|
5
|
-
describe
|
6
|
-
subject { described_class.new(
|
7
|
-
let(:request) { double(:request, :
|
7
|
+
describe 'subdomain' do
|
8
|
+
subject { described_class.new('test').parse_tenant_name(request) }
|
9
|
+
let(:request) { double(:request, host: "#{subdomain}.example.com") }
|
8
10
|
|
9
|
-
context
|
10
|
-
let(:subdomain) {
|
11
|
-
it { is_expected.to eq(
|
11
|
+
context 'one subdomain' do
|
12
|
+
let(:subdomain) { 'test' }
|
13
|
+
it { is_expected.to eq('test') }
|
12
14
|
end
|
13
15
|
|
14
|
-
context
|
15
|
-
let(:subdomain) {
|
16
|
-
it { is_expected.to eq(
|
16
|
+
context 'nested subdomains' do
|
17
|
+
let(:subdomain) { 'test1.test2' }
|
18
|
+
it { is_expected.to eq('test1') }
|
17
19
|
end
|
18
|
-
|
19
|
-
context
|
20
|
+
|
21
|
+
context 'no subdomain' do
|
20
22
|
let(:subdomain) { nil }
|
21
23
|
it { is_expected.to eq(nil) }
|
22
24
|
end
|
23
25
|
end
|
24
|
-
end
|
26
|
+
end
|
@@ -1,49 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'apartment/elevators/generic'
|
3
5
|
|
4
6
|
describe Apartment::Elevators::Generic do
|
5
|
-
|
6
7
|
class MyElevator < described_class
|
7
8
|
def parse_tenant_name(*)
|
8
9
|
'tenant2'
|
9
10
|
end
|
10
11
|
end
|
11
12
|
|
12
|
-
subject(:elevator){ described_class.new(
|
13
|
+
subject(:elevator) { described_class.new(proc {}) }
|
13
14
|
|
14
|
-
describe
|
15
|
-
it
|
16
|
-
elevator = described_class.new(
|
15
|
+
describe '#call' do
|
16
|
+
it 'calls the processor if given' do
|
17
|
+
elevator = described_class.new(proc {}, proc { 'tenant1' })
|
17
18
|
|
18
19
|
expect(Apartment::Tenant).to receive(:switch).with('tenant1')
|
19
20
|
|
20
21
|
elevator.call('HTTP_HOST' => 'foo.bar.com')
|
21
22
|
end
|
22
23
|
|
23
|
-
it
|
24
|
-
expect
|
24
|
+
it 'raises if parse_tenant_name not implemented' do
|
25
|
+
expect do
|
25
26
|
elevator.call('HTTP_HOST' => 'foo.bar.com')
|
26
|
-
|
27
|
+
end.to raise_error(RuntimeError)
|
27
28
|
end
|
28
29
|
|
29
|
-
it
|
30
|
-
elevator = MyElevator.new(
|
30
|
+
it 'switches to the parsed db_name' do
|
31
|
+
elevator = MyElevator.new(proc {})
|
31
32
|
|
32
33
|
expect(Apartment::Tenant).to receive(:switch).with('tenant2')
|
33
34
|
|
34
35
|
elevator.call('HTTP_HOST' => 'foo.bar.com')
|
35
36
|
end
|
36
37
|
|
37
|
-
it
|
38
|
-
elevator = MyElevator.new(
|
38
|
+
it 'calls the block implementation of `switch`' do
|
39
|
+
elevator = MyElevator.new(proc {}, proc { 'tenant2' })
|
39
40
|
|
40
41
|
expect(Apartment::Tenant).to receive(:switch).with('tenant2').and_yield
|
41
42
|
elevator.call('HTTP_HOST' => 'foo.bar.com')
|
42
43
|
end
|
43
44
|
|
44
|
-
it
|
45
|
-
app =
|
46
|
-
elevator = MyElevator.new(app,
|
45
|
+
it 'does not call `switch` if no database given' do
|
46
|
+
app = proc {}
|
47
|
+
elevator = MyElevator.new(app, proc {})
|
47
48
|
|
48
49
|
expect(Apartment::Tenant).not_to receive(:switch)
|
49
50
|
expect(app).to receive :call
|