Add tests for cancelling orders
This commit is contained in:
parent
ae28317b07
commit
bb76b36d86
3 changed files with 33 additions and 7 deletions
|
@ -3,7 +3,7 @@ class OrdersController < ApplicationController
|
|||
include ApplicationHelper
|
||||
|
||||
load_resource :user
|
||||
load_and_authorize_resource :order, through: :user, shallow: true
|
||||
load_and_authorize_resource :order, through: :user, shallow: true, except: :destroy
|
||||
|
||||
def new
|
||||
products = (@user.products.for_sale.select("products.*", "sum(order_items.count) as count").group(:product_id).order("count desc") | Product.for_sale)
|
||||
|
@ -21,12 +21,12 @@ class OrdersController < ApplicationController
|
|||
end
|
||||
|
||||
def destroy
|
||||
order = Order.find(params[:id])
|
||||
if order.created_at > 5.minutes.ago
|
||||
order.cancel
|
||||
order = Order.unscoped.find(params[:id])
|
||||
authorize! :destroy, order
|
||||
if order.cancel
|
||||
flash[:success] = "Order has been removed."
|
||||
else
|
||||
flash[:error] = "This order has been placed too long ago, it can't be removed. Please contact a sysadmin."
|
||||
flash[:error] = "Something went wrong. Perhaps this order was already cancelled, or it has been place too long ago."
|
||||
end
|
||||
redirect_to root_path
|
||||
end
|
||||
|
|
|
@ -37,10 +37,12 @@ class Order < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def cancel
|
||||
return if self.cancelled
|
||||
return false if cancelled || created_at < 5.minutes.ago
|
||||
|
||||
User.decrement_counter(:orders_count, user.id)
|
||||
update_attribute(:cancelled, true)
|
||||
self.order_items.each(&:cancel)
|
||||
true
|
||||
end
|
||||
|
||||
def to_sentence
|
||||
|
|
|
@ -12,10 +12,34 @@
|
|||
|
||||
describe Order do
|
||||
before :each do
|
||||
@order = create :order
|
||||
@user = create :user
|
||||
@order = create :order, user: @user
|
||||
end
|
||||
|
||||
it 'has a valid factory' do
|
||||
expect(@order).to be_valid
|
||||
end
|
||||
|
||||
describe 'cancelling' do
|
||||
it 'should cancel the order' do
|
||||
@order.cancel
|
||||
expect(@order.cancelled).to be true
|
||||
end
|
||||
|
||||
it 'should not happen twice' do
|
||||
@order.cancel
|
||||
expect(@order.cancel).to be false
|
||||
end
|
||||
|
||||
it 'should not work on old orders' do
|
||||
order = create :order, created_at: 3.days.ago
|
||||
expect(order.cancel).to be false
|
||||
end
|
||||
|
||||
it 'should change the orders_count' do
|
||||
expect(@user.reload.orders_count).to eq(1)
|
||||
@order.cancel
|
||||
expect(@user.reload.orders_count).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue