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:
parent
5231beb6e1
commit
585d137cfc
@ -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)
|
||||
|
9
test/fixtures/coupons.yml
vendored
9
test/fixtures/coupons.yml
vendored
@ -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
|
@ -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
|
||||
|
28
test/services/coupon_apply_service_test.rb
Normal file
28
test/services/coupon_apply_service_test.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user