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