Change summary
lib/low_balance.rb | 26 +++++++++++++++++++-------
test/test_low_balance.rb | 18 ++++++++++++++++++
test/test_web.rb | 6 ++++++
3 files changed, 43 insertions(+), 7 deletions(-)
Detailed changes
@@ -137,6 +137,20 @@ class LowBalance
)
end
+ # @param e [StandardError]
+ # @return [String]
+ def failure_msg(e)
+ "Automatic top-up transaction for " \
+ "$#{'%.2f' % top_up_amount} failed: #{e.message}"
+ end
+
+ # @param tx [Transaction]
+ # @return [String]
+ def success_msg(tx)
+ "Automatic top-up has charged your default " \
+ "payment method and added #{tx} to your balance."
+ end
+
def failed(e)
churnbuster(e)
@method && REDIS.setex(
@@ -144,18 +158,16 @@ class LowBalance
60 * 60 * 24 * 30,
Time.now
)
- @message.body =
- "Automatic top-up transaction for " \
- "$#{'%.2f' % top_up_amount} failed: #{e.message}"
+ @message.body = failure_msg(e)
0
end
def notify!
sale.then { |tx|
- @message.body =
- "Automatic top-up has charged your default " \
- "payment method and added #{tx} to your balance."
- tx.total
+ @message.body = success_msg(tx)
+ REDIS.del(
+ "jmp_customer_low_balance-#{@customer.billing_customer_id}"
+ ).then { tx.total }
}.catch(&method(:failed)).then { |amount|
@customer.stanza_to(@message)
amount
@@ -331,5 +331,23 @@ class LowBalanceTest < Minitest::Test
assert_requested req
end
em :test_decline_notify!
+
+ def test_auto_topup_clears_low_balance_key
+ tx = OpenStruct.new(total: 13)
+ LowBalance::AutoTopUp::CreditCardSale.expect(
+ :create,
+ EMPromise.resolve(tx),
+ [@customer], amount: 100
+ )
+ LowBalance::AutoTopUp::REDIS.expect(
+ :del,
+ EMPromise.resolve(100),
+ ["jmp_customer_low_balance-test"]
+ )
+ @customer.expect(:stanza_to, nil, [Blather::Stanza::Message])
+ @auto_top_up.notify!.sync
+ assert_mock LowBalance::AutoTopUp::REDIS
+ end
+ em :test_auto_topup_clears_low_balance_key
end
end
@@ -259,6 +259,11 @@ class WebTest < Minitest::Test
0,
["jmp_auto_top_up_block-abcd"]
)
+ LowBalance::AutoTopUp::REDIS.expect(
+ :del,
+ EMPromise.resolve(1),
+ ["jmp_customer_low_balance-customerid_topup"]
+ )
braintree_customer = Minitest::Mock.new
CustomerFinancials::BRAINTREE.expect(:customer, braintree_customer)
payment_methods = OpenStruct.new(payment_methods: [
@@ -296,6 +301,7 @@ class WebTest < Minitest::Test
assert_mock ExpiringLock::REDIS
assert_mock Customer::BLATHER
assert_mock LowBalance::AutoTopUp::CreditCardSale
+ assert_mock LowBalance::AutoTopUp::REDIS
assert_mock CustomerPlan::DB
end
em :test_outbound_low_balance_top_up