~singpolyma/sgx-jmp

ref: c3da8bdb25cdaded490a0ed1870c4f51ce5edcf3 sgx-jmp/test/test_low_balance.rb -rw-r--r-- 2.3 KiB
c3da8bdbStephen Paul Weber Fix typo, add test 1 year, 1 month 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
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true

require "test_helper"
require "low_balance"

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

class LowBalanceTest < Minitest::Test
	def test_for_locked
		ExpiringLock::REDIS.expect(
			:exists,
			EMPromise.resolve(1),
			["jmp_customer_low_balance-test"]
		)
		assert_kind_of LowBalance::Locked, LowBalance.for(customer).sync
	end
	em :test_for_locked

	def test_for_no_auto_top_up
		ExpiringLock::REDIS.expect(
			:exists,
			EMPromise.resolve(0),
			["jmp_customer_low_balance-test"]
		)
		Customer::REDIS.expect(
			:smembers,
			EMPromise.resolve([]),
			["jmp_customer_btc_addresses-test"]
		)
		ExpiringLock::REDIS.expect(
			:setex,
			EMPromise.resolve(nil),
			["jmp_customer_low_balance-test", 60 * 60 * 24 * 7, Time]
		)
		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(
			:exists,
			EMPromise.resolve(0),
			["jmp_customer_low_balance-test"]
		)
		ExpiringLock::REDIS.expect(
			:setex,
			EMPromise.resolve(nil),
			["jmp_customer_low_balance-test", 60 * 60 * 24 * 7, Time]
		)
		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