Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App configuration: Use SimpleOrderContents by default #5775

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions api/spec/requests/spree/api/orders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
class_name_attribute :order_contents_class, default: 'Spree::SimpleOrderContents'

# Allows providing your own class for shipping an order.
#
Expand Down
4 changes: 4 additions & 0 deletions core/spec/lib/spree/app_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 0 additions & 25 deletions core/spec/models/spree/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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! }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
50 changes: 50 additions & 0 deletions legacy_promotions/spec/requests/spree/api/orders_spec.rb
Original file line number Diff line number Diff line change
@@ -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: "[email protected]")
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