mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Add specs for error cases in shipments_controller_spec
This commit is contained in:
@@ -17,11 +17,12 @@ describe Spree::Api::ShipmentsController, type: :controller do
|
||||
let!(:variant) { create(:variant) }
|
||||
let(:params) do
|
||||
{ quantity: 2,
|
||||
variant_id: stock_location.stock_items.first.variant.to_param,
|
||||
variant_id: variant.to_param,
|
||||
order_id: order.number,
|
||||
stock_location_id: stock_location.to_param,
|
||||
format: :json }
|
||||
end
|
||||
let(:error_message) { "broken shipments creation" }
|
||||
|
||||
before do
|
||||
order.update_attribute(:ship_address_id, order_ship_address.id)
|
||||
@@ -63,6 +64,14 @@ describe Spree::Api::ShipmentsController, type: :controller do
|
||||
expect(json_response["inventory_units"].size).to eq 2
|
||||
expect(order.reload.line_items.first.price).to eq(variant_override.price)
|
||||
end
|
||||
|
||||
it 'returns error code when adding to order contents fails' do
|
||||
make_order_contents_fail
|
||||
|
||||
spree_post :create, params
|
||||
|
||||
expect_error_response
|
||||
end
|
||||
end
|
||||
|
||||
context 'for a completed order with shipment' do
|
||||
@@ -70,38 +79,60 @@ describe Spree::Api::ShipmentsController, type: :controller do
|
||||
|
||||
before { params[:id] = order.shipments.first.to_param }
|
||||
|
||||
it 'adds a variant to the shipment' do
|
||||
spree_put :add, params
|
||||
context '#add' do
|
||||
it 'adds a variant to the shipment' do
|
||||
spree_put :add, params
|
||||
|
||||
expect_valid_response
|
||||
expect(inventory_units_for(json_response["inventory_units"], variant).size).to eq 2
|
||||
expect_valid_response
|
||||
expect(inventory_units_for(json_response["inventory_units"], variant).size).to eq 2
|
||||
end
|
||||
|
||||
it 'returns error code when adding to order contents fails' do
|
||||
make_order_contents_fail
|
||||
|
||||
spree_put :add, params
|
||||
|
||||
expect_error_response
|
||||
end
|
||||
|
||||
it 'adds a variant override to the shipment' do
|
||||
hub = create(:distributor_enterprise)
|
||||
order.update_attribute(:distributor, hub)
|
||||
variant_override = create(:variant_override, hub: hub, variant: variant)
|
||||
|
||||
spree_put :add, params
|
||||
|
||||
expect_valid_response
|
||||
expect(inventory_units_for(json_response["inventory_units"], variant).size).to eq 2
|
||||
expect(order.reload.line_items.last.price).to eq(variant_override.price)
|
||||
end
|
||||
end
|
||||
|
||||
it 'removes a variant from the shipment' do
|
||||
params[:variant_id] = order.line_items.first.variant.to_param
|
||||
params[:quantity] = 1
|
||||
context '#remove' do
|
||||
before do
|
||||
params[:variant_id] = order.line_items.first.variant.to_param
|
||||
params[:quantity] = 1
|
||||
end
|
||||
|
||||
spree_put :remove, params
|
||||
it 'removes a variant from the shipment' do
|
||||
spree_put :remove, params
|
||||
|
||||
expect_valid_response
|
||||
expect(inventory_units_for(json_response["inventory_units"], variant).size).to eq 0
|
||||
expect_valid_response
|
||||
expect(inventory_units_for(json_response["inventory_units"], variant).size).to eq 0
|
||||
end
|
||||
|
||||
it 'returns error code when removing from order contents fails' do
|
||||
make_order_contents_fail
|
||||
|
||||
spree_put :remove, params
|
||||
|
||||
expect_error_response
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it 'adds a variant override to the shipment' do
|
||||
hub = create(:distributor_enterprise)
|
||||
order.update_attribute(:distributor, hub)
|
||||
variant_override = create(:variant_override, hub: hub, variant: variant)
|
||||
|
||||
spree_put :add, params
|
||||
|
||||
expect_valid_response
|
||||
expect(inventory_units_for(json_response["inventory_units"], variant).size).to eq 2
|
||||
expect(order.reload.line_items.last.price).to eq(variant_override.price)
|
||||
end
|
||||
|
||||
def inventory_units_for(inventory_units, variant)
|
||||
inventory_units.select { |unit| unit['variant_id'] == variant.id }
|
||||
end
|
||||
def inventory_units_for(inventory_units, variant)
|
||||
inventory_units.select { |unit| unit['variant_id'] == variant.id }
|
||||
end
|
||||
|
||||
def expect_valid_response
|
||||
@@ -109,5 +140,15 @@ describe Spree::Api::ShipmentsController, type: :controller do
|
||||
attributes.all?{ |attr| json_response.key? attr.to_s }
|
||||
expect(json_response["shipping_method"]["name"]).to eq order.shipping_method.name
|
||||
end
|
||||
|
||||
def make_order_contents_fail
|
||||
expect(Spree::Order).to receive(:find_by_number!) { order }
|
||||
expect(order).to receive(:contents) { raise error_message }
|
||||
end
|
||||
|
||||
def expect_error_response
|
||||
expect(response.status).to eq 422
|
||||
expect(json_response["exception"]).to eq error_message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user