diff --git a/app/controllers/spree/admin/shipping_methods_controller.rb b/app/controllers/spree/admin/shipping_methods_controller.rb index a8c5dbb1e13..fe3bdb5d864 100644 --- a/app/controllers/spree/admin/shipping_methods_controller.rb +++ b/app/controllers/spree/admin/shipping_methods_controller.rb @@ -86,7 +86,12 @@ def permitted_resource_params params.require(:shipping_method).permit( :name, :description, :display_on, :require_ship_address, :tag_list, :calculator_type, - distributor_ids: [] + distributor_ids: [], + calculator_attributes: [ + :id, :preferred_currency, :preferred_amount, :preferred_per_kg, :preferred_flat_percent, + :preferred_first_item, :preferred_additional_item, :preferred_max_items, + :preferred_minimal_amount, :preferred_normal_amount, :preferred_discount_amount + ] ) end end diff --git a/spec/controllers/spree/admin/shipping_methods_controller_spec.rb b/spec/controllers/spree/admin/shipping_methods_controller_spec.rb index d705972c877..531d01c11e3 100644 --- a/spec/controllers/spree/admin/shipping_methods_controller_spec.rb +++ b/spec/controllers/spree/admin/shipping_methods_controller_spec.rb @@ -3,29 +3,104 @@ describe Spree::Admin::ShippingMethodsController, type: :controller do include AuthenticationWorkflow - describe "shipping method not referenced by order" do + describe "#update" do let(:shipping_method) { create(:shipping_method) } + let(:params) { + { + id: shipping_method.id, + shipping_method: { + calculator_attributes: { + id: shipping_method.calculator.id + } + } + } + } - scenario "is soft deleted" do - login_as_admin - expect(shipping_method.deleted_at).to be_nil + before { login_as_admin } - spree_delete :destroy, "id" => shipping_method.id + it "updates preferred_amount and preferred_currency of a FlatRate calculator" do + shipping_method.calculator = create(:calculator_flat_rate, calculable: shipping_method) + params[:shipping_method][:calculator_attributes][:preferred_amount] = 123 + params[:shipping_method][:calculator_attributes][:preferred_currency] = "EUR" - expect(shipping_method.reload.deleted_at).not_to be_nil + spree_post :update, params + + expect(shipping_method.reload.calculator.preferred_amount).to eq 123 + expect(shipping_method.reload.calculator.preferred_currency).to eq "EUR" + end + + it "updates preferred_per_kg of a Weight calculator" do + shipping_method.calculator = create(:weight_calculator, calculable: shipping_method) + params[:shipping_method][:calculator_attributes][:preferred_per_kg] = 10 + + spree_post :update, params + + expect(shipping_method.reload.calculator.preferred_per_kg).to eq 10 + end + + it "updates preferred_flat_percent of a FlatPercentPerItem calculator" do + shipping_method.calculator = Calculator::FlatPercentPerItem.new(preferred_flat_percent: 20, + + calculable: shipping_method) + params[:shipping_method][:calculator_attributes][:preferred_flat_percent] = 30 + + spree_post :update, params + + expect(shipping_method.reload.calculator.preferred_flat_percent).to eq 30 + end + + it "updates details of a FlexiRate calculator" do + shipping_method.calculator = Spree::Calculator::FlexiRate.new(calculable: shipping_method) + params[:shipping_method][:calculator_attributes][:preferred_first_item] = 10 + params[:shipping_method][:calculator_attributes][:preferred_additional_item] = 20 + params[:shipping_method][:calculator_attributes][:preferred_max_items] = 30 + + spree_post :update, params + + expect(shipping_method.reload.calculator.preferred_first_item).to eq 10 + expect(shipping_method.reload.calculator.preferred_additional_item).to eq 20 + expect(shipping_method.reload.calculator.preferred_max_items).to eq 30 + end + + it "updates details of a PriceSack calculator" do + shipping_method.calculator = Spree::Calculator::PriceSack.new(calculable: shipping_method) + params[:shipping_method][:calculator_attributes][:preferred_minimal_amount] = 10 + params[:shipping_method][:calculator_attributes][:preferred_normal_amount] = 20 + params[:shipping_method][:calculator_attributes][:preferred_discount_amount] = 30 + + spree_post :update, params + + expect(shipping_method.reload.calculator.preferred_minimal_amount).to eq 10 + expect(shipping_method.reload.calculator.preferred_normal_amount).to eq 20 + expect(shipping_method.reload.calculator.preferred_discount_amount).to eq 30 end end - describe "shipping method referenced by order" do - let(:order) { create(:order_with_line_items) } + describe "#delete" do + describe "shipping method not referenced by order" do + let(:shipping_method) { create(:shipping_method) } + + scenario "is soft deleted" do + login_as_admin + expect(shipping_method.deleted_at).to be_nil + + spree_delete :destroy, "id" => shipping_method.id + + expect(shipping_method.reload.deleted_at).not_to be_nil + end + end + + describe "shipping method referenced by order" do + let(:order) { create(:order_with_line_items) } - scenario "is not soft deleted" do - login_as_admin - expect(order.shipping_method.deleted_at).to be_nil + scenario "is not soft deleted" do + login_as_admin + expect(order.shipping_method.deleted_at).to be_nil - spree_delete :destroy, "id" => order.shipping_method.id + spree_delete :destroy, "id" => order.shipping_method.id - expect(order.shipping_method.reload.deleted_at).to be_nil + expect(order.shipping_method.reload.deleted_at).to be_nil + end end end end