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
|
include ApplicationHelper
|
||||||
|
|
||||||
load_resource :user
|
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
|
def new
|
||||||
products = (@user.products.for_sale.select("products.*", "sum(order_items.count) as count").group(:product_id).order("count desc") | Product.for_sale)
|
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
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
order = Order.find(params[:id])
|
order = Order.unscoped.find(params[:id])
|
||||||
if order.created_at > 5.minutes.ago
|
authorize! :destroy, order
|
||||||
order.cancel
|
if order.cancel
|
||||||
flash[:success] = "Order has been removed."
|
flash[:success] = "Order has been removed."
|
||||||
else
|
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
|
end
|
||||||
redirect_to root_path
|
redirect_to root_path
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,10 +37,12 @@ class Order < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def cancel
|
def cancel
|
||||||
return if self.cancelled
|
return false if cancelled || created_at < 5.minutes.ago
|
||||||
|
|
||||||
User.decrement_counter(:orders_count, user.id)
|
User.decrement_counter(:orders_count, user.id)
|
||||||
update_attribute(:cancelled, true)
|
update_attribute(:cancelled, true)
|
||||||
self.order_items.each(&:cancel)
|
self.order_items.each(&:cancel)
|
||||||
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_sentence
|
def to_sentence
|
||||||
|
|
|
@ -12,10 +12,34 @@
|
||||||
|
|
||||||
describe Order do
|
describe Order do
|
||||||
before :each do
|
before :each do
|
||||||
@order = create :order
|
@user = create :user
|
||||||
|
@order = create :order, user: @user
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'has a valid factory' do
|
it 'has a valid factory' do
|
||||||
expect(@order).to be_valid
|
expect(@order).to be_valid
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue