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