~singpolyma/sgx-jmp

ref: a9ebca9a66da1fa0e10bee066a6ba71fe31177a3 sgx-jmp/test/test_transaction.rb -rw-r--r-- 2.2 KiB
a9ebca9aStephen Paul Weber Usage command 1 year, 5 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
# 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: 123
		)

	def test_sale_fails
		Transaction::REDIS.expect(
			:get,
			EMPromise.resolve("1"),
			["jmp_pay_decline-test"]
		)
		Transaction::REDIS.expect(
			:incr,
			EMPromise.resolve(nil),
			["jmp_pay_decline-test"]
		)
		Transaction::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.new("test", plan_name: "test_usd"),
				amount: 123,
				payment_method: OpenStruct.new(token: "token")
			).sync
		end
	end
	em :test_sale_fails

	def test_sale
		Transaction::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.new("test", plan_name: "test_usd"),
			amount: 123,
			payment_method: OpenStruct.new(token: "token")
		).sync
		assert_kind_of Transaction, result
	end
	em :test_sale

	def test_insert
		Transaction::DB.expect(
			:exec_defer,
			EMPromise.resolve(nil),
			[
				String,
				["customer", "transaction", Time.at(0), 123]
			]
		)
		Transaction.new(FAKE_BRAINTREE_TRANSACTION).insert.sync
	end
	em :test_insert
end