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
 10class CustomerRepoTest < Minitest::Test
 11	FAKE_REDIS = FakeRedis.new(
 12		# sgx-jmp customer
 13		"jmp_customer_jid-test" => "test@example.com",
 14		"jmp_customer_id-test@example.com" => "test",
 15		"catapult_jid-+13334445555" => "customer_test@component",
 16		"catapult_cred-customer_test@component" => [
 17			"test_bw_customer", "", "", "+13334445555"
 18		],
 19		# sgx-jmp customer, empty DB
 20		"jmp_customer_jid-empty" => "empty@example.com",
 21		"jmp_customer_id-empty@example.com" => "empty",
 22		"catapult_jid-+16667778888" => "customer_empty@component",
 23		"catapult_cred-customer_empty@component" => [
 24			"test_bw_customer", "", "", "+16667778888"
 25		],
 26		# v2 customer
 27		"jmp_customer_jid-test_v2" => "test_v2@example.com",
 28		"jmp_customer_id-test_v2@example.com" => "test_v2",
 29		"catapult_jid-+14445556666" => "test_v2@example.com",
 30		"catapult_cred-test_v2@example.com" => [
 31			"test_bw_customer", "", "", "+14445556666"
 32		]
 33	)
 34
 35	FAKE_DB = FakeDB.new(
 36		["test"] => [{
 37			"balance" => BigDecimal(1234),
 38			"plan_name" => "test_usd",
 39			"expires_at" => Time.now + 100
 40		}],
 41		["test_v2"] => [{
 42			"balance" => BigDecimal(2345),
 43			"plan_name" => "test_usd",
 44			"expires_at" => Time.now + 100
 45		}]
 46	)
 47
 48	def mkrepo(
 49		redis: FAKE_REDIS,
 50		db: FAKE_DB,
 51		braintree: Minitest::Mock.new
 52	)
 53		sgx_repo = Minitest::Mock.new(TrivialBackendSgxRepo.new)
 54		CustomerRepo.new(
 55			redis: redis,
 56			db: db,
 57			braintree: braintree,
 58			sgx_repo: sgx_repo
 59		)
 60	end
 61
 62	def setup
 63		@repo = mkrepo
 64	end
 65
 66	def test_find_by_jid
 67		customer = @repo.find_by_jid("test@example.com").sync
 68		assert_kind_of Customer, customer
 69		assert_equal 1234, customer.balance
 70		assert_equal "merchant_usd", customer.merchant_account
 71	end
 72	em :test_find_by_jid
 73
 74	def test_find_by_id
 75		customer = @repo.find("test").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_id
 81
 82	def test_find_by_customer_jid
 83		customer = @repo.find_by_jid("customer_test@component").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_customer_jid
 89
 90	def test_find_by_jid_not_found
 91		assert_raises do
 92			@repo.find_by_jid("test2@example.com").sync
 93		end
 94	end
 95	em :test_find_by_jid_not_found
 96
 97	def test_find_sgx_customer_by_phone
 98		customer = @repo.find_by_tel("+13334445555").sync
 99		assert_kind_of Customer, customer
100		assert_equal "test", customer.customer_id
101	end
102	em :test_find_sgx_customer_by_phone
103
104	def test_find_v2_customer_by_phone
105		customer = @repo.find_by_tel("+14445556666").sync
106		assert_kind_of Customer, customer
107		assert_equal "test_v2", customer.customer_id
108	end
109	em :test_find_v2_customer_by_phone
110
111	def test_find_missing_phone
112		assert_raises do
113			@repo.find_by_tel("+15556667777").sync
114		end
115	end
116	em :test_find_missing_phone
117
118	def test_find_db_empty
119		customer = @repo.find("empty").sync
120		assert_equal BigDecimal(0), customer.balance
121	end
122	em :test_find_db_empty
123
124	def test_create
125		redis = Minitest::Mock.new
126		braintree = Minitest::Mock.new
127		repo = mkrepo(redis: redis, braintree: braintree)
128		braintree_customer = Minitest::Mock.new
129		braintree.expect(:customer, braintree_customer)
130		braintree_customer.expect(
131			:create,
132			EMPromise.resolve(
133				OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
134			)
135		)
136		redis.expect(
137			:msetnx,
138			EMPromise.resolve(1),
139			[
140				"jmp_customer_id-test@example.com", "test",
141				"jmp_customer_jid-test", "test@example.com"
142			]
143		)
144		assert_kind_of Customer, repo.create("test@example.com").sync
145		assert_mock braintree
146		assert_mock braintree_customer
147		assert_mock redis
148	end
149	em :test_create
150
151	def test_put_lidb_name
152		post = stub_request(
153			:post,
154			"https://dashboard.bandwidth.com/v1.0/accounts//lidbs"
155		).with(body: {
156			LidbTnGroups: {
157				LidbTnGroup: {
158					TelephoneNumbers: {
159						TelephoneNumber: "5556667777"
160					},
161					SubscriberInformation: "Hank",
162					UseType: "RESIDENTIAL",
163					Visibility: "PUBLIC"
164				}
165			}
166		}.to_xml(root: "LidbOrder", indent: 0)).to_return(
167			status: 201,
168			headers: { location: "/boop/123" }
169		)
170
171		stub_request(
172			:get,
173			"https://dashboard.bandwidth.com/v1.0/accounts//lidbs/123"
174		)
175
176		@repo.put_lidb_name(
177			Customer.new(
178				"test",
179				"test@exmple.com",
180				sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777"))
181			),
182			"Hank"
183		)
184
185		assert_requested post
186	end
187	em :test_put_lidb_name
188
189	def test_put_transcription_enabled
190		@repo.sgx_repo.expect(
191			:put_transcription_enabled,
192			EMPromise.resolve(nil),
193			["test", true]
194		)
195		@repo.put_transcription_enabled(
196			Customer.new("test", "test@exmple.com"),
197			true
198		)
199		assert_mock @repo.sgx_repo
200	end
201	em :test_put_transcription_enabled
202
203	def test_put_fwd
204		@repo.sgx_repo.expect(
205			:put_fwd,
206			EMPromise.resolve(nil),
207			["test", "+15556667777", :fwd]
208		)
209		@repo.put_fwd(
210			Customer.new(
211				"test",
212				"test@exmple.com",
213				sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777"))
214			),
215			:fwd
216		)
217		assert_mock @repo.sgx_repo
218	end
219	em :test_put_fwd
220end