1# frozen_string_literal: true
2
3require "test_helper"
4require "customer_repo"
5
6class CustomerRepoTest < Minitest::Test
7 FAKE_REDIS = FakeRedis.new(
8 # sgx-jmp customer
9 "jmp_customer_jid-test" => "test@example.com",
10 "jmp_customer_id-test@example.com" => "test",
11 "catapult_jid-+13334445555" => "customer_test@component",
12 "catapult_cred-customer_test@component" => [
13 "test_bw_customer", "", "", "+13334445555"
14 ],
15 # sgx-jmp customer, empty DB
16 "jmp_customer_jid-empty" => "empty@example.com",
17 "jmp_customer_id-empty@example.com" => "empty",
18 "catapult_jid-+16667778888" => "customer_empty@component",
19 "catapult_cred-customer_empty@component" => [
20 "test_bw_customer", "", "", "+16667778888"
21 ],
22 # v2 customer
23 "jmp_customer_jid-test_v2" => "test_v2@example.com",
24 "jmp_customer_id-test_v2@example.com" => "test_v2",
25 "catapult_jid-+14445556666" => "test_v2@example.com",
26 "catapult_cred-test_v2@example.com" => [
27 "test_bw_customer", "", "", "+14445556666"
28 ],
29 # legacy customer
30 "catapult_cred-legacy@example.com" => [
31 "catapult_user", "", "", "+12223334444"
32 ],
33 "catapult_jid-+12223334444" => "legacy@example.com"
34 )
35
36 FAKE_DB = FakeDB.new(
37 ["test"] => [{
38 "balance" => BigDecimal(1234),
39 "plan_name" => "test_usd",
40 "expires_at" => Time.now + 100
41 }],
42 ["test_v2"] => [{
43 "balance" => BigDecimal(2345),
44 "plan_name" => "test_usd",
45 "expires_at" => Time.now + 100
46 }]
47 )
48
49 def mkrepo(
50 redis: FAKE_REDIS,
51 db: FAKE_DB,
52 braintree: Minitest::Mock.new
53 )
54 CustomerRepo.new(redis: redis, db: db, braintree: braintree)
55 end
56
57 def setup
58 @repo = mkrepo
59 end
60
61 def test_find_by_jid
62 customer = @repo.find_by_jid("test@example.com").sync
63 assert_kind_of Customer, customer
64 assert_equal 1234, customer.balance
65 assert_equal "merchant_usd", customer.merchant_account
66 end
67 em :test_find_by_jid
68
69 def test_find_by_id
70 customer = @repo.find("test").sync
71 assert_kind_of Customer, customer
72 assert_equal 1234, customer.balance
73 assert_equal "merchant_usd", customer.merchant_account
74 end
75 em :test_find_by_id
76
77 def test_find_by_customer_jid
78 customer = @repo.find_by_jid("customer_test@component").sync
79 assert_kind_of Customer, customer
80 assert_equal 1234, customer.balance
81 assert_equal "merchant_usd", customer.merchant_account
82 end
83 em :test_find_by_customer_jid
84
85 def test_find_by_jid_not_found
86 assert_raises do
87 @repo.find_by_jid("test2@example.com").sync
88 end
89 end
90 em :test_find_by_jid_not_found
91
92 def test_find_legacy_customer
93 customer = @repo.find_by_jid("legacy@example.com").sync
94 assert_kind_of LegacyCustomer, customer
95 assert_equal "+12223334444", customer.tel
96 end
97 em :test_find_legacy_customer
98
99 def test_find_sgx_customer_by_phone
100 customer = @repo.find_by_tel("+13334445555").sync
101 assert_kind_of Customer, customer
102 assert_equal "test", customer.customer_id
103 end
104 em :test_find_sgx_customer_by_phone
105
106 def test_find_v2_customer_by_phone
107 customer = @repo.find_by_tel("+14445556666").sync
108 assert_kind_of Customer, customer
109 assert_equal "test_v2", customer.customer_id
110 end
111 em :test_find_v2_customer_by_phone
112
113 def test_find_legacy_customer_by_phone
114 customer = @repo.find_by_tel("+12223334444").sync
115 assert_kind_of LegacyCustomer, customer
116 assert_equal "legacy@example.com", customer.jid.to_s
117 end
118 em :test_find_legacy_customer_by_phone
119
120 def test_find_missing_phone
121 assert_raises do
122 @repo.find_by_tel("+15556667777").sync
123 end
124 end
125 em :test_find_missing_phone
126
127 def test_find_db_empty
128 customer = @repo.find("empty").sync
129 assert_equal BigDecimal(0), customer.balance
130 end
131 em :test_find_db_empty
132
133 def test_create
134 redis = Minitest::Mock.new
135 braintree = Minitest::Mock.new
136 repo = mkrepo(redis: redis, braintree: braintree)
137 braintree_customer = Minitest::Mock.new
138 braintree.expect(:customer, braintree_customer)
139 braintree_customer.expect(:create, EMPromise.resolve(
140 OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
141 ))
142 redis.expect(
143 :msetnx,
144 EMPromise.resolve(1),
145 [
146 "jmp_customer_id-test@example.com", "test",
147 "jmp_customer_jid-test", "test@example.com"
148 ]
149 )
150 assert_kind_of Customer, repo.create("test@example.com").sync
151 assert_mock braintree
152 assert_mock braintree_customer
153 assert_mock redis
154 end
155 em :test_create
156end