diff --git a/api/spec/requests/spree/api/orders_spec.rb b/api/spec/requests/spree/api/orders_spec.rb index 4ca347a2ab..4d3a541022 100644 --- a/api/spec/requests/spree/api/orders_spec.rb +++ b/api/spec/requests/spree/api/orders_spec.rb @@ -77,20 +77,6 @@ module Spree::Api end end end - - context "with existing promotion" do - let(:discount) { 2 } - before do - create(:promotion, :with_line_item_adjustment, apply_automatically: true, adjustment_rate: discount ) - end - - it "activates the promotion" do - post spree.api_orders_path, params: { order: { line_items: { "0" => { variant_id: variant.to_param, quantity: 1 } } } } - order = Spree::Order.last - line_item = order.line_items.first - expect(order.total).to eq(line_item.price - discount) - end - end end context "when the current user can administrate the order" do diff --git a/backend/spec/controllers/spree/admin/orders_controller_spec.rb b/backend/spec/controllers/spree/admin/orders_controller_spec.rb index 491c2b9d52..a9b4028198 100644 --- a/backend/spec/controllers/spree/admin/orders_controller_spec.rb +++ b/backend/spec/controllers/spree/admin/orders_controller_spec.rb @@ -27,7 +27,7 @@ before do allow(Spree::Order).to receive_message_chain(:includes, find_by!: order) - allow(order).to receive_messages(contents: Spree::OrderContents.new(order)) + allow(order).to receive_messages(contents: Spree::Config.order_contents_class.new(order)) end context "#approve" do diff --git a/core/lib/spree/app_configuration.rb b/core/lib/spree/app_configuration.rb index b10ddde992..37d48e16e2 100644 --- a/core/lib/spree/app_configuration.rb +++ b/core/lib/spree/app_configuration.rb @@ -394,7 +394,7 @@ def default_pricing_options # @!attribute [rw] order_contents_class # @return [Class] a class with the same public interfaces as # Spree::OrderContents. - class_name_attribute :order_contents_class, default: 'Spree::OrderContents' + class_name_attribute :order_contents_class, default: 'Spree::SimpleOrderContents' # Allows providing your own class for shipping an order. # diff --git a/core/spec/lib/spree/app_configuration_spec.rb b/core/spec/lib/spree/app_configuration_spec.rb index 7ffc0e84d6..6770b9c556 100644 --- a/core/spec/lib/spree/app_configuration_spec.rb +++ b/core/spec/lib/spree/app_configuration_spec.rb @@ -36,6 +36,10 @@ expect(prefs.variant_search_class).to eq Spree::Core::Search::Variant end + it "uses simple order contents class by default" do + expect(prefs.order_contents_class).to eq Spree::SimpleOrderContents + end + it "uses variant price selector class by default" do expect(prefs.variant_price_selector_class).to eq Spree::Variant::PriceSelector end diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb index 449e06730b..c4b696192f 100644 --- a/core/spec/models/spree/promotion_spec.rb +++ b/core/spec/models/spree/promotion_spec.rb @@ -915,31 +915,6 @@ end end - describe "adding items to the cart" do - let(:order) { create :order } - let(:line_item) { create :line_item, order: order } - let(:promo) { create :promotion_with_item_adjustment, adjustment_rate: 5, code: 'promo' } - let(:promotion_code) { promo.codes.first } - let(:variant) { create :variant } - - it "updates the promotions for new line items" do - expect(line_item.adjustments).to be_empty - expect(order.adjustment_total).to eq 0 - - promo.activate order: order, promotion_code: promotion_code - order.recalculate - - expect(line_item.adjustments.size).to eq(1) - expect(order.adjustment_total).to eq(-5) - - other_line_item = order.contents.add(variant, 1, currency: order.currency) - - expect(other_line_item).not_to eq line_item - expect(other_line_item.adjustments.size).to eq(1) - expect(order.adjustment_total).to eq(-10) - end - end - describe "promotion deletion" do subject { promotion.destroy! } diff --git a/core/app/models/spree/order_contents.rb b/legacy_promotions/app/models/spree/order_contents.rb similarity index 100% rename from core/app/models/spree/order_contents.rb rename to legacy_promotions/app/models/spree/order_contents.rb diff --git a/core/spec/models/spree/order_contents_spec.rb b/legacy_promotions/spec/models/spree/order_contents_spec.rb similarity index 100% rename from core/spec/models/spree/order_contents_spec.rb rename to legacy_promotions/spec/models/spree/order_contents_spec.rb diff --git a/legacy_promotions/spec/models/spree/promotion_integration_spec.rb b/legacy_promotions/spec/models/spree/promotion_integration_spec.rb index 90ad8f194d..6ed19aba4c 100644 --- a/legacy_promotions/spec/models/spree/promotion_integration_spec.rb +++ b/legacy_promotions/spec/models/spree/promotion_integration_spec.rb @@ -181,4 +181,29 @@ }.to change{ order.reload.state }.from("confirm").to("address") end end + + describe "adding items to the cart" do + let(:order) { create :order } + let(:line_item) { create :line_item, order: order } + let(:promo) { create :promotion_with_item_adjustment, adjustment_rate: 5, code: 'promo' } + let(:promotion_code) { promo.codes.first } + let(:variant) { create :variant } + + it "updates the promotions for new line items" do + expect(line_item.adjustments).to be_empty + expect(order.adjustment_total).to eq 0 + + promo.activate order: order, promotion_code: promotion_code + order.recalculate + + expect(line_item.adjustments.size).to eq(1) + expect(order.adjustment_total).to eq(-5) + + other_line_item = order.contents.add(variant, 1, currency: order.currency) + + expect(other_line_item).not_to eq line_item + expect(other_line_item.adjustments.size).to eq(1) + expect(order.adjustment_total).to eq(-10) + end + end end diff --git a/legacy_promotions/spec/requests/spree/api/orders_spec.rb b/legacy_promotions/spec/requests/spree/api/orders_spec.rb new file mode 100644 index 0000000000..3fc4f700f4 --- /dev/null +++ b/legacy_promotions/spec/requests/spree/api/orders_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'API Orders', type: :request do + let!(:order) { create(:order) } + let(:variant) { create(:variant) } + let(:line_item) { create(:line_item) } + let(:address_params) { { country_id: Country.first.id, state_id: State.first.id } } + + let(:current_api_user) do + user = Spree.user_class.new(email: "solidus@example.com") + user.generate_spree_api_key! + user + end + + before do + stub_authentication! + end + + describe "POST create" do + let(:target_user) { create :user } + let(:attributes) { { user_id: target_user.id, email: target_user.email } } + + subject do + post spree.api_orders_path, params: { order: attributes } + response + end + + context "when the current user cannot administrate the order" do + custom_authorization! do |_| + can :create, Spree::Order + end + + context "with existing promotion" do + let(:discount) { 2 } + before do + create(:promotion, :with_line_item_adjustment, apply_automatically: true, adjustment_rate: discount ) + end + + it "activates the promotion" do + post spree.api_orders_path, params: { order: { line_items: { "0" => { variant_id: variant.to_param, quantity: 1 } } } } + order = Spree::Order.last + line_item = order.line_items.first + expect(order.total).to eq(line_item.price - discount) + end + end + end + end +end