diff --git a/app/controllers/admin/standing_order_orders_controller.rb b/app/controllers/admin/standing_order_orders_controller.rb index fe56c85c67..39f54edfa2 100644 --- a/app/controllers/admin/standing_order_orders_controller.rb +++ b/app/controllers/admin/standing_order_orders_controller.rb @@ -9,7 +9,7 @@ module Admin end else respond_with(@standing_order_order) do |format| - format.json { render json: { errors: @standing_order_order.errors.full_messages }, status: :unprocessable_entity } + format.json { render json: { errors: [t(:could_not_cancel_the_order)] }, status: :unprocessable_entity } end end end diff --git a/app/models/standing_order_order.rb b/app/models/standing_order_order.rb index 904468d21b..6572a96fa4 100644 --- a/app/models/standing_order_order.rb +++ b/app/models/standing_order_order.rb @@ -17,12 +17,11 @@ class StandingOrderOrder < ActiveRecord::Base end def cancel + return false unless order.order_cycle.orders_close_at > Time.zone.now transaction do - if order.order_cycle.orders_close_at > Time.zone.now - self.update_column(:cancelled_at, Time.zone.now) - order.send('cancel') if order.complete? - end - self + self.update_column(:cancelled_at, Time.zone.now) + order.send('cancel') if order.complete? + true end end end diff --git a/spec/controllers/admin/standing_order_orders_controller_spec.rb b/spec/controllers/admin/standing_order_orders_controller_spec.rb index 33fa106116..8f0b420e2a 100644 --- a/spec/controllers/admin/standing_order_orders_controller_spec.rb +++ b/spec/controllers/admin/standing_order_orders_controller_spec.rb @@ -6,7 +6,8 @@ describe Admin::StandingOrderOrdersController, type: :controller do describe 'cancel' do let!(:user) { create(:user, enterprise_limit: 10) } let!(:shop) { create(:distributor_enterprise) } - let!(:order) { create(:order, order_cycle: create(:simple_order_cycle)) } + let!(:order_cycle) { create(:simple_order_cycle, orders_close_at: 1.day.from_now) } + let!(:order) { create(:order, order_cycle: order_cycle) } let!(:standing_order) { create(:standing_order_with_items, shop: shop, orders: [order]) } let!(:standing_order_order) { standing_order.standing_order_orders.first } @@ -38,12 +39,24 @@ describe Admin::StandingOrderOrdersController, type: :controller do context "with authorisation" do before { shop.update_attributes(owner: user) } - it 'renders the cancelled standing_order_order as json' do - spree_get :cancel, params - json_response = JSON.parse(response.body) - expect(json_response['status']).to eq "cancelled" - expect(json_response['id']).to eq standing_order_order.id - expect(standing_order_order.reload.cancelled_at).to be_within(5.seconds).of Time.now + context "when cancellation succeeds" do + it 'renders the cancelled standing_order_order as json' do + spree_get :cancel, params + json_response = JSON.parse(response.body) + expect(json_response['status']).to eq "cancelled" + expect(json_response['id']).to eq standing_order_order.id + expect(standing_order_order.reload.cancelled_at).to be_within(5.seconds).of Time.now + end + end + + context "when cancellation fails" do + before { order_cycle.update_attributes(orders_close_at: 1.day.ago) } + + it "shows an error" do + spree_get :cancel, params + json_response = JSON.parse(response.body) + expect(json_response['errors']).to eq ['Could not cancel the order'] + end end end end diff --git a/spec/models/standing_order_order_spec.rb b/spec/models/standing_order_order_spec.rb index 663432a6dd..17fc13c782 100644 --- a/spec/models/standing_order_order_spec.rb +++ b/spec/models/standing_order_order_spec.rb @@ -12,8 +12,8 @@ describe StandingOrderOrder, type: :model do context "and the order has already been completed" do let(:order) { create(:completed_order_with_totals, order_cycle: order_cycle) } - it "sets cancelled_at to the current time, and cancels the order" do - standing_order_order.cancel + it "returns true and sets cancelled_at to the current time, and cancels the order" do + expect(standing_order_order.cancel).to be true expect(standing_order_order.reload.cancelled_at).to be_within(5.seconds).of Time.now expect(order.reload.state).to eq 'canceled' end @@ -22,8 +22,8 @@ describe StandingOrderOrder, type: :model do context "and the order has not already been completed" do let(:order) { create(:order, order_cycle: order_cycle) } - it "just sets cancelled at to the current time" do - standing_order_order.cancel + it "returns true and sets cancelled at to the current time" do + expect(standing_order_order.cancel).to be true expect(standing_order_order.reload.cancelled_at).to be_within(5.seconds).of Time.now expect(order.reload.state).to eq 'cart' end @@ -34,8 +34,8 @@ describe StandingOrderOrder, type: :model do let(:order) { create(:order, order_cycle: order_cycle) } before { order_cycle.update_attributes(orders_open_at: 3.days.ago, orders_close_at: 1.minute.ago) } - it "does nothing" do - standing_order_order.cancel + it "returns false and does nothing" do + expect(standing_order_order.cancel).to eq false expect(standing_order_order.reload.cancelled_at).to be nil expect(order.reload.state).to eq 'cart' end