ros-apartment 2.3.0 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.pryrc +5 -3
- data/.rubocop.yml +22 -0
- data/.rubocop_todo.yml +29 -0
- data/.story_branch.yml +5 -0
- data/.travis.yml +20 -36
- data/Appraisals +16 -29
- data/Gemfile +5 -2
- data/Guardfile +3 -1
- data/HISTORY.md +57 -0
- data/README.md +64 -21
- data/Rakefile +36 -22
- data/TODO.md +0 -1
- data/apartment.gemspec +17 -10
- data/gemfiles/rails_4_2.gemfile +12 -10
- data/gemfiles/rails_5_0.gemfile +2 -1
- data/gemfiles/rails_5_1.gemfile +2 -1
- data/gemfiles/rails_5_2.gemfile +2 -1
- data/gemfiles/rails_6_0.gemfile +6 -5
- data/gemfiles/rails_master.gemfile +2 -1
- data/lib/apartment.rb +38 -14
- data/lib/apartment/active_record/connection_handling.rb +17 -0
- data/lib/apartment/active_record/internal_metadata.rb +11 -0
- data/lib/apartment/active_record/log_subscriber.rb +41 -0
- data/lib/apartment/active_record/schema_migration.rb +13 -0
- data/lib/apartment/adapters/abstract_adapter.rb +49 -45
- 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 +20 -13
- data/lib/apartment/adapters/mysql2_adapter.rb +12 -9
- data/lib/apartment/adapters/postgis_adapter.rb +3 -2
- data/lib/apartment/adapters/postgresql_adapter.rb +59 -27
- data/lib/apartment/adapters/sqlite3_adapter.rb +18 -8
- data/lib/apartment/console.rb +35 -3
- data/lib/apartment/custom_console.rb +42 -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/model.rb +27 -0
- data/lib/apartment/railtie.rb +27 -15
- data/lib/apartment/reloader.rb +2 -1
- data/lib/apartment/tasks/enhancements.rb +4 -6
- data/lib/apartment/tasks/task_helper.rb +35 -0
- data/lib/apartment/tenant.rb +19 -9
- data/lib/apartment/version.rb +3 -1
- data/lib/generators/apartment/install/install_generator.rb +4 -3
- data/lib/generators/apartment/install/templates/apartment.rb +8 -2
- data/lib/tasks/apartment.rake +22 -44
- metadata +51 -230
- data/spec/adapters/jdbc_mysql_adapter_spec.rb +0 -19
- data/spec/adapters/jdbc_postgresql_adapter_spec.rb +0 -41
- data/spec/adapters/mysql2_adapter_spec.rb +0 -59
- data/spec/adapters/postgresql_adapter_spec.rb +0 -61
- data/spec/adapters/sqlite3_adapter_spec.rb +0 -83
- data/spec/apartment_spec.rb +0 -11
- data/spec/config/database.yml.sample +0 -49
- data/spec/dummy/Rakefile +0 -7
- data/spec/dummy/app/controllers/application_controller.rb +0 -6
- data/spec/dummy/app/helpers/application_helper.rb +0 -2
- data/spec/dummy/app/models/company.rb +0 -3
- data/spec/dummy/app/models/user.rb +0 -3
- data/spec/dummy/app/views/application/index.html.erb +0 -1
- data/spec/dummy/app/views/layouts/application.html.erb +0 -14
- data/spec/dummy/config.ru +0 -4
- data/spec/dummy/config/application.rb +0 -49
- data/spec/dummy/config/boot.rb +0 -11
- data/spec/dummy/config/database.yml.sample +0 -44
- data/spec/dummy/config/environment.rb +0 -5
- data/spec/dummy/config/environments/development.rb +0 -28
- data/spec/dummy/config/environments/production.rb +0 -51
- data/spec/dummy/config/environments/test.rb +0 -34
- data/spec/dummy/config/initializers/apartment.rb +0 -4
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy/config/initializers/inflections.rb +0 -10
- data/spec/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/dummy/config/initializers/secret_token.rb +0 -7
- data/spec/dummy/config/initializers/session_store.rb +0 -8
- data/spec/dummy/config/locales/en.yml +0 -5
- data/spec/dummy/config/routes.rb +0 -3
- data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +0 -39
- data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +0 -14
- data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +0 -13
- data/spec/dummy/db/schema.rb +0 -55
- data/spec/dummy/db/seeds.rb +0 -5
- data/spec/dummy/db/seeds/import.rb +0 -5
- data/spec/dummy/public/404.html +0 -26
- data/spec/dummy/public/422.html +0 -26
- data/spec/dummy/public/500.html +0 -26
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +0 -6
- data/spec/dummy_engine/.gitignore +0 -8
- data/spec/dummy_engine/Gemfile +0 -15
- data/spec/dummy_engine/Rakefile +0 -34
- data/spec/dummy_engine/bin/rails +0 -12
- data/spec/dummy_engine/config/initializers/apartment.rb +0 -51
- data/spec/dummy_engine/dummy_engine.gemspec +0 -24
- data/spec/dummy_engine/lib/dummy_engine.rb +0 -4
- data/spec/dummy_engine/lib/dummy_engine/engine.rb +0 -4
- data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -3
- data/spec/dummy_engine/test/dummy/Rakefile +0 -6
- data/spec/dummy_engine/test/dummy/config.ru +0 -4
- data/spec/dummy_engine/test/dummy/config/application.rb +0 -22
- data/spec/dummy_engine/test/dummy/config/boot.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/database.yml +0 -25
- data/spec/dummy_engine/test/dummy/config/environment.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/environments/development.rb +0 -37
- data/spec/dummy_engine/test/dummy/config/environments/production.rb +0 -78
- data/spec/dummy_engine/test/dummy/config/environments/test.rb +0 -39
- data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +0 -8
- data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +0 -3
- data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +0 -16
- data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +0 -4
- data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +0 -3
- data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/spec/dummy_engine/test/dummy/config/locales/en.yml +0 -23
- data/spec/dummy_engine/test/dummy/config/routes.rb +0 -56
- data/spec/dummy_engine/test/dummy/config/secrets.yml +0 -22
- data/spec/examples/connection_adapter_examples.rb +0 -42
- data/spec/examples/generic_adapter_custom_configuration_example.rb +0 -95
- data/spec/examples/generic_adapter_examples.rb +0 -163
- data/spec/examples/schema_adapter_examples.rb +0 -234
- data/spec/integration/apartment_rake_integration_spec.rb +0 -107
- data/spec/integration/query_caching_spec.rb +0 -81
- data/spec/integration/use_within_an_engine_spec.rb +0 -28
- data/spec/schemas/v1.rb +0 -16
- data/spec/schemas/v2.rb +0 -43
- data/spec/schemas/v3.rb +0 -49
- data/spec/spec_helper.rb +0 -61
- data/spec/support/apartment_helpers.rb +0 -43
- data/spec/support/capybara_sessions.rb +0 -15
- data/spec/support/config.rb +0 -10
- data/spec/support/contexts.rb +0 -52
- data/spec/support/requirements.rb +0 -35
- data/spec/support/setup.rb +0 -46
- data/spec/tasks/apartment_rake_spec.rb +0 -129
- data/spec/tenant_spec.rb +0 -190
- data/spec/unit/config_spec.rb +0 -112
- data/spec/unit/elevators/domain_spec.rb +0 -32
- data/spec/unit/elevators/first_subdomain_spec.rb +0 -24
- data/spec/unit/elevators/generic_spec.rb +0 -54
- data/spec/unit/elevators/host_hash_spec.rb +0 -32
- data/spec/unit/elevators/host_spec.rb +0 -89
- data/spec/unit/elevators/subdomain_spec.rb +0 -76
- data/spec/unit/migrator_spec.rb +0 -77
- data/spec/unit/reloader_spec.rb +0 -24
data/spec/unit/config_spec.rb
DELETED
@@ -1,112 +0,0 @@
|
|
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
|
@@ -1,32 +0,0 @@
|
|
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
|
@@ -1,24 +0,0 @@
|
|
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
|
@@ -1,54 +0,0 @@
|
|
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
|
@@ -1,32 +0,0 @@
|
|
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
|
@@ -1,89 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'apartment/elevators/host'
|
3
|
-
|
4
|
-
describe Apartment::Elevators::Host do
|
5
|
-
|
6
|
-
subject(:elevator){ described_class.new(Proc.new{}) }
|
7
|
-
|
8
|
-
describe "#parse_tenant_name" do
|
9
|
-
|
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,76 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'apartment/elevators/subdomain'
|
3
|
-
|
4
|
-
describe Apartment::Elevators::Subdomain do
|
5
|
-
|
6
|
-
subject(:elevator){ described_class.new(Proc.new{}) }
|
7
|
-
|
8
|
-
describe "#parse_tenant_name" do
|
9
|
-
context "assuming one tld" do
|
10
|
-
it "should parse subdomain" do
|
11
|
-
request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.com')
|
12
|
-
expect(elevator.parse_tenant_name(request)).to eq('foo')
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should return nil when no subdomain" do
|
16
|
-
request = ActionDispatch::Request.new('HTTP_HOST' => 'bar.com')
|
17
|
-
expect(elevator.parse_tenant_name(request)).to be_nil
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context "assuming two tlds" do
|
22
|
-
it "should parse subdomain in the third level domain" do
|
23
|
-
request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.co.uk')
|
24
|
-
expect(elevator.parse_tenant_name(request)).to eq("foo")
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should return nil when no subdomain in the third level domain" do
|
28
|
-
request = ActionDispatch::Request.new('HTTP_HOST' => 'bar.co.uk')
|
29
|
-
expect(elevator.parse_tenant_name(request)).to be_nil
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
context "assuming two subdomains" do
|
34
|
-
it "should parse two subdomains in the two level domain" do
|
35
|
-
request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.xyz.bar.com')
|
36
|
-
expect(elevator.parse_tenant_name(request)).to eq("foo")
|
37
|
-
end
|
38
|
-
|
39
|
-
it "should parse two subdomains in the third level domain" do
|
40
|
-
request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.xyz.bar.co.uk')
|
41
|
-
expect(elevator.parse_tenant_name(request)).to eq("foo")
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context "assuming localhost" do
|
46
|
-
it "should return nil for localhost" do
|
47
|
-
request = ActionDispatch::Request.new('HTTP_HOST' => 'localhost')
|
48
|
-
expect(elevator.parse_tenant_name(request)).to be_nil
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
context "assuming ip address" do
|
53
|
-
it "should return nil for an ip address" do
|
54
|
-
request = ActionDispatch::Request.new('HTTP_HOST' => '127.0.0.1')
|
55
|
-
expect(elevator.parse_tenant_name(request)).to be_nil
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
describe "#call" do
|
61
|
-
it "switches to the proper tenant" do
|
62
|
-
expect(Apartment::Tenant).to receive(:switch).with('tenant1')
|
63
|
-
elevator.call('HTTP_HOST' => 'tenant1.example.com')
|
64
|
-
end
|
65
|
-
|
66
|
-
it "ignores excluded subdomains" do
|
67
|
-
described_class.excluded_subdomains = %w{foo}
|
68
|
-
|
69
|
-
expect(Apartment::Tenant).not_to receive(:switch)
|
70
|
-
|
71
|
-
elevator.call('HTTP_HOST' => 'foo.bar.com')
|
72
|
-
|
73
|
-
described_class.excluded_subdomains = nil
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
data/spec/unit/migrator_spec.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'apartment/migrator'
|
3
|
-
|
4
|
-
describe Apartment::Migrator do
|
5
|
-
|
6
|
-
let(:tenant){ Apartment::Test.next_db }
|
7
|
-
|
8
|
-
# Don't need a real switch here, just testing behaviour
|
9
|
-
before { allow(Apartment::Tenant.adapter).to receive(:connect_to_new) }
|
10
|
-
|
11
|
-
context "with ActiveRecord below 5.2.0", skip: ActiveRecord.version >= Gem::Version.new("5.2.0") do
|
12
|
-
before do
|
13
|
-
allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w(spec/dummy/db/migrate) }
|
14
|
-
allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { true }
|
15
|
-
end
|
16
|
-
|
17
|
-
describe "::migrate" do
|
18
|
-
it "switches and migrates" do
|
19
|
-
expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
|
20
|
-
expect(ActiveRecord::Migrator).to receive(:migrate)
|
21
|
-
|
22
|
-
Apartment::Migrator.migrate(tenant)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "::run" do
|
27
|
-
it "switches and runs" do
|
28
|
-
expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
|
29
|
-
expect(ActiveRecord::Migrator).to receive(:run).with(:up, anything, 1234)
|
30
|
-
|
31
|
-
Apartment::Migrator.run(:up, tenant, 1234)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe "::rollback" do
|
36
|
-
it "switches and rolls back" do
|
37
|
-
expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
|
38
|
-
expect(ActiveRecord::Migrator).to receive(:rollback).with(anything, 2)
|
39
|
-
|
40
|
-
Apartment::Migrator.rollback(tenant, 2)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context "with ActiveRecord above or equal to 5.2.0", skip: ActiveRecord.version < Gem::Version.new("5.2.0") do
|
46
|
-
before do
|
47
|
-
allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { false }
|
48
|
-
end
|
49
|
-
|
50
|
-
describe "::migrate" do
|
51
|
-
it "switches and migrates" do
|
52
|
-
expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
|
53
|
-
expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:migrate)
|
54
|
-
|
55
|
-
Apartment::Migrator.migrate(tenant)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
describe "::run" do
|
60
|
-
it "switches and runs" do
|
61
|
-
expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
|
62
|
-
expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:run).with(:up, 1234)
|
63
|
-
|
64
|
-
Apartment::Migrator.run(:up, tenant, 1234)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
describe "::rollback" do
|
69
|
-
it "switches and rolls back" do
|
70
|
-
expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
|
71
|
-
expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:rollback).with(2)
|
72
|
-
|
73
|
-
Apartment::Migrator.rollback(tenant, 2)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|