2023-02-17 11:44:04 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-03 17:25:00 +02:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class CouponTest < ActiveSupport::TestCase
|
2016-12-13 15:33:24 +01:00
|
|
|
test 'valid coupon with percentage' do
|
2023-02-17 11:44:04 +01:00
|
|
|
c = Coupon.new({ name: 'Hot deals', code: 'HOT15', percent_off: 15, validity_per_user: 'once', valid_until: 2.weeks.from_now,
|
|
|
|
max_usages: 100, active: true })
|
2016-12-13 15:33:24 +01:00
|
|
|
assert c.valid?
|
|
|
|
assert_equal 'active', c.status, 'Invalid coupon status'
|
|
|
|
assert_equal 'percent_off', c.type, 'Invalid coupon type'
|
|
|
|
end
|
|
|
|
|
2016-08-03 17:25:00 +02:00
|
|
|
test 'coupon must have a valid percentage' do
|
2023-02-17 11:44:04 +01:00
|
|
|
c = Coupon.new({ name: 'Amazing deal', code: 'DISCOUNT', percent_off: 200, validity_per_user: 'once' })
|
2016-08-08 15:43:02 +02:00
|
|
|
assert c.invalid?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'expired coupon must return the proper status' do
|
2016-11-23 16:30:19 +01:00
|
|
|
c = Coupon.find_by(code: 'XMAS10')
|
2016-11-28 10:40:02 +01:00
|
|
|
assert_equal 'expired', c.status
|
2016-08-08 15:43:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'two coupons cannot have the same code' do
|
2023-02-17 11:44:04 +01:00
|
|
|
c = Coupon.new({ name: 'Summer deals', code: 'SUNNYFABLAB', percent_off: 15, validity_per_user: 'always' })
|
2016-08-03 17:25:00 +02:00
|
|
|
assert c.invalid?
|
|
|
|
end
|
2016-11-28 10:40:02 +01:00
|
|
|
|
2016-12-13 15:37:43 +01:00
|
|
|
test 'valid coupon with cash amount' do
|
2023-02-17 11:44:04 +01:00
|
|
|
c = Coupon.new({ name: 'Essential Box', code: 'KWXX2M', amount_off: 2000, validity_per_user: 'once', max_usages: 1, active: true })
|
2016-12-13 15:37:43 +01:00
|
|
|
assert c.valid?
|
|
|
|
assert_equal 'active', c.status, 'Invalid coupon status'
|
|
|
|
assert_equal 'amount_off', c.type, 'Invalid coupon type'
|
2016-11-28 10:40:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'coupon with cash amount cannot be used with cheaper cart' do
|
2023-02-17 11:44:04 +01:00
|
|
|
c = Coupon.new({ name: 'Premium Box', code: '6DDX2T44MQ', amount_off: 20_000, validity_per_user: 'once', max_usages: 1, active: true })
|
2016-11-28 10:40:02 +01:00
|
|
|
assert_equal 'amount_exceeded', c.status(User.find_by(username: 'jdupond').id, 2000)
|
|
|
|
end
|
2016-08-03 17:25:00 +02:00
|
|
|
end
|