1# frozen_string_literal: true
2
3require "test_helper"
4require "customer"
5
6Customer::BLATHER = Minitest::Mock.new
7Customer::BRAINTREE = Minitest::Mock.new
8Customer::ELECTRUM = Minitest::Mock.new
9Customer::REDIS = Minitest::Mock.new
10Customer::DB = Minitest::Mock.new
11Customer::IQ_MANAGER = Minitest::Mock.new
12CustomerPlan::DB = Minitest::Mock.new
13CustomerUsage::REDIS = Minitest::Mock.new
14CustomerUsage::DB = Minitest::Mock.new
15
16class SipAccount
17 public :username, :url
18
19 class New
20 public :username
21 end
22end
23
24class CustomerTest < Minitest::Test
25 def test_bill_plan_activate
26 CustomerPlan::DB.expect(:transaction, nil) do |&block|
27 block.call
28 true
29 end
30 CustomerPlan::DB.expect(
31 :exec,
32 nil,
33 [
34 String,
35 Matching.new do |params|
36 params[0] == "test" &&
37 params[1].is_a?(String) &&
38 BigDecimal(-1) == params[2]
39 end
40 ]
41 )
42 CustomerPlan::DB.expect(
43 :exec,
44 OpenStruct.new(cmd_tuples: 1),
45 [String, ["test", "test_usd"]]
46 )
47 customer(plan_name: "test_usd").bill_plan.sync
48 CustomerPlan::DB.verify
49 end
50 em :test_bill_plan_activate
51
52 def test_bill_plan_update
53 CustomerPlan::DB.expect(:transaction, nil) do |&block|
54 block.call
55 true
56 end
57 CustomerPlan::DB.expect(
58 :exec,
59 nil,
60 [
61 String,
62 Matching.new do |params|
63 params[0] == "test" &&
64 params[1].is_a?(String) &&
65 BigDecimal(-1) == params[2]
66 end
67 ]
68 )
69 CustomerPlan::DB.expect(
70 :exec,
71 OpenStruct.new(cmd_tuples: 0),
72 [String, ["test", "test_usd"]]
73 )
74 CustomerPlan::DB.expect(:exec, nil, [String, ["test"]])
75 customer(plan_name: "test_usd").bill_plan.sync
76 CustomerPlan::DB.verify
77 end
78 em :test_bill_plan_update
79
80 def test_stanza_to
81 Customer::BLATHER.expect(
82 :<<,
83 nil,
84 [Matching.new do |stanza|
85 assert_equal "+15555550000@component/a", stanza.from.to_s
86 assert_equal "test@example.net/b", stanza.to.to_s
87 end]
88 )
89 m = Blather::Stanza::Message.new
90 m.from = "+15555550000@sgx/a"
91 m.to = "customer_test@component/b"
92 customer.stanza_to(m)
93 assert_mock Customer::BLATHER
94 end
95 em :test_stanza_to
96
97 def test_stanza_from
98 Customer::BLATHER.expect(
99 :<<,
100 nil,
101 [Matching.new do |stanza|
102 assert_equal "customer_test@component/a", stanza.from.to_s
103 assert_equal "+15555550000@sgx/b", stanza.to.to_s
104 end]
105 )
106 m = Blather::Stanza::Message.new
107 m.from = "test@example.com/a"
108 m.to = "+15555550000@component/b"
109 customer.stanza_from(m)
110 Customer::BLATHER.verify
111 end
112
113 def test_fetch_vcard_temp
114 result = Blather::Stanza::Iq::Vcard.new(:result)
115 result.vcard["FN"] = "name"
116 Customer::IQ_MANAGER.expect(
117 :method,
118 ->(*) { EMPromise.resolve(result) },
119 [:write]
120 )
121 assert_equal "name", customer.fetch_vcard_temp("+15551234567").sync["FN"]
122 end
123 em :test_fetch_vcard_temp
124
125 def test_customer_usage_report
126 report_for = (Date.today..(Date.today - 1))
127 report_for.first.downto(report_for.last).each.with_index do |day, idx|
128 CustomerUsage::REDIS.expect(
129 :zscore,
130 EMPromise.resolve(idx),
131 ["jmp_customer_outbound_messages-test", day.strftime("%Y%m%d")]
132 )
133 end
134 CustomerUsage::DB.expect(
135 :query_defer,
136 EMPromise.resolve([{ "day" => report_for.first, "minutes" => 123 }]),
137 [String, ["test", report_for.first, report_for.last]]
138 )
139 assert_equal(
140 UsageReport.new(
141 report_for, {
142 Date.today => 0,
143 (Date.today - 1) => 1
144 },
145 Date.today => 123
146 ),
147 customer.usage_report(report_for).sync
148 )
149 end
150 em :test_customer_usage_report
151
152 def test_sip_account_new
153 req = stub_request(
154 :get,
155 "https://api.catapult.inetwork.com/v1/users/" \
156 "catapult_user/domains/catapult_domain/endpoints?page=0&size=1000"
157 ).with(
158 headers: {
159 "Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
160 }
161 ).to_return(status: 404)
162 sip = customer.sip_account.sync
163 assert_kind_of SipAccount::New, sip
164 assert_equal "test", sip.username
165 assert_requested req
166 end
167 em :test_sip_account_new
168
169 def test_sip_account_existing
170 req1 = stub_request(
171 :get,
172 "https://api.catapult.inetwork.com/v1/users/" \
173 "catapult_user/domains/catapult_domain/endpoints?page=0&size=1000"
174 ).with(
175 headers: {
176 "Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
177 }
178 ).to_return(status: 200, body: [
179 { name: "NOTtest", domainId: "domain", id: "endpoint" }
180 ].to_json)
181
182 req2 = stub_request(
183 :get,
184 "https://api.catapult.inetwork.com/v1/users/" \
185 "catapult_user/domains/catapult_domain/endpoints?page=1&size=1000"
186 ).with(
187 headers: {
188 "Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
189 }
190 ).to_return(status: 200, body: [
191 { name: "test", domainId: "domain", id: "endpoint" }
192 ].to_json)
193
194 sip = customer.sip_account.sync
195 assert_kind_of SipAccount, sip
196 assert_equal "test", sip.username
197 assert_equal(
198 "https://api.catapult.inetwork.com/v1/users/" \
199 "catapult_user/domains/domain/endpoints/endpoint",
200 sip.url
201 )
202
203 assert_requested req1
204 assert_requested req2
205 end
206 em :test_sip_account_existing
207
208 def test_sip_account_error
209 stub_request(
210 :get,
211 "https://api.catapult.inetwork.com/v1/users/" \
212 "catapult_user/domains/catapult_domain/endpoints?page=0&size=1000"
213 ).to_return(status: 400)
214
215 assert_raises(RuntimeError) do
216 customer.sip_account.sync
217 end
218 end
219 em :test_sip_account_error
220
221 def test_btc_addresses
222 Customer::REDIS.expect(
223 :smembers,
224 EMPromise.resolve(["testaddr"]),
225 ["jmp_customer_btc_addresses-test"]
226 )
227 assert_equal ["testaddr"], customer.btc_addresses.sync
228 assert_mock Customer::REDIS
229 end
230 em :test_btc_addresses
231
232 def test_add_btc_address
233 Customer::REDIS.expect(
234 :spopsadd,
235 EMPromise.resolve("testaddr"),
236 [["jmp_available_btc_addresses", "jmp_customer_btc_addresses-test"]]
237 )
238 Customer::ELECTRUM.expect(
239 :notify,
240 EMPromise.resolve(nil),
241 ["testaddr", "http://notify.example.com"]
242 )
243 assert_equal "testaddr", customer.add_btc_address.sync
244 assert_mock Customer::REDIS
245 assert_mock Customer::ELECTRUM
246 end
247 em :test_add_btc_address
248end