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	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	def initialize(items={})
215		@items = items
216	end
217
218	def query_defer(_, args)
219		EMPromise.resolve(@items.fetch(args, []))
220	end
221end
222
223class FakeLog
224	def initialize
225		@logs = []
226	end
227
228	def respond_to_missing?(*)
229		true
230	end
231
232	def method_missing(*args)
233		@logs << args
234	end
235end
236
237class FakeIBRRepo
238	def initialize(registrations={})
239		@registrations = registrations
240	end
241
242	def registered?(jid, from:)
243		@registrations.dig(jid.to_s, from.to_s) || false
244	end
245end
246
247module EventMachine
248	class << self
249		# Patch EM.add_timer to be instant in tests
250		alias old_add_timer add_timer
251		def add_timer(*args, &block)
252			args[0] = 0
253			old_add_timer(*args, &block)
254		end
255	end
256end
257
258module Minitest
259	class Test
260		def self.property(m, &block)
261			define_method("test_#{m}") do
262				property_of(&block).check { |args| send(m, *args) }
263			end
264		end
265
266		def self.em(m)
267			alias_method "raw_#{m}", m
268			define_method(m) do
269				EM.run do
270					Fiber.new {
271						begin
272							send("raw_#{m}")
273						ensure
274							EM.stop
275						end
276					}.resume
277				end
278			end
279		end
280	end
281end