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(status: 404)
160 sip = customer.sip_account
161 assert_kind_of SipAccount::New, sip
162 assert_equal "test", sip.username
163 assert_requested req
164 end
165 em :test_sip_account_new
166
167 def test_sip_account_existing
168 req1 = stub_request(
169 :get,
170 "https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/test"
171 ).with(
172 headers: {
173 "Authorization" => "Basic Og=="
174 }
175 ).to_return(status: 200, body: {
176 SipCredential: {
177 UserName: "test",
178 Realm: "sip.example.com"
179 }
180 }.to_xml)
181
182 sip = customer.sip_account
183 assert_kind_of SipAccount, sip
184 assert_equal "test", sip.username
185
186 assert_requested req1
187 end
188 em :test_sip_account_existing
189
190 def test_sip_account_error
191 stub_request(
192 :get,
193 "https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/test"
194 ).to_return(status: 404)
195
196 assert_equal "test", customer.sip_account.username
197 end
198 em :test_sip_account_error
199
200 def test_btc_addresses
201 CustomerFinancials::REDIS.expect(
202 :smembers,
203 EMPromise.resolve(["testaddr"]),
204 ["jmp_customer_btc_addresses-test"]
205 )
206 assert_equal ["testaddr"], customer.btc_addresses.sync
207 assert_mock Customer::REDIS
208 end
209 em :test_btc_addresses
210
211 def test_add_btc_address
212 CustomerFinancials::REDIS.expect(
213 :spopsadd,
214 EMPromise.resolve("testaddr"),
215 [["jmp_available_btc_addresses", "jmp_customer_btc_addresses-test"]]
216 )
217 CustomerFinancials::ELECTRUM.expect(
218 :notify,
219 EMPromise.resolve(nil),
220 ["testaddr", "http://notify.example.com"]
221 )
222 assert_equal "testaddr", customer.add_btc_address.sync
223 assert_mock CustomerFinancials::REDIS
224 assert_mock CustomerFinancials::ELECTRUM
225 end
226 em :test_add_btc_address
227end