~singpolyma/sgx-jmp

ref: da30c371e56b8bb577897f1151b59c24e209127f sgx-jmp/test/test_helper.rb -rw-r--r-- 4.6 KiB
da30c371Stephen Paul Weber Allow finishing admin command 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# frozen_string_literal: true

require "simplecov"
SimpleCov.start do
	add_filter "/test/"
	enable_coverage :branch
end

require "em_promise"
require "fiber"
require "minitest/autorun"
require "rantly/minitest_extensions"
require "sentry-ruby"
require "webmock/minitest"
begin
	require "pry-rescue/minitest"
	require "pry-reload"

	module Minitest
		class Test
			alias old_capture_exceptions capture_exceptions
			def capture_exceptions
				old_capture_exceptions do
					yield
				rescue Minitest::Skip => e
					failures << e
				end
			end
		end
	end
rescue LoadError
	# Just helpers for dev, no big deal if missing
	nil
end

require "backend_sgx"
require "tel_selections"

$VERBOSE = nil
Sentry.init

def customer(
	customer_id="test",
	plan_name: nil,
	jid: Blather::JID.new("#{customer_id}@example.net"),
	expires_at: Time.now,
	auto_top_up_amount: 0,
	**kwargs
)
	plan = CustomerPlan.for(
		customer_id,
		plan_name: plan_name,
		expires_at: expires_at,
		auto_top_up_amount: auto_top_up_amount
	)
	Customer.new(customer_id, jid, plan: plan, **kwargs)
end

CONFIG = {
	sgx: "sgx",
	component: {
		jid: "component"
	},
	creds: {
		account: "test_bw_account",
		username: "test_bw_user",
		password: "test_bw_password"
	},
	notify_from: "notify_from@example.org",
	activation_amount: 1,
	activation_amount_accept: 1,
	plans: [
		{
			name: "test_usd",
			currency: :USD,
			monthly_price: 10000,
			messages: :unlimited,
			minutes: { included: 10440, price: 87 }
		},
		{
			name: "test_bad_currency",
			currency: :BAD
		},
		{
			name: "test_cad",
			currency: :CAD,
			monthly_price: 10000
		}
	],
	braintree: {
		merchant_accounts: {
			USD: "merchant_usd"
		}
	},
	sip: {
		realm: "sip.example.com",
		app: "sipappid"
	},
	credit_card_url: ->(*) { "http://creditcard.example.com" },
	electrum_notify_url: ->(*) { "http://notify.example.com" },
	upstream_domain: "example.net",
	approved_domains: {
		"approved.example.com": nil,
		"refer.example.com": "refer_to"
	}
}.freeze

def panic(e)
	raise e
end

LOG = Class.new {
	def child(*)
		Minitest::Mock.new
	end
}.new.freeze

BLATHER = Class.new {
	def <<(*); end
}.new.freeze

def execute_command(
	iq=Blather::Stanza::Iq::Command.new.tap { |i| i.from = "test@example.com" },
	blather: BLATHER,
	&blk
)
	Command::Execution.new(
		Minitest::Mock.new,
		blather,
		:to_s.to_proc,
		iq
	).execute(&blk).sync
end

class Matching
	def initialize(&block)
		@block = block
	end

	def ===(other)
		@block.call(other)
	end
end

class PromiseMock < Minitest::Mock
	def then(succ=nil, _=nil)
		if succ
			succ.call(self)
		else
			yield self
		end
	end
end

class FakeTelSelections
	def initialize
		@selections = {}
	end

	def set(jid, tel)
		@selections[jid] = EMPromise.resolve(TelSelections::HaveTel.new(tel))
	end

	def delete(jid)
		@selections.delete(jid)
		EMPromise.resolve("OK")
	end

	def [](jid)
		@selections.fetch(jid) do
			TelSelections::ChooseTel.new
		end
	end
end

class FakeRedis
	def initialize(values={})
		@values = values
	end

	def set(key, value)
		@values[key] = value
		EMPromise.resolve("OK")
	end

	def setex(key, _expiry, value)
		set(key, value)
	end

	def mget(*keys)
		EMPromise.all(keys.map(&method(:get)))
	end

	def get(key)
		EMPromise.resolve(@values[key])
	end

	def getbit(key, bit)
		get(key).then { |v| v.to_i.to_s(2)[bit].to_i }
	end

	def exists(*keys)
		EMPromise.resolve(
			@values.select { |k, _| keys.include? k }.size
		)
	end

	def lindex(key, index)
		get(key).then { |v| v&.fetch(index) }
	end
end

class FakeDB
	class MultiResult
		def initialize(*args)
			@results = args
		end

		def to_a
			@results.shift
		end
	end

	def initialize(items={})
		@items = items
	end

	def query_defer(_, args)
		EMPromise.resolve(@items.fetch(args, []).to_a)
	end
end

class FakeLog
	def initialize
		@logs = []
	end

	def respond_to_missing?(*)
		true
	end

	def method_missing(*args)
		@logs << args
	end
end

class FakeIBRRepo
	def initialize(registrations={})
		@registrations = registrations
	end

	def registered?(jid, from:)
		@registrations.dig(jid.to_s, from.to_s) || false
	end
end

module EventMachine
	class << self
		# Patch EM.add_timer to be instant in tests
		alias old_add_timer add_timer
		def add_timer(*args, &block)
			args[0] = 0
			old_add_timer(*args, &block)
		end
	end
end

module Minitest
	class Test
		def self.property(m, &block)
			define_method("test_#{m}") do
				property_of(&block).check { |args| send(m, *args) }
			end
		end

		def self.em(m)
			alias_method "raw_#{m}", m
			define_method(m) do
				EM.run do
					Fiber.new {
						begin
							send("raw_#{m}")
						ensure
							EM.stop
						end
					}.resume
				end
			end
		end
	end
end