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::ELECTRUM = Minitest::Mock.new
  9Customer::REDIS = Minitest::Mock.new
 10Customer::DB = Minitest::Mock.new
 11CustomerPlan::DB = Minitest::Mock.new
 12CustomerUsage::REDIS = Minitest::Mock.new
 13CustomerUsage::DB = Minitest::Mock.new
 14
 15class SipAccount
 16	public :username, :url
 17
 18	class New
 19		public :username
 20	end
 21end
 22
 23class CustomerTest < Minitest::Test
 24	def test_bill_plan_activate
 25		CustomerPlan::DB.expect(:transaction, nil) do |&block|
 26			block.call
 27			true
 28		end
 29		CustomerPlan::DB.expect(
 30			:exec,
 31			nil,
 32			[
 33				String,
 34				Matching.new do |params|
 35					params[0] == "test" &&
 36					params[1].is_a?(String) &&
 37					BigDecimal(-1) == params[2]
 38				end
 39			]
 40		)
 41		CustomerPlan::DB.expect(
 42			:exec,
 43			OpenStruct.new(cmd_tuples: 1),
 44			[String, ["test", "test_usd"]]
 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_customer_usage_report
113		report_for = (Date.today..(Date.today - 1))
114		report_for.first.downto(report_for.last).each.with_index do |day, idx|
115			CustomerUsage::REDIS.expect(
116				:zscore,
117				EMPromise.resolve(idx),
118				["jmp_customer_outbound_messages-test", day.strftime("%Y%m%d")]
119			)
120		end
121		CustomerUsage::DB.expect(
122			:query_defer,
123			EMPromise.resolve([{ "day" => report_for.first, "minutes" => 123 }]),
124			[String, ["test", report_for.first, report_for.last]]
125		)
126		assert_equal(
127			UsageReport.new(
128				report_for, {
129					Date.today => 0,
130					(Date.today - 1) => 1
131				},
132				Date.today => 123
133			),
134			customer.usage_report(report_for).sync
135		)
136	end
137	em :test_customer_usage_report
138
139	def test_sip_account_new
140		req = stub_request(
141			:get,
142			"https://api.catapult.inetwork.com/v1/users/" \
143			"catapult_user/domains/catapult_domain/endpoints?page=0&size=1000"
144		).with(
145			headers: {
146				"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
147			}
148		).to_return(status: 404)
149		sip = customer.sip_account.sync
150		assert_kind_of SipAccount::New, sip
151		assert_equal "test", sip.username
152		assert_requested req
153	end
154	em :test_sip_account_new
155
156	def test_sip_account_existing
157		req1 = stub_request(
158			:get,
159			"https://api.catapult.inetwork.com/v1/users/" \
160			"catapult_user/domains/catapult_domain/endpoints?page=0&size=1000"
161		).with(
162			headers: {
163				"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
164			}
165		).to_return(status: 200, body: [
166			{ name: "NOTtest", domainId: "domain", id: "endpoint" }
167		].to_json)
168
169		req2 = stub_request(
170			:get,
171			"https://api.catapult.inetwork.com/v1/users/" \
172			"catapult_user/domains/catapult_domain/endpoints?page=1&size=1000"
173		).with(
174			headers: {
175				"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
176			}
177		).to_return(status: 200, body: [
178			{ name: "test", domainId: "domain", id: "endpoint" }
179		].to_json)
180
181		sip = customer.sip_account.sync
182		assert_kind_of SipAccount, sip
183		assert_equal "test", sip.username
184		assert_equal(
185			"https://api.catapult.inetwork.com/v1/users/" \
186			"catapult_user/domains/domain/endpoints/endpoint",
187			sip.url
188		)
189
190		assert_requested req1
191		assert_requested req2
192	end
193	em :test_sip_account_existing
194
195	def test_sip_account_error
196		stub_request(
197			:get,
198			"https://api.catapult.inetwork.com/v1/users/" \
199			"catapult_user/domains/catapult_domain/endpoints?page=0&size=1000"
200		).to_return(status: 400)
201
202		assert_raises(RuntimeError) do
203			customer.sip_account.sync
204		end
205	end
206	em :test_sip_account_error
207
208	def test_btc_addresses
209		Customer::REDIS.expect(
210			:smembers,
211			EMPromise.resolve(["testaddr"]),
212			["jmp_customer_btc_addresses-test"]
213		)
214		assert_equal ["testaddr"], customer.btc_addresses.sync
215		assert_mock Customer::REDIS
216	end
217	em :test_btc_addresses
218
219	def test_add_btc_address
220		Customer::REDIS.expect(
221			:spopsadd,
222			EMPromise.resolve("testaddr"),
223			[["jmp_available_btc_addresses", "jmp_customer_btc_addresses-test"]]
224		)
225		Customer::ELECTRUM.expect(
226			:notify,
227			EMPromise.resolve(nil),
228			["testaddr", "http://notify.example.com"]
229		)
230		assert_equal "testaddr", customer.add_btc_address.sync
231		assert_mock Customer::REDIS
232		assert_mock Customer::ELECTRUM
233	end
234	em :test_add_btc_address
235end