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