~singpolyma/sgx-jmp

ref: 045da39f25b74034e7bba44cb5f1db48a935ba84 sgx-jmp/test/test_customer_repo.rb -rw-r--r-- 4.2 KiB
045da39fStephen Paul Weber Hotfix: arguments in wrong order 1 year, 1 month 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
# frozen_string_literal: true

require "test_helper"
require "customer_repo"

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@jmp.chat",
		"catapult_cred-customer_test@jmp.chat" => [
			"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@jmp.chat",
		"catapult_cred-customer_empty@jmp.chat" => [
			"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
	)
		CustomerRepo.new(redis: redis, db: db, braintree: braintree)
	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@jmp.chat").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
end