Skip to content

Commit

Permalink
Merge pull request #3275 from nebulab/kennyadsl/freeze_preferences
Browse files Browse the repository at this point in the history
Freeze preferences for Backend, Frontend and Api specs as well
  • Loading branch information
kennyadsl authored Aug 7, 2019
2 parents d702e64 + bf083ed commit d931061
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 32 deletions.
4 changes: 3 additions & 1 deletion api/spec/features/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

module Spree
describe 'Api Feature Specs', type: :request do
before { Spree::Api::Config[:requires_authentication] = false }
before do
stub_spree_preferences(Spree::Api::Config, requires_authentication: false)
end
let!(:promotion) { FactoryBot.create(:promotion, :with_order_adjustment, code: 'foo', weighted_order_adjustment_amount: 10) }
let(:promotion_code) { promotion.codes.first }
let!(:store) { FactoryBot.create(:store) }
Expand Down
2 changes: 1 addition & 1 deletion api/spec/models/spree/legacy_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module Spree

before {
user.clear_spree_api_key!
stub_spree_preferences roles_for_auto_api_key: ['hobbit']
stub_spree_preferences(roles_for_auto_api_key: ['hobbit'])
}

it { expect { subject }.to change { user.reload.spree_api_key }.from(nil) }
Expand Down
2 changes: 1 addition & 1 deletion api/spec/requests/spree/api/checkouts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Spree
describe Api::CheckoutsController, type: :request do
before(:each) do
stub_authentication!
stub_spree_preferences track_inventory_levels: false
stub_spree_preferences(track_inventory_levels: false)
country_zone = create(:zone, name: 'CountryZone')
@state = create(:state)
@country = @state.country
Expand Down
6 changes: 1 addition & 5 deletions api/spec/requests/spree/api/products_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,7 @@ module Spree
# Regression test for https://github.com/spree/spree/issues/2140
context "with authentication_required set to false" do
before do
Spree::Api::Config.requires_authentication = false
end

after do
Spree::Api::Config.requires_authentication = true
stub_spree_preferences(Spree::Api::Config, requires_authentication: false)
end

it "can still create a product" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module Spree
let(:attributes) { [:id, :name, :description, :price, :available_on, :slug, :meta_description, :meta_keywords, :taxon_ids, :meta_title] }

context "without authentication" do
before { Spree::Api::Config[:requires_authentication] = false }
before do
stub_spree_preferences(Spree::Api::Config, requires_authentication: false)
end

it "retrieves a list of products" do
get spree.api_products_path
Expand Down
1 change: 0 additions & 1 deletion api/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@

config.before(:each) do
Rails.cache.clear
Spree::Api::Config[:requires_authentication] = true
end

config.include ActiveJob::TestHelper
Expand Down
3 changes: 1 addition & 2 deletions backend/spec/features/admin/locale_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
},
listing_orders: "Ordres"
})
Spree::Backend::Config[:locale] = "fr"
stub_spree_preferences(Spree::Backend::Config, locale: "fr")
end

after do
I18n.locale = I18n.default_locale
Spree::Backend::Config[:locale] = "en"
ActionView::Base.raise_on_missing_translations = true
end

Expand Down
69 changes: 54 additions & 15 deletions core/lib/spree/testing_support/preferences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,36 @@ def assert_preference_unset(preference)
# This method may be used for stubbing one or more different preferences
# at the same time.
#
# @param [Hash] preferences names and values to be stubbed
# @param prefs_or_conf_class [Class, Hash] the class we want to stub
# preferences for or the preferences hash (see prefs param). If this
# param is an Hash, preferences will be stubbed on Spree::Config.
# @param prefs [Hash, nil] names and values to be stubbed
#
# @example Stubs `currency` and `track_inventory_levels` preferences
# @example Stubs `currency` and `track_inventory_levels` on `Spree::Config`:
# stub_spree_preferences(currency: 'EUR', track_inventory_levels: false)
# expect(Spree::Config.currency).to eql 'EUR'
#
# @example Stubs `locale` preference on `Spree::Backend::Config`:
# stub_spree_preferences(Spree::Backend::Config, locale: 'fr'),
# expect(Spree::Backend::Config.locale).to eql 'fr'
#
# @see https://github.com/solidusio/solidus/issues/3219
# Solidus #3219 for more details and motivations.
def stub_spree_preferences(preferences)
def stub_spree_preferences(prefs_or_conf_class, prefs = nil)
if prefs_or_conf_class.is_a?(Hash)
preference_store_class = Spree::Config
preferences = prefs_or_conf_class
else
preference_store_class = prefs_or_conf_class
preferences = prefs
end

preferences.each do |name, value|
if Spree::Config.method(:[]).owner >= Spree::Config.class
allow(Spree::Config).to receive(:[]).and_call_original
if preference_store_class.method(:[]).owner >= preference_store_class.class
allow(preference_store_class).to receive(:[]).and_call_original
end
allow(Spree::Config).to receive(:[]).with(name) { value }
allow(Spree::Config).to receive(name) { value }
allow(preference_store_class).to receive(:[]).with(name) { value }
allow(preference_store_class).to receive(name) { value }
end
end

Expand All @@ -75,22 +90,46 @@ def stub_spree_preferences(preferences)
# expect(Spree::Config.currency).to eql 'EUR'
# end
# @see Spree::TestingSupport::Preferences#stub_spree_preferences
def with_unfrozen_spree_preference_store
frozen_store = Spree::Config.preference_store
Spree::Config.preference_store = Spree::Config[:unfrozen_preference_store].dup
def with_unfrozen_spree_preference_store(preference_store_class: Spree::Config)
frozen_store = preference_store_class.preference_store
preference_store_class.preference_store = preference_store_class[:unfrozen_preference_store].dup
yield
ensure
Spree::Config.preference_store = frozen_store
preference_store_class.preference_store = frozen_store
end

# This class method allows to freeze preferences for a specific
# configuration store class. It also stores the current state into
# a new preference of that store, so it can be reused when needed
# (eg. with_unfrozen_spree_preference_store)
#
# It is meant to be used by extensions as well, for example if one
# extension has its own Spree::ExtensionName::Config class, we can
# freeze it and be sure we always stub values on it during tests.
#
# @param preference_store_class [Class] the configuration class we want
# to freeze.
def self.freeze_preferences(preference_store_class)
config_class = preference_store_class.class
config_class.preference :unfrozen_preference_store, :hash
preference_store_class.unfrozen_preference_store = preference_store_class.preference_store.dup
preference_store_class.preference_store.freeze
end
end
end
end

RSpec.configure do |config|
config.before :suite do
# keep a copy of the original unfrozen preference_store for later use:
Spree::AppConfiguration.preference :unfrozen_preference_store, :hash
Spree::Config.unfrozen_preference_store = Spree::Config.preference_store.dup
Spree::Config.preference_store.freeze
%w[
Spree::Config
Spree::Frontend::Config
Spree::Backend::Config
Spree::Api::Config
].each do |configuration_class|
if Object.const_defined?(configuration_class)
Spree::TestingSupport::Preferences.freeze_preferences(configuration_class.constantize)
end
end
end
end
2 changes: 1 addition & 1 deletion core/spec/models/spree/stock/quantifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Stock
end

context 'when track_inventory_levels is false' do
before { stub_spree_preferences track_inventory_levels: false }
before { stub_spree_preferences(track_inventory_levels: false) }

specify { expect(subject.total_on_hand).to eq(Float::INFINITY) }

Expand Down
3 changes: 1 addition & 2 deletions frontend/spec/controllers/controller_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
describe Spree::ProductsController, type: :controller do
before do
I18n.enforce_available_locales = false
Spree::Frontend::Config[:locale] = :de
stub_spree_preferences(Spree::Frontend::Config, locale: :de)
I18n.backend.store_translations(:de, spree: {
i18n: { this_file_language: "Deutsch (DE)" }
})
end

after do
I18n.reload!
Spree::Frontend::Config[:locale] = :en
I18n.locale = :en
I18n.enforce_available_locales = true
end
Expand Down
3 changes: 1 addition & 2 deletions frontend/spec/features/locale_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
let!(:store) { create(:store) }
def with_locale(locale)
I18n.locale = locale
Spree::Frontend::Config[:locale] = locale
stub_spree_preferences(Spree::Frontend::Config, locale: locale)
yield
ensure
I18n.locale = I18n.default_locale
Spree::Frontend::Config[:locale] = 'en'
end

context 'shopping cart link and page' do
Expand Down

0 comments on commit d931061

Please sign in to comment.