diff --git a/spec/controllers/enterprises_controller_spec.rb b/spec/controllers/enterprises_controller_spec.rb index c9ee7b4437..13f3209436 100644 --- a/spec/controllers/enterprises_controller_spec.rb +++ b/spec/controllers/enterprises_controller_spec.rb @@ -9,13 +9,17 @@ describe EnterprisesController, type: :controller do let(:line_item) { create(:line_item) } let!(:current_distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) } let!(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) } + let!(:shipping_method) { distributor.shipping_methods.first } let!(:order_cycle1) { create(:simple_order_cycle, distributors: [distributor], orders_open_at: 2.days.ago, - orders_close_at: 3.days.from_now, variants: [line_item.variant] ) + orders_close_at: 3.days.from_now, + shipping_methods: [shipping_method], + variants: [line_item.variant] ) } let!(:order_cycle2) { create(:simple_order_cycle, distributors: [distributor], orders_open_at: 3.days.ago, - orders_close_at: 4.days.from_now ) + orders_close_at: 4.days.from_now, + shipping_methods: [shipping_method]) } before do @@ -55,8 +59,10 @@ describe EnterprisesController, type: :controller do context "using FilterOrderCycles tag rules" do let!(:order_cycle3) { - create(:simple_order_cycle, distributors: [distributor], orders_open_at: 3.days.ago, - orders_close_at: 4.days.from_now) + create(:simple_order_cycle, distributors: [distributor], + orders_open_at: 3.days.ago, + orders_close_at: 4.days.from_now, + shipping_methods: [shipping_method]) } let!(:oc3_exchange) { order_cycle3.exchanges.outgoing.to_enterprise(distributor).first } let(:customer) { create(:customer, user: user, enterprise: distributor) } @@ -116,7 +122,10 @@ describe EnterprisesController, type: :controller do let(:variant) { create(:variant, on_demand: false, on_hand: 10) } let(:line_item) { create(:line_item, variant: variant) } let(:order_cycle) { - create(:simple_order_cycle, distributors: [current_distributor], variants: [variant]) + create(:simple_order_cycle, + distributors: [current_distributor], + shipping_methods: [current_distributor.shipping_methods.first], + variants: [variant]) } before do diff --git a/spec/controllers/shop_controller_spec.rb b/spec/controllers/shop_controller_spec.rb index b072152e72..d55d88b603 100644 --- a/spec/controllers/shop_controller_spec.rb +++ b/spec/controllers/shop_controller_spec.rb @@ -21,21 +21,21 @@ describe ShopController, type: :controller do describe "selecting an order cycle" do it "should select an order cycle when only one order cycle is open" do - oc1 = create(:simple_order_cycle, distributors: [distributor]) + oc1 = create(:simple_order_cycle, distributors: [distributor], shipping_methods: [sm]) get :show expect(controller.current_order_cycle).to eq(oc1) end it "should not set an order cycle when multiple order cycles are open" do - oc1 = create(:simple_order_cycle, distributors: [distributor]) - oc2 = create(:simple_order_cycle, distributors: [distributor]) + oc1 = create(:simple_order_cycle, distributors: [distributor], shipping_methods: [sm]) + oc2 = create(:simple_order_cycle, distributors: [distributor], shipping_methods: [sm]) get :show expect(controller.current_order_cycle).to be_nil end it "should allow the user to post to select the current order cycle" do - oc1 = create(:simple_order_cycle, distributors: [distributor]) - oc2 = create(:simple_order_cycle, distributors: [distributor]) + oc1 = create(:simple_order_cycle, distributors: [distributor], shipping_methods: [sm]) + oc2 = create(:simple_order_cycle, distributors: [distributor], shipping_methods: [sm]) spree_post :order_cycle, order_cycle_id: oc2.id expect(response.status).to eq 200 @@ -46,8 +46,8 @@ describe ShopController, type: :controller do render_views it "should return the order cycle details when the OC is selected" do - oc1 = create(:simple_order_cycle, distributors: [distributor]) - oc2 = create(:simple_order_cycle, distributors: [distributor]) + oc1 = create(:simple_order_cycle, distributors: [distributor], shipping_methods: [sm]) + oc2 = create(:simple_order_cycle, distributors: [distributor], shipping_methods: [sm]) spree_post :order_cycle, order_cycle_id: oc2.id expect(response.status).to eq 200 @@ -55,15 +55,19 @@ describe ShopController, type: :controller do end it "should return the current order cycle when hit with GET" do - oc1 = create(:simple_order_cycle, distributors: [distributor]) + oc1 = create(:simple_order_cycle, distributors: [distributor], shipping_methods: [sm]) allow(controller).to receive(:current_order_cycle).and_return oc1 get :order_cycle expect(response.body).to have_content oc1.id end context "when the order cycle has already been set" do - let(:oc1) { create(:simple_order_cycle, distributors: [distributor]) } - let(:oc2) { create(:simple_order_cycle, distributors: [distributor]) } + let(:oc1) do + create(:simple_order_cycle, distributors: [distributor], shipping_methods: [sm]) + end + let(:oc2) do + create(:simple_order_cycle, distributors: [distributor], shipping_methods: [sm]) + end let(:order) { create(:order, order_cycle: oc1) } before { allow(controller).to receive(:current_order) { order } } @@ -77,9 +81,12 @@ describe ShopController, type: :controller do end it "should not allow the user to select an invalid order cycle" do - oc1 = create(:simple_order_cycle, distributors: [distributor]) - oc2 = create(:simple_order_cycle, distributors: [distributor]) - oc3 = create(:simple_order_cycle, distributors: [create(:distributor_enterprise)]) + oc1 = create(:simple_order_cycle, distributors: [distributor], shipping_methods: [sm]) + oc2 = create(:simple_order_cycle, distributors: [distributor], shipping_methods: [sm]) + other_distributor = create(:distributor_enterprise, with_payment_and_shipping: true) + oc3 = create(:simple_order_cycle, + distributors: [other_distributor], + shipping_methods: [other_distributor.shipping_methods.first]) spree_post :order_cycle, order_cycle_id: oc3.id expect(response.status).to eq(404) diff --git a/spec/factories/order_factory.rb b/spec/factories/order_factory.rb index 6f38184372..b1b2b3e66c 100644 --- a/spec/factories/order_factory.rb +++ b/spec/factories/order_factory.rb @@ -13,7 +13,7 @@ FactoryBot.define do factory :order_ready_for_details do distributor { create(:distributor_enterprise, with_payment_and_shipping: true) } - order_cycle { create(:order_cycle, distributors: [distributor]) } + order_cycle { create(:order_cycle, distributors: [distributor], shipping_methods: [shipping_method]) } after(:create) do |order| order.line_items << build(:line_item, order: order) diff --git a/spec/factories/subscription_factory.rb b/spec/factories/subscription_factory.rb index f0b67c0631..5bccb7eaf0 100644 --- a/spec/factories/subscription_factory.rb +++ b/spec/factories/subscription_factory.rb @@ -3,7 +3,13 @@ FactoryBot.define do factory :subscription, class: Subscription do shop { create :enterprise } - schedule { create(:schedule, order_cycles: [create(:simple_order_cycle, coordinator: shop)]) } + schedule do + order_cycle = create(:distributor_order_cycle, + coordinator: shop, + distributors: [shop], + shipping_methods: [shipping_method]) + create(:schedule, order_cycles: [order_cycle]) + end customer { create(:customer, enterprise: shop) } bill_address { create(:address, :randomized) } ship_address { create(:address, :randomized) } diff --git a/spec/lib/open_food_network/order_cycle_permissions_spec.rb b/spec/lib/open_food_network/order_cycle_permissions_spec.rb index 4cabc5d3ac..cc7a33c91a 100644 --- a/spec/lib/open_food_network/order_cycle_permissions_spec.rb +++ b/spec/lib/open_food_network/order_cycle_permissions_spec.rb @@ -10,7 +10,7 @@ module OpenFoodNetwork let(:producer) { create(:supplier_enterprise) } let(:user) { double(:user) } let(:oc) { create(:simple_order_cycle, coordinator: coordinator) } - let(:permissions) { OrderCyclePermissions.new(user, oc) } + let(:permissions) { OrderCyclePermissions.new(user, oc.reload) } describe "finding enterprises that can be viewed in the order cycle interface" do context "when permissions are initialized without an order_cycle" do @@ -53,7 +53,7 @@ module OpenFoodNetwork end context "where the coordinator sells 'own'" do - before { allow(coordinator).to receive(:sells) { 'own' } } + before { allow(oc.coordinator).to receive(:sells) { 'own' } } it "returns just the coordinator" do enterprises = permissions.visible_enterprises expect(enterprises).to_not include hub, producer @@ -80,7 +80,7 @@ module OpenFoodNetwork end context "where the coordinator sells 'own'" do - before { allow(coordinator).to receive(:sells) { 'own' } } + before { allow(oc.coordinator).to receive(:sells) { 'own' } } it "returns just the coordinator" do enterprises = permissions.visible_enterprises expect(enterprises).to_not include hub, producer diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index 06443fb7ef..ab66327504 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -434,39 +434,65 @@ describe OrderCycle do end end - it "clones itself" do - coordinator = create(:enterprise); - oc = create(:simple_order_cycle, - coordinator_fees: [create(:enterprise_fee, enterprise: coordinator)], - preferred_product_selection_from_coordinator_inventory_only: true, - automatic_notifications: true, processed_at: Time.zone.now, mails_sent: true) - schedule = create(:schedule, order_cycles: [oc]) - ex1 = create(:exchange, order_cycle: oc) - ex2 = create(:exchange, order_cycle: oc) - oc.clone! + describe "clone!" do + it "clones itself" do + coordinator = create(:enterprise); + oc = create(:simple_order_cycle, + coordinator_fees: [create(:enterprise_fee, enterprise: coordinator)], + preferred_product_selection_from_coordinator_inventory_only: true, + automatic_notifications: true, processed_at: Time.zone.now, mails_sent: true) + schedule = create(:schedule, order_cycles: [oc]) + ex1 = create(:exchange, order_cycle: oc) + ex2 = create(:exchange, order_cycle: oc) + oc.clone! - occ = OrderCycle.last - expect(occ.name).to eq("COPY OF #{oc.name}") - expect(occ.orders_open_at).to be_nil - expect(occ.orders_close_at).to be_nil - expect(occ.coordinator).not_to be_nil - expect(occ.preferred_product_selection_from_coordinator_inventory_only).to be true - expect(occ.automatic_notifications).to eq(oc.automatic_notifications) - expect(occ.processed_at).to eq(nil) - expect(occ.mails_sent).to eq(nil) - expect(occ.coordinator).to eq(oc.coordinator) + occ = OrderCycle.last + expect(occ.name).to eq("COPY OF #{oc.name}") + expect(occ.orders_open_at).to be_nil + expect(occ.orders_close_at).to be_nil + expect(occ.coordinator).not_to be_nil + expect(occ.preferred_product_selection_from_coordinator_inventory_only).to be true + expect(occ.automatic_notifications).to eq(oc.automatic_notifications) + expect(occ.processed_at).to eq(nil) + expect(occ.mails_sent).to eq(nil) + expect(occ.coordinator).to eq(oc.coordinator) - expect(occ.coordinator_fee_ids).not_to be_empty - expect(occ.coordinator_fee_ids).to eq(oc.coordinator_fee_ids) - expect(occ.preferred_product_selection_from_coordinator_inventory_only).to eq(oc.preferred_product_selection_from_coordinator_inventory_only) - expect(occ.schedule_ids).not_to be_empty - expect(occ.schedule_ids).to eq(oc.schedule_ids) + expect(occ.coordinator_fee_ids).not_to be_empty + expect(occ.coordinator_fee_ids).to eq(oc.coordinator_fee_ids) + expect(occ.preferred_product_selection_from_coordinator_inventory_only).to eq(oc.preferred_product_selection_from_coordinator_inventory_only) + expect(occ.schedule_ids).not_to be_empty + expect(occ.schedule_ids).to eq(oc.schedule_ids) - # Check that the exchanges have been cloned. - original_exchange_attributes = oc.exchanges.map { |ex| core_exchange_attributes(ex) } - cloned_exchange_attributes = occ.exchanges.map { |ex| core_exchange_attributes(ex) } + # Check that the exchanges have been cloned. + original_exchange_attributes = oc.exchanges.map { |ex| core_exchange_attributes(ex) } + cloned_exchange_attributes = occ.exchanges.map { |ex| core_exchange_attributes(ex) } - expect(cloned_exchange_attributes).to match_array original_exchange_attributes + expect(cloned_exchange_attributes).to match_array original_exchange_attributes + end + + context "distributor order cycle created before the customisable shipping methods feature was available" do + it "allows the clone to have customisable shipping methods" do + order_cycle = create(:distributor_order_cycle, shipping_methods_customisable: false) + + order_cycle.clone! + + order_cycle_clone = OrderCycle.last + expect(order_cycle_clone.shipping_methods_customisable).to eq(true) + end + end + + context "when it has shipping methods which can longer be applied validly e.g. shipping method is backoffice only" do + it "raises an error (TODO: display a message to user explaining why clone failed)" do + distributor = create(:distributor_enterprise) + shipping_method = create(:shipping_method, distributors: [distributor]) + order_cycle = create(:distributor_order_cycle, distributors: [distributor], shipping_methods: [shipping_method]) + shipping_method.update_column(:display_on, "back_end") + + expect { + order_cycle.clone! + }.to raise_error ActiveRecord::RecordInvalid + end + end end describe "finding recently closed order cycles" do diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index c490457aa3..43738baa40 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -711,9 +711,8 @@ describe Spree::Order do end it "keeps the order cycle when it is available at the new distributor" do - d = create(:distributor_enterprise) - oc = create(:simple_order_cycle) - create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: d, incoming: false) + oc = create(:distributor_order_cycle, with_distributor_and_shipping_method: true) + d = oc.distributors.first subject.order_cycle = oc subject.set_distributor! d @@ -759,7 +758,7 @@ describe Spree::Order do end describe "setting the order cycle" do - let(:oc) { create(:simple_order_cycle) } + let(:oc) { create(:distributor_order_cycle, with_distributor_and_shipping_method: true) } it "empties the cart when changing the order cycle" do expect(subject).to receive(:empty!) @@ -777,8 +776,7 @@ describe Spree::Order do end it "keeps the distributor when it is available in the new order cycle" do - d = create(:distributor_enterprise) - create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: d, incoming: false) + d = oc.distributors.first subject.distributor = d subject.set_order_cycle! oc diff --git a/spec/system/consumer/caching/shops_caching_spec.rb b/spec/system/consumer/caching/shops_caching_spec.rb index aa5bc79141..89d70edb5b 100644 --- a/spec/system/consumer/caching/shops_caching_spec.rb +++ b/spec/system/consumer/caching/shops_caching_spec.rb @@ -10,7 +10,10 @@ describe "Shops caching", js: true, caching: true do create(:distributor_enterprise, with_payment_and_shipping: true, is_primary_producer: true) } let!(:order_cycle) { - create(:open_order_cycle, distributors: [distributor], coordinator: distributor) + create(:open_order_cycle, + distributors: [distributor], + coordinator: distributor, + shipping_methods: [distributor.shipping_methods.first]) } describe "caching enterprises AMS data" do diff --git a/spec/system/consumer/shopping/checkout_auth_spec.rb b/spec/system/consumer/shopping/checkout_auth_spec.rb index 747a6d2413..1ab5e93436 100644 --- a/spec/system/consumer/shopping/checkout_auth_spec.rb +++ b/spec/system/consumer/shopping/checkout_auth_spec.rb @@ -14,7 +14,9 @@ describe "As a consumer I want to check out my cart", js: true do let(:supplier) { create(:supplier_enterprise) } let!(:order_cycle) { create(:simple_order_cycle, distributors: [distributor], - coordinator: create(:distributor_enterprise), variants: [product.variants.first]) + coordinator: create(:distributor_enterprise), + shipping_methods: [distributor.shipping_methods.first], + variants: [product.variants.first]) } let(:product) { create(:simple_product, supplier: supplier) } let(:order) { create(:order, order_cycle: order_cycle, distributor: distributor) } diff --git a/spec/system/consumer/shopping/checkout_paypal_spec.rb b/spec/system/consumer/shopping/checkout_paypal_spec.rb index 950d2153b7..0f3e46556f 100644 --- a/spec/system/consumer/shopping/checkout_paypal_spec.rb +++ b/spec/system/consumer/shopping/checkout_paypal_spec.rb @@ -42,6 +42,7 @@ describe "Check out with Paypal", js: true do before do distributor.shipping_methods << free_shipping + order_cycle.shipping_methods << free_shipping set_order order add_product_to_cart order, product end diff --git a/spec/system/consumer/shopping/checkout_spec.rb b/spec/system/consumer/shopping/checkout_spec.rb index 04015ae715..249b76d694 100644 --- a/spec/system/consumer/shopping/checkout_spec.rb +++ b/spec/system/consumer/shopping/checkout_spec.rb @@ -65,9 +65,9 @@ describe "As a consumer I want to check out my cart", js: true do set_order order add_product_to_cart order, product - distributor.shipping_methods << free_shipping - distributor.shipping_methods << shipping_with_fee - distributor.shipping_methods << tagged_shipping + shipping_methods = [free_shipping, shipping_with_fee, tagged_shipping] + distributor.shipping_methods << shipping_methods + order_cycle.shipping_methods << shipping_methods end describe "when I have an out of stock product in my cart" do diff --git a/spec/system/consumer/shopping/checkout_stripe_spec.rb b/spec/system/consumer/shopping/checkout_stripe_spec.rb index c91d64de10..f1ea41192b 100644 --- a/spec/system/consumer/shopping/checkout_stripe_spec.rb +++ b/spec/system/consumer/shopping/checkout_stripe_spec.rb @@ -34,7 +34,9 @@ describe "Check out with Stripe", js: true do setup_stripe set_order order add_product_to_cart order, product - distributor.shipping_methods << [shipping_with_fee, free_shipping] + shipping_methods = [shipping_with_fee, free_shipping] + distributor.shipping_methods << shipping_methods + order_cycle.shipping_methods << shipping_methods end describe "using Stripe SCA" do diff --git a/spec/system/consumer/shopping/products_spec.rb b/spec/system/consumer/shopping/products_spec.rb index c310d6ef3e..52e2787036 100644 --- a/spec/system/consumer/shopping/products_spec.rb +++ b/spec/system/consumer/shopping/products_spec.rb @@ -18,7 +18,9 @@ describe "As a consumer I want to view products", js: true do let(:supplier) { create(:supplier_enterprise) } let(:oc1) { create(:simple_order_cycle, distributors: [distributor], - coordinator: create(:distributor_enterprise), orders_close_at: 2.days.from_now) + coordinator: create(:distributor_enterprise), + orders_close_at: 2.days.from_now, + shipping_methods: [distributor.shipping_methods.first]) } let(:product) { create(:simple_product, supplier: supplier, primary_taxon: taxon, properties: [property], name: "Beans") diff --git a/spec/system/consumer/shopping/shopping_spec.rb b/spec/system/consumer/shopping/shopping_spec.rb index 823018143b..57867455a4 100644 --- a/spec/system/consumer/shopping/shopping_spec.rb +++ b/spec/system/consumer/shopping/shopping_spec.rb @@ -14,11 +14,15 @@ describe "As a consumer I want to shop with a distributor", js: true do let(:supplier) { create(:supplier_enterprise) } let(:oc1) { create(:simple_order_cycle, distributors: [distributor], - coordinator: create(:distributor_enterprise), orders_close_at: 2.days.from_now) + coordinator: create(:distributor_enterprise), + orders_close_at: 2.days.from_now, + shipping_methods: [distributor.shipping_methods.first]) } let(:oc2) { create(:simple_order_cycle, distributors: [distributor], - coordinator: create(:distributor_enterprise), orders_close_at: 3.days.from_now) + coordinator: create(:distributor_enterprise), + orders_close_at: 3.days.from_now, + shipping_methods: [distributor.shipping_methods.first]) } let(:product) { create(:simple_product, supplier: supplier, meta_keywords: "Domestic") } let(:variant) { product.variants.first } diff --git a/spec/system/consumer/shopping/unit_price_spec.rb b/spec/system/consumer/shopping/unit_price_spec.rb index c134dd4f4d..a1c998887e 100644 --- a/spec/system/consumer/shopping/unit_price_spec.rb +++ b/spec/system/consumer/shopping/unit_price_spec.rb @@ -12,7 +12,9 @@ describe "As a consumer, I want to check unit price information for a product", let(:supplier) { create(:supplier_enterprise) } let(:oc1) { create(:simple_order_cycle, distributors: [distributor], - coordinator: create(:distributor_enterprise), orders_close_at: 2.days.from_now) + coordinator: create(:distributor_enterprise), + orders_close_at: 2.days.from_now, + shipping_methods: [distributor.shipping_methods.first]) } let(:product) { create(:simple_product, supplier: supplier) } let(:variant) { product.variants.first } diff --git a/spec/system/consumer/shopping/variant_overrides_spec.rb b/spec/system/consumer/shopping/variant_overrides_spec.rb index 5fbb24de3e..26ed5c2955 100644 --- a/spec/system/consumer/shopping/variant_overrides_spec.rb +++ b/spec/system/consumer/shopping/variant_overrides_spec.rb @@ -12,7 +12,8 @@ describe "shopping with variant overrides defined", js: true do let(:hub) { create(:distributor_enterprise, with_payment_and_shipping: true) } let(:producer) { create(:supplier_enterprise) } let(:oc) { - create(:simple_order_cycle, suppliers: [producer], coordinator: hub, distributors: [hub]) + create(:simple_order_cycle, + suppliers: [producer], coordinator: hub, distributors: [hub], shipping_methods: [sm]) } let(:outgoing_exchange) { oc.exchanges.outgoing.first } let(:sm) { hub.shipping_methods.first } diff --git a/spec/system/consumer/split_checkout_spec.rb b/spec/system/consumer/split_checkout_spec.rb index ab6568c2f0..cfd01afb2f 100644 --- a/spec/system/consumer/split_checkout_spec.rb +++ b/spec/system/consumer/split_checkout_spec.rb @@ -71,7 +71,8 @@ describe "As a consumer, I want to checkout my order", js: true do add_enterprise_fee enterprise_fee set_order order - distributor.shipping_methods.push(shipping_methods) + distributor.shipping_methods << shipping_methods + order_cycle.shipping_methods << shipping_methods end context "guest checkout when distributor doesn't allow guest orders" do