test_helper.rb

  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	plans: [
 72		{
 73			name: "test_usd",
 74			currency: :USD,
 75			monthly_price: 10000,
 76			messages: :unlimited,
 77			minutes: { included: 10440, price: 87 }
 78		},
 79		{
 80			name: "test_bad_currency",
 81			currency: :BAD
 82		},
 83		{
 84			name: "test_cad",
 85			currency: :CAD,
 86			monthly_price: 10000
 87		}
 88	],
 89	braintree: {
 90		merchant_accounts: {
 91			USD: "merchant_usd"
 92		}
 93	},
 94	sip: {
 95		realm: "sip.example.com",
 96		app: "sipappid"
 97	},
 98	credit_card_url: ->(*) { "http://creditcard.example.com" },
 99	electrum_notify_url: ->(*) { "http://notify.example.com" },
100	upstream_domain: "example.net",
101	approved_domains: {
102		"approved.example.com": nil,
103		"refer.example.com": "refer_to"
104	}
105}.freeze
106
107def panic(e)
108	raise e
109end
110
111LOG = Class.new {
112	def child(*)
113		Minitest::Mock.new
114	end
115}.new.freeze
116
117BLATHER = Class.new {
118	def <<(*); end
119}.new.freeze
120
121def execute_command(
122	iq=Blather::Stanza::Iq::Command.new.tap { |i| i.from = "test@example.com" },
123	blather: BLATHER,
124	&blk
125)
126	Command::Execution.new(
127		Minitest::Mock.new,
128		blather,
129		:to_s.to_proc,
130		iq
131	).execute(&blk).sync
132end
133
134class Matching
135	def initialize(&block)
136		@block = block
137	end
138
139	def ===(other)
140		@block.call(other)
141	end
142end
143
144class PromiseMock < Minitest::Mock
145	def then(succ=nil, _=nil)
146		if succ
147			succ.call(self)
148		else
149			yield self
150		end
151	end
152end
153
154class FakeTelSelections
155	def initialize
156		@selections = {}
157	end
158
159	def set(jid, tel)
160		@selections[jid] = EMPromise.resolve(TelSelections::HaveTel.new(tel))
161	end
162
163	def delete(jid)
164		@selections.delete(jid)
165		EMPromise.resolve("OK")
166	end
167
168	def [](jid)
169		@selections.fetch(jid) do
170			TelSelections::ChooseTel.new
171		end
172	end
173end
174
175class FakeRedis
176	def initialize(values={})
177		@values = values
178	end
179
180	def set(key, value)
181		@values[key] = value
182		EMPromise.resolve("OK")
183	end
184
185	def setex(key, _expiry, value)
186		set(key, value)
187	end
188
189	def mget(*keys)
190		EMPromise.all(keys.map(&method(:get)))
191	end
192
193	def get(key)
194		EMPromise.resolve(@values[key])
195	end
196
197	def getbit(key, bit)
198		get(key).then { |v| v.to_i.to_s(2)[bit].to_i }
199	end
200
201	def exists(*keys)
202		EMPromise.resolve(
203			@values.select { |k, _| keys.include? k }.size
204		)
205	end
206
207	def lindex(key, index)
208		get(key).then { |v| v&.fetch(index) }
209	end
210end
211
212class FakeDB
213	def initialize(items={})
214		@items = items
215	end
216
217	def query_defer(_, args)
218		EMPromise.resolve(@items.fetch(args, []))
219	end
220end
221
222class FakeLog
223	def initialize
224		@logs = []
225	end
226
227	def respond_to_missing?(*)
228		true
229	end
230
231	def method_missing(*args)
232		@logs << args
233	end
234end
235
236class FakeIBRRepo
237	def initialize(registrations={})
238		@registrations = registrations
239	end
240
241	def registered?(jid, from:)
242		@registrations.dig(jid.to_s, from.to_s) || false
243	end
244end
245
246module EventMachine
247	class << self
248		# Patch EM.add_timer to be instant in tests
249		alias old_add_timer add_timer
250		def add_timer(*args, &block)
251			args[0] = 0
252			old_add_timer(*args, &block)
253		end
254	end
255end
256
257module Minitest
258	class Test
259		def self.property(m, &block)
260			define_method("test_#{m}") do
261				property_of(&block).check { |args| send(m, *args) }
262			end
263		end
264
265		def self.em(m)
266			alias_method "raw_#{m}", m
267			define_method(m) do
268				EM.run do
269					Fiber.new {
270						begin
271							send("raw_#{m}")
272						ensure
273							EM.stop
274						end
275					}.resume
276				end
277			end
278		end
279	end
280end