diff --git a/lib/open_food_network/cached_products_renderer.rb b/lib/open_food_network/cached_products_renderer.rb index 2174bf4094..c605f47a05 100644 --- a/lib/open_food_network/cached_products_renderer.rb +++ b/lib/open_food_network/cached_products_renderer.rb @@ -16,13 +16,17 @@ module OpenFoodNetwork def products_json raise NoProducts.new if @distributor.nil? || @order_cycle.nil? - products_json = Rails.cache.fetch("products-json-#{@distributor.id}-#{@order_cycle.id}") do - log_warning + products_json = unless Rails.env.production? || Rails.env.staging? + uncached_products_json + else + Rails.cache.fetch("products-json-#{@distributor.id}-#{@order_cycle.id}") do + log_warning - begin - uncached_products_json - rescue ProductsRenderer::NoProducts - nil + begin + uncached_products_json + rescue ProductsRenderer::NoProducts + nil + end end end diff --git a/spec/lib/open_food_network/cached_products_renderer_spec.rb b/spec/lib/open_food_network/cached_products_renderer_spec.rb index 03b0ab05d5..6e096fc6d1 100644 --- a/spec/lib/open_food_network/cached_products_renderer_spec.rb +++ b/spec/lib/open_food_network/cached_products_renderer_spec.rb @@ -8,70 +8,88 @@ module OpenFoodNetwork let(:order_cycle) { double(:order_cycle, id: 456) } let(:cpr) { CachedProductsRenderer.new(distributor, order_cycle) } - describe "when the distribution is not set" do - let(:cpr) { CachedProductsRenderer.new(nil, nil) } - - it "raises an exception and returns no products" do - expect { cpr.products_json }.to raise_error CachedProductsRenderer::NoProducts - end - end - - describe "when the products JSON is already cached" do - before do - Rails.cache.write "products-json-#{distributor.id}-#{order_cycle.id}", 'products' - end - - it "returns the cached JSON" do - expect(cpr.products_json).to eq 'products' - end - - it "raises an exception when there are no products" do - Rails.cache.write "products-json-#{distributor.id}-#{order_cycle.id}", nil - expect { cpr.products_json }.to raise_error CachedProductsRenderer::NoProducts - end - end - - describe "when the products JSON is not cached" do - let(:cached_json) { Rails.cache.read "products-json-#{distributor.id}-#{order_cycle.id}" } - let(:cache_present) { Rails.cache.exist? "products-json-#{distributor.id}-#{order_cycle.id}" } - - before do - Rails.cache.clear - cpr.stub(:uncached_products_json) { 'fresh products' } - end - - describe "when there are products" do - it "returns products as JSON" do - expect(cpr.products_json).to eq 'fresh products' + describe "fetching cached products JSON" do + context "when in testing / development" do + before do + allow(cpr).to receive(:uncached_products_json) { "uncached products" } end - it "caches the JSON" do - cpr.products_json - expect(cached_json).to eq 'fresh products' - end - - it "logs a warning" do - cpr.should_receive :log_warning - cpr.products_json + it "returns uncaches products JSON" do + expect(cpr.products_json).to eq 'uncached products' end end - describe "when there are no products" do - before { cpr.stub(:uncached_products_json).and_raise ProductsRenderer::NoProducts } - - it "raises an error" do - expect { cpr.products_json }.to raise_error CachedProductsRenderer::NoProducts + context "when in production / staging" do + before do + allow(Rails.env).to receive(:production?) { true } end - it "caches the products as nil" do - expect { cpr.products_json }.to raise_error CachedProductsRenderer::NoProducts - expect(cache_present).to be - expect(cached_json).to be_nil + describe "when the distribution is not set" do + let(:cpr) { CachedProductsRenderer.new(nil, nil) } + + it "raises an exception and returns no products" do + expect { cpr.products_json }.to raise_error CachedProductsRenderer::NoProducts + end end - it "logs a warning" do - cpr.should_receive :log_warning - expect { cpr.products_json }.to raise_error CachedProductsRenderer::NoProducts + describe "when the products JSON is already cached" do + before do + Rails.cache.write "products-json-#{distributor.id}-#{order_cycle.id}", 'products' + end + + it "returns the cached JSON" do + expect(cpr.products_json).to eq 'products' + end + + it "raises an exception when there are no products" do + Rails.cache.write "products-json-#{distributor.id}-#{order_cycle.id}", nil + expect { cpr.products_json }.to raise_error CachedProductsRenderer::NoProducts + end + end + + describe "when the products JSON is not cached" do + let(:cached_json) { Rails.cache.read "products-json-#{distributor.id}-#{order_cycle.id}" } + let(:cache_present) { Rails.cache.exist? "products-json-#{distributor.id}-#{order_cycle.id}" } + + before do + Rails.cache.clear + cpr.stub(:uncached_products_json) { 'fresh products' } + end + + describe "when there are products" do + it "returns products as JSON" do + expect(cpr.products_json).to eq 'fresh products' + end + + it "caches the JSON" do + cpr.products_json + expect(cached_json).to eq 'fresh products' + end + + it "logs a warning" do + cpr.should_receive :log_warning + cpr.products_json + end + end + + describe "when there are no products" do + before { cpr.stub(:uncached_products_json).and_raise ProductsRenderer::NoProducts } + + it "raises an error" do + expect { cpr.products_json }.to raise_error CachedProductsRenderer::NoProducts + end + + it "caches the products as nil" do + expect { cpr.products_json }.to raise_error CachedProductsRenderer::NoProducts + expect(cache_present).to be + expect(cached_json).to be_nil + end + + it "logs a warning" do + cpr.should_receive :log_warning + expect { cpr.products_json }.to raise_error CachedProductsRenderer::NoProducts + end + end end end end