test_customer_repo.rb

  1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "customer_repo"
  5
  6class CustomerRepo
  7	attr_reader :sgx_repo
  8end
  9
 10CustomerPlan::DB = Minitest::Mock.new
 11
 12class CustomerRepoTest < Minitest::Test
 13	FAKE_REDIS = FakeRedis.new(
 14		# sgx-jmp customer
 15		"jmp_customer_jid-test" => "test@example.com",
 16		"jmp_customer_id-test@example.com" => "test",
 17		"catapult_jid-+13334445555" => "customer_test@component",
 18		"catapult_cred-customer_test@component" => [
 19			"test_bw_customer", "", "", "+13334445555"
 20		],
 21		# sgx-jmp customer, empty DB
 22		"jmp_customer_jid-empty" => "empty@example.com",
 23		"jmp_customer_id-empty@example.com" => "empty",
 24		"catapult_jid-+16667778888" => "customer_empty@component",
 25		"catapult_cred-customer_empty@component" => [
 26			"test_bw_customer", "", "", "+16667778888"
 27		],
 28		# v2 customer
 29		"jmp_customer_jid-test_v2" => "test_v2@example.com",
 30		"jmp_customer_id-test_v2@example.com" => "test_v2",
 31		"catapult_jid-+14445556666" => "test_v2@example.com",
 32		"catapult_cred-test_v2@example.com" => [
 33			"test_bw_customer", "", "", "+14445556666"
 34		]
 35	)
 36
 37	FAKE_DB = FakeDB.new(
 38		["test"] => [{
 39			"balance" => BigDecimal(1234),
 40			"plan_name" => "test_usd",
 41			"expires_at" => Time.now + 100
 42		}],
 43		["testp"] => [{
 44			"balance" => BigDecimal(1234),
 45			"plan_name" => "test_usd",
 46			"expires_at" => Time.now + 100,
 47			"parent_customer_id" => "1234"
 48		}],
 49		["test_v2"] => [{
 50			"balance" => BigDecimal(2345),
 51			"plan_name" => "test_usd",
 52			"expires_at" => Time.now + 100
 53		}]
 54	)
 55
 56	def mkrepo(
 57		redis: FAKE_REDIS,
 58		db: FAKE_DB,
 59		braintree: Minitest::Mock.new
 60	)
 61		sgx_repo = Minitest::Mock.new(TrivialBackendSgxRepo.new)
 62		CustomerRepo.new(
 63			redis: redis,
 64			db: db,
 65			braintree: braintree,
 66			sgx_repo: sgx_repo
 67		)
 68	end
 69
 70	def setup
 71		@repo = mkrepo
 72	end
 73
 74	def test_find_by_jid
 75		customer = @repo.find_by_jid("test@example.com").sync
 76		assert_kind_of Customer, customer
 77		assert_equal 1234, customer.balance
 78		assert_equal "merchant_usd", customer.merchant_account
 79	end
 80	em :test_find_by_jid
 81
 82	def test_find_by_id
 83		customer = @repo.find("test").sync
 84		assert_kind_of Customer, customer
 85		assert_equal 1234, customer.balance
 86		assert_equal "merchant_usd", customer.merchant_account
 87	end
 88	em :test_find_by_id
 89
 90	def test_find_by_customer_jid
 91		customer = @repo.find_by_jid("customer_test@component").sync
 92		assert_kind_of Customer, customer
 93		assert_equal 1234, customer.balance
 94		assert_equal "merchant_usd", customer.merchant_account
 95	end
 96	em :test_find_by_customer_jid
 97
 98	def test_find_by_jid_not_found
 99		assert_raises do
100			@repo.find_by_jid("test2@example.com").sync
101		end
102	end
103	em :test_find_by_jid_not_found
104
105	def test_find_sgx_customer_by_phone
106		customer = @repo.find_by_tel("+13334445555").sync
107		assert_kind_of Customer, customer
108		assert_equal "test", customer.customer_id
109	end
110	em :test_find_sgx_customer_by_phone
111
112	def test_find_v2_customer_by_phone
113		customer = @repo.find_by_tel("+14445556666").sync
114		assert_kind_of Customer, customer
115		assert_equal "test_v2", customer.customer_id
116	end
117	em :test_find_v2_customer_by_phone
118
119	def test_find_missing_phone
120		assert_raises do
121			@repo.find_by_tel("+15556667777").sync
122		end
123	end
124	em :test_find_missing_phone
125
126	def test_find_db_empty
127		customer = @repo.find("empty").sync
128		assert_equal BigDecimal(0), customer.balance
129	end
130	em :test_find_db_empty
131
132	def test_create
133		redis = Minitest::Mock.new
134		braintree = Minitest::Mock.new
135		repo = mkrepo(redis: redis, braintree: braintree)
136		braintree_customer = Minitest::Mock.new
137		braintree.expect(:customer, braintree_customer)
138		braintree_customer.expect(
139			:create,
140			EMPromise.resolve(
141				OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
142			)
143		)
144		redis.expect(
145			:msetnx,
146			EMPromise.resolve(1),
147			[
148				"jmp_customer_id-test@example.com", "test",
149				"jmp_customer_jid-test", "test@example.com"
150			]
151		)
152		assert_kind_of Customer, repo.create("test@example.com").sync
153		assert_mock braintree
154		assert_mock braintree_customer
155		assert_mock redis
156	end
157	em :test_create
158
159	def test_create_parented
160		redis = Minitest::Mock.new
161		braintree = Minitest::Mock.new
162		repo = mkrepo(redis: redis, braintree: braintree)
163		braintree_customer = Minitest::Mock.new
164		braintree.expect(:customer, braintree_customer)
165		braintree_customer.expect(
166			:create,
167			EMPromise.resolve(
168				OpenStruct.new(success?: true, customer: OpenStruct.new(id: "testp"))
169			)
170		)
171		redis.expect(
172			:msetnx,
173			EMPromise.resolve(1),
174			[
175				"jmp_customer_id-test@parented.example.com", "testp",
176				"jmp_customer_jid-testp", "test@parented.example.com"
177			]
178		)
179		redis.expect(
180			:get,
181			EMPromise.resolve("test@parented.example.com"),
182			["jmp_customer_jid-testp"]
183		)
184		redis.expect(
185			:mget,
186			EMPromise.resolve([nil, nil]),
187			[
188				"jmp_customer_auto_top_up_amount-testp",
189				"jmp_customer_monthly_overage_limit-testp"
190			]
191		)
192		redis.expect(
193			:smembers,
194			EMPromise.resolve([]),
195			["jmp_customer_feature_flags-testp"]
196		)
197		CustomerPlan::DB.expect(
198			:query,
199			[{ "plan_name" => "test_usd" }],
200			[String, ["1234"]]
201		)
202		CustomerPlan::DB.expect(
203			:exec_defer,
204			EMPromise.resolve(nil),
205			[String, ["testp", "test_usd", "1234"]]
206		)
207		result = repo.create("test@parented.example.com").sync
208		assert_kind_of Customer::ChildCustomer, result
209		assert_equal "1234", result.billing_customer_id
210		assert_mock braintree
211		assert_mock braintree_customer
212		assert_mock redis
213		assert_mock CustomerPlan::DB
214	end
215	em :test_create_parented
216
217	def test_put_lidb_name
218		post = stub_request(
219			:post,
220			"https://dashboard.bandwidth.com/v1.0/accounts//lidbs"
221		).with(body: {
222			LidbTnGroups: {
223				LidbTnGroup: {
224					TelephoneNumbers: {
225						TelephoneNumber: "5556667777"
226					},
227					SubscriberInformation: "Hank",
228					UseType: "RESIDENTIAL",
229					Visibility: "PUBLIC"
230				}
231			}
232		}.to_xml(root: "LidbOrder", indent: 0)).to_return(
233			status: 201,
234			headers: { location: "/boop/123" }
235		)
236
237		stub_request(
238			:get,
239			"https://dashboard.bandwidth.com/v1.0/accounts//lidbs/123"
240		)
241
242		@repo.put_lidb_name(
243			Customer.new(
244				"test",
245				"test@exmple.com",
246				sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777"))
247			),
248			"Hank"
249		)
250
251		assert_requested post
252	end
253	em :test_put_lidb_name
254
255	def test_put_transcription_enabled
256		@repo.sgx_repo.expect(
257			:put_transcription_enabled,
258			EMPromise.resolve(nil),
259			["test", true]
260		)
261		@repo.put_transcription_enabled(
262			Customer.new("test", "test@exmple.com", sgx: nil),
263			true
264		)
265		assert_mock @repo.sgx_repo
266	end
267	em :test_put_transcription_enabled
268
269	def test_put_fwd
270		@repo.sgx_repo.expect(
271			:put_fwd,
272			EMPromise.resolve(nil),
273			["test", "+15556667777", :fwd]
274		)
275		@repo.put_fwd(
276			Customer.new(
277				"test",
278				"test@exmple.com",
279				sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777"))
280			),
281			:fwd
282		)
283		assert_mock @repo.sgx_repo
284	end
285	em :test_put_fwd
286end