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