Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
RSpec & multitenancy. Preferences test should fail
Due to the `Capybara-shared-connection` issue, we cannot provide
multitenant feature specs.
See influitive/apartment#444

.......

Include rspec-rails gem and install with:
$ rails g rspec:install

*also add standard Rails test for preferences (skipped)
  • Loading branch information
Flavio Auciello committed Sep 26, 2018
1 parent 9f7ca93 commit 284099a
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--require spec_helper
3 changes: 3 additions & 0 deletions Gemfile
Expand Up @@ -47,6 +47,9 @@ gem 'bootsnap', '>= 1.1.0', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'dev_tools', github: 'nebulab/dev_tools'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'rspec-rails', '~> 3.6.0'
end

group :development do
Expand Down
103 changes: 103 additions & 0 deletions Gemfile.lock
@@ -1,6 +1,21 @@
GIT
remote: https://github.com/nebulab/dev_tools.git
revision: 2dd4a8ff8c4e9754f27397bd669f0147aa8dda67
specs:
dev_tools (0.0.1)
awesome_print
dotenv-rails
invoker
letter_opener
pry-byebug
pry-doc
pry-rails
rubocop

GEM
remote: https://rubygems.org/
specs:
CFPropertyList (2.3.6)
actioncable (5.2.0)
actionpack (= 5.2.0)
nio4r (~> 2.0)
Expand Down Expand Up @@ -59,10 +74,12 @@ GEM
archive-zip (0.11.0)
io-like (~> 0.3.0)
arel (9.0.0)
ast (2.4.0)
autoprefixer-rails (7.2.6)
execjs
awesome_nested_set (3.1.4)
activerecord (>= 4.0.0, < 5.3)
awesome_print (1.8.0)
bcrypt (3.1.12)
bindex (0.5.0)
bootsnap (1.3.0)
Expand All @@ -87,13 +104,15 @@ GEM
archive-zip (~> 0.10)
nokogiri (~> 1.8)
climate_control (0.2.0)
coderay (1.1.2)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.12.2)
colorize (0.8.1)
concurrent-ruby (1.0.5)
crass (1.0.4)
devise (4.4.3)
Expand All @@ -104,13 +123,25 @@ GEM
warden (~> 1.2.3)
devise-encryptable (0.2.0)
devise (>= 2.1.0)
diff-lcs (1.3)
discard (1.0.0)
activerecord (>= 4.2, < 6)
dotenv (2.5.0)
dotenv-rails (2.5.0)
dotenv (= 2.5.0)
railties (>= 3.2, < 6.0)
em-proxy (0.1.9)
eventmachine
erubi (1.7.1)
eventmachine (1.0.9.1)
execjs (2.7.0)
facter (2.5.1)
facter (2.5.1-universal-darwin)
CFPropertyList (~> 2.2)
ffi (1.9.25)
font-awesome-rails (4.7.0.4)
railties (>= 3.2, < 6.0)
formatador (0.2.5)
friendly_id (5.2.4)
activerecord (>= 4.0.0)
globalid (0.4.1)
Expand All @@ -119,9 +150,25 @@ GEM
execjs (~> 2.0)
sprockets (>= 2.0.0)
tilt (>= 1.2)
http-parser-lite (0.6.0)
i18n (1.0.1)
concurrent-ruby (~> 1.0)
iniparse (1.4.4)
invoker (1.5.6)
colorize (~> 0.8.1)
dotenv (~> 2.0)
em-proxy (~> 0.1)
eventmachine (~> 1.0.4)
facter (~> 2.2)
formatador (~> 0.2)
http-parser-lite (~> 0.6)
iniparse (~> 1.1)
rubydns (~> 0.8.5)
thor (~> 0.19)
uuid (~> 2.3)
io-like (0.3.0)
jaro_winkler (1.5.1)
jaro_winkler (1.5.1-x86_64-darwin-17)
jbuilder (2.7.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
Expand All @@ -141,13 +188,19 @@ GEM
activerecord
kaminari-core (= 1.1.1)
kaminari-core (1.1.1)
launchy (2.4.3)
addressable (~> 2.3)
letter_opener (1.6.0)
launchy (~> 2.2)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
ruby_dep (~> 1.2)
loofah (2.2.2)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
macaddr (1.7.1)
systemu (~> 2.6.2)
mail (2.7.0)
mini_mime (>= 0.1.1)
marcel (0.3.2)
Expand Down Expand Up @@ -179,9 +232,23 @@ GEM
parallel (1.12.1)
paranoia (2.4.1)
activerecord (>= 4.0, < 5.3)
parser (2.5.1.2)
ast (~> 2.4.0)
pg (1.0.0)
polyamorous (1.3.3)
activerecord (>= 3.0)
powerpack (0.1.2)
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-byebug (3.6.0)
byebug (~> 10.0)
pry (~> 0.10)
pry-doc (0.13.4)
pry (~> 0.11)
yard (~> 0.9.11)
pry-rails (0.3.6)
pry (>= 0.10.4)
public_suffix (3.0.2)
puma (3.11.4)
rack (2.0.5)
Expand Down Expand Up @@ -211,6 +278,7 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rainbow (3.0.0)
rake (12.3.1)
ransack (1.8.8)
actionpack (>= 3.0)
Expand All @@ -224,7 +292,35 @@ GEM
responders (2.4.0)
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
rspec-core (3.6.0)
rspec-support (~> 3.6.0)
rspec-expectations (3.6.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.6.0)
rspec-mocks (3.6.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.6.0)
rspec-rails (3.6.1)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.6.0)
rspec-expectations (~> 3.6.0)
rspec-mocks (~> 3.6.0)
rspec-support (~> 3.6.0)
rspec-support (3.6.0)
rubocop (0.59.2)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5, != 2.5.1.1)
powerpack (~> 0.1)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.10.0)
ruby_dep (1.5.0)
rubydns (0.8.5)
eventmachine (~> 1.0.0)
rubyzip (1.2.1)
sass (3.5.6)
sass-listen (~> 4.0.0)
Expand Down Expand Up @@ -321,6 +417,7 @@ GEM
state_machines-activerecord (0.5.1)
activerecord (>= 4.1, < 6.0)
state_machines-activemodel (>= 0.5.0)
systemu (2.6.5)
terrapin (0.6.0)
climate_control (>= 0.0.3, < 1.0)
thor (0.20.0)
Expand All @@ -334,6 +431,9 @@ GEM
thread_safe (~> 0.1)
uglifier (4.1.12)
execjs (>= 0.3.0, < 3)
unicode-display_width (1.4.0)
uuid (2.3.9)
macaddr (~> 1.0)
warden (1.2.7)
rack (>= 1.0)
web-console (3.6.2)
Expand All @@ -346,6 +446,7 @@ GEM
websocket-extensions (0.1.3)
xpath (3.1.0)
nokogiri (~> 1.8)
yard (0.9.16)

PLATFORMS
ruby
Expand All @@ -358,11 +459,13 @@ DEPENDENCIES
capybara (>= 2.15, < 4.0)
chromedriver-helper
coffee-rails (~> 4.2)
dev_tools!
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
puma (~> 3.11)
rails (~> 5.2.0)
rspec-rails (~> 3.6.0)
sass-rails (~> 5.0)
selenium-webdriver
solidus (~> 2.6)
Expand Down
28 changes: 28 additions & 0 deletions README.md
Expand Up @@ -78,3 +78,31 @@ psql nebulab_shop_development -c "\dn"
public | seller
(3 rows)
```

# Testing with RSpec

``` ruby
# spec/spec_helper.rb

config.before(:suite) do
DatabaseCleaner.strategy = :transaction
end

config.before(:each) do
DatabaseCleaner.start
end

config.after(:each) do
Tenant.all.each do |tenant|
ActiveRecord::Base.connection.execute("DROP SCHEMA tenant#{tenanti.id} CASCADE;")
end

DatabaseCleaner.clean
end
```

Note that if you use this, you shuold turn off the activerecord transactional features by commenting out this line:

``` ruby
config.use_transactional_fixtures = true
```
14 changes: 14 additions & 0 deletions app/views/spree/shared/_main_nav_bar.html.erb
@@ -0,0 +1,14 @@
<nav class="columns sixteen">
<ul id="main-nav-bar" class="inline" data-hook>
<li id="home-link" data-hook><%= link_to t('spree.home'), spree.root_path %></li>
<li id="mails-from" class="testing">Mails from: <%= Spree::Config[:mails_from] %></li>
<li id="locale" class="testing">Locale: <%= Spree::Frontend::Config[:locale] %></li>
<li id="link-to-cart" data-hook>
<noscript>
<%= link_to t('spree.cart'), spree.cart_path %>
</noscript>
&nbsp;
</li>
</ul>
<script>Spree.fetch_cart('<%= j spree.cart_link_path %>')</script>
</nav>
32 changes: 32 additions & 0 deletions spec/preferences_spec.rb
@@ -0,0 +1,32 @@
require 'rails_helper'

RSpec.feature Spree::Preferences, type: :feature do
TENANTS_EXPECTATIONS = {
latina_shop: {
mails_from: 'store@latina-shop.com',
locale: 'en'
},
pescara_shop: {
mails_from: 'store@pescara-shop.com',
locale: 'it'
}
}

NebulabShop::Stores.each do |tenant, domain|

describe "When visiting #{tenant}", tenant: tenant do
let(:mails_from) { TENANTS_EXPECTATIONS[tenant][:mails_from] }
let(:locale) { TENANTS_EXPECTATIONS[tenant][:locale] }

before do
visit '/'
end

it 'should fetch Solidus preferences from the correct tenant' do
expect(page).to have_css("mails-from.testing", text: mails_from)
expect(page).to have_css("locale.testing", text: locale)
end
end

end
end
57 changes: 57 additions & 0 deletions spec/rails_helper.rb
@@ -0,0 +1,57 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!

# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end

0 comments on commit 284099a

Please sign in to comment.