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"
 37
 38$VERBOSE = nil
 39Sentry.init
 40
 41def customer(customer_id="test", plan_name: nil, **kwargs)
 42	jid = Blather::JID.new("#{customer_id}@example.net")
 43	if plan_name
 44		expires_at = kwargs.delete(:expires_at) || Time.now
 45		plan = CustomerPlan.new(
 46			customer_id,
 47			plan: Plan.for(plan_name),
 48			expires_at: expires_at
 49		)
 50		Customer.new(customer_id, jid, plan: plan, **kwargs)
 51	else
 52		Customer.new(customer_id, jid, **kwargs)
 53	end
 54end
 55
 56CONFIG = {
 57	sgx: "sgx",
 58	component: {
 59		jid: "component"
 60	},
 61	creds: {
 62		account: "test_bw_account",
 63		username: "test_bw_user",
 64		password: "test_bw_password"
 65	},
 66	catapult: {
 67		user: "catapult_user",
 68		token: "catapult_token",
 69		secret: "catapult_secret",
 70		domain: "catapult_domain",
 71		sip_host: "host.bwapp.io.example.com",
 72		application_id: "catapult_app"
 73	},
 74	activation_amount: 1,
 75	plans: [
 76		{
 77			name: "test_usd",
 78			currency: :USD,
 79			monthly_price: 10000
 80		},
 81		{
 82			name: "test_bad_currency",
 83			currency: :BAD
 84		},
 85		{
 86			name: "test_cad",
 87			currency: :CAD,
 88			monthly_price: 10000
 89		}
 90	],
 91	braintree: {
 92		merchant_accounts: {
 93			USD: "merchant_usd"
 94		}
 95	},
 96	credit_card_url: ->(*) { "http://creditcard.example.com" },
 97	electrum_notify_url: ->(*) { "http://notify.example.com" }
 98}.freeze
 99
100def panic(e)
101	raise e
102end
103
104LOG = Class.new {
105	def child(*)
106		Minitest::Mock.new
107	end
108}.new.freeze
109
110BLATHER = Class.new {
111	def <<(*); end
112}.new.freeze
113
114class Matching
115	def initialize(&block)
116		@block = block
117	end
118
119	def ===(other)
120		@block.call(other)
121	end
122end
123
124class PromiseMock < Minitest::Mock
125	def then(succ=nil, _=nil)
126		if succ
127			succ.call(self)
128		else
129			yield self
130		end
131	end
132end
133
134module EventMachine
135	class << self
136		# Patch EM.add_timer to be instant in tests
137		alias old_add_timer add_timer
138		def add_timer(*args, &block)
139			args[0] = 0
140			old_add_timer(*args, &block)
141		end
142	end
143end
144
145module Minitest
146	class Test
147		def self.property(m, &block)
148			define_method("test_#{m}") do
149				property_of(&block).check { |args| send(m, *args) }
150			end
151		end
152
153		def self.em(m)
154			alias_method "raw_#{m}", m
155			define_method(m) do
156				EM.run do
157					Fiber.new {
158						begin
159							send("raw_#{m}")
160						ensure
161							EM.stop
162						end
163					}.resume
164				end
165			end
166		end
167	end
168end