1# frozen_string_literal: true
2
3require "test_helper"
4require "customer"
5
6Customer::BLATHER = Minitest::Mock.new
7Customer::BRAINTREE = Minitest::Mock.new
8Customer::REDIS = Minitest::Mock.new
9Customer::DB = Minitest::Mock.new
10CustomerPlan::DB = Minitest::Mock.new
11
12class CustomerTest < Minitest::Test
13 def test_for_jid
14 Customer::REDIS.expect(
15 :get,
16 EMPromise.resolve(1),
17 ["jmp_customer_id-test@example.com"]
18 )
19 Customer::DB.expect(
20 :query_defer,
21 EMPromise.resolve([{ balance: 1234, plan_name: "test_usd" }]),
22 [String, [1]]
23 )
24 customer = Customer.for_jid("test@example.com").sync
25 assert_kind_of Customer, customer
26 assert_equal 1234, customer.balance
27 assert_equal "merchant_usd", customer.merchant_account
28 end
29 em :test_for_jid
30
31 def test_for_jid_not_found
32 Customer::REDIS.expect(
33 :get,
34 EMPromise.resolve(nil),
35 ["jmp_customer_id-test2@example.com"]
36 )
37 assert_raises do
38 Customer.for_jid("test2@example.com").sync
39 end
40 end
41 em :test_for_jid_not_found
42
43 def test_for_customer_id_not_found
44 Customer::DB.expect(
45 :query_defer,
46 EMPromise.resolve([]),
47 [String, [7357]]
48 )
49 customer = Customer.for_customer_id(7357).sync
50 assert_equal BigDecimal.new(0), customer.balance
51 end
52 em :test_for_customer_id_not_found
53
54 def test_create
55 braintree_customer = Minitest::Mock.new
56 Customer::BRAINTREE.expect(:customer, braintree_customer)
57 braintree_customer.expect(:create, EMPromise.resolve(
58 OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
59 ))
60 Customer::REDIS.expect(
61 :msetnx,
62 EMPromise.resolve(1),
63 [
64 "jmp_customer_id-test@example.com", "test",
65 "jmp_customer_jid-test", "test@example.com"
66 ]
67 )
68 assert_kind_of Customer, Customer.create("test@example.com").sync
69 braintree_customer.verify
70 Customer::REDIS.verify
71 end
72 em :test_create
73
74 def test_bill_plan_activate
75 CustomerPlan::DB.expect(:transaction, nil) do |&block|
76 block.call
77 true
78 end
79 CustomerPlan::DB.expect(
80 :exec,
81 nil,
82 [
83 String,
84 Matching.new do |params|
85 params[0] == "test" &&
86 params[1].is_a?(String) &&
87 BigDecimal.new(-1) == params[2]
88 end
89 ]
90 )
91 CustomerPlan::DB.expect(
92 :exec,
93 OpenStruct.new(cmd_tuples: 1),
94 [String, ["test", "test_usd"]]
95 )
96 Customer.new("test", plan_name: "test_usd").bill_plan.sync
97 CustomerPlan::DB.verify
98 end
99 em :test_bill_plan_activate
100
101 def test_bill_plan_update
102 CustomerPlan::DB.expect(:transaction, nil) do |&block|
103 block.call
104 true
105 end
106 CustomerPlan::DB.expect(
107 :exec,
108 nil,
109 [
110 String,
111 Matching.new do |params|
112 params[0] == "test" &&
113 params[1].is_a?(String) &&
114 BigDecimal.new(-1) == params[2]
115 end
116 ]
117 )
118 CustomerPlan::DB.expect(
119 :exec,
120 OpenStruct.new(cmd_tuples: 0),
121 [String, ["test", "test_usd"]]
122 )
123 CustomerPlan::DB.expect(:exec, nil, [String, ["test"]])
124 Customer.new("test", plan_name: "test_usd").bill_plan.sync
125 CustomerPlan::DB.verify
126 end
127 em :test_bill_plan_update
128
129 def test_jid
130 Customer::REDIS.expect(
131 :get,
132 EMPromise.resolve("test@example.com"),
133 ["jmp_customer_jid-test"]
134 )
135 jid = Customer.new("test").jid.sync
136 assert_kind_of Blather::JID, jid
137 assert_equal "test@example.com", jid.to_s
138 Customer::REDIS.verify
139 end
140 em :test_jid
141
142 def test_stanza_to
143 Customer::REDIS.expect(
144 :get,
145 EMPromise.resolve("test@example.com"),
146 ["jmp_customer_jid-test"]
147 )
148 Customer::BLATHER.expect(
149 :<<,
150 nil,
151 [Matching.new do |stanza|
152 assert_equal "+15555550000@component/a", stanza.from.to_s
153 assert_equal "test@example.com/b", stanza.to.to_s
154 end]
155 )
156 m = Blather::Stanza::Message.new
157 m.from = "+15555550000@sgx/a"
158 m.to = "customer_test@component/b"
159 Customer.new("test").stanza_to(m).sync
160 Customer::BLATHER.verify
161 end
162 em :test_stanza_to
163
164 def test_stanza_from
165 Customer::BLATHER.expect(
166 :<<,
167 nil,
168 [Matching.new do |stanza|
169 assert_equal "customer_test@component/a", stanza.from.to_s
170 assert_equal "+15555550000@sgx/b", stanza.to.to_s
171 end]
172 )
173 m = Blather::Stanza::Message.new
174 m.from = "test@example.com/a"
175 m.to = "+15555550000@component/b"
176 Customer.new("test").stanza_from(m)
177 Customer::BLATHER.verify
178 end
179end