ros-apartment 2.4.0 → 2.5.0

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