Skip to content

Commit 91bff13

Browse files
committed
Make current tenant won't reset by integration and system test when using rspec
1 parent 885ffd1 commit 91bff13

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ group :test do
1515
gem 'minitest-focus'
1616
gem 'sidekiq'
1717
gem 'capybara'
18+
gem 'rspec'
19+
gem 'rspec-rails'
1820
end

lib/multi_tenant_support/rspec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require_relative "./test/integration"
2+
require_relative "./test/system"
3+
require_relative "./test/capybara"
4+
5+
RSpec.configure do |config|
6+
config.include MultiTenantSupport::Test::Integration, type: :request
7+
config.include MultiTenantSupport::Test::Integration, type: :controller
8+
9+
config.include MultiTenantSupport::Test::System, type: :system
10+
config.include MultiTenantSupport::Test::System, type: :feature
11+
end
12+
13+
Capybara::Node::Element.prepend(MultiTenantSupport::Test::Capybara)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require_relative "../spec_helper"
2+
3+
RSpec.describe "Current tenant protect during integration test", type: :request do
4+
5+
before do
6+
host! "amazon.example.com"
7+
MultiTenantSupport::Current.tenant_account = accounts(:amazon)
8+
end
9+
10+
it "get won't reset current tenant" do
11+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
12+
get users_path
13+
expect(response.status).to eq(200)
14+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
15+
end
16+
17+
it "post won't reset current tenant" do
18+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
19+
post users_path
20+
assert_redirected_to users_path
21+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
22+
end
23+
24+
it "patch won't reset current tenant" do
25+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
26+
patch user_path(id: 1)
27+
assert_redirected_to users_path
28+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
29+
end
30+
31+
it "put won't reset current tenant" do
32+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
33+
put user_path(id: 1)
34+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
35+
end
36+
37+
it "delete won't reset current tenant" do
38+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
39+
delete user_path(id: 1)
40+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
41+
end
42+
43+
end

test/spec_helper.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Configure Rails Environment
2+
ENV["RAILS_ENV"] = "test"
3+
4+
require_relative "../test/dummy/config/environment"
5+
ActiveRecord::Migrator.migrations_paths = [File.expand_path("../test/dummy/db/migrate", __dir__)]
6+
ActiveRecord::Migration.maintain_test_schema!
7+
8+
require "rspec/rails"
9+
require 'multi_tenant_support/rspec'
10+
11+
RSpec.configure do |config|
12+
config.fixture_path = "test/fixtures"
13+
config.global_fixtures = :all
14+
config.use_transactional_fixtures = true
15+
end
16+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require_relative '../spec_helper'
2+
3+
RSpec.describe "current tenant protect during system test", type: :system do
4+
5+
before do
6+
driven_by :rack_test
7+
Capybara.app_host = "http://amazon.example.com"
8+
MultiTenantSupport::Current.tenant_account = accounts(:amazon)
9+
end
10+
11+
it "visit won't reset current tenant" do
12+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
13+
14+
expect{
15+
visit users_path
16+
}.not_to change{ MultiTenantSupport.current_tenant }
17+
18+
expect(page).to have_content("Users#index")
19+
expect(page).to have_selector("#id", text: accounts(:amazon).id.to_s)
20+
expect(page).to have_selector("#name", text: "Amazon")
21+
expect(page).to have_selector("#domain", text: "amazon.com")
22+
expect(page).to have_selector("#subdomain", text: "amazon")
23+
end
24+
25+
it "refresh won't reset current tenant" do
26+
visit users_path
27+
28+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
29+
30+
expect{
31+
refresh
32+
}.not_to change{ MultiTenantSupport.current_tenant }
33+
end
34+
35+
it "click_on won't reset current tenant" do
36+
visit users_path
37+
38+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
39+
40+
expect{
41+
click_on "Show"
42+
}.not_to change{ MultiTenantSupport.current_tenant }
43+
44+
expect(page).to have_content("User - Show")
45+
end
46+
47+
it "Element#click won't reset current tenant" do
48+
visit users_path
49+
50+
expect(MultiTenantSupport.current_tenant).to eq(accounts(:amazon))
51+
52+
expect{
53+
find_link("Show").click
54+
}.not_to change{ MultiTenantSupport.current_tenant }
55+
56+
expect(page).to have_content("User - Show")
57+
end
58+
59+
end

0 commit comments

Comments
 (0)