~singpolyma/sgx-jmp

sgx-jmp/test/test_transaction.rb -rw-r--r-- 4.7 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# 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
TrustLevelRepo::REDIS = Minitest::Mock.new
TrustLevelRepo::DB = 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
		Transaction::REDIS.expect(
			:exists,
			EMPromise.resolve(0),
			["jmp_customer_credit_card_lock-test"]
		)
		TrustLevelRepo::REDIS.expect(
			:get,
			EMPromise.resolve("Customer"),
			["jmp_customer_trust_level-test"]
		)
		TrustLevelRepo::DB.expect(
			:query_one,
			EMPromise.resolve({}),
			[String, "test", Hash]
		)
		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: 99,
				payment_method: OpenStruct.new(token: "token")
			).sync
		end
		assert_mock CustomerFinancials::REDIS
		assert_mock Transaction::REDIS
		assert_mock TrustLevelRepo::REDIS
		assert_mock TrustLevelRepo::DB
	end
	em :test_sale_fails

	def test_sale_locked
		Transaction::REDIS.expect(
			:exists,
			EMPromise.resolve(1),
			["jmp_customer_credit_card_lock-test"]
		)
		TrustLevelRepo::REDIS.expect(
			:get,
			EMPromise.resolve("Customer"),
			["jmp_customer_trust_level-test"]
		)
		TrustLevelRepo::DB.expect(
			:query_one,
			EMPromise.resolve({}),
			[String, "test", Hash]
		)
		CustomerFinancials::REDIS.expect(
			:get,
			EMPromise.resolve("0"),
			["jmp_pay_decline-test"]
		)
		assert_raises("locked") do
			Transaction.sale(
				customer(plan_name: "test_usd"),
				amount: 123,
				payment_method: OpenStruct.new(token: "token")
			).sync
		end
		assert_mock CustomerFinancials::REDIS
		assert_mock Transaction::REDIS
		assert_mock TrustLevelRepo::REDIS
		assert_mock TrustLevelRepo::DB
	end
	em :test_sale_locked

	def test_sale
		Transaction::REDIS.expect(
			:exists,
			EMPromise.resolve(0),
			["jmp_customer_credit_card_lock-test"]
		)
		TrustLevelRepo::REDIS.expect(
			:get,
			EMPromise.resolve("Customer"),
			["jmp_customer_trust_level-test"]
		)
		TrustLevelRepo::DB.expect(
			:query_one,
			EMPromise.resolve({}),
			[String, "test", Hash]
		)
		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: 99,
				payment_method_token: "token",
				merchant_account_id: "merchant_usd",
				options: { submit_for_settlement: true }
			}]
		)
		Transaction::REDIS.expect(
			:setex,
			EMPromise.resolve(1),
			["jmp_customer_credit_card_lock-test", 86400, "1"]
		)
		result = Transaction.sale(
			customer(plan_name: "test_usd"),
			amount: 99,
			payment_method: OpenStruct.new(token: "token")
		).sync
		assert_kind_of Transaction, result
		assert_mock CustomerFinancials::REDIS
		assert_mock Transaction::REDIS
		assert_mock TrustLevelRepo::REDIS
		assert_mock TrustLevelRepo::DB
	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), Time.at(7776000), 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), Time.at(7776000), 100]
			]
		)
		Transaction::DB.expect(
			:exec,
			EMPromise.resolve(nil),
			[
				String,
				["customer", "bonus_for_transaction", Time.at(0), Time.at(7776000), 3]
			]
		)
		tx = FAKE_BRAINTREE_TRANSACTION.dup
		tx.amount = 100
		Transaction.new(tx).insert.sync
		Transaction::DB.verify
	end
	em :test_insert_with_bonus
end