test_low_balance.rb

  1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "low_balance"
  5
  6ExpiringLock::REDIS = Minitest::Mock.new
  7CustomerPlan::REDIS = Minitest::Mock.new
  8CustomerFinancials::REDIS = Minitest::Mock.new
  9LowBalance::AutoTopUp::REDIS = Minitest::Mock.new
 10
 11class LowBalanceTest < Minitest::Test
 12	def test_for_locked
 13		ExpiringLock::REDIS.expect(
 14			:set,
 15			EMPromise.resolve(nil),
 16			["jmp_customer_low_balance-test", Time, "EX", 604800, "NX"]
 17		)
 18		assert_kind_of LowBalance::Locked, LowBalance.for(customer).sync
 19	end
 20	em :test_for_locked
 21
 22	def test_for_no_auto_top_up
 23		ExpiringLock::REDIS.expect(
 24			:set,
 25			EMPromise.resolve("OK"),
 26			["jmp_customer_low_balance-test", Time, "EX", 604800, "NX"]
 27		)
 28		CustomerFinancials::REDIS.expect(
 29			:smembers,
 30			EMPromise.resolve([]),
 31			["jmp_customer_btc_addresses-test"]
 32		)
 33		assert_kind_of(
 34			LowBalance,
 35			LowBalance.for(customer).sync
 36		)
 37		assert_mock ExpiringLock::REDIS
 38	end
 39	em :test_for_no_auto_top_up
 40
 41	def test_for_auto_top_up
 42		ExpiringLock::REDIS.expect(
 43			:set,
 44			EMPromise.resolve("OK"),
 45			["jmp_customer_low_balance-test", Time, "EX", 604800, "NX"]
 46		)
 47		CustomerFinancials::REDIS.expect(
 48			:smembers,
 49			EMPromise.resolve([]),
 50			["block_credit_cards"]
 51		)
 52		LowBalance::AutoTopUp::REDIS.expect(
 53			:exists,
 54			0,
 55			["jmp_auto_top_up_block-abcd"]
 56		)
 57		braintree_customer = Minitest::Mock.new
 58		CustomerFinancials::BRAINTREE.expect(:customer, braintree_customer)
 59		payment_methods = OpenStruct.new(payment_methods: [
 60			OpenStruct.new(default?: true, unique_number_identifier: "abcd")
 61		])
 62		braintree_customer.expect(
 63			:find,
 64			EMPromise.resolve(payment_methods),
 65			["test"]
 66		)
 67		assert_kind_of(
 68			LowBalance::AutoTopUp,
 69			LowBalance.for(customer(auto_top_up_amount: 15)).sync
 70		)
 71		assert_mock ExpiringLock::REDIS
 72		assert_mock CustomerFinancials::REDIS
 73		assert_mock CustomerFinancials::BRAINTREE
 74		assert_mock braintree_customer
 75	end
 76	em :test_for_auto_top_up
 77
 78	def test_for_auto_top_up_blocked
 79		ExpiringLock::REDIS.expect(
 80			:set,
 81			EMPromise.resolve("OK"),
 82			["jmp_customer_low_balance-test", Time, "EX", 604800, "NX"]
 83		)
 84		CustomerFinancials::REDIS.expect(
 85			:smembers,
 86			EMPromise.resolve([]),
 87			["block_credit_cards"]
 88		)
 89		CustomerFinancials::REDIS.expect(
 90			:smembers,
 91			EMPromise.resolve([]),
 92			["jmp_customer_btc_addresses-test"]
 93		)
 94		LowBalance::AutoTopUp::REDIS.expect(
 95			:exists,
 96			1,
 97			["jmp_auto_top_up_block-blocked"]
 98		)
 99		braintree_customer = Minitest::Mock.new
100		CustomerFinancials::BRAINTREE.expect(:customer, braintree_customer)
101		payment_methods = OpenStruct.new(payment_methods: [
102			OpenStruct.new(default?: true, unique_number_identifier: "blocked")
103		])
104		braintree_customer.expect(
105			:find,
106			EMPromise.resolve(payment_methods),
107			["test"]
108		)
109		assert_kind_of(
110			LowBalance,
111			LowBalance.for(customer(auto_top_up_amount: 15)).sync
112		)
113		assert_mock ExpiringLock::REDIS
114		assert_mock CustomerFinancials::REDIS
115		assert_mock CustomerFinancials::BRAINTREE
116		assert_mock braintree_customer
117	end
118	em :test_for_auto_top_up_blocked
119
120	class AutoTopUpTest < Minitest::Test
121		LowBalance::AutoTopUp::Transaction = Minitest::Mock.new
122
123		def setup
124			@customer = Minitest::Mock.new(customer(auto_top_up_amount: 100))
125			@auto_top_up = LowBalance::AutoTopUp.new(@customer)
126		end
127
128		def test_notify!
129			tx = PromiseMock.new
130			tx.expect(:insert, EMPromise.resolve(nil))
131			LowBalance::AutoTopUp::Transaction.expect(
132				:sale,
133				tx,
134				[@customer, { amount: 100 }]
135			)
136			@auto_top_up.notify!
137			assert_mock tx
138		end
139		em :test_notify!
140
141		def test_decline_notify!
142			@customer.expect(
143				:stanza_to,
144				nil,
145				[Matching.new { |m|
146					assert_equal(
147						"Automatic top-up transaction for $100 failed: test",
148						m.body
149					)
150				}]
151			)
152			LowBalance::AutoTopUp::Transaction.expect(
153				:sale,
154				EMPromise.reject(RuntimeError.new("test")),
155				[@customer, { amount: 100 }]
156			)
157			@auto_top_up.notify!.sync
158			assert_mock @customer
159		end
160		em :test_decline_notify!
161	end
162end