~singpolyma/sgx-jmp

ref: a5cf98aa2aef066c2317bc3a822d099743f3a815 sgx-jmp/test/test_transaction.rb -rw-r--r-- 2.9 KiB
a5cf98aaChristopher Vollick Customer Financials 8 months 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# frozen_string_literal: true

require "test_helper"
require "customer"
require "transaction"

Transaction::DB = Minitest::Mock.new
Transaction::BRAINTREE = Minitest::Mock.new
Transaction::REDIS = Minitest::Mock.new

class TransactionTest < Minitest::Test
	FAKE_BRAINTREE_TRANSACTION =
		OpenStruct.new(
			customer_details: OpenStruct.new(id: "customer"),
			id: "transaction",
			created_at: Time.at(0),
			amount: 12
		)

	def test_sale_fails
		CustomerFinancials::REDIS.expect(
			:get,
			EMPromise.resolve("1"),
			["jmp_pay_decline-test"]
		)
		CustomerFinancials::REDIS.expect(
			:incr,
			EMPromise.resolve(nil),
			["jmp_pay_decline-test"]
		)
		CustomerFinancials::REDIS.expect(
			:expire,
			EMPromise.resolve(nil),
			["jmp_pay_decline-test", 60 * 60 * 24]
		)
		braintree_transaction = Minitest::Mock.new
		Transaction::BRAINTREE.expect(:transaction, braintree_transaction)
		braintree_transaction.expect(
			:sale,
			EMPromise.resolve(
				OpenStruct.new(success?: false, message: "declined")
			),
			[Hash]
		)
		assert_raises("declined") do
			Transaction.sale(
				customer(plan_name: "test_usd"),
				amount: 123,
				payment_method: OpenStruct.new(token: "token")
			).sync
		end
		assert_mock CustomerFinancials::REDIS
	end
	em :test_sale_fails

	def test_sale
		CustomerFinancials::REDIS.expect(
			:get,
			EMPromise.resolve("1"),
			["jmp_pay_decline-test"]
		)
		braintree_transaction = Minitest::Mock.new
		Transaction::BRAINTREE.expect(:transaction, braintree_transaction)
		braintree_transaction.expect(
			:sale,
			EMPromise.resolve(
				OpenStruct.new(
					success?: true,
					transaction: FAKE_BRAINTREE_TRANSACTION
				)
			),
			[{
				amount: 123,
				payment_method_token: "token",
				merchant_account_id: "merchant_usd",
				options: { submit_for_settlement: true }
			}]
		)
		result = Transaction.sale(
			customer(plan_name: "test_usd"),
			amount: 123,
			payment_method: OpenStruct.new(token: "token")
		).sync
		assert_kind_of Transaction, result
		assert_mock CustomerFinancials::REDIS
	end
	em :test_sale

	def test_insert
		Transaction::DB.expect(:transaction, []) do |&block|
			block.call
			true
		end
		Transaction::DB.expect(
			:exec,
			EMPromise.resolve(nil),
			[
				String,
				["customer", "transaction", Time.at(0), 12]
			]
		)
		Transaction.new(FAKE_BRAINTREE_TRANSACTION).insert.sync
		Transaction::DB.verify
	end
	em :test_insert

	def test_insert_with_bonus
		Transaction::DB.expect(:transaction, []) do |&block|
			block.call
			true
		end
		Transaction::DB.expect(
			:exec,
			EMPromise.resolve(nil),
			[
				String,
				["customer", "transaction", Time.at(0), 100]
			]
		)
		Transaction::DB.expect(
			:exec,
			EMPromise.resolve(nil),
			[
				String,
				["customer", "bonus_for_transaction", Time.at(0), 3]
			]
		)
		tx = FAKE_BRAINTREE_TRANSACTION.dup
		tx.amount = 100
		Transaction.new(tx).insert.sync
		Transaction::DB.verify
	end
	em :test_insert_with_bonus
end