Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Support different cache keys for signed/not-signed/admin users when viewing Storefront cached pages

Damian Legawiecb426f249e45e
diff --git a/frontend/app/controllers/spree/store_controller.rb b/frontend/app/controllers/spree/store_controller.rb
index 5face1ccce..245b456b9b 100644
--- a/frontend/app/controllers/spree/store_controller.rb
+++ b/frontend/app/controllers/spree/store_controller.rb
@@ -41,8 +41,10 @@ module Spree
[
current_store,
current_currency,
- I18n.locale
- ]
+ I18n.locale,
+ try_spree_current_user.present?,
+ try_spree_current_user.try(:has_spree_role?, 'admin')
+ ].compact
end
def store_last_modified
diff --git a/frontend/spec/controllers/spree/store_controller_spec.rb b/frontend/spec/controllers/spree/store_controller_spec.rb
new file mode 100644
index 0000000000..20bcd6c899
--- /dev/null
+++ b/frontend/spec/controllers/spree/store_controller_spec.rb
@@ -0,0 +1,55 @@
+require 'spec_helper'
+
+describe Spree::StoreController, type: :controller do
+ describe '#store_etag' do
+ let!(:store) { create(:store, default: true, default_locale: 'es', default_currency: 'EUR') }
+
+ before { controller.send(:set_locale) }
+
+ context 'guest visitor' do
+ it do
+ expect(controller.send(:store_etag)).to eq [
+ store,
+ 'EUR',
+ :es,
+ false
+ ]
+ end
+ end
+
+ context 'with signed in user' do
+ let(:user) { stub_model(Spree::LegacyUser) }
+
+ before do
+ allow(controller).to receive_messages try_spree_current_user: user
+ allow(controller).to receive_messages spree_current_user: user
+ end
+
+ context 'regular user' do
+ it do
+ expect(controller.send(:store_etag)).to eq [
+ store,
+ 'EUR',
+ :es,
+ true,
+ false
+ ]
+ end
+ end
+
+ context 'admin user' do
+ before { user.spree_roles << Spree::Role.find_or_create_by(name: :admin) }
+
+ it do
+ expect(controller.send(:store_etag)).to eq [
+ store,
+ 'EUR',
+ :es,
+ true,
+ true
+ ]
+ end
+ end
+ end
+ end
+end