test_customer.rb

  1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "customer"
  5
  6Customer::BLATHER = Minitest::Mock.new
  7Customer::BRAINTREE = Minitest::Mock.new
  8Customer::REDIS = Minitest::Mock.new
  9Customer::DB = Minitest::Mock.new
 10CustomerPlan::DB = Minitest::Mock.new
 11CustomerUsage::REDIS = Minitest::Mock.new
 12CustomerUsage::DB = Minitest::Mock.new
 13
 14class SipAccount
 15	public :username, :url
 16
 17	class New
 18		public :username
 19	end
 20end
 21
 22class CustomerTest < Minitest::Test
 23	def test_for_jid
 24		Customer::REDIS.expect(
 25			:get,
 26			EMPromise.resolve(1),
 27			["jmp_customer_id-test@example.com"]
 28		)
 29		Customer::DB.expect(
 30			:query_defer,
 31			EMPromise.resolve([{ balance: 1234, plan_name: "test_usd" }]),
 32			[String, [1]]
 33		)
 34		customer = Customer.for_jid("test@example.com").sync
 35		assert_kind_of Customer, customer
 36		assert_equal 1234, customer.balance
 37		assert_equal "merchant_usd", customer.merchant_account
 38	end
 39	em :test_for_jid
 40
 41	def test_for_jid_not_found
 42		Customer::REDIS.expect(
 43			:get,
 44			EMPromise.resolve(nil),
 45			["jmp_customer_id-test2@example.com"]
 46		)
 47		assert_raises do
 48			Customer.for_jid("test2@example.com").sync
 49		end
 50	end
 51	em :test_for_jid_not_found
 52
 53	def test_for_customer_id_not_found
 54		Customer::DB.expect(
 55			:query_defer,
 56			EMPromise.resolve([]),
 57			[String, [7357]]
 58		)
 59		customer = Customer.for_customer_id(7357).sync
 60		assert_equal BigDecimal.new(0), customer.balance
 61	end
 62	em :test_for_customer_id_not_found
 63
 64	def test_create
 65		braintree_customer = Minitest::Mock.new
 66		Customer::BRAINTREE.expect(:customer, braintree_customer)
 67		braintree_customer.expect(:create, EMPromise.resolve(
 68			OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
 69		))
 70		Customer::REDIS.expect(
 71			:msetnx,
 72			EMPromise.resolve(1),
 73			[
 74				"jmp_customer_id-test@example.com", "test",
 75				"jmp_customer_jid-test", "test@example.com"
 76			]
 77		)
 78		assert_kind_of Customer, Customer.create("test@example.com").sync
 79		braintree_customer.verify
 80		Customer::REDIS.verify
 81	end
 82	em :test_create
 83
 84	def test_bill_plan_activate
 85		CustomerPlan::DB.expect(:transaction, nil) do |&block|
 86			block.call
 87			true
 88		end
 89		CustomerPlan::DB.expect(
 90			:exec,
 91			nil,
 92			[
 93				String,
 94				Matching.new do |params|
 95					params[0] == "test" &&
 96					params[1].is_a?(String) &&
 97					BigDecimal.new(-1) == params[2]
 98				end
 99			]
100		)
101		CustomerPlan::DB.expect(
102			:exec,
103			OpenStruct.new(cmd_tuples: 1),
104			[String, ["test", "test_usd"]]
105		)
106		Customer.new("test", plan_name: "test_usd").bill_plan.sync
107		CustomerPlan::DB.verify
108	end
109	em :test_bill_plan_activate
110
111	def test_bill_plan_update
112		CustomerPlan::DB.expect(:transaction, nil) do |&block|
113			block.call
114			true
115		end
116		CustomerPlan::DB.expect(
117			:exec,
118			nil,
119			[
120				String,
121				Matching.new do |params|
122					params[0] == "test" &&
123					params[1].is_a?(String) &&
124					BigDecimal.new(-1) == params[2]
125				end
126			]
127		)
128		CustomerPlan::DB.expect(
129			:exec,
130			OpenStruct.new(cmd_tuples: 0),
131			[String, ["test", "test_usd"]]
132		)
133		CustomerPlan::DB.expect(:exec, nil, [String, ["test"]])
134		Customer.new("test", plan_name: "test_usd").bill_plan.sync
135		CustomerPlan::DB.verify
136	end
137	em :test_bill_plan_update
138
139	def test_jid
140		Customer::REDIS.expect(
141			:get,
142			EMPromise.resolve("test@example.com"),
143			["jmp_customer_jid-test"]
144		)
145		jid = Customer.new("test").jid.sync
146		assert_kind_of Blather::JID, jid
147		assert_equal "test@example.com", jid.to_s
148		Customer::REDIS.verify
149	end
150	em :test_jid
151
152	def test_stanza_to
153		Customer::REDIS.expect(
154			:get,
155			EMPromise.resolve("test@example.com"),
156			["jmp_customer_jid-test"]
157		)
158		Customer::BLATHER.expect(
159			:<<,
160			nil,
161			[Matching.new do |stanza|
162				assert_equal "+15555550000@component/a", stanza.from.to_s
163				assert_equal "test@example.com/b", stanza.to.to_s
164			end]
165		)
166		m = Blather::Stanza::Message.new
167		m.from = "+15555550000@sgx/a"
168		m.to = "customer_test@component/b"
169		Customer.new("test").stanza_to(m).sync
170		Customer::BLATHER.verify
171	end
172	em :test_stanza_to
173
174	def test_stanza_from
175		Customer::BLATHER.expect(
176			:<<,
177			nil,
178			[Matching.new do |stanza|
179				assert_equal "customer_test@component/a", stanza.from.to_s
180				assert_equal "+15555550000@sgx/b", stanza.to.to_s
181			end]
182		)
183		m = Blather::Stanza::Message.new
184		m.from = "test@example.com/a"
185		m.to = "+15555550000@component/b"
186		Customer.new("test").stanza_from(m)
187		Customer::BLATHER.verify
188	end
189
190	def test_customer_usage_report
191		report_for = (Date.today..(Date.today - 1))
192		report_for.first.downto(report_for.last).each.with_index do |day, idx|
193			CustomerUsage::REDIS.expect(
194				:zscore,
195				EMPromise.resolve(idx),
196				["jmp_customer_outbound_messages-test", day.strftime("%Y%m%d")]
197			)
198		end
199		CustomerUsage::DB.expect(
200			:query_defer,
201			EMPromise.resolve([{ "day" => report_for.first, "minutes" => 123 }]),
202			[String, ["test", report_for.first, report_for.last]]
203		)
204		assert_equal(
205			UsageReport.new(
206				report_for, {
207					Date.today => 0,
208					(Date.today - 1) => 1
209				},
210				Date.today => 123
211			),
212			Customer.new("test").usage_report(report_for).sync
213		)
214	end
215	em :test_customer_usage_report
216
217	def test_sip_account_new
218		req = stub_request(
219			:get,
220			"https://api.catapult.inetwork.com/v1/users/" \
221			"catapult_user/domains/catapult_domain/endpoints?page=0&size=1000"
222		).with(
223			headers: {
224				"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
225			}
226		).to_return(status: 404)
227		sip = Customer.new("test").sip_account.sync
228		assert_kind_of SipAccount::New, sip
229		assert_equal "test", sip.username
230		assert_requested req
231	end
232	em :test_sip_account_new
233
234	def test_sip_account_existing
235		req1 = stub_request(
236			:get,
237			"https://api.catapult.inetwork.com/v1/users/" \
238			"catapult_user/domains/catapult_domain/endpoints?page=0&size=1000"
239		).with(
240			headers: {
241				"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
242			}
243		).to_return(status: 200, body: [
244			{ name: "NOTtest", domainId: "domain", id: "endpoint" }
245		].to_json)
246
247		req2 = stub_request(
248			:get,
249			"https://api.catapult.inetwork.com/v1/users/" \
250			"catapult_user/domains/catapult_domain/endpoints?page=1&size=1000"
251		).with(
252			headers: {
253				"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
254			}
255		).to_return(status: 200, body: [
256			{ name: "test", domainId: "domain", id: "endpoint" }
257		].to_json)
258
259		sip = Customer.new("test").sip_account.sync
260		assert_kind_of SipAccount, sip
261		assert_equal "test", sip.username
262		assert_equal(
263			"https://api.catapult.inetwork.com/v1/users/" \
264			"catapult_user/domains/domain/endpoints/endpoint",
265			sip.url
266		)
267
268		assert_requested req1
269		assert_requested req2
270	end
271	em :test_sip_account_existing
272
273	def test_sip_account_error
274		stub_request(
275			:get,
276			"https://api.catapult.inetwork.com/v1/users/" \
277			"catapult_user/domains/catapult_domain/endpoints?page=0&size=1000"
278		).to_return(status: 400)
279
280		assert_raises(RuntimeError) do
281			Customer.new("test").sip_account.sync
282		end
283	end
284	em :test_sip_account_error
285
286	def test_btc_addresses
287		Customer::REDIS.expect(
288			:smembers,
289			EMPromise.resolve(["testaddr"]),
290			["jmp_customer_btc_addresses-test"]
291		)
292		assert_equal ["testaddr"], Customer.new("test").btc_addresses.sync
293		assert_mock Customer::REDIS
294	end
295	em :test_btc_addresses
296
297	def test_add_btc_address
298		Customer::ELECTRUM.expect(
299			:createnewaddress,
300			EMPromise.resolve("testaddr")
301		)
302		Customer::REDIS.expect(
303			:sadd,
304			EMPromise.resolve(nil),
305			["jmp_customer_btc_addresses-test", "testaddr"]
306		)
307		assert_equal "testaddr", Customer.new("test").add_btc_address.sync
308		assert_mock Customer::ELECTRUM
309		assert_mock Customer::REDIS
310	end
311	em :test_add_btc_address
312end