~singpolyma/sgx-jmp

sgx-jmp/test/test_low_balance.rb -rw-r--r-- 2.1 KiB
cd3111beStephen Paul Weber Merge branch 'snikket' 19 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true

require "test_helper"
require "low_balance"

ExpiringLock::REDIS = Minitest::Mock.new
CustomerPlan::REDIS = Minitest::Mock.new
CustomerFinancials::REDIS = Minitest::Mock.new

class LowBalanceTest < Minitest::Test
	def test_for_locked
		ExpiringLock::REDIS.expect(
			:set,
			EMPromise.resolve(nil),
			["jmp_customer_low_balance-test", Time, "EX", 604800, "NX"]
		)
		assert_kind_of LowBalance::Locked, LowBalance.for(customer).sync
	end
	em :test_for_locked

	def test_for_no_auto_top_up
		ExpiringLock::REDIS.expect(
			:set,
			EMPromise.resolve("OK"),
			["jmp_customer_low_balance-test", Time, "EX", 604800, "NX"]
		)
		CustomerFinancials::REDIS.expect(
			:smembers,
			EMPromise.resolve([]),
			["jmp_customer_btc_addresses-test"]
		)
		assert_kind_of(
			LowBalance,
			LowBalance.for(customer).sync
		)
		assert_mock ExpiringLock::REDIS
	end
	em :test_for_no_auto_top_up

	def test_for_auto_top_up
		ExpiringLock::REDIS.expect(
			:set,
			EMPromise.resolve("OK"),
			["jmp_customer_low_balance-test", Time, "EX", 604800, "NX"]
		)
		assert_kind_of(
			LowBalance::AutoTopUp,
			LowBalance.for(customer(auto_top_up_amount: 15)).sync
		)
		assert_mock ExpiringLock::REDIS
	end
	em :test_for_auto_top_up

	class AutoTopUpTest < Minitest::Test
		LowBalance::AutoTopUp::Transaction = Minitest::Mock.new

		def setup
			@customer = Minitest::Mock.new(customer(auto_top_up_amount: 100))
			@auto_top_up = LowBalance::AutoTopUp.new(@customer)
		end

		def test_notify!
			tx = PromiseMock.new
			tx.expect(:insert, EMPromise.resolve(nil))
			LowBalance::AutoTopUp::Transaction.expect(
				:sale,
				tx,
				[@customer, { amount: 100 }]
			)
			@auto_top_up.notify!
			assert_mock tx
		end
		em :test_notify!

		def test_decline_notify!
			@customer.expect(
				:stanza_to,
				nil,
				[Matching.new { |m|
					assert_equal(
						"Automatic top-up transaction for $100 failed: test",
						m.body
					)
				}]
			)
			LowBalance::AutoTopUp::Transaction.expect(
				:sale,
				EMPromise.reject(RuntimeError.new("test")),
				[@customer, { amount: 100 }]
			)
			@auto_top_up.notify!.sync
			assert_mock @customer
		end
		em :test_decline_notify!
	end
end