diff --git a/spec/controllers/admin/proxy_orders_controller_spec.rb b/spec/controllers/admin/proxy_orders_controller_spec.rb index d6a0c6522a..2d90227594 100644 --- a/spec/controllers/admin/proxy_orders_controller_spec.rb +++ b/spec/controllers/admin/proxy_orders_controller_spec.rb @@ -7,7 +7,7 @@ describe Admin::ProxyOrdersController, type: :controller do let!(:user) { create(:user, enterprise_limit: 10) } let!(:shop) { create(:distributor_enterprise) } let!(:order_cycle) { create(:simple_order_cycle, orders_close_at: 1.day.from_now) } - let!(:standing_order) { create(:standing_order_with_items, shop: shop) } + let!(:standing_order) { create(:standing_order, shop: shop, with_items: true) } let!(:proxy_order) { create(:proxy_order, standing_order: standing_order, order_cycle: order_cycle) } before do @@ -68,7 +68,7 @@ describe Admin::ProxyOrdersController, type: :controller do let!(:order_cycle) { create(:simple_order_cycle, orders_close_at: 1.day.from_now) } let!(:payment_method) { create(:payment_method) } let!(:shipping_method) { create(:shipping_method) } - let!(:standing_order) { create(:standing_order_with_items, shop: shop) } + let!(:standing_order) { create(:standing_order, shop: shop, with_items: true) } let!(:proxy_order) { create(:proxy_order, standing_order: standing_order, order_cycle: order_cycle) } let(:order) { proxy_order.order } diff --git a/spec/controllers/admin/standing_orders_controller_spec.rb b/spec/controllers/admin/standing_orders_controller_spec.rb index 6d665f565f..a4a98e1b76 100644 --- a/spec/controllers/admin/standing_orders_controller_spec.rb +++ b/spec/controllers/admin/standing_orders_controller_spec.rb @@ -377,7 +377,7 @@ describe Admin::StandingOrdersController, type: :controller do let!(:user) { create(:user, enterprise_limit: 10) } let!(:shop) { create(:distributor_enterprise) } let!(:order_cycle) { create(:simple_order_cycle, orders_close_at: 1.day.from_now) } - let!(:standing_order) { create(:standing_order_with_items, shop: shop) } + let!(:standing_order) { create(:standing_order, shop: shop, with_items: true) } let!(:proxy_order) { create(:proxy_order, standing_order: standing_order, order_cycle: order_cycle) } before do @@ -423,7 +423,7 @@ describe Admin::StandingOrdersController, type: :controller do describe 'pause' do let!(:user) { create(:user, enterprise_limit: 10) } let!(:shop) { create(:distributor_enterprise) } - let!(:standing_order) { create(:standing_order_with_items, shop: shop) } + let!(:standing_order) { create(:standing_order, shop: shop, with_items: true) } before do allow(controller).to receive(:spree_current_user) { user } @@ -468,7 +468,7 @@ describe Admin::StandingOrdersController, type: :controller do describe 'unpause' do let!(:user) { create(:user, enterprise_limit: 10) } let!(:shop) { create(:distributor_enterprise) } - let!(:standing_order) { create(:standing_order_with_items, shop: shop, paused_at: Time.zone.now) } + let!(:standing_order) { create(:standing_order, shop: shop, paused_at: Time.zone.now, with_items: true) } before do allow(controller).to receive(:spree_current_user) { user } diff --git a/spec/factories.rb b/spec/factories.rb index 3296913a9b..20a36f5102 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -147,15 +147,26 @@ FactoryGirl.define do payment_method { create(:payment_method, distributors: [shop]) } shipping_method { create(:shipping_method, distributors: [shop]) } begins_at { 1.month.ago } - end - factory :standing_order_with_items, parent: :standing_order do |standing_order| + ignore do + with_items false + with_orders false + end + after(:create) do |standing_order, proxy| - standing_order.standing_line_items = build_list(:standing_line_item, 3, standing_order: standing_order) - standing_order.schedule.order_cycles.each do |oc| - ex = oc.exchanges.outgoing.find_by_sender_id_and_receiver_id(standing_order.shop_id, standing_order.shop_id) || - create(:exchange, :order_cycle => oc, :sender => standing_order.shop, :receiver => standing_order.shop, :incoming => false, :pickup_time => 'time', :pickup_instructions => 'instructions') - standing_order.standing_line_items.each { |sli| ex.variants << sli.variant } + if proxy.with_items + standing_order.standing_line_items = build_list(:standing_line_item, 3, standing_order: standing_order) + standing_order.order_cycles.each do |oc| + ex = oc.exchanges.outgoing.find_by_sender_id_and_receiver_id(standing_order.shop_id, standing_order.shop_id) || + create(:exchange, :order_cycle => oc, :sender => standing_order.shop, :receiver => standing_order.shop, :incoming => false, :pickup_time => 'time', :pickup_instructions => 'instructions') + standing_order.standing_line_items.each { |sli| ex.variants << sli.variant } + end + end + + if proxy.with_orders + standing_order.order_cycles.each do |oc| + standing_order.proxy_orders << create(:proxy_order, standing_order: standing_order, order_cycle: oc) + end end end end @@ -168,11 +179,10 @@ FactoryGirl.define do factory :proxy_order, :class => ProxyOrder do standing_order - order - order_cycle - after(:create) do |proxy_order, proxy| - proxy_order.order.order_cycle = proxy_order.order_cycle - proxy_order.order.save! + order_cycle { standing_order.order_cycles.first } + before(:create) do |proxy_order, proxy| + proxy_order.initialise_order! unless proxy_order.order + proxy_order.order.update_attribute(:order_cycle_id, proxy_order.order_cycle_id) end end diff --git a/spec/features/admin/standing_orders_spec.rb b/spec/features/admin/standing_orders_spec.rb index c9fa35ab0a..3ebe750471 100644 --- a/spec/features/admin/standing_orders_spec.rb +++ b/spec/features/admin/standing_orders_spec.rb @@ -13,16 +13,9 @@ feature 'Standing Orders' do before { quick_login_as user } context 'listing standing orders' do - let!(:standing_order) { create(:standing_order_with_items, shop: shop) } - let!(:standing_order2) { create(:standing_order_with_items, shop: shop2) } - let!(:standing_order_unmanaged) { create(:standing_order_with_items, shop: shop_unmanaged) } - - before do - # initialise standing orders - StandingOrderForm.new(standing_order).send(:initialise_proxy_orders!) - StandingOrderForm.new(standing_order2).send(:initialise_proxy_orders!) - StandingOrderForm.new(standing_order_unmanaged).send(:initialise_proxy_orders!) - end + let!(:standing_order) { create(:standing_order, shop: shop, with_items: true, with_orders: true) } + let!(:standing_order2) { create(:standing_order, shop: shop2, with_items: true, with_orders: true) } + let!(:standing_order_unmanaged) { create(:standing_order, shop: shop_unmanaged, with_items: true, with_orders: true) } it "passes the smoke test" do visit spree.admin_path diff --git a/spec/forms/standing_order_form_spec.rb b/spec/forms/standing_order_form_spec.rb index ad5a304214..e1c3b3af26 100644 --- a/spec/forms/standing_order_form_spec.rb +++ b/spec/forms/standing_order_form_spec.rb @@ -88,7 +88,7 @@ describe StandingOrderForm do end describe "making a change that causes an error" do - let(:standing_order) { create(:standing_order_with_items) } + let(:standing_order) { create(:standing_order, with_items: true, with_orders: true) } let(:shipping_method) { standing_order.shipping_method } let(:invalid_shipping_method) { create(:shipping_method, distributors: [create(:enterprise)]) } let(:order) { standing_order.orders.first } @@ -96,7 +96,6 @@ describe StandingOrderForm do let(:form) { StandingOrderForm.new(standing_order, params) } before do - form.send(:initialise_proxy_orders!) form.save end @@ -108,7 +107,7 @@ describe StandingOrderForm do end describe "changing the shipping method" do - let(:standing_order) { create(:standing_order_with_items) } + let(:standing_order) { create(:standing_order, with_items: true, with_orders: true) } let(:shipping_method) { standing_order.shipping_method } let(:new_shipping_method) { create(:shipping_method, distributors: [standing_order.shop]) } let(:order) { standing_order.orders.first } @@ -116,8 +115,6 @@ describe StandingOrderForm do let(:form) { StandingOrderForm.new(standing_order, params) } context "when the shipping method on an order is the same as the standing order" do - before { form.send(:initialise_proxy_orders!) } - it "updates the shipping_method on the order and on shipments" do expect(order.shipments.first.shipping_method).to eq shipping_method form.save @@ -130,7 +127,6 @@ describe StandingOrderForm do let(:changed_shipping_method) { create(:shipping_method) } before do - form.send(:initialise_proxy_orders!) # Updating the shipping method on a shipment updates the shipping method on the order, # and vice-versa via logic in Spree's shipments controller. So updating both here mimics that # behaviour. @@ -147,7 +143,7 @@ describe StandingOrderForm do end describe "changing the payment method" do - let(:standing_order) { create(:standing_order_with_items) } + let(:standing_order) { create(:standing_order, with_items: true, with_orders: true) } let(:order) { standing_order.orders.first } let(:payment_method) { standing_order.payment_method } let(:new_payment_method) { create(:payment_method, distributors: [standing_order.shop]) } @@ -155,8 +151,6 @@ describe StandingOrderForm do let(:form) { StandingOrderForm.new(standing_order, params) } context "when the payment method on an order is the same as the standing order" do - before { form.send(:initialise_proxy_orders!) } - it "voids existing payments and creates a new payment with the relevant payment method" do expect(order.payments.reload.first.payment_method).to eq payment_method form.save @@ -173,7 +167,6 @@ describe StandingOrderForm do let(:changed_payment_method) { create(:payment_method) } before do - form.send(:initialise_proxy_orders!) order.payments.first.update_attribute(:payment_method_id, changed_payment_method.id) form.save end @@ -187,12 +180,10 @@ describe StandingOrderForm do end describe "changing begins_at" do - let(:standing_order) { create(:standing_order_with_items, begins_at: Time.zone.now) } + let(:standing_order) { create(:standing_order, begins_at: Time.zone.now, with_items: true, with_orders: true) } let(:params) { { begins_at: 1.year.from_now, ends_at: 2.years.from_now } } let(:form) { StandingOrderForm.new(standing_order, params) } - before { form.send(:initialise_proxy_orders!) } - it "removes orders outside the newly specified date range" do expect(standing_order.reload.orders.count).to be 1 form.save @@ -204,7 +195,7 @@ describe StandingOrderForm do end describe "changing the quantity of a line item" do - let(:standing_order) { create(:standing_order_with_items) } + let(:standing_order) { create(:standing_order, with_items: true, with_orders: true) } let(:sli) { standing_order.standing_line_items.first } let(:variant) { sli.variant } @@ -213,7 +204,6 @@ describe StandingOrderForm do context "when quantity is less than available stock" do let(:params) { { standing_line_items_attributes: [ { id: sli.id, quantity: 2} ] } } let(:form) { StandingOrderForm.new(standing_order, params) } - before { form.send(:initialise_proxy_orders!) } it "updates the line_item quantities and totals on all orders" do expect(standing_order.orders.first.reload.total.to_f).to eq 59.97 @@ -227,7 +217,6 @@ describe StandingOrderForm do context "when quantity is greater than available stock" do let(:params) { { standing_line_items_attributes: [ { id: sli.id, quantity: 3} ] } } let(:form) { StandingOrderForm.new(standing_order, params) } - before { form.send(:initialise_proxy_orders!) } it "updates the line_item quantities and totals on all orders" do expect(standing_order.orders.first.reload.total.to_f).to eq 59.97 @@ -240,7 +229,7 @@ describe StandingOrderForm do end describe "adding a new line item" do - let!(:standing_order) { create(:standing_order_with_items) } + let!(:standing_order) { create(:standing_order, with_items: true, with_orders: true) } let!(:variant) { create(:variant) } let!(:order_cycle) { standing_order.schedule.order_cycles.first } let!(:params) { { standing_line_items_attributes: [ { id: nil, variant_id: variant.id, quantity: 1} ] } } @@ -248,7 +237,6 @@ describe StandingOrderForm do before do order_cycle.variants << variant - form.send(:initialise_proxy_orders!) end it "add the line item and updates the total on all orders" do @@ -261,14 +249,12 @@ describe StandingOrderForm do end describe "removing an existing line item" do - let(:standing_order) { create(:standing_order_with_items) } + let(:standing_order) { create(:standing_order, with_items: true, with_orders: true) } let(:sli) { standing_order.standing_line_items.first } let(:variant) { sli.variant} let(:params) { { standing_line_items_attributes: [ { id: sli.id, _destroy: true } ] } } let(:form) { StandingOrderForm.new(standing_order, params) } - before { form.send(:initialise_proxy_orders!) } - it "removes the line item and updates totals on all orders" do expect(standing_order.orders.first.reload.total.to_f).to eq 59.97 form.save diff --git a/spec/jobs/standing_order_confirm_job_spec.rb b/spec/jobs/standing_order_confirm_job_spec.rb index 0543fbcc60..dadd13bfc1 100644 --- a/spec/jobs/standing_order_confirm_job_spec.rb +++ b/spec/jobs/standing_order_confirm_job_spec.rb @@ -5,7 +5,7 @@ describe StandingOrderConfirmJob do let(:order_cycle1) { create(:simple_order_cycle, coordinator: shop) } let(:order_cycle2) { create(:simple_order_cycle, coordinator: shop) } let(:schedule1) { create(:schedule, order_cycles: [order_cycle1, order_cycle2]) } - let(:standing_order1) { create(:standing_order_with_items, shop: shop, schedule: schedule1) } + let(:standing_order1) { create(:standing_order, shop: shop, schedule: schedule1, with_items: true) } let!(:job) { StandingOrderConfirmJob.new(order_cycle1) } describe "finding standing_order orders for the specified order cycle" do @@ -27,11 +27,10 @@ describe StandingOrderConfirmJob do end describe "processing an order" do - let(:order) { standing_order1.orders.first } + let(:proxy_order) { create(:proxy_order, standing_order: standing_order1) } + let(:order) { proxy_order.order } before do - form = StandingOrderForm.new(standing_order1) - form.send(:initialise_proxy_orders!) while !order.completed? do break unless order.next! end allow(job).to receive(:send_confirm_email).and_call_original Spree::MailMethod.create!( diff --git a/spec/jobs/standing_order_placement_job_spec.rb b/spec/jobs/standing_order_placement_job_spec.rb index cf507c0e57..43f3d985ca 100644 --- a/spec/jobs/standing_order_placement_job_spec.rb +++ b/spec/jobs/standing_order_placement_job_spec.rb @@ -1,27 +1,25 @@ require 'spec_helper' describe StandingOrderPlacementJob do - let(:shop) { create(:distributor_enterprise) } - let(:order_cycle1) { create(:simple_order_cycle, coordinator: shop) } - let(:order_cycle2) { create(:simple_order_cycle, coordinator: shop) } - let(:schedule1) { create(:schedule, order_cycles: [order_cycle1, order_cycle2]) } - let(:standing_order1) { create(:standing_order_with_items, shop: shop, schedule: schedule1) } - let(:standing_order2) { create(:standing_order_with_items, shop: shop, schedule: schedule1, paused_at: 1.minute.ago) } - let(:standing_order3) { create(:standing_order_with_items, shop: shop, schedule: schedule1, canceled_at: 1.minute.ago) } - let(:standing_order4) { create(:standing_order_with_items, shop: shop, schedule: schedule1, begins_at: 1.minute.from_now) } - let(:standing_order5) { create(:standing_order_with_items, shop: shop, schedule: schedule1, ends_at: 1.minute.ago) } - - let!(:job) { StandingOrderPlacementJob.new(order_cycle1) } describe "finding standing_order orders for the specified order cycle" do - let(:order1) { create(:order, completed_at: 5.minutes.ago) } # Complete + Linked + OC Matches - let(:order2) { create(:order) } # Incomplete + Not-Linked + OC Matches - let(:order3) { create(:order) } # Incomplete + Linked + OC Mismatch - let(:order4) { create(:order) } # Incomplete + Linked + OC Matches + Paused - let(:order5) { create(:order) } # Incomplete + Linked + OC Matches + Cancelled - let(:order6) { create(:order) } # Incomplete + Linked + OC Matches + Yet To Begin - let(:order7) { create(:order) } # Incomplete + Linked + OC Matches + Ended - let(:order8) { create(:order) } # Incomplete + Linked + OC Matches + let(:shop) { create(:distributor_enterprise) } + let(:order_cycle1) { create(:simple_order_cycle, coordinator: shop) } + let(:order_cycle2) { create(:simple_order_cycle, coordinator: shop) } + let(:schedule) { create(:schedule, order_cycles: [order_cycle1, order_cycle2]) } + let(:standing_order1) { create(:standing_order, shop: shop, schedule: schedule) } + let(:standing_order2) { create(:standing_order, shop: shop, schedule: schedule, paused_at: 1.minute.ago) } + let(:standing_order3) { create(:standing_order, shop: shop, schedule: schedule, canceled_at: 1.minute.ago) } + let(:standing_order4) { create(:standing_order, shop: shop, schedule: schedule, begins_at: 1.minute.from_now) } + let(:standing_order5) { create(:standing_order, shop: shop, schedule: schedule, ends_at: 1.minute.ago) } + let!(:order1) { create(:order, completed_at: 5.minutes.ago) } # Complete + Linked + OC Matches + let!(:order2) { create(:order) } # Incomplete + Not-Linked + OC Matches + let!(:order3) { create(:order) } # Incomplete + Linked + OC Mismatch + let!(:order4) { create(:order) } # Incomplete + Linked + OC Matches + Paused + let!(:order5) { create(:order) } # Incomplete + Linked + OC Matches + Cancelled + let!(:order6) { create(:order) } # Incomplete + Linked + OC Matches + Yet To Begin + let!(:order7) { create(:order) } # Incomplete + Linked + OC Matches + Ended + let!(:order8) { create(:order) } # Incomplete + Linked + OC Matches let!(:proxy_order1) { create(:proxy_order, standing_order: standing_order1, order: order1, order_cycle: order_cycle1) } let!(:proxy_order3) { create(:proxy_order, standing_order: standing_order1, order: order3, order_cycle: order_cycle2) } let!(:proxy_order4) { create(:proxy_order, standing_order: standing_order2, order: order4, order_cycle: order_cycle1) } @@ -30,6 +28,8 @@ describe StandingOrderPlacementJob do let!(:proxy_order7) { create(:proxy_order, standing_order: standing_order5, order: order7, order_cycle: order_cycle1) } let!(:proxy_order8) { create(:proxy_order, standing_order: standing_order1, order: order8, order_cycle: order_cycle1) } + let!(:job) { StandingOrderPlacementJob.new(order_cycle1) } + it "only returns incomplete orders in the relevant order cycle that are linked to a standing order" do orders = job.send(:orders) expect(orders).to include order8 @@ -38,7 +38,8 @@ describe StandingOrderPlacementJob do end describe "processing an order containing items with insufficient stock" do - let(:order) { create(:order, order_cycle: order_cycle1) } + let(:order_cycle) { create(:simple_order_cycle) } + let(:order) { create(:order, order_cycle: order_cycle) } let(:variant1) { create(:variant, count_on_hand: 5) } let(:variant2) { create(:variant, count_on_hand: 2) } let(:variant3) { create(:variant, count_on_hand: 0) } @@ -46,6 +47,8 @@ describe StandingOrderPlacementJob do let(:line_item2) { create(:line_item, order: order, variant: variant2, quantity: 2) } let(:line_item3) { create(:line_item, order: order, variant: variant3, quantity: 0) } + let!(:job) { StandingOrderPlacementJob.new(order_cycle) } + before do Spree::Config.set(:allow_backorders, false) line_item1.update_attribute(:quantity, 3) @@ -64,11 +67,14 @@ describe StandingOrderPlacementJob do end describe "processing a standing order order" do + let(:standing_order) { create(:standing_order, with_orders: true, with_items: true) } + let(:proxy_order) { standing_order.proxy_orders.first } + let!(:order) { proxy_order.order } let(:changes) { {} } + let!(:job) { StandingOrderPlacementJob.new(proxy_order.order_cycle) } + before do - form = StandingOrderForm.new(standing_order1) - form.send(:initialise_proxy_orders!) expect_any_instance_of(Spree::Payment).to_not receive(:process!) allow(job).to receive(:cap_quantity_and_store_changes) { changes } allow(job).to receive(:send_placement_email).and_call_original @@ -77,7 +83,6 @@ describe StandingOrderPlacementJob do it "moves orders to completion, but does not process the payment" do # If this spec starts complaining about no shipping methods being available # on CI, there is probably another spec resetting the currency though Rails.cache.clear - order = standing_order1.orders.first ActionMailer::Base.deliveries.clear expect{job.send(:process, order)}.to change{order.reload.completed_at}.from(nil) expect(order.completed_at).to be_within(5.seconds).of Time.now @@ -85,7 +90,6 @@ describe StandingOrderPlacementJob do end it "sends only a placement email, no confirmation emails" do - order = standing_order1.orders.first ActionMailer::Base.deliveries.clear expect{job.send(:process, order)}.to_not enqueue_job ConfirmOrderJob expect(job).to have_received(:send_placement_email).with(order, changes).once diff --git a/spec/jobs/standing_order_sync_job_spec.rb b/spec/jobs/standing_order_sync_job_spec.rb index bcac9be1fc..8f87af7042 100644 --- a/spec/jobs/standing_order_sync_job_spec.rb +++ b/spec/jobs/standing_order_sync_job_spec.rb @@ -9,11 +9,11 @@ describe StandingOrderSyncJob do let(:job) { StandingOrderSyncJob.new(schedule1) } describe "finding standing_orders for the specified schedule" do - let!(:standing_order1) { create(:standing_order_with_items, shop: shop, schedule: schedule1) } - let!(:standing_order2) { create(:standing_order_with_items, shop: shop, schedule: schedule1, paused_at: 1.minute.ago) } - let!(:standing_order3) { create(:standing_order_with_items, shop: shop, schedule: schedule1, canceled_at: 1.minute.ago) } - let!(:standing_order4) { create(:standing_order_with_items, shop: shop, schedule: schedule1, begins_at: 1.minute.from_now) } - let!(:standing_order5) { create(:standing_order_with_items, shop: shop, schedule: schedule1, ends_at: 1.minute.ago) } + let!(:standing_order1) { create(:standing_order, with_items: true, shop: shop, schedule: schedule1) } + let!(:standing_order2) { create(:standing_order, with_items: true, shop: shop, schedule: schedule1, paused_at: 1.minute.ago) } + let!(:standing_order3) { create(:standing_order, with_items: true, shop: shop, schedule: schedule1, canceled_at: 1.minute.ago) } + let!(:standing_order4) { create(:standing_order, with_items: true, shop: shop, schedule: schedule1, begins_at: 1.minute.from_now) } + let!(:standing_order5) { create(:standing_order, with_items: true, shop: shop, schedule: schedule1, ends_at: 1.minute.ago) } it "only returns incomplete orders in the relevant order cycle that are linked to a standing order" do standing_orders = job.send(:standing_orders) diff --git a/spec/models/proxy_order_spec.rb b/spec/models/proxy_order_spec.rb index 766273c565..aaebaf981a 100644 --- a/spec/models/proxy_order_spec.rb +++ b/spec/models/proxy_order_spec.rb @@ -46,11 +46,10 @@ describe ProxyOrder, type: :model do end describe "resume" do - let(:order_cycle) { create(:simple_order_cycle) } let!(:payment_method) { create(:payment_method) } - let(:standing_order) { create(:standing_order) } let(:order) { create(:order_with_totals, shipping_method: create(:shipping_method)) } - let(:proxy_order) { create(:proxy_order, standing_order: standing_order, order: order, order_cycle: order_cycle) } + let(:proxy_order) { create(:proxy_order, order: order) } + let(:order_cycle) { proxy_order.order_cycle} before do @@ -105,7 +104,7 @@ describe ProxyOrder, type: :model do end describe "initialising an the order" do - let(:standing_order) { create(:standing_order_with_items) } + let(:standing_order) { create(:standing_order, with_items: true) } let!(:proxy_order) { create(:proxy_order, standing_order: standing_order) } it "builds a new order based the standing order" do diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index f053de5dc5..96adc3880c 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -574,7 +574,7 @@ describe Spree::Order do end it "does not send confirmation emails when the order belongs to a standing order" do - create(:standing_order_with_items, orders: [order]) + create(:proxy_order, order: order) expect do order.deliver_order_confirmation_email