1# frozen_string_literal: true
2
3require "simplecov"
4SimpleCov.start do
5 add_filter "/test/"
6 enable_coverage :branch
7end
8
9require "em_promise"
10require "fiber"
11require "minitest/autorun"
12require "rantly/minitest_extensions"
13require "sentry-ruby"
14require "webmock/minitest"
15begin
16 require "pry-rescue/minitest"
17 require "pry-reload"
18
19 module Minitest
20 class Test
21 alias old_capture_exceptions capture_exceptions
22 def capture_exceptions
23 old_capture_exceptions do
24 yield
25 rescue Minitest::Skip => e
26 failures << e
27 end
28 end
29 end
30 end
31rescue LoadError
32 # Just helpers for dev, no big deal if missing
33 nil
34end
35
36require "backend_sgx"
37require "tel_selections"
38
39$VERBOSE = nil
40Sentry.init
41
42def customer(
43 customer_id="test",
44 plan_name: nil,
45 jid: Blather::JID.new("#{customer_id}@example.net"),
46 expires_at: Time.now,
47 auto_top_up_amount: 0,
48 **kwargs
49)
50 plan = CustomerPlan.for(
51 customer_id,
52 plan_name: plan_name,
53 expires_at: expires_at,
54 auto_top_up_amount: auto_top_up_amount
55 )
56 Customer.new(customer_id, jid, plan: plan, **kwargs)
57end
58
59CONFIG = {
60 sgx: "sgx",
61 component: {
62 jid: "component"
63 },
64 creds: {
65 account: "test_bw_account",
66 username: "test_bw_user",
67 password: "test_bw_password"
68 },
69 notify_from: "notify_from@example.org",
70 activation_amount: 1,
71 activation_amount_accept: 1,
72 plans: [
73 {
74 name: "test_usd",
75 currency: :USD,
76 monthly_price: 10000,
77 messages: :unlimited,
78 minutes: { included: 10440, price: 87 }
79 },
80 {
81 name: "test_bad_currency",
82 currency: :BAD
83 },
84 {
85 name: "test_cad",
86 currency: :CAD,
87 monthly_price: 10000
88 }
89 ],
90 braintree: {
91 merchant_accounts: {
92 USD: "merchant_usd"
93 }
94 },
95 sip: {
96 realm: "sip.example.com",
97 app: "sipappid"
98 },
99 credit_card_url: ->(*) { "http://creditcard.example.com" },
100 electrum_notify_url: ->(*) { "http://notify.example.com" },
101 upstream_domain: "example.net",
102 approved_domains: {
103 "approved.example.com": nil,
104 "refer.example.com": "refer_to"
105 }
106}.freeze
107
108def panic(e)
109 raise e
110end
111
112LOG = Class.new {
113 def child(*)
114 Minitest::Mock.new
115 end
116}.new.freeze
117
118BLATHER = Class.new {
119 def <<(*); end
120}.new.freeze
121
122def execute_command(
123 iq=Blather::Stanza::Iq::Command.new.tap { |i| i.from = "test@example.com" },
124 blather: BLATHER,
125 &blk
126)
127 Command::Execution.new(
128 Minitest::Mock.new,
129 blather,
130 :to_s.to_proc,
131 iq
132 ).execute(&blk).sync
133end
134
135class Matching
136 def initialize(&block)
137 @block = block
138 end
139
140 def ===(other)
141 @block.call(other)
142 end
143end
144
145class PromiseMock < Minitest::Mock
146 def then(succ=nil, _=nil)
147 if succ
148 succ.call(self)
149 else
150 yield self
151 end
152 end
153end
154
155class FakeTelSelections
156 def initialize
157 @selections = {}
158 end
159
160 def set(jid, tel)
161 @selections[jid] = EMPromise.resolve(TelSelections::HaveTel.new(tel))
162 end
163
164 def delete(jid)
165 @selections.delete(jid)
166 EMPromise.resolve("OK")
167 end
168
169 def [](jid)
170 @selections.fetch(jid) do
171 TelSelections::ChooseTel.new
172 end
173 end
174end
175
176class FakeRedis
177 def initialize(values={})
178 @values = values
179 end
180
181 def set(key, value)
182 @values[key] = value
183 EMPromise.resolve("OK")
184 end
185
186 def setex(key, _expiry, value)
187 set(key, value)
188 end
189
190 def mget(*keys)
191 EMPromise.all(keys.map(&method(:get)))
192 end
193
194 def get(key)
195 EMPromise.resolve(@values[key])
196 end
197
198 def getbit(key, bit)
199 get(key).then { |v| v.to_i.to_s(2)[bit].to_i }
200 end
201
202 def exists(*keys)
203 EMPromise.resolve(
204 @values.select { |k, _| keys.include? k }.size
205 )
206 end
207
208 def lindex(key, index)
209 get(key).then { |v| v&.fetch(index) }
210 end
211end
212
213class FakeDB
214 class MultiResult
215 def initialize(*args)
216 @results = args
217 end
218
219 def to_a
220 @results.shift
221 end
222 end
223
224 def initialize(items={})
225 @items = items
226 end
227
228 def query_defer(_, args)
229 EMPromise.resolve(@items.fetch(args, []).to_a)
230 end
231end
232
233class FakeLog
234 def initialize
235 @logs = []
236 end
237
238 def respond_to_missing?(*)
239 true
240 end
241
242 def method_missing(*args)
243 @logs << args
244 end
245end
246
247class FakeIBRRepo
248 def initialize(registrations={})
249 @registrations = registrations
250 end
251
252 def registered?(jid, from:)
253 @registrations.dig(jid.to_s, from.to_s) || false
254 end
255end
256
257module EventMachine
258 class << self
259 # Patch EM.add_timer to be instant in tests
260 alias old_add_timer add_timer
261 def add_timer(*args, &block)
262 args[0] = 0
263 old_add_timer(*args, &block)
264 end
265 end
266end
267
268module Minitest
269 class Test
270 def self.property(m, &block)
271 define_method("test_#{m}") do
272 property_of(&block).check { |args| send(m, *args) }
273 end
274 end
275
276 def self.em(m)
277 alias_method "raw_#{m}", m
278 define_method(m) do
279 EM.run do
280 Fiber.new {
281 begin
282 send("raw_#{m}")
283 ensure
284 EM.stop
285 end
286 }.resume
287 end
288 end
289 end
290 end
291end