~singpolyma/sgx-jmp

ref: a7f39ff089fa21ceff25f10860c8d38e42973dfe sgx-jmp/test/test_customer.rb -rw-r--r-- 7.4 KiB
a7f39ff0Stephen Paul Weber Get new Bitcoin address from Redis set 2 years 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# frozen_string_literal: true

require "test_helper"
require "customer"

Customer::BLATHER = Minitest::Mock.new
Customer::BRAINTREE = Minitest::Mock.new
Customer::REDIS = Minitest::Mock.new
Customer::DB = Minitest::Mock.new
CustomerPlan::DB = Minitest::Mock.new
CustomerUsage::REDIS = Minitest::Mock.new
CustomerUsage::DB = Minitest::Mock.new

class SipAccount
	public :username, :url

	class New
		public :username
	end
end

class CustomerTest < Minitest::Test
	def test_for_jid
		Customer::REDIS.expect(
			:get,
			EMPromise.resolve(1),
			["jmp_customer_id-test@example.com"]
		)
		Customer::DB.expect(
			:query_defer,
			EMPromise.resolve([{ balance: 1234, plan_name: "test_usd" }]),
			[String, [1]]
		)
		customer = Customer.for_jid("test@example.com").sync
		assert_kind_of Customer, customer
		assert_equal 1234, customer.balance
		assert_equal "merchant_usd", customer.merchant_account
	end
	em :test_for_jid

	def test_for_jid_not_found
		Customer::REDIS.expect(
			:get,
			EMPromise.resolve(nil),
			["jmp_customer_id-test2@example.com"]
		)
		assert_raises do
			Customer.for_jid("test2@example.com").sync
		end
	end
	em :test_for_jid_not_found

	def test_for_customer_id_not_found
		Customer::DB.expect(
			:query_defer,
			EMPromise.resolve([]),
			[String, [7357]]
		)
		customer = Customer.for_customer_id(7357).sync
		assert_equal BigDecimal.new(0), customer.balance
	end
	em :test_for_customer_id_not_found

	def test_create
		braintree_customer = Minitest::Mock.new
		Customer::BRAINTREE.expect(:customer, braintree_customer)
		braintree_customer.expect(:create, EMPromise.resolve(
			OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
		))
		Customer::REDIS.expect(
			:msetnx,
			EMPromise.resolve(1),
			[
				"jmp_customer_id-test@example.com", "test",
				"jmp_customer_jid-test", "test@example.com"
			]
		)
		assert_kind_of Customer, Customer.create("test@example.com").sync
		braintree_customer.verify
		Customer::REDIS.verify
	end
	em :test_create

	def test_bill_plan_activate
		CustomerPlan::DB.expect(:transaction, nil) do |&block|
			block.call
			true
		end
		CustomerPlan::DB.expect(
			:exec,
			nil,
			[
				String,
				Matching.new do |params|
					params[0] == "test" &&
					params[1].is_a?(String) &&
					BigDecimal.new(-1) == params[2]
				end
			]
		)
		CustomerPlan::DB.expect(
			:exec,
			OpenStruct.new(cmd_tuples: 1),
			[String, ["test", "test_usd"]]
		)
		Customer.new("test", plan_name: "test_usd").bill_plan.sync
		CustomerPlan::DB.verify
	end
	em :test_bill_plan_activate

	def test_bill_plan_update
		CustomerPlan::DB.expect(:transaction, nil) do |&block|
			block.call
			true
		end
		CustomerPlan::DB.expect(
			:exec,
			nil,
			[
				String,
				Matching.new do |params|
					params[0] == "test" &&
					params[1].is_a?(String) &&
					BigDecimal.new(-1) == params[2]
				end
			]
		)
		CustomerPlan::DB.expect(
			:exec,
			OpenStruct.new(cmd_tuples: 0),
			[String, ["test", "test_usd"]]
		)
		CustomerPlan::DB.expect(:exec, nil, [String, ["test"]])
		Customer.new("test", plan_name: "test_usd").bill_plan.sync
		CustomerPlan::DB.verify
	end
	em :test_bill_plan_update

	def test_jid
		Customer::REDIS.expect(
			:get,
			EMPromise.resolve("test@example.com"),
			["jmp_customer_jid-test"]
		)
		jid = Customer.new("test").jid.sync
		assert_kind_of Blather::JID, jid
		assert_equal "test@example.com", jid.to_s
		Customer::REDIS.verify
	end
	em :test_jid

	def test_stanza_to
		Customer::REDIS.expect(
			:get,
			EMPromise.resolve("test@example.com"),
			["jmp_customer_jid-test"]
		)
		Customer::BLATHER.expect(
			:<<,
			nil,
			[Matching.new do |stanza|
				assert_equal "+15555550000@component/a", stanza.from.to_s
				assert_equal "test@example.com/b", stanza.to.to_s
			end]
		)
		m = Blather::Stanza::Message.new
		m.from = "+15555550000@sgx/a"
		m.to = "customer_test@component/b"
		Customer.new("test").stanza_to(m).sync
		Customer::BLATHER.verify
	end
	em :test_stanza_to

	def test_stanza_from
		Customer::BLATHER.expect(
			:<<,
			nil,
			[Matching.new do |stanza|
				assert_equal "customer_test@component/a", stanza.from.to_s
				assert_equal "+15555550000@sgx/b", stanza.to.to_s
			end]
		)
		m = Blather::Stanza::Message.new
		m.from = "test@example.com/a"
		m.to = "+15555550000@component/b"
		Customer.new("test").stanza_from(m)
		Customer::BLATHER.verify
	end

	def test_customer_usage_report
		report_for = (Date.today..(Date.today - 1))
		report_for.first.downto(report_for.last).each.with_index do |day, idx|
			CustomerUsage::REDIS.expect(
				:zscore,
				EMPromise.resolve(idx),
				["jmp_customer_outbound_messages-test", day.strftime("%Y%m%d")]
			)
		end
		CustomerUsage::DB.expect(
			:query_defer,
			EMPromise.resolve([{ "day" => report_for.first, "minutes" => 123 }]),
			[String, ["test", report_for.first, report_for.last]]
		)
		assert_equal(
			UsageReport.new(
				report_for, {
					Date.today => 0,
					(Date.today - 1) => 1
				},
				Date.today => 123
			),
			Customer.new("test").usage_report(report_for).sync
		)
	end
	em :test_customer_usage_report

	def test_sip_account_new
		req = stub_request(
			:get,
			"https://api.catapult.inetwork.com/v1/users/" \
			"catapult_user/domains/catapult_domain/endpoints?page=0&size=1000"
		).with(
			headers: {
				"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
			}
		).to_return(status: 404)
		sip = Customer.new("test").sip_account.sync
		assert_kind_of SipAccount::New, sip
		assert_equal "test", sip.username
		assert_requested req
	end
	em :test_sip_account_new

	def test_sip_account_existing
		req1 = stub_request(
			:get,
			"https://api.catapult.inetwork.com/v1/users/" \
			"catapult_user/domains/catapult_domain/endpoints?page=0&size=1000"
		).with(
			headers: {
				"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
			}
		).to_return(status: 200, body: [
			{ name: "NOTtest", domainId: "domain", id: "endpoint" }
		].to_json)

		req2 = stub_request(
			:get,
			"https://api.catapult.inetwork.com/v1/users/" \
			"catapult_user/domains/catapult_domain/endpoints?page=1&size=1000"
		).with(
			headers: {
				"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
			}
		).to_return(status: 200, body: [
			{ name: "test", domainId: "domain", id: "endpoint" }
		].to_json)

		sip = Customer.new("test").sip_account.sync
		assert_kind_of SipAccount, sip
		assert_equal "test", sip.username
		assert_equal(
			"https://api.catapult.inetwork.com/v1/users/" \
			"catapult_user/domains/domain/endpoints/endpoint",
			sip.url
		)

		assert_requested req1
		assert_requested req2
	end
	em :test_sip_account_existing

	def test_sip_account_error
		stub_request(
			:get,
			"https://api.catapult.inetwork.com/v1/users/" \
			"catapult_user/domains/catapult_domain/endpoints?page=0&size=1000"
		).to_return(status: 400)

		assert_raises(RuntimeError) do
			Customer.new("test").sip_account.sync
		end
	end
	em :test_sip_account_error

	def test_btc_addresses
		Customer::REDIS.expect(
			:smembers,
			EMPromise.resolve(["testaddr"]),
			["jmp_customer_btc_addresses-test"]
		)
		assert_equal ["testaddr"], Customer.new("test").btc_addresses.sync
		assert_mock Customer::REDIS
	end
	em :test_btc_addresses

	def test_add_btc_address
		Customer::REDIS.expect(
			:spopsadd,
			EMPromise.resolve("testaddr"),
			[["jmp_available_btc_addresses", "jmp_customer_btc_addresses-test"]]
		)
		assert_equal "testaddr", Customer.new("test").add_btc_address.sync
		assert_mock Customer::REDIS
	end
	em :test_add_btc_address
end