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"]]
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_update
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: 0),
71 [String, ["test", "test_usd"]]
72 )
73 CustomerPlan::DB.expect(:exec, nil, [String, ["test"]])
74 customer(plan_name: "test_usd").bill_plan.sync
75 CustomerPlan::DB.verify
76 end
77 em :test_bill_plan_update
78
79 def test_stanza_to
80 Customer::BLATHER.expect(
81 :<<,
82 nil,
83 [Matching.new do |stanza|
84 assert_equal "+15555550000@component/a", stanza.from.to_s
85 assert_equal "test@example.net/b", stanza.to.to_s
86 end]
87 )
88 m = Blather::Stanza::Message.new
89 m.from = "+15555550000@sgx/a"
90 m.to = "customer_test@component/b"
91 customer.stanza_to(m)
92 assert_mock Customer::BLATHER
93 end
94 em :test_stanza_to
95
96 def test_stanza_from
97 Customer::BLATHER.expect(
98 :<<,
99 nil,
100 [Matching.new do |stanza|
101 assert_equal "customer_test@component/a", stanza.from.to_s
102 assert_equal "+15555550000@sgx/b", stanza.to.to_s
103 end]
104 )
105 m = Blather::Stanza::Message.new
106 m.from = "test@example.com/a"
107 m.to = "+15555550000@component/b"
108 customer.stanza_from(m)
109 Customer::BLATHER.verify
110 end
111
112 def test_fetch_vcard_temp
113 result = Blather::Stanza::Iq::Vcard.new(:result)
114 result.vcard["FN"] = "name"
115 Customer::IQ_MANAGER.expect(
116 :method,
117 ->(*) { EMPromise.resolve(result) },
118 [:write]
119 )
120 assert_equal "name", customer.fetch_vcard_temp("+15551234567").sync["FN"]
121 end
122 em :test_fetch_vcard_temp
123
124 def test_customer_usage_report
125 report_for = (Date.today..(Date.today - 1))
126 report_for.first.downto(report_for.last).each.with_index do |day, idx|
127 CustomerUsage::REDIS.expect(
128 :zscore,
129 EMPromise.resolve(idx),
130 ["jmp_customer_outbound_messages-test", day.strftime("%Y%m%d")]
131 )
132 end
133 CustomerUsage::DB.expect(
134 :query_defer,
135 EMPromise.resolve([{ "day" => report_for.first, "minutes" => 123 }]),
136 [String, ["test", report_for.first, report_for.last]]
137 )
138 assert_equal(
139 UsageReport.new(
140 report_for, {
141 Date.today => 0,
142 (Date.today - 1) => 1
143 },
144 Date.today => 123
145 ),
146 customer.usage_report(report_for).sync
147 )
148 end
149 em :test_customer_usage_report
150
151 def test_sip_account_new
152 req = stub_request(
153 :get,
154 "https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/test"
155 ).with(
156 headers: {
157 "Authorization" => "Basic Og=="
158 }
159 ).to_return(
160 status: 404,
161 body:
162 "<r><ResponseStatus><ErrorCode>0</ErrorCode>" \
163 "<Description>desc</Description></ResponseStatus></r>"
164 )
165 sip = customer.sip_account
166 assert_kind_of SipAccount::New, sip
167 assert_equal "test", sip.username
168 assert_requested req
169 end
170 em :test_sip_account_new
171
172 def test_sip_account_existing
173 req1 = stub_request(
174 :get,
175 "https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/test"
176 ).with(
177 headers: {
178 "Authorization" => "Basic Og=="
179 }
180 ).to_return(status: 200, body: {
181 SipCredential: {
182 UserName: "test",
183 Realm: "sip.example.com"
184 }
185 }.to_xml)
186
187 sip = customer.sip_account
188 assert_kind_of SipAccount, sip
189 assert_equal "test", sip.username
190
191 assert_requested req1
192 end
193 em :test_sip_account_existing
194
195 def test_sip_account_error
196 stub_request(
197 :get,
198 "https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/test"
199 ).to_return(
200 status: 404,
201 body:
202 "<r><ResponseStatus><ErrorCode>0</ErrorCode>" \
203 "<Description>desc</Description></ResponseStatus></r>"
204 )
205
206 assert_equal "test", customer.sip_account.username
207 end
208 em :test_sip_account_error
209
210 def test_btc_addresses
211 CustomerFinancials::REDIS.expect(
212 :smembers,
213 EMPromise.resolve(["testaddr"]),
214 ["jmp_customer_btc_addresses-test"]
215 )
216 assert_equal ["testaddr"], customer.btc_addresses.sync
217 assert_mock Customer::REDIS
218 end
219 em :test_btc_addresses
220
221 def test_add_btc_address
222 CustomerFinancials::REDIS.expect(
223 :spopsadd,
224 EMPromise.resolve("testaddr"),
225 [["jmp_available_btc_addresses", "jmp_customer_btc_addresses-test"]]
226 )
227 CustomerFinancials::ELECTRUM.expect(
228 :notify,
229 EMPromise.resolve(nil),
230 ["testaddr", "http://notify.example.com"]
231 )
232 assert_equal "testaddr", customer.add_btc_address.sync
233 assert_mock CustomerFinancials::REDIS
234 assert_mock CustomerFinancials::ELECTRUM
235 end
236 em :test_add_btc_address
237end