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