Skip to content

Commit

Permalink
Create subscriptions with set up shipping methods
Browse files Browse the repository at this point in the history
The shipping methods are updated to their target settings after the
subscription order has been created, so the order is created with the
"require_ship_address" factory default which is "true".

The shipping method setting at time of order creation has implications
on whether a shipment is set up for the order or not.
  • Loading branch information
kristinalim committed Mar 14, 2019
1 parent 3755fbc commit 9e4c213
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions spec/services/order_syncer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,24 @@
let!(:original_ship_address) do
create(:address, firstname: "Melanie", lastname: "Miller", address1: "Magenta")
end
let!(:shipping_method) { create(:shipping_method, distributors: [distributor]) }

let(:subscription) do
create(:subscription, shop: distributor, bill_address: original_bill_address,
ship_address: original_ship_address, with_items: true,
with_proxy_orders: true)
ship_address: original_ship_address, shipping_method: shipping_method,
with_items: true, with_proxy_orders: true)
end

let(:shipping_method) { subscription.shipping_method }
let!(:order) { subscription.proxy_orders.first.initialise_order! }
let!(:bill_address_attrs) { subscription.bill_address.attributes }
let!(:ship_address_attrs) { subscription.ship_address.attributes }
let(:params) { { bill_address_attributes: { id: bill_address_attrs["id"], firstname: "Bill", address1: "123 abc st", phone: "1123581321" } } }
let(:syncer) { OrderSyncer.new(subscription) }

context "when a ship address is not required" do
before { shipping_method.update_attributes(require_ship_address: false) }
let!(:shipping_method) do
create(:shipping_method, distributors: [distributor], require_ship_address: false)
end

context "when the bill_address on the order matches that on the subscription" do
it "updates all bill_address attrs and ship_address names + phone" do
Expand Down Expand Up @@ -188,7 +190,9 @@
end

context "when a ship address is required" do
before { shipping_method.update_attributes(require_ship_address: true) }
let!(:shipping_method) do
create(:shipping_method, distributors: [distributor], require_ship_address: true)
end

context "when the bill_address on the order matches that on the subscription" do
it "only updates bill_address attrs" do
Expand Down Expand Up @@ -237,11 +241,14 @@
let!(:original_ship_address) do
create(:address, firstname: "Melanie", lastname: "Miller", address1: "Magenta")
end
let!(:shipping_method) do
create(:shipping_method, distributors: [distributor], require_ship_address: true)
end

let(:subscription) do
create(:subscription, shop: distributor, bill_address: original_bill_address,
ship_address: original_ship_address, with_items: true,
with_proxy_orders: true)
ship_address: original_ship_address, shipping_method: shipping_method,
with_items: true, with_proxy_orders: true)
end

let(:shipping_method) { subscription.shipping_method }
Expand All @@ -252,7 +259,9 @@
let(:syncer) { OrderSyncer.new(subscription) }

context "when a ship address is not required" do
before { shipping_method.update_attributes(require_ship_address: false) }
let!(:shipping_method) do
create(:shipping_method, distributors: [distributor], require_ship_address: false)
end

it "does not change the ship address" do
subscription.assign_attributes(params)
Expand All @@ -277,22 +286,41 @@
address1: distributor_address.address1)
end

it "updates ship_address attrs" do
before do
subscription.assign_attributes(params)
end

it "updates ship_address attrs" do
expect(syncer.sync!).to be true
expect(syncer.order_update_issues.keys).to_not include order.id
order.reload;
expect(order.ship_address.firstname).to eq "Ship"
expect(syncer.order_update_issues.keys).to include order.id
order.reload
expect(order.ship_address.firstname).to eq ship_address_attrs["firstname"]
expect(order.ship_address.lastname).to eq ship_address_attrs["lastname"]
expect(order.ship_address.address1).to eq "123 abc st"
expect(order.ship_address.phone).to eq "1123581321"
expect(order.ship_address.address1).to eq ship_address_attrs["address1"]
expect(order.ship_address.phone).to eq ship_address_attrs["phone"]
end

context "when the order has a pending shipment using the former shipping method" do
let!(:shipment) { create(:shipment, order: order, shipping_method: shipping_method) }

it "updates ship_address attrs" do
expect(syncer.sync!).to be true
expect(syncer.order_update_issues.keys).not_to include order.id
order.reload
expect(order.ship_address.firstname).to eq "Ship"
expect(order.ship_address.lastname).to eq ship_address_attrs["lastname"]
expect(order.ship_address.address1).to eq "123 abc st"
expect(order.ship_address.phone).to eq "1123581321"
end
end
end
end
end

context "when a ship address is required" do
before { shipping_method.update_attributes(require_ship_address: true) }
let!(:shipping_method) do
create(:shipping_method, distributors: [distributor], require_ship_address: true)
end

context "when the ship address on the order matches that on the subscription" do
it "updates ship_address attrs" do
Expand Down

0 comments on commit 9e4c213

Please sign in to comment.