~singpolyma/sgx-jmp

ref: fea0ffeb22d0f229d528b30f3844c425b5009ad9 sgx-jmp/test/test_customer_repo.rb -rw-r--r-- 5.7 KiB
fea0ffebStephen Paul Weber Merge branch 'no-more-catapult' 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# frozen_string_literal: true

require "test_helper"
require "customer_repo"

class CustomerRepo
	attr_reader :sgx_repo
end

class CustomerRepoTest < Minitest::Test
	FAKE_REDIS = FakeRedis.new(
		# sgx-jmp customer
		"jmp_customer_jid-test" => "test@example.com",
		"jmp_customer_id-test@example.com" => "test",
		"catapult_jid-+13334445555" => "customer_test@component",
		"catapult_cred-customer_test@component" => [
			"test_bw_customer", "", "", "+13334445555"
		],
		# sgx-jmp customer, empty DB
		"jmp_customer_jid-empty" => "empty@example.com",
		"jmp_customer_id-empty@example.com" => "empty",
		"catapult_jid-+16667778888" => "customer_empty@component",
		"catapult_cred-customer_empty@component" => [
			"test_bw_customer", "", "", "+16667778888"
		],
		# v2 customer
		"jmp_customer_jid-test_v2" => "test_v2@example.com",
		"jmp_customer_id-test_v2@example.com" => "test_v2",
		"catapult_jid-+14445556666" => "test_v2@example.com",
		"catapult_cred-test_v2@example.com" => [
			"test_bw_customer", "", "", "+14445556666"
		],
		# legacy customer
		"catapult_cred-legacy@example.com" => [
			"catapult_user", "", "", "+12223334444"
		],
		"catapult_jid-+12223334444" => "legacy@example.com"
	)

	FAKE_DB = FakeDB.new(
		["test"] => [{
			"balance" => BigDecimal(1234),
			"plan_name" => "test_usd",
			"expires_at" => Time.now + 100
		}],
		["test_v2"] => [{
			"balance" => BigDecimal(2345),
			"plan_name" => "test_usd",
			"expires_at" => Time.now + 100
		}]
	)

	def mkrepo(
		redis: FAKE_REDIS,
		db: FAKE_DB,
		braintree: Minitest::Mock.new
	)
		sgx_repo = Minitest::Mock.new(TrivialBackendSgxRepo.new)
		CustomerRepo.new(
			redis: redis,
			db: db,
			braintree: braintree,
			sgx_repo: sgx_repo
		)
	end

	def setup
		@repo = mkrepo
	end

	def test_find_by_jid
		customer = @repo.find_by_jid("test@example.com").sync
		assert_kind_of Customer, customer
		assert_equal 1234, customer.balance
		assert_equal "merchant_usd", customer.merchant_account
	end
	em :test_find_by_jid

	def test_find_by_id
		customer = @repo.find("test").sync
		assert_kind_of Customer, customer
		assert_equal 1234, customer.balance
		assert_equal "merchant_usd", customer.merchant_account
	end
	em :test_find_by_id

	def test_find_by_customer_jid
		customer = @repo.find_by_jid("customer_test@component").sync
		assert_kind_of Customer, customer
		assert_equal 1234, customer.balance
		assert_equal "merchant_usd", customer.merchant_account
	end
	em :test_find_by_customer_jid

	def test_find_by_jid_not_found
		assert_raises do
			@repo.find_by_jid("test2@example.com").sync
		end
	end
	em :test_find_by_jid_not_found

	def test_find_legacy_customer
		customer = @repo.find_by_jid("legacy@example.com").sync
		assert_kind_of LegacyCustomer, customer
		assert_equal "+12223334444", customer.tel
	end
	em :test_find_legacy_customer

	def test_find_sgx_customer_by_phone
		customer = @repo.find_by_tel("+13334445555").sync
		assert_kind_of Customer, customer
		assert_equal "test", customer.customer_id
	end
	em :test_find_sgx_customer_by_phone

	def test_find_v2_customer_by_phone
		customer = @repo.find_by_tel("+14445556666").sync
		assert_kind_of Customer, customer
		assert_equal "test_v2", customer.customer_id
	end
	em :test_find_v2_customer_by_phone

	def test_find_legacy_customer_by_phone
		customer = @repo.find_by_tel("+12223334444").sync
		assert_kind_of LegacyCustomer, customer
		assert_equal "legacy@example.com", customer.jid.to_s
	end
	em :test_find_legacy_customer_by_phone

	def test_find_missing_phone
		assert_raises do
			@repo.find_by_tel("+15556667777").sync
		end
	end
	em :test_find_missing_phone

	def test_find_db_empty
		customer = @repo.find("empty").sync
		assert_equal BigDecimal(0), customer.balance
	end
	em :test_find_db_empty

	def test_create
		redis = Minitest::Mock.new
		braintree = Minitest::Mock.new
		repo = mkrepo(redis: redis, braintree: braintree)
		braintree_customer = Minitest::Mock.new
		braintree.expect(:customer, braintree_customer)
		braintree_customer.expect(
			:create,
			EMPromise.resolve(
				OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
			)
		)
		redis.expect(
			:msetnx,
			EMPromise.resolve(1),
			[
				"jmp_customer_id-test@example.com", "test",
				"jmp_customer_jid-test", "test@example.com"
			]
		)
		assert_kind_of Customer, repo.create("test@example.com").sync
		assert_mock braintree
		assert_mock braintree_customer
		assert_mock redis
	end
	em :test_create

	def test_put_lidb_name
		post = stub_request(
			:post,
			"https://dashboard.bandwidth.com/v1.0/accounts//lidbs"
		).with(body: {
			CustomerOrderId: "test",
			LidbTnGroups: {
				LidbTnGroup: {
					TelephoneNumbers: "5556667777",
					SubscriberInformation: "Hank",
					UseType: "RESIDENTIAL",
					Visibility: "PUBLIC"
				}
			}
		}.to_xml(root: "LidbOrder", indent: 0)).to_return(
			status: 201,
			headers: { location: "/boop/123" }
		)

		stub_request(
			:get,
			"https://dashboard.bandwidth.com/v1.0/accounts//lidbs/123"
		)

		@repo.put_lidb_name(
			Customer.new(
				"test",
				"test@exmple.com",
				sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777"))
			),
			"Hank"
		)

		assert_requested post
	end
	em :test_put_lidb_name

	def test_put_transcription_enabled
		@repo.sgx_repo.expect(
			:put_transcription_enabled,
			EMPromise.resolve(nil),
			["test", true]
		)
		@repo.put_transcription_enabled(
			Customer.new("test", "test@exmple.com"),
			true
		)
		assert_mock @repo.sgx_repo
	end
	em :test_put_transcription_enabled

	def test_put_fwd
		@repo.sgx_repo.expect(
			:put_fwd,
			EMPromise.resolve(nil),
			["test", "+15556667777", :fwd]
		)
		@repo.put_fwd(
			Customer.new(
				"test",
				"test@exmple.com",
				sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777"))
			),
			:fwd
		)
		assert_mock @repo.sgx_repo
	end
	em :test_put_fwd
end