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
 10Customer::IQ_MANAGER = Minitest::Mock.new
 11CustomerPlan::DB = Minitest::Mock.new
 12CustomerUsage::REDIS = Minitest::Mock.new
 13CustomerUsage::DB = Minitest::Mock.new
 14CustomerFinancials::REDIS = Minitest::Mock.new
 15CustomerFinancials::ELECTRUM = Minitest::Mock.new
 16CustomerFinancials::BRAINTREE = Minitest::Mock.new
 17
 18class CustomerTest < Minitest::Test
 19	def test_bill_plan_activate
 20		CustomerPlan::DB.expect(:transaction, nil) do |&block|
 21			block.call
 22			true
 23		end
 24		CustomerPlan::DB.expect(
 25			:exec,
 26			nil,
 27			[
 28				String,
 29				Matching.new do |params|
 30					params[0] == "test" &&
 31					params[1].is_a?(String) &&
 32					BigDecimal(-1) == params[2]
 33				end
 34			]
 35		)
 36		CustomerPlan::DB.expect(
 37			:exec,
 38			OpenStruct.new(cmd_tuples: 1),
 39			[String, ["test", "test_usd"]]
 40		)
 41		customer(plan_name: "test_usd").bill_plan.sync
 42		CustomerPlan::DB.verify
 43	end
 44	em :test_bill_plan_activate
 45
 46	def test_bill_plan_update
 47		CustomerPlan::DB.expect(:transaction, nil) do |&block|
 48			block.call
 49			true
 50		end
 51		CustomerPlan::DB.expect(
 52			:exec,
 53			nil,
 54			[
 55				String,
 56				Matching.new do |params|
 57					params[0] == "test" &&
 58					params[1].is_a?(String) &&
 59					BigDecimal(-1) == params[2]
 60				end
 61			]
 62		)
 63		CustomerPlan::DB.expect(
 64			:exec,
 65			OpenStruct.new(cmd_tuples: 0),
 66			[String, ["test", "test_usd"]]
 67		)
 68		CustomerPlan::DB.expect(:exec, nil, [String, ["test"]])
 69		customer(plan_name: "test_usd").bill_plan.sync
 70		CustomerPlan::DB.verify
 71	end
 72	em :test_bill_plan_update
 73
 74	def test_stanza_to
 75		Customer::BLATHER.expect(
 76			:<<,
 77			nil,
 78			[Matching.new do |stanza|
 79				assert_equal "+15555550000@component/a", stanza.from.to_s
 80				assert_equal "test@example.net/b", stanza.to.to_s
 81			end]
 82		)
 83		m = Blather::Stanza::Message.new
 84		m.from = "+15555550000@sgx/a"
 85		m.to = "customer_test@component/b"
 86		customer.stanza_to(m)
 87		assert_mock Customer::BLATHER
 88	end
 89	em :test_stanza_to
 90
 91	def test_stanza_from
 92		Customer::BLATHER.expect(
 93			:<<,
 94			nil,
 95			[Matching.new do |stanza|
 96				assert_equal "customer_test@component/a", stanza.from.to_s
 97				assert_equal "+15555550000@sgx/b", stanza.to.to_s
 98			end]
 99		)
100		m = Blather::Stanza::Message.new
101		m.from = "test@example.com/a"
102		m.to = "+15555550000@component/b"
103		customer.stanza_from(m)
104		Customer::BLATHER.verify
105	end
106
107	def test_fetch_vcard_temp
108		result = Blather::Stanza::Iq::Vcard.new(:result)
109		result.vcard["FN"] = "name"
110		Customer::IQ_MANAGER.expect(
111			:method,
112			->(*) { EMPromise.resolve(result) },
113			[:write]
114		)
115		assert_equal "name", customer.fetch_vcard_temp("+15551234567").sync["FN"]
116	end
117	em :test_fetch_vcard_temp
118
119	def test_customer_usage_report
120		report_for = (Date.today..(Date.today - 1))
121		report_for.first.downto(report_for.last).each.with_index do |day, idx|
122			CustomerUsage::REDIS.expect(
123				:zscore,
124				EMPromise.resolve(idx),
125				["jmp_customer_outbound_messages-test", day.strftime("%Y%m%d")]
126			)
127		end
128		CustomerUsage::DB.expect(
129			:query_defer,
130			EMPromise.resolve([{ "day" => report_for.first, "minutes" => 123 }]),
131			[String, ["test", report_for.first, report_for.last]]
132		)
133		assert_equal(
134			UsageReport.new(
135				report_for, {
136					Date.today => 0,
137					(Date.today - 1) => 1
138				},
139				Date.today => 123
140			),
141			customer.usage_report(report_for).sync
142		)
143	end
144	em :test_customer_usage_report
145
146	def test_sip_account_new
147		req = stub_request(
148			:get,
149			"https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/test"
150		).with(
151			headers: {
152				"Authorization" => "Basic Og=="
153			}
154		).to_return(status: 404)
155		sip = customer.sip_account
156		assert_kind_of SipAccount::New, sip
157		assert_equal "test", sip.username
158		assert_requested req
159	end
160	em :test_sip_account_new
161
162	def test_sip_account_existing
163		req1 = stub_request(
164			:get,
165			"https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/test"
166		).with(
167			headers: {
168				"Authorization" => "Basic Og=="
169			}
170		).to_return(status: 200, body: {
171			SipCredential: {
172				UserName: "test",
173				Realm: "sip.example.com"
174			}
175		}.to_xml)
176
177		sip = customer.sip_account
178		assert_kind_of SipAccount, sip
179		assert_equal "test", sip.username
180
181		assert_requested req1
182	end
183	em :test_sip_account_existing
184
185	def test_sip_account_error
186		stub_request(
187			:get,
188			"https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/test"
189		).to_return(status: 404)
190
191		assert_equal "test", customer.sip_account.username
192	end
193	em :test_sip_account_error
194
195	def test_btc_addresses
196		CustomerFinancials::REDIS.expect(
197			:smembers,
198			EMPromise.resolve(["testaddr"]),
199			["jmp_customer_btc_addresses-test"]
200		)
201		assert_equal ["testaddr"], customer.btc_addresses.sync
202		assert_mock Customer::REDIS
203	end
204	em :test_btc_addresses
205
206	def test_add_btc_address
207		CustomerFinancials::REDIS.expect(
208			:spopsadd,
209			EMPromise.resolve("testaddr"),
210			[["jmp_available_btc_addresses", "jmp_customer_btc_addresses-test"]]
211		)
212		CustomerFinancials::ELECTRUM.expect(
213			:notify,
214			EMPromise.resolve(nil),
215			["testaddr", "http://notify.example.com"]
216		)
217		assert_equal "testaddr", customer.add_btc_address.sync
218		assert_mock CustomerFinancials::REDIS
219		assert_mock CustomerFinancials::ELECTRUM
220	end
221	em :test_add_btc_address
222end