1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-28 09:24:24 +01:00

some automated tests about cash coupons

This commit is contained in:
Sylvain 2016-11-28 10:40:02 +01:00
parent 5231beb6e1
commit 585d137cfc
4 changed files with 50 additions and 2 deletions

View File

@ -12,13 +12,14 @@ class CouponApplyService
def call(total, coupon, user_id = nil)
price = total
_coupon = nil
if coupon.instance_of? Coupon
_coupon = coupon
elsif coupon.instance_of? String
_coupon = Coupon.find_by(code: coupon)
end
unless coupon.nil?
unless _coupon.nil?
if _coupon.status(user_id, total) == 'active'
if _coupon.type == 'percent_off'
price = price - (price * _coupon.percent_off / 100.0)

View File

@ -17,3 +17,12 @@ two:
max_usages: 10
active: true
validity_per_user: always
cash:
name: Cash Code
code: ZERG6H1R65H
amount_off: 10000
valid_until: <%= 1.year.from_now.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
max_usages: 1
active: true
validity_per_user: once

View File

@ -8,11 +8,21 @@ class CouponTest < ActiveSupport::TestCase
test 'expired coupon must return the proper status' do
c = Coupon.find_by(code: 'XMAS10')
assert c.status == 'expired'
assert_equal 'expired', c.status
end
test 'two coupons cannot have the same code' do
c = Coupon.new({name: 'Summer deals', code: 'SUNNYFABLAB', percent_off: 15, validity_per_user: 'always'})
assert c.invalid?
end
test 'coupon with cash amount has amount_off type' do
c = Coupon.new({name: 'Essential Box', code: 'KWXX2M', amount_off: 2000, validity_per_user: 'once', max_usages: 1})
assert_equal 'amount_off', c.type
end
test 'coupon with cash amount cannot be used with cheaper cart' do
c = Coupon.new({name: 'Premium Box', code: '6DDX2T44MQ', amount_off: 20000, validity_per_user: 'once', max_usages: 1, active: true})
assert_equal 'amount_exceeded', c.status(User.find_by(username: 'jdupond').id, 2000)
end
end

View File

@ -0,0 +1,28 @@
require 'test_helper'
class CouponApplyServiceTest < ActiveSupport::TestCase
setup do
@jdupond = User.find_by(username: 'jdupond')
@cash_coupon = Coupon.find_by(code: 'ZERG6H1R65H')
end
test 'user apply percent coupon to cart' do
total = CouponApplyService.new.(1000, 'SUNNYFABLAB', @jdupond.id)
assert_equal 850, total
end
test 'user cannot apply excessive coupon to cart' do
total = CouponApplyService.new.(1000, @cash_coupon, @jdupond.id)
assert_equal 1000, total
end
test 'user cannot apply invalid coupon to cart' do
total = CouponApplyService.new.(1000, 'INVALIDCODE', @jdupond.id)
assert_equal 1000, total
end
test 'user cannot apply expired coupon to cart' do
total = CouponApplyService.new.(1000, 'XMAS10', @jdupond.id)
assert_equal 1000, total
end
end