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.

Admin promotions: final fixes and specs

Damian Legawieced04f3f7eee1
diff --git a/admin/app/controllers/spree/admin/promotions_controller.rb b/admin/app/controllers/spree/admin/promotions_controller.rb
index 150e868b88..94dcbe313e 100644
--- a/admin/app/controllers/spree/admin/promotions_controller.rb
+++ b/admin/app/controllers/spree/admin/promotions_controller.rb
@@ -15,7 +15,7 @@ module Spree
redirect_to spree.admin_promotion_path(@new_promo)
else
flash[:error] = Spree.t('promotion_not_cloned', error: @new_promo.errors.full_messages.to_sentence)
- redirect_to spree.admin_promotions_path(@new_promo)
+ redirect_to spree.admin_promotions_path
end
end
diff --git a/admin/app/views/spree/admin/promotion_actions/_promotion_action.html.erb b/admin/app/views/spree/admin/promotion_actions/_promotion_action.html.erb
index 8d8268c6fb..29737e62b3 100644
--- a/admin/app/views/spree/admin/promotion_actions/_promotion_action.html.erb
+++ b/admin/app/views/spree/admin/promotion_actions/_promotion_action.html.erb
@@ -20,7 +20,7 @@
<%= render 'spree/admin/shared/preferences', object: promotion_action %>
<% end %>
- <% if promotion_action.respond_to?(:calculator) && promotion_action.calculator.preferences.present? %>
+ <% if promotion_action.respond_to?(:calculator) && promotion_action.calculator.present? && promotion_action.calculator.preferences.present? %>
<h6><%= Spree.t(:calculator) %>: <%= promotion_action.calculator.description %></h6>
<%= render 'spree/admin/shared/preferences', object: promotion_action.calculator %>
<% end %>
diff --git a/admin/spec/controllers/spree/admin/promotion_actions_controller_spec.rb b/admin/spec/controllers/spree/admin/promotion_actions_controller_spec.rb
new file mode 100644
index 0000000000..d7be1fe3df
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/promotion_actions_controller_spec.rb
@@ -0,0 +1,126 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::PromotionActionsController, type: :controller do
+ stub_authorization!
+
+ render_views
+
+ let(:user) { create(:admin_user) }
+ let(:store) { Spree::Store.default }
+ let(:promotion) { create(:promotion, stores: [store]) }
+
+ before do
+ allow(controller).to receive(:current_ability).and_call_original
+ end
+
+ describe 'GET #new' do
+ it 'returns a successful response' do
+ get :new, params: { promotion_id: promotion.id }
+ expect(response).to be_successful
+ end
+
+ context 'when type is provided' do
+ let(:action_type) { 'Spree::Promotion::Actions::CreateAdjustment' }
+
+ it 'returns a successful response' do
+ get :new, params: { promotion_id: promotion.id, promotion_action: { type: action_type } }
+ expect(response).to be_successful
+ end
+
+ it 'loads the correct action type' do
+ get :new, params: { promotion_id: promotion.id, promotion_action: { type: action_type } }
+ expect(assigns(:promotion_action).class).to eq(action_type.constantize)
+ end
+ end
+ end
+
+ describe 'POST #create' do
+ let(:action_type) { 'Spree::Promotion::Actions::CreateAdjustment' }
+ let(:action_params) do
+ {
+ type: action_type
+ }
+ end
+
+ it 'creates a new promotion action' do
+ expect {
+ post :create, params: { promotion_id: promotion.id, promotion_action: action_params }
+ }.to change(Spree::PromotionAction, :count).by(1)
+ end
+
+ it 'redirects to the promotion page' do
+ post :create, params: { promotion_id: promotion.id, promotion_action: action_params }
+ expect(response).to redirect_to(spree.admin_promotion_path(promotion))
+ end
+
+ context 'with invalid action type' do
+ let(:action_type) { 'InvalidActionType' }
+
+ it 'raises an error' do
+ expect {
+ post :create, params: { promotion_id: promotion.id, promotion_action: action_params }
+ }.to raise_error('Unknown promotion action type')
+ end
+ end
+ end
+
+ describe 'GET #edit' do
+ let!(:promotion_action) { create(:promotion_action_create_adjustment, promotion: promotion) }
+
+ it 'returns a successful response' do
+ get :edit, params: { promotion_id: promotion.id, id: promotion_action.id }
+ expect(response).to be_successful
+ end
+
+ it 'assigns the promotion action' do
+ get :edit, params: { promotion_id: promotion.id, id: promotion_action.id }
+ expect(assigns(:promotion_action)).to eq(promotion_action)
+ end
+ end
+
+ describe 'PATCH #update' do
+ let!(:promotion_action) { create(:promotion_action_create_adjustment, promotion: promotion) }
+ let(:calculator) { promotion_action.calculator }
+ let(:update_params) do
+ {
+ calculator_attributes: {
+ id: calculator.id,
+ preferred_amount: 20.0
+ }
+ }
+ end
+
+ it 'updates the promotion action' do
+ patch :update, params: { promotion_id: promotion.id, id: promotion_action.id, promotion_action: update_params }
+ calculator.reload
+ expect(calculator.preferred_amount).to eq(20.0)
+ end
+
+ it 'redirects to the promotion page' do
+ patch :update, params: { promotion_id: promotion.id, id: promotion_action.id, promotion_action: update_params }
+ expect(response).to redirect_to(spree.admin_promotion_path(promotion))
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ let!(:promotion_action) { create(:promotion_action_create_adjustment, promotion: promotion) }
+
+ it 'destroys the promotion action' do
+ expect {
+ delete :destroy, params: { promotion_id: promotion.id, id: promotion_action.id }
+ }.to change(Spree::PromotionAction, :count).by(-1)
+ end
+
+ it 'redirects to the promotion page' do
+ delete :destroy, params: { promotion_id: promotion.id, id: promotion_action.id }
+ expect(response).to redirect_to(spree.admin_promotion_path(promotion))
+ end
+ end
+
+ describe 'helper methods' do
+ it 'provides allowed_action_types' do
+ get :new, params: { promotion_id: promotion.id }
+ expect(controller.send(:allowed_action_types)).to eq(Rails.application.config.spree.promotions.actions)
+ end
+ end
+end
diff --git a/admin/spec/controllers/spree/admin/promotion_rules_controller_spec.rb b/admin/spec/controllers/spree/admin/promotion_rules_controller_spec.rb
new file mode 100644
index 0000000000..3f7ef0f35c
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/promotion_rules_controller_spec.rb
@@ -0,0 +1,123 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::PromotionRulesController, type: :controller do
+ stub_authorization!
+
+ render_views
+
+ let(:user) { create(:admin_user) }
+ let(:store) { Spree::Store.default }
+ let(:promotion) { create(:promotion, stores: [store]) }
+
+ before do
+ allow(controller).to receive(:current_ability).and_call_original
+ end
+
+ describe 'GET #new' do
+ it 'returns a successful response' do
+ get :new, params: { promotion_id: promotion.id }
+ expect(response).to be_successful
+ end
+
+ context 'when type is provided' do
+ let(:rule_type) { 'Spree::Promotion::Rules::Product' }
+
+ it 'returns a successful response' do
+ get :new, params: { promotion_id: promotion.id, promotion_rule: { type: rule_type } }
+ expect(response).to be_successful
+ end
+
+ it 'loads the correct rule type' do
+ get :new, params: { promotion_id: promotion.id, promotion_rule: { type: rule_type } }
+ expect(assigns(:promotion_rule).class).to eq(rule_type.constantize)
+ end
+ end
+ end
+
+ describe 'POST #create' do
+ let(:rule_type) { 'Spree::Promotion::Rules::Product' }
+ let(:rule_params) do
+ {
+ type: rule_type,
+ preferred_match_policy: 'any'
+ }
+ end
+
+ it 'creates a new promotion rule' do
+ expect {
+ post :create, params: { promotion_id: promotion.id, promotion_rule: rule_params }
+ }.to change(Spree::PromotionRule, :count).by(1)
+ end
+
+ it 'redirects to the promotion page' do
+ post :create, params: { promotion_id: promotion.id, promotion_rule: rule_params }
+ expect(response).to redirect_to(spree.admin_promotion_path(promotion))
+ end
+
+ context 'with invalid rule type' do
+ let(:rule_type) { 'InvalidRuleType' }
+
+ it 'raises an error' do
+ expect {
+ post :create, params: { promotion_id: promotion.id, promotion_rule: rule_params }
+ }.to raise_error('Unknown promotion rule type')
+ end
+ end
+ end
+
+ describe 'GET #edit' do
+ let!(:promotion_rule) { create(:promotion_rule_user, promotion: promotion) }
+
+ it 'returns a successful response' do
+ get :edit, params: { promotion_id: promotion.id, id: promotion_rule.id }
+ expect(response).to be_successful
+ end
+
+ it 'assigns the promotion rule' do
+ get :edit, params: { promotion_id: promotion.id, id: promotion_rule.id }
+ expect(assigns(:promotion_rule)).to eq(promotion_rule)
+ end
+ end
+
+ describe 'PATCH #update' do
+ let!(:promotion_rule) { create(:promotion_rule_user, promotion: promotion) }
+ let(:update_params) do
+ {
+ user_ids_to_add: [user.id]
+ }
+ end
+
+ it 'updates the promotion rule' do
+ patch :update, params: { promotion_id: promotion.id, id: promotion_rule.id, promotion_rule: update_params }
+ promotion_rule.reload
+ expect(promotion_rule.user_ids).to include(user.id)
+ end
+
+ it 'redirects to the promotion page' do
+ patch :update, params: { promotion_id: promotion.id, id: promotion_rule.id, promotion_rule: update_params }
+ expect(response).to redirect_to(spree.admin_promotion_path(promotion))
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ let!(:promotion_rule) { create(:promotion_rule, promotion: promotion) }
+
+ it 'destroys the promotion rule' do
+ expect {
+ delete :destroy, params: { promotion_id: promotion.id, id: promotion_rule.id }
+ }.to change(Spree::PromotionRule, :count).by(-1)
+ end
+
+ it 'redirects to the promotion page' do
+ delete :destroy, params: { promotion_id: promotion.id, id: promotion_rule.id }
+ expect(response).to redirect_to(spree.admin_promotion_path(promotion))
+ end
+ end
+
+ describe 'helper methods' do
+ it 'provides allowed_rule_types' do
+ get :new, params: { promotion_id: promotion.id }
+ expect(controller.send(:allowed_rule_types)).to eq(Rails.application.config.spree.promotions.rules)
+ end
+ end
+end
diff --git a/admin/spec/controllers/spree/admin/promotions_controller_spec.rb b/admin/spec/controllers/spree/admin/promotions_controller_spec.rb
new file mode 100644
index 0000000000..bc73f560ac
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/promotions_controller_spec.rb
@@ -0,0 +1,171 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::PromotionsController, type: :controller do
+ stub_authorization!
+
+ render_views
+
+ let(:user) { create(:admin_user) }
+ let(:store) { Spree::Store.default }
+
+ before do
+ allow(controller).to receive(:current_ability).and_call_original
+ end
+
+ describe 'GET #index' do
+ let!(:promotion) { create(:promotion, name: 'Test Promotion', stores: [store]) }
+
+ it 'returns a successful response' do
+ get :index
+ expect(response).to be_successful
+ end
+
+ it 'assigns promotions for the current store' do
+ get :index
+ expect(assigns[:collection]).to include(promotion)
+ end
+
+ context 'with active filter' do
+ let!(:active_promotion) { create(:promotion, name: 'Active Promotion', stores: [store], kind: :automatic) }
+ let!(:expired_promotion) { create(:promotion, name: 'Expired Promotion', stores: [store], starts_at: 3.day.ago, expires_at: 1.day.ago) }
+
+ it 'returns only active promotions' do
+ get :index, params: { q: { active: 'true' } }
+ expect(assigns[:collection]).to include(active_promotion)
+ expect(assigns[:collection]).not_to include(expired_promotion)
+ end
+ end
+
+ describe 'GET #show' do
+ let!(:promotion) { create(:promotion, name: 'Test Promotion', stores: [store]) }
+ let!(:promotion_action) { create(:promotion_action_create_adjustment) }
+ let!(:promotion_rule) { create(:promotion_rule_product, promotion: promotion) }
+
+ it 'returns a successful response' do
+ get :show, params: { id: promotion.id }
+ expect(response).to be_successful
+ end
+
+ it 'assigns the promotion' do
+ get :show, params: { id: promotion.id }
+ expect(assigns[:promotion]).to eq(promotion)
+ end
+ end
+
+ context 'with expired filter' do
+ let!(:active_promotion) { create(:promotion, name: 'Active Promotion', stores: [store]) }
+ let!(:expired_promotion) { create(:promotion, name: 'Expired Promotion', stores: [store], starts_at: 3.day.ago, expires_at: 1.day.ago) }
+
+ it 'returns only expired promotions' do
+ get :index, params: { q: { expired: 'true' } }
+ expect(assigns[:collection]).to include(expired_promotion)
+ expect(assigns[:collection]).not_to include(active_promotion)
+ end
+ end
+ end
+
+ describe 'GET #new' do
+ it 'returns a successful response' do
+ get :new
+ expect(response).to be_successful
+ end
+
+ it 'loads form data' do
+ get :new
+ expect(assigns[:promotion_rules]).not_to be_empty
+ expect(assigns[:rule_types]).not_to be_empty
+ end
+ end
+
+ describe 'POST #create' do
+ let(:promotion_params) do
+ {
+ name: 'New Promotion',
+ description: 'Promotion description',
+ code: 'PROMO123',
+ store_ids: [store.id]
+ }
+ end
+
+ it 'creates a new promotion' do
+ expect {
+ post :create, params: { promotion: promotion_params }
+ }.to change(Spree::Promotion, :count).by(1)
+ end
+
+ it 'redirects to the edit page' do
+ post :create, params: { promotion: promotion_params }
+ expect(response).to redirect_to(spree.admin_promotion_path(assigns[:promotion]))
+ end
+ end
+
+ describe 'GET #edit' do
+ let!(:promotion) { create(:promotion, stores: [store]) }
+
+ it 'returns a successful response' do
+ get :edit, params: { id: promotion.id }
+ expect(response).to be_successful
+ end
+
+ it 'loads form data' do
+ get :edit, params: { id: promotion.id }
+ expect(assigns[:promotion_rules]).not_to be_empty
+ expect(assigns[:rule_types]).not_to be_empty
+ end
+ end
+
+ describe 'PATCH #update' do
+ let!(:promotion) { create(:promotion, name: 'Old Name', stores: [store]) }
+ let(:promotion_params) { { name: 'Updated Name' } }
+
+ it 'updates the promotion' do
+ patch :update, params: { id: promotion.id, promotion: promotion_params }
+ expect(promotion.reload.name).to eq('Updated Name')
+ end
+
+ it 'redirects to the edit page' do
+ patch :update, params: { id: promotion.id, promotion: promotion_params }
+ expect(response).to redirect_to(spree.admin_promotion_path(promotion))
+ end
+ end
+
+ describe 'POST #clone' do
+ let!(:promotion) { create(:promotion, name: 'Test Promotion', stores: [store]) }
+
+ context 'when cloning succeeds' do
+ it 'creates a duplicate promotion' do
+ expect {
+ post :clone, params: { id: promotion.id }
+ }.to change(Spree::Promotion, :count).by(1)
+ end
+
+ it 'redirects to the new promotion' do
+ post :clone, params: { id: promotion.id }
+ expect(response).to redirect_to(spree.admin_promotion_path(assigns[:new_promo]))
+ end
+
+ it 'sets a success flash message' do
+ post :clone, params: { id: promotion.id }
+ expect(flash[:success]).to eq(Spree.t('promotion_cloned'))
+ end
+ end
+
+ context 'when cloning fails' do
+ before do
+ allow_any_instance_of(Spree::PromotionHandler::PromotionDuplicator).to receive(:duplicate).and_return(
+ double(errors: double(empty?: false, full_messages: ['Error message']), to_sentence: 'Error message')
+ )
+ end
+
+ it 'redirects to promotions index' do
+ post :clone, params: { id: promotion.id }
+ expect(response).to redirect_to(spree.admin_promotions_path)
+ end
+
+ it 'sets an error flash message' do
+ post :clone, params: { id: promotion.id }
+ expect(flash[:error]).to include('Error message')
+ end
+ end
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/promotion_action_factory.rb b/core/lib/spree/testing_support/factories/promotion_action_factory.rb
index 6d78bc86d8..22b6f70700 100644
--- a/core/lib/spree/testing_support/factories/promotion_action_factory.rb
+++ b/core/lib/spree/testing_support/factories/promotion_action_factory.rb
@@ -2,4 +2,9 @@ FactoryBot.define do
factory :promotion_action, class: Spree::PromotionAction do
association :promotion
end
+
+ factory :promotion_action_create_adjustment, class: Spree::Promotion::Actions::CreateAdjustment do
+ association :promotion
+ association :calculator, factory: :flat_rate_calculator
+ end
end