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.

Scope resources in Platform API to the current store

Damian Legawiec83eaf6a1e031
diff --git a/api/app/controllers/spree/api/v2/platform/resource_controller.rb b/api/app/controllers/spree/api/v2/platform/resource_controller.rb
index 3656ea2c1c..46baa4317e 100644
--- a/api/app/controllers/spree/api/v2/platform/resource_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/resource_controller.rb
@@ -61,7 +61,7 @@ module Spree
def scope
return super if spree_current_user.present?
- model_class.includes(scope_includes)
+ super(skip_cancancan: true)
end
# We're overwriting this method because the original one calls `dookreeper_authorize`
diff --git a/api/app/controllers/spree/api/v2/resource_controller.rb b/api/app/controllers/spree/api/v2/resource_controller.rb
index 39b24e7c21..9e6bf0829a 100644
--- a/api/app/controllers/spree/api/v2/resource_controller.rb
+++ b/api/app/controllers/spree/api/v2/resource_controller.rb
@@ -26,7 +26,7 @@ module Spree
[:id, :name, :number, :updated_at, :created_at]
end
- def scope
+ def scope(skip_cancancan: false)
plural_model_name = model_class.model_name.plural.gsub(/spree_/, '').to_sym
base_scope = if current_store.respond_to?(plural_model_name)
@@ -35,7 +35,7 @@ module Spree
model_class
end
- base_scope = base_scope.accessible_by(current_ability, :show)
+ base_scope = base_scope.accessible_by(current_ability, :show) unless skip_cancancan
base_scope = base_scope.includes(scope_includes) if scope_includes.any?
base_scope
end
diff --git a/api/spec/controllers/spree/api/v2/platform/resource_controller_spec.rb b/api/spec/controllers/spree/api/v2/platform/resource_controller_spec.rb
index 1c8ee65d3a..69be04dcab 100644
--- a/api/spec/controllers/spree/api/v2/platform/resource_controller_spec.rb
+++ b/api/spec/controllers/spree/api/v2/platform/resource_controller_spec.rb
@@ -4,44 +4,44 @@ class PlatformApiDummyController < Spree::Api::V2::Platform::ResourceController
private
def model_class
- Spree::Address
- end
-
- def scope
- Spree::Address
+ Spree::Product
end
end
describe Spree::Api::V2::Platform::ResourceController, type: :controller do
let(:dummy_controller) { PlatformApiDummyController.new }
+ let(:store) { Spree::Store.default }
describe '#resource_serializer' do
- it { expect(dummy_controller.send(:resource_serializer)).to be Spree::Api::V2::Platform::AddressSerializer }
+ it { expect(dummy_controller.send(:resource_serializer)).to be Spree::Api::V2::Platform::ProductSerializer }
end
describe '#collection_serializer' do
- it { expect(dummy_controller.send(:collection_serializer)).to be Spree::Api::V2::Platform::AddressSerializer }
+ it { expect(dummy_controller.send(:collection_serializer)).to be Spree::Api::V2::Platform::ProductSerializer }
end
describe '#collection' do
+ let!(:product) { create(:product, stores: [store]) }
+ let!(:product_from_another_store) { create(:product, stores: [create(:store)]) }
+ let(:collection) { dummy_controller.send(:collection) }
+
before do
- create(:address)
dummy_controller.params = {}
+ allow(dummy_controller).to receive(:current_store).and_return(store)
+ allow(dummy_controller).to receive(:spree_current_user).and_return(nil)
end
- let(:collection) { dummy_controller.send(:collection) }
-
- it { expect(collection).to be_instance_of(Spree::Address.const_get(:ActiveRecord_Relation)) }
- it { expect(collection.first).to be_instance_of(Spree::Address) }
- it { expect(collection.count).to eq(1) }
+ it { expect(collection.first).to be_instance_of(Spree::Product) }
+ it { expect(collection.count).to eq(store.products.count) }
+ it { expect(collection.map(&:id)).to include(product.id) }
+ it { expect(collection.map(&:id)).not_to include(product_from_another_store.id) }
end
describe '#permitted_resource_params' do
let(:valid_attributes) do
{
- address: {
- firstname: 'John',
- lastname: 'Snow'
+ product: {
+ name: 'Test'
}
}
end
@@ -50,6 +50,6 @@ describe Spree::Api::V2::Platform::ResourceController, type: :controller do
dummy_controller.params = valid_attributes
end
- it { expect(dummy_controller.send(:permitted_resource_params)).to eq(ActionController::Parameters.new(valid_attributes).require(:address).permit!) }
+ it { expect(dummy_controller.send(:permitted_resource_params)).to eq(ActionController::Parameters.new(valid_attributes).require(:product).permit!) }
end
end
diff --git a/api/spec/controllers/spree/api/v2/resource_controller_spec.rb b/api/spec/controllers/spree/api/v2/resource_controller_spec.rb
index 1352c2905c..48c1270300 100644
--- a/api/spec/controllers/spree/api/v2/resource_controller_spec.rb
+++ b/api/spec/controllers/spree/api/v2/resource_controller_spec.rb
@@ -4,7 +4,7 @@ class ApiV2DummyController < Spree::Api::V2::ResourceController
private
def model_class
- Spree::Order
+ Spree::Product
end
end
@@ -53,4 +53,21 @@ describe Spree::Api::V2::ResourceController, type: :controller do
it_behaves_like 'returns proper values'
end
end
+
+ describe '#collection' do
+ let!(:product) { create(:product, stores: [store]) }
+ let!(:product_from_another_store) { create(:product, stores: [create(:store)]) }
+ let(:collection) { dummy_controller.send(:collection) }
+
+ before do
+ dummy_controller.params = {}
+ allow(dummy_controller).to receive(:current_store).and_return(store)
+ allow(dummy_controller).to receive(:spree_current_user).and_return(nil)
+ end
+
+ it { expect(collection.first).to be_instance_of(Spree::Product) }
+ it { expect(collection.count).to eq(store.products.count) }
+ it { expect(collection.map(&:id)).to include(product.id) }
+ it { expect(collection.map(&:id)).not_to include(product_from_another_store.id) }
+ end
end