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::ELECTRUM_BCH = Minitest::Mock.new
 17CustomerFinancials::BRAINTREE = Minitest::Mock.new
 18
 19class CustomerTest < Minitest::Test
 20	def test_bill_plan_activate
 21		CustomerPlan::DB.expect(:transaction, nil) do |&block|
 22			block.call
 23			true
 24		end
 25		CustomerPlan::DB.expect(
 26			:exec,
 27			nil,
 28			[
 29				String,
 30				Matching.new do |params|
 31					params[0] == "test" &&
 32					params[1].is_a?(String) &&
 33					BigDecimal(-1) == params[2]
 34				end
 35			]
 36		)
 37		CustomerPlan::DB.expect(
 38			:exec,
 39			OpenStruct.new(cmd_tuples: 1),
 40			[String, ["test", "test_usd", nil]]
 41		)
 42		CustomerPlan::DB.expect(
 43			:exec,
 44			OpenStruct.new(cmd_tuples: 0),
 45			[String, ["test"]]
 46		)
 47		customer(plan_name: "test_usd").bill_plan.sync
 48		CustomerPlan::DB.verify
 49	end
 50	em :test_bill_plan_activate
 51
 52	def test_bill_plan_reactivate_child
 53		CustomerPlan::DB.expect(
 54			:query,
 55			[{ "plan_name" => "test_usd" }],
 56			[String, ["parent"]]
 57		)
 58		CustomerPlan::DB.expect(:transaction, nil) do |&block|
 59			block.call
 60			true
 61		end
 62		CustomerPlan::DB.expect(
 63			:exec,
 64			nil,
 65			[
 66				String,
 67				Matching.new do |params|
 68					params[0] == "test" &&
 69					params[1].is_a?(String) &&
 70					BigDecimal(-1) == params[2]
 71				end
 72			]
 73		)
 74		CustomerPlan::DB.expect(
 75			:exec,
 76			OpenStruct.new(cmd_tuples: 1),
 77			[String, ["test", "test_usd", "parent"]]
 78		)
 79		CustomerPlan::DB.expect(
 80			:exec,
 81			OpenStruct.new(cmd_tuples: 0),
 82			[String, ["test"]]
 83		)
 84		customer(plan_name: "test_usd", parent_customer_id: "parent").bill_plan.sync
 85		CustomerPlan::DB.verify
 86	end
 87	em :test_bill_plan_reactivate_child
 88
 89	def test_bill_plan_update
 90		CustomerPlan::DB.expect(:transaction, nil) do |&block|
 91			block.call
 92			true
 93		end
 94		CustomerPlan::DB.expect(
 95			:exec,
 96			nil,
 97			[
 98				String,
 99				Matching.new do |params|
100					params[0] == "test" &&
101					params[1].is_a?(String) &&
102					BigDecimal(-1) == params[2]
103				end
104			]
105		)
106		CustomerPlan::DB.expect(
107			:exec,
108			OpenStruct.new(cmd_tuples: 0),
109			[String, ["test", "test_usd", nil]]
110		)
111		CustomerPlan::DB.expect(:exec, nil, [String, ["test"]])
112		customer(plan_name: "test_usd").bill_plan.sync
113		CustomerPlan::DB.verify
114	end
115	em :test_bill_plan_update
116
117	def test_stanza_to
118		Customer::BLATHER.expect(
119			:<<,
120			nil,
121			[Matching.new do |stanza|
122				assert_equal "+15555550000@component/a", stanza.from.to_s
123				assert_equal "test@example.net/b", stanza.to.to_s
124			end]
125		)
126		m = Blather::Stanza::Message.new
127		m.from = "+15555550000@sgx/a"
128		m.to = "customer_test@component/b"
129		customer.stanza_to(m)
130		assert_mock Customer::BLATHER
131	end
132	em :test_stanza_to
133
134	def test_stanza_from
135		Customer::BLATHER.expect(
136			:<<,
137			nil,
138			[Matching.new do |stanza|
139				assert_equal "customer_test@component/a", stanza.from.to_s
140				assert_equal "+15555550000@sgx/b", stanza.to.to_s
141			end]
142		)
143		m = Blather::Stanza::Message.new
144		m.from = "test@example.com/a"
145		m.to = "+15555550000@component/b"
146		customer.stanza_from(m)
147		Customer::BLATHER.verify
148	end
149
150	def test_fetch_pep
151		result = Blather::Stanza::PubSub::Items.new(:result)
152		result.items_node <<
153			Blather::Stanza::PubSubItem.new("current", Nokogiri.parse(<<~XML).root)
154				<vcard xmlns="urn:ietf:params:xml:ns:vcard-4.0">
155					<fn><text>A Human</text></fn>
156				</vcard4>
157			XML
158		Customer::IQ_MANAGER.expect(
159			:method,
160			->(*) { EMPromise.resolve(result) },
161			[:write]
162		)
163		assert_equal(
164			"A Human",
165			customer.fetch_pep("urn:xmpp:vcard4", "+15551234567").sync
166				.first.payload_node.find_first(
167					"./ns:fn/ns:text",
168					ns: "urn:ietf:params:xml:ns:vcard-4.0"
169				)&.content
170		)
171	end
172	em :test_fetch_pep
173
174	def test_customer_usage_report
175		report_for = (Date.today..(Date.today - 1))
176		report_for.first.downto(report_for.last).each.with_index do |day, idx|
177			CustomerUsage::REDIS.expect(
178				:zscore,
179				EMPromise.resolve(idx),
180				["jmp_customer_outbound_messages-test", day.strftime("%Y%m%d")]
181			)
182		end
183		CustomerUsage::DB.expect(
184			:query_defer,
185			EMPromise.resolve([{ "day" => report_for.first, "minutes" => 123 }]),
186			[String, ["test", report_for.first, report_for.last]]
187		)
188		assert_equal(
189			UsageReport.new(
190				report_for, {
191					Date.today => 0,
192					(Date.today - 1) => 1
193				},
194				Date.today => 123
195			),
196			customer.usage_report(report_for).sync
197		)
198	end
199	em :test_customer_usage_report
200
201	def test_sip_account_new
202		req = stub_request(
203			:get,
204			"https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/ctest"
205		).with(
206			headers: {
207				"Authorization" => "Basic Og=="
208			}
209		).to_return(
210			status: 404,
211			body:
212				"<r><ResponseStatus><ErrorCode>0</ErrorCode>" \
213				"<Description>desc</Description></ResponseStatus></r>"
214		)
215		sip = customer.sip_account
216		assert_kind_of SipAccount::New, sip
217		assert_equal "ctest", sip.username
218		assert_requested req
219	end
220	em :test_sip_account_new
221
222	def test_sip_account_existing
223		req1 = stub_request(
224			:get,
225			"https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/ctest"
226		).with(
227			headers: {
228				"Authorization" => "Basic Og=="
229			}
230		).to_return(status: 200, body: {
231			SipCredential: {
232				UserName: "ctest",
233				Realm: "sip.example.com"
234			}
235		}.to_xml)
236
237		sip = customer.sip_account
238		assert_kind_of SipAccount, sip
239		assert_equal "ctest", sip.username
240
241		assert_requested req1
242	end
243	em :test_sip_account_existing
244
245	def test_sip_account_error
246		stub_request(
247			:get,
248			"https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/ctest"
249		).to_return(
250			status: 404,
251			body:
252				"<r><ResponseStatus><ErrorCode>0</ErrorCode>" \
253				"<Description>desc</Description></ResponseStatus></r>"
254		)
255
256		assert_equal "ctest", customer.sip_account.username
257	end
258	em :test_sip_account_error
259
260	def test_btc_addresses
261		CustomerFinancials::REDIS.expect(
262			:smembers,
263			EMPromise.resolve(["testaddr"]),
264			["jmp_customer_btc_addresses-test"]
265		)
266		assert_equal ["testaddr"], customer.btc_addresses.sync
267		assert_mock Customer::REDIS
268	end
269	em :test_btc_addresses
270
271	def test_add_btc_address
272		CustomerFinancials::REDIS.expect(
273			:spopsadd,
274			EMPromise.resolve("testaddr"),
275			[["jmp_available_btc_addresses", "jmp_customer_btc_addresses-test"]]
276		)
277		CustomerFinancials::ELECTRUM.expect(
278			:notify,
279			EMPromise.resolve(nil),
280			["testaddr", "http://notify.example.com"]
281		)
282		assert_equal "testaddr", customer.add_btc_address.sync
283		assert_mock CustomerFinancials::REDIS
284		assert_mock CustomerFinancials::ELECTRUM
285	end
286	em :test_add_btc_address
287
288	def test_add_bch_address
289		CustomerFinancials::REDIS.expect(
290			:spopsadd,
291			EMPromise.resolve("testaddr"),
292			[["jmp_available_bch_addresses", "jmp_customer_bch_addresses-test"]]
293		)
294		CustomerFinancials::ELECTRUM_BCH.expect(
295			:notify,
296			EMPromise.resolve(nil),
297			["testaddr", "http://notify.example.com"]
298		)
299		assert_equal "testaddr", customer.add_bch_address.sync
300		assert_mock CustomerFinancials::REDIS
301		assert_mock CustomerFinancials::ELECTRUM_BCH
302	end
303	em :test_add_bch_address
304end