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.

CHG to permitted resource params

Michał Brożekdd9af2a1aea0
diff --git a/admin/app/controllers/spree/admin/gift_card_batches_controller.rb b/admin/app/controllers/spree/admin/gift_card_batches_controller.rb
index 02783a2db8..0b3923b8a3 100644
--- a/admin/app/controllers/spree/admin/gift_card_batches_controller.rb
+++ b/admin/app/controllers/spree/admin/gift_card_batches_controller.rb
@@ -10,6 +10,10 @@ module Spree
def collection_url
spree.admin_gift_cards_path
end
+
+ def permitted_resource_params
+ params.require(:gift_card_batch).permit(permitted_gift_card_batch_attributes)
+ end
end
end
end
diff --git a/admin/app/controllers/spree/admin/gift_cards_controller.rb b/admin/app/controllers/spree/admin/gift_cards_controller.rb
index d648c124da..f1ad478f2f 100644
--- a/admin/app/controllers/spree/admin/gift_cards_controller.rb
+++ b/admin/app/controllers/spree/admin/gift_cards_controller.rb
@@ -9,10 +9,14 @@ module Spree
private
def permitted_resource_params
- if @user.present?
- super.merge(user_id: @user.id)
- else
- super
+ @permitted_resource_params ||= begin
+ params_hash = params.require(:gift_card).permit(permitted_gift_card_attributes)
+
+ if @user.present?
+ params_hash.merge(user_id: @user.id)
+ else
+ params_hash
+ end
end
end
@@ -63,7 +67,7 @@ module Spree
end
def gift_cards_filter_dropdown_value
- case params[:q][:status_eq]
+ case params.dig(:q, :status_eq)
when 'active'
Spree.t('admin.gift_cards.active')
when 'expired'
diff --git a/api/app/controllers/spree/api/v2/platform/gift_cards_controller.rb b/api/app/controllers/spree/api/v2/platform/gift_cards_controller.rb
index 9d8ada2616..f7dc2bb9b2 100644
--- a/api/app/controllers/spree/api/v2/platform/gift_cards_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/gift_cards_controller.rb
@@ -8,6 +8,10 @@ module Spree
def model_class
Spree::GiftCard
end
+
+ def permitted_resource_params
+ params.require(:gift_card).permit(permitted_gift_card_attributes)
+ end
end
end
end
diff --git a/api/app/serializers/spree/api/v2/gift_card_serializer.rb b/api/app/serializers/spree/api/v2/platform/gift_card_serializer.rb
similarity index 100%
rename from api/app/serializers/spree/api/v2/gift_card_serializer.rb
rename to api/app/serializers/spree/api/v2/platform/gift_card_serializer.rb
diff --git a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
index af9a5c796d..4adefba1a6 100644
--- a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
@@ -1118,4 +1118,52 @@ describe 'API V2 Storefront Checkout Spec', type: :request do
end
end
end
+
+ describe 'checkout#validate_gift_card_data' do
+ let(:order) { create(:order) }
+
+ let(:execute) { post '/api/v2/storefront/checkout/validate_gift_card_data', params: params, headers: headers }
+
+ context 'when order is not present' do
+ let(:params) { { order_token: '' } }
+
+ it 'returns a 404 status' do
+ execute
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ context 'when order is present' do
+ context 'and gift card code is not present' do
+ let(:params) { { order_token: order.token, gift_card_code: '' } }
+
+ it 'returns a 200 status' do
+ execute
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
+ context 'and gift_card_code param present but order has no gift card' do
+ let(:params) { { order_token: order.token, gift_card_code: '123456' } }
+
+ it 'returns a 422 status' do
+ execute
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']).to eq(Spree.t('api.v2.gift_card.not_found'))
+ end
+ end
+
+ context 'and gift_card_amount param present but order gift card total is different' do
+ let(:params) { { order_token: order.token, gift_card_amount: 100 } }
+ let(:gift_card) { create(:gift_card, amount: 200) }
+ let(:order) { create(:order, gift_card: gift_card) }
+
+ it 'returns a 422 status' do
+ execute
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']).to eq(Spree.t('api.v2.gift_card.amount_not_valid'))
+ end
+ end
+ end
+ end
end
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index 84caf84f2b..d535333c89 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -12,6 +12,8 @@ module Spree
:digital_attributes,
:digital_link_attributes,
:export_attributes,
+ :gift_card_attributes,
+ :gift_card_batch_attributes,
:image_attributes,
:integration_attributes,
:inventory_unit_attributes,
@@ -105,6 +107,10 @@ module Spree
@@export_attributes = [:type, :format, :record_selection, :search_params]
+ @@gift_card_attributes = [:code, :amount, :minimum_order_amount, :expires_at, :user_id]
+
+ @@gift_card_batch_attributes = [:prefix, :codes_count, :amount, :minimum_order_amount, :expires_at]
+
@@image_attributes = [:alt, :attachment, :position, :viewable_type, :viewable_id]
@@integration_attributes = [:type, :active]
diff --git a/storefront/spec/controllers/spree/account/gift_cards_controller_spec.rb b/storefront/spec/controllers/spree/account/gift_cards_controller_spec.rb
new file mode 100644
index 0000000000..0552b1f0db
--- /dev/null
+++ b/storefront/spec/controllers/spree/account/gift_cards_controller_spec.rb
@@ -0,0 +1,54 @@
+require 'spec_helper'
+
+describe Spree::Account::GiftCardsController, type: :controller do
+ render_views
+
+ let(:store) { @default_store }
+ let(:user) { create(:user) }
+
+ before do
+ allow(controller).to receive(:current_store).and_return(store)
+ allow(controller).to receive(:spree_login_path).and_return('/login')
+ end
+
+ describe '#index' do
+ subject { get :index }
+
+ context 'when user is logged in' do
+ before do
+ allow(controller).to receive_messages try_spree_current_user: user
+ allow(controller).to receive_messages spree_current_user: user
+ end
+
+ it 'renders the index template' do
+ subject
+ expect(response).to render_template(:index)
+ expect(response).to have_http_status(:ok)
+ end
+
+ context 'when user has no gift cards' do
+ it 'assigns an empty array to @gift_cards' do
+ subject
+ expect(assigns(:gift_cards)).to eq([])
+ end
+ end
+
+ context 'when user has gift cards' do
+ let!(:gift_card) { create(:gift_card, user: user) }
+ let!(:gift_card_2) { create(:gift_card, user: user) }
+
+ it 'assigns the gift cards to gift_cards in descending order' do
+ subject
+ expect(assigns(:gift_cards)).to eq([gift_card_2, gift_card])
+ end
+ end
+ end
+
+ context 'when user is not logged in' do
+ it 'redirects to the login page' do
+ subject
+ expect(response).to have_http_status(:found)
+ end
+ end
+ end
+end