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
10Customer::IQ_MANAGER = Minitest::Mock.new
11CustomerPlan::DB = Minitest::Mock.new
12CustomerUsage::REDIS = Minitest::Mock.new
13CustomerUsage::DB = Minitest::Mock.new
14CustomerFinancials::REDIS = Minitest::Mock.new
15CustomerFinancials::ELECTRUM = Minitest::Mock.new
16CustomerFinancials::BRAINTREE = Minitest::Mock.new
17
18class CustomerTest < Minitest::Test
19 def test_bill_plan_activate
20 CustomerPlan::DB.expect(:transaction, nil) do |&block|
21 block.call
22 true
23 end
24 CustomerPlan::DB.expect(
25 :exec,
26 nil,
27 [
28 String,
29 Matching.new do |params|
30 params[0] == "test" &&
31 params[1].is_a?(String) &&
32 BigDecimal(-1) == params[2]
33 end
34 ]
35 )
36 CustomerPlan::DB.expect(
37 :exec,
38 OpenStruct.new(cmd_tuples: 1),
39 [String, ["test", "test_usd", nil]]
40 )
41 CustomerPlan::DB.expect(
42 :exec,
43 OpenStruct.new(cmd_tuples: 0),
44 [String, ["test"]]
45 )
46 customer(plan_name: "test_usd").bill_plan.sync
47 CustomerPlan::DB.verify
48 end
49 em :test_bill_plan_activate
50
51 def test_bill_plan_reactivate_child
52 CustomerPlan::DB.expect(:transaction, nil) do |&block|
53 block.call
54 true
55 end
56 CustomerPlan::DB.expect(
57 :exec,
58 nil,
59 [
60 String,
61 Matching.new do |params|
62 params[0] == "test" &&
63 params[1].is_a?(String) &&
64 BigDecimal(-1) == params[2]
65 end
66 ]
67 )
68 CustomerPlan::DB.expect(
69 :exec,
70 OpenStruct.new(cmd_tuples: 1),
71 [String, ["test", "test_usd", "parent"]]
72 )
73 CustomerPlan::DB.expect(
74 :exec,
75 OpenStruct.new(cmd_tuples: 0),
76 [String, ["test"]]
77 )
78 customer(plan_name: "test_usd", parent_customer_id: "parent").bill_plan.sync
79 CustomerPlan::DB.verify
80 end
81 em :test_bill_plan_reactivate_child
82
83 def test_bill_plan_update
84 CustomerPlan::DB.expect(:transaction, nil) do |&block|
85 block.call
86 true
87 end
88 CustomerPlan::DB.expect(
89 :exec,
90 nil,
91 [
92 String,
93 Matching.new do |params|
94 params[0] == "test" &&
95 params[1].is_a?(String) &&
96 BigDecimal(-1) == params[2]
97 end
98 ]
99 )
100 CustomerPlan::DB.expect(
101 :exec,
102 OpenStruct.new(cmd_tuples: 0),
103 [String, ["test", "test_usd", nil]]
104 )
105 CustomerPlan::DB.expect(:exec, nil, [String, ["test"]])
106 customer(plan_name: "test_usd").bill_plan.sync
107 CustomerPlan::DB.verify
108 end
109 em :test_bill_plan_update
110
111 def test_stanza_to
112 Customer::BLATHER.expect(
113 :<<,
114 nil,
115 [Matching.new do |stanza|
116 assert_equal "+15555550000@component/a", stanza.from.to_s
117 assert_equal "test@example.net/b", stanza.to.to_s
118 end]
119 )
120 m = Blather::Stanza::Message.new
121 m.from = "+15555550000@sgx/a"
122 m.to = "customer_test@component/b"
123 customer.stanza_to(m)
124 assert_mock Customer::BLATHER
125 end
126 em :test_stanza_to
127
128 def test_stanza_from
129 Customer::BLATHER.expect(
130 :<<,
131 nil,
132 [Matching.new do |stanza|
133 assert_equal "customer_test@component/a", stanza.from.to_s
134 assert_equal "+15555550000@sgx/b", stanza.to.to_s
135 end]
136 )
137 m = Blather::Stanza::Message.new
138 m.from = "test@example.com/a"
139 m.to = "+15555550000@component/b"
140 customer.stanza_from(m)
141 Customer::BLATHER.verify
142 end
143
144 def test_fetch_pep
145 result = Blather::Stanza::PubSub::Items.new(:result)
146 result.items_node <<
147 Blather::Stanza::PubSubItem.new("current", Nokogiri.parse(<<~XML).root)
148 <vcard xmlns="urn:ietf:params:xml:ns:vcard-4.0">
149 <fn><text>A Human</text></fn>
150 </vcard4>
151 XML
152 Customer::IQ_MANAGER.expect(
153 :method,
154 ->(*) { EMPromise.resolve(result) },
155 [:write]
156 )
157 assert_equal(
158 "A Human",
159 customer.fetch_pep("urn:xmpp:vcard4", "+15551234567").sync
160 .first.payload_node.find_first(
161 "./ns:fn/ns:text",
162 ns: "urn:ietf:params:xml:ns:vcard-4.0"
163 )&.content
164 )
165 end
166 em :test_fetch_pep
167
168 def test_customer_usage_report
169 report_for = (Date.today..(Date.today - 1))
170 report_for.first.downto(report_for.last).each.with_index do |day, idx|
171 CustomerUsage::REDIS.expect(
172 :zscore,
173 EMPromise.resolve(idx),
174 ["jmp_customer_outbound_messages-test", day.strftime("%Y%m%d")]
175 )
176 end
177 CustomerUsage::DB.expect(
178 :query_defer,
179 EMPromise.resolve([{ "day" => report_for.first, "minutes" => 123 }]),
180 [String, ["test", report_for.first, report_for.last]]
181 )
182 assert_equal(
183 UsageReport.new(
184 report_for, {
185 Date.today => 0,
186 (Date.today - 1) => 1
187 },
188 Date.today => 123
189 ),
190 customer.usage_report(report_for).sync
191 )
192 end
193 em :test_customer_usage_report
194
195 def test_sip_account_new
196 req = stub_request(
197 :get,
198 "https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/test"
199 ).with(
200 headers: {
201 "Authorization" => "Basic Og=="
202 }
203 ).to_return(
204 status: 404,
205 body:
206 "<r><ResponseStatus><ErrorCode>0</ErrorCode>" \
207 "<Description>desc</Description></ResponseStatus></r>"
208 )
209 sip = customer.sip_account
210 assert_kind_of SipAccount::New, sip
211 assert_equal "test", sip.username
212 assert_requested req
213 end
214 em :test_sip_account_new
215
216 def test_sip_account_existing
217 req1 = stub_request(
218 :get,
219 "https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/test"
220 ).with(
221 headers: {
222 "Authorization" => "Basic Og=="
223 }
224 ).to_return(status: 200, body: {
225 SipCredential: {
226 UserName: "test",
227 Realm: "sip.example.com"
228 }
229 }.to_xml)
230
231 sip = customer.sip_account
232 assert_kind_of SipAccount, sip
233 assert_equal "test", sip.username
234
235 assert_requested req1
236 end
237 em :test_sip_account_existing
238
239 def test_sip_account_error
240 stub_request(
241 :get,
242 "https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/test"
243 ).to_return(
244 status: 404,
245 body:
246 "<r><ResponseStatus><ErrorCode>0</ErrorCode>" \
247 "<Description>desc</Description></ResponseStatus></r>"
248 )
249
250 assert_equal "test", customer.sip_account.username
251 end
252 em :test_sip_account_error
253
254 def test_btc_addresses
255 CustomerFinancials::REDIS.expect(
256 :smembers,
257 EMPromise.resolve(["testaddr"]),
258 ["jmp_customer_btc_addresses-test"]
259 )
260 assert_equal ["testaddr"], customer.btc_addresses.sync
261 assert_mock Customer::REDIS
262 end
263 em :test_btc_addresses
264
265 def test_add_btc_address
266 CustomerFinancials::REDIS.expect(
267 :spopsadd,
268 EMPromise.resolve("testaddr"),
269 [["jmp_available_btc_addresses", "jmp_customer_btc_addresses-test"]]
270 )
271 CustomerFinancials::ELECTRUM.expect(
272 :notify,
273 EMPromise.resolve(nil),
274 ["testaddr", "http://notify.example.com"]
275 )
276 assert_equal "testaddr", customer.add_btc_address.sync
277 assert_mock CustomerFinancials::REDIS
278 assert_mock CustomerFinancials::ELECTRUM
279 end
280 em :test_add_btc_address
281end