From b4b749cf960ccb1f29b993e3bddef0c464fa71dc Mon Sep 17 00:00:00 2001 From: Phillip Davis Date: Fri, 27 Mar 2026 15:32:15 -0400 Subject: [PATCH] block AutoTopUp for onboarding customers --- lib/low_balance.rb | 4 +++- test/test_low_balance.rb | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/low_balance.rb b/lib/low_balance.rb index 00198793dd939589ddadb371c634497aaf490dff..7e5af3ac7a91326fef6bed23576ac9c476e05c72 100644 --- a/lib/low_balance.rb +++ b/lib/low_balance.rb @@ -3,6 +3,7 @@ require_relative "churnbuster" require_relative "credit_card_sale" require_relative "expiring_lock" +require_relative "onboarding" require_relative "transaction" class LowBalance @@ -83,7 +84,8 @@ class LowBalance def self.for(customer, target=0) customer.payment_methods.then(&:default_payment_method).then do |method| blocked?(method).then do |block| - next AutoTopUp.new(customer, method, target) if block.zero? + next AutoTopUp.new(customer, method, target) if + block.zero? && !customer.jid.onboarding? log.info( "#{customer.customer_id} auto top up blocked #{block} #{method}" diff --git a/test/test_low_balance.rb b/test/test_low_balance.rb index 4d296c9b3dace14ce4f2e278ec30aded84526d23..5672077117e951fad53f07a5b947e415dce75630 100644 --- a/test/test_low_balance.rb +++ b/test/test_low_balance.rb @@ -2,6 +2,7 @@ require "test_helper" require "low_balance" +require "onboarding" ExpiringLock::REDIS = Minitest::Mock.new CustomerPlan::REDIS = Minitest::Mock.new @@ -113,6 +114,51 @@ class LowBalanceTest < Minitest::Test end em :test_for_auto_top_up + def test_for_auto_top_up_onboarding + ExpiringLock::REDIS.expect( + :set, + EMPromise.resolve("OK"), + ["jmp_customer_low_balance-test", Time, "EX", 604800, "NX"] + ) + CustomerFinancials::REDIS.expect( + :smembers, + EMPromise.resolve([]), + ["block_credit_cards"] + ) + CustomerFinancials::REDIS.expect( + :smembers, + EMPromise.resolve([]), + ["jmp_customer_btc_addresses-test"] + ) + LowBalance::AutoTopUp::REDIS.expect( + :exists, + 0, + ["jmp_auto_top_up_block-abcd"] + ) + braintree_customer = Minitest::Mock.new + CustomerFinancials::BRAINTREE.expect(:customer, braintree_customer) + payment_methods = OpenStruct.new(payment_methods: [ + OpenStruct.new(default?: true, unique_number_identifier: "abcd") + ]) + braintree_customer.expect( + :find, + EMPromise.resolve(payment_methods), + ["test"] + ) + assert_kind_of( + LowBalance, + LowBalance.for(customer( + auto_top_up_amount: 15, + jid: Blather::JID.new("test\\40onboarding.example.com@proxy") + )).sync + ) + assert_mock ExpiringLock::REDIS + assert_mock CustomerFinancials::REDIS + assert_mock CustomerFinancials::BRAINTREE + assert_mock braintree_customer + end + em :test_for_auto_top_up_onboarding + def test_for_auto_top_up_blocked ExpiringLock::REDIS.expect( :set,