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.

Fix store credit card scope (#14362)

Chiu9d80cbc04bf2
diff --git a/spree/api/app/controllers/spree/api/v3/store/customer/credit_cards_controller.rb b/spree/api/app/controllers/spree/api/v3/store/customer/credit_cards_controller.rb
index 35b72f0454..932dc7960c 100644
--- a/spree/api/app/controllers/spree/api/v3/store/customer/credit_cards_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/customer/credit_cards_controller.rb
@@ -16,6 +16,13 @@ module Spree
:credit_cards
end
+ # Customers are global; a saved card belongs to the store-scoped
+ # payment method it was created against, so the nested collection is
+ # bound to the current store's payment methods.
+ def scope
+ @parent.credit_cards.where(payment_method_id: current_store.payment_methods.select(:id))
+ end
+
def model_class
Spree::CreditCard
end
diff --git a/spree/api/spec/controllers/spree/api/v3/store/customer/credit_cards_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/customer/credit_cards_controller_spec.rb
index 5b87834d6d..2ab97bfdfc 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/customer/credit_cards_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/customer/credit_cards_controller_spec.rb
@@ -5,7 +5,8 @@ RSpec.describe Spree::Api::V3::Store::Customer::CreditCardsController, type: :co
include_context 'API v3 Store'
- let!(:credit_card) { create(:credit_card, user: user) }
+ let(:payment_method) { create(:credit_card_payment_method, store: store) }
+ let!(:credit_card) { create(:credit_card, user: user, payment_method: payment_method) }
before do
request.headers['X-Spree-Api-Key'] = api_key.token
@@ -29,7 +30,7 @@ RSpec.describe Spree::Api::V3::Store::Customer::CreditCardsController, type: :co
it 'only returns credit cards belonging to the current user' do
other_user = create(:user)
- other_card = create(:credit_card, user: other_user)
+ other_card = create(:credit_card, user: other_user, payment_method: payment_method)
get :index
@@ -59,7 +60,7 @@ RSpec.describe Spree::Api::V3::Store::Customer::CreditCardsController, type: :co
context 'when credit card belongs to another user' do
let(:other_user) { create(:user) }
- let(:other_card) { create(:credit_card, user: other_user) }
+ let(:other_card) { create(:credit_card, user: other_user, payment_method: payment_method) }
it 'returns not found' do
get :show, params: { id: other_card.prefixed_id }
@@ -90,7 +91,7 @@ RSpec.describe Spree::Api::V3::Store::Customer::CreditCardsController, type: :co
context 'when credit card belongs to another user' do
let!(:other_user) { create(:user) }
- let!(:other_card) { create(:credit_card, user: other_user) }
+ let!(:other_card) { create(:credit_card, user: other_user, payment_method: payment_method) }
it 'returns not found' do
expect {
@@ -111,4 +112,31 @@ RSpec.describe Spree::Api::V3::Store::Customer::CreditCardsController, type: :co
end
end
end
+
+ describe 'cross-store isolation' do
+ let(:other_store) { create(:store) }
+ let(:other_payment_method) { create(:credit_card_payment_method, store: other_store) }
+ let!(:foreign_card) { create(:credit_card, user: user, payment_method: other_payment_method) }
+
+ it 'excludes cards from other stores in the index' do
+ get :index
+
+ ids = json_response['data'].map { |card| card['id'] }
+ expect(ids).to include(credit_card.prefixed_id)
+ expect(ids).not_to include(foreign_card.prefixed_id)
+ end
+
+ it 'cannot show a card stored against another store' do
+ get :show, params: { id: foreign_card.prefixed_id }
+
+ expect(response).to have_http_status(:not_found)
+ end
+
+ it 'cannot delete a card stored against another store' do
+ delete :destroy, params: { id: foreign_card.prefixed_id }
+
+ expect(response).to have_http_status(:not_found)
+ expect(Spree::CreditCard.exists?(foreign_card.id)).to be true
+ end
+ end
end
diff --git a/spree/api/spec/integration/spree/api/v3/store/credit_cards_spec.rb b/spree/api/spec/integration/spree/api/v3/store/credit_cards_spec.rb
index 2c01912515..3e37bc3590 100644
--- a/spree/api/spec/integration/spree/api/v3/store/credit_cards_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/store/credit_cards_spec.rb
@@ -5,14 +5,15 @@ require 'swagger_helper'
RSpec.describe 'Credit Cards API', type: :request, swagger_doc: 'api-reference/store.yaml' do
include_context 'API v3 Store'
- let!(:credit_card) { create(:credit_card, user: user) }
+ let(:payment_method) { create(:credit_card_payment_method, store: store) }
+ let!(:credit_card) { create(:credit_card, user: user, payment_method: payment_method) }
path '/api/v3/store/customers/me/credit_cards' do
get 'List saved credit cards' do
tags 'Customers'
produces 'application/json'
security [api_key: [], bearer_auth: []]
- description 'Returns all saved credit cards for the authenticated customer'
+ description "Returns the authenticated customer's saved credit cards for the current store's payment methods"
sdk_example 'customer-credit-cards/list'