From 585d137cfcc6deac3c1eb26e443d2a34d1b8de18 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 28 Nov 2016 10:40:02 +0100 Subject: [PATCH] some automated tests about cash coupons --- app/services/coupon_apply_service.rb | 3 ++- test/fixtures/coupons.yml | 9 +++++++ test/models/coupon_test.rb | 12 +++++++++- test/services/coupon_apply_service_test.rb | 28 ++++++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 test/services/coupon_apply_service_test.rb diff --git a/app/services/coupon_apply_service.rb b/app/services/coupon_apply_service.rb index 73e0361f1..0da03e83b 100644 --- a/app/services/coupon_apply_service.rb +++ b/app/services/coupon_apply_service.rb @@ -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) diff --git a/test/fixtures/coupons.yml b/test/fixtures/coupons.yml index f5eb6d362..41d78d4f4 100644 --- a/test/fixtures/coupons.yml +++ b/test/fixtures/coupons.yml @@ -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 \ No newline at end of file diff --git a/test/models/coupon_test.rb b/test/models/coupon_test.rb index 66a14618b..f1cef7ee7 100644 --- a/test/models/coupon_test.rb +++ b/test/models/coupon_test.rb @@ -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 diff --git a/test/services/coupon_apply_service_test.rb b/test/services/coupon_apply_service_test.rb new file mode 100644 index 000000000..3286d6748 --- /dev/null +++ b/test/services/coupon_apply_service_test.rb @@ -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