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