aboutsummaryrefslogtreecommitdiff
path: root/tests/managers/thl/test_wallet/test_user_payout.py
blob: 2a89c979b86aa4c8d01ad3381e06adaea0d1519c (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
from datetime import datetime
from decimal import Decimal
from random import randint
from unittest.mock import Mock
from uuid import uuid4

import pytest
from faker import Faker

from generalresearch.currency import USDCent
from generalresearch.managers.thl.cashout_method import CashoutMethodManager
from generalresearch.managers.thl.ipinfo import GeoIpInfoManager
from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
from generalresearch.managers.thl.userhealth import UserIpHistoryManager
from generalresearch.managers.thl.wallet.tango import TangoManager
from generalresearch.managers.thl.wallet.user_payout import (
    UserPayoutEventManager,
    make_request_paypal,
)
from generalresearch.models.thl.definitions import PayoutStatus
from generalresearch.models.thl.payout import UserPayoutEvent
from generalresearch.models.thl.user import User
from generalresearch.models.thl.wallet.cashout_method import (
    CashMailCashoutMethodData,
    CashMailCashoutMethodRequestData,
    CashoutRequestDetail,
    PaypalCashoutMethodData,
    PaypalCashoutMethodRequestData,
    USDeliveryAddress,
)
from generalresearch.models.thl.wallet.definitions import PayoutType

fake = Faker()


class TestUserPayoutEventManager:
    def test_get_by_uuid_and_create(
        self,
        user: User,
        user_payout_event_manager: UserPayoutEventManager,
        thl_ledger_manager: ThlLedgerManager,
        utc_now: datetime,
    ):
        user_account = thl_ledger_manager.get_account_or_create_user_wallet(user=user)
        request_data = PaypalCashoutMethodRequestData.model_validate(
            {
                "email": fake.email(),
                "interface": "api",
            }
        )
        pe1: UserPayoutEvent = user_payout_event_manager.create(
            debit_account_uuid=user_account.uuid,
            payout_type=PayoutType.PAYPAL,
            cashout_method_uuid=uuid4().hex,
            amount=100,
            created=utc_now,
            request_data=request_data.model_dump(mode="json"),
        )
        # these get added by the query
        pe1.account_reference_type = "user"
        pe1.account_reference_uuid = user.uuid

        pe2 = user_payout_event_manager.get_by_uuid(pe_uuid=pe1.uuid)

        assert pe1 == pe2

    def test_get_payout_detail_cash_in_mail(
        self,
        user_with_wallet: User,
        user_payout_event_manager: UserPayoutEventManager,
        cashout_method_manager: CashoutMethodManager,
        thl_ledger_manager: ThlLedgerManager,
    ):
        user = user_with_wallet
        data = CashMailCashoutMethodData(
            delivery_address=USDeliveryAddress.model_validate(
                {
                    "name_or_attn": "Josh Ackerman",
                    "address": "123 Fake St",
                    "city": "San Francisco",
                    "state": "CA",
                    "postal_code": "12345",
                }
            )
        )
        cm = cashout_method_manager.create_cash_in_mail_cashout_method(
            data=data, user=user
        )
        user_account = thl_ledger_manager.get_account_or_create_user_wallet(user=user)

        rand_amount = randint(a=99, b=999)
        request_data = CashMailCashoutMethodRequestData.model_validate(
            cm.data.model_dump()
        )

        pe = user_payout_event_manager.create(
            debit_account_uuid=user_account.uuid,
            cashout_method_uuid=cm.id,
            amount=rand_amount,
            ext_ref_id=uuid4().hex,
            payout_type=PayoutType.CASH_IN_MAIL,
            request_data=request_data.model_dump(mode="json"),
        )

        res = user_payout_event_manager.get_payout_detail(pe_uuid=pe.uuid)
        assert isinstance(res, CashoutRequestDetail)

    def test_get_payout_detail_paypal(
        self,
        user_with_wallet: User,
        user_payout_event_manager: UserPayoutEventManager,
        cashout_method_manager: CashoutMethodManager,
        thl_ledger_manager: ThlLedgerManager,
    ):
        user = user_with_wallet
        data = PaypalCashoutMethodData(email=fake.email())
        cm = cashout_method_manager.create_paypal_cashout_method(data=data, user=user)
        user_account = thl_ledger_manager.get_account_or_create_user_wallet(user=user)

        rand_amount = randint(a=99, b=999)
        request_data = make_request_paypal(cm)
        pe = user_payout_event_manager.create(
            debit_account_uuid=user_account.uuid,
            cashout_method_uuid=cm.id,
            amount=rand_amount,
            ext_ref_id=uuid4().hex,
            payout_type=PayoutType.PAYPAL,
            request_data=request_data.model_dump(mode="json"),
        )

        res = user_payout_event_manager.get_payout_detail(pe_uuid=pe.uuid)
        assert isinstance(res, CashoutRequestDetail)


class TestUserRequestRedeem:
    @staticmethod
    def request_paypal_payout(
        *,
        user: User,
        amount: USDCent,
        user_payout_event_manager: UserPayoutEventManager,
        cashout_method_manager: CashoutMethodManager,
        thl_ledger_manager: ThlLedgerManager,
        user_iphistory_manager: UserIpHistoryManager,
        ip_record_factory,
        thl_redis_client,
        ip: str = "8.8.8.8",
        fund_amount: Decimal = Decimal("10.00"),
    ) -> UserPayoutEvent:
        email = fake.email()
        cashout_method = cashout_method_manager.create_paypal_cashout_method(
            data=PaypalCashoutMethodData(email=email),
            user=user,
        )
        ip_record_factory(user=user, ip=ip)
        thl_ledger_manager.create_tx_user_bonus(
            user=user,
            amount=fund_amount,
            ref_uuid=uuid4().hex,
            description="Fund PayPal redemption test",
        )

        tango_manager = Mock(spec=TangoManager)
        tango_manager.get_exchange_rates.return_value = {}

        return user_payout_event_manager.user_request_redeem(
            user=user,
            cashout_method_id=cashout_method.id,
            amount=amount,
            tango_manager=tango_manager,
            cashout_method_manager=cashout_method_manager,
            ledger_manager=thl_ledger_manager,
            user_ip_history_manager=user_iphistory_manager,
            redis_client=thl_redis_client,
        )

    def test_user_request_redeem_paypal(
        self,
        user_with_wallet: User,
        user_payout_event_manager: UserPayoutEventManager,
        cashout_method_manager: CashoutMethodManager,
        thl_ledger_manager: ThlLedgerManager,
        user_iphistory_manager: UserIpHistoryManager,
        geoip_info_manager: GeoIpInfoManager,
        ip_record_factory,
        thl_redis_client,
    ):
        user = user_with_wallet
        payout = self.request_paypal_payout(
            user=user,
            amount=USDCent(600),
            user_payout_event_manager=user_payout_event_manager,
            cashout_method_manager=cashout_method_manager,
            thl_ledger_manager=thl_ledger_manager,
            user_iphistory_manager=user_iphistory_manager,
            ip_record_factory=ip_record_factory,
            thl_redis_client=thl_redis_client,
        )

        assert payout.status == PayoutStatus.PENDING
        assert payout.payout_type == PayoutType.PAYPAL
        assert payout.amount == 600
        assert payout.request_data["interface"] == "api"
        assert thl_ledger_manager.get_user_wallet_balance(user) == 400
        assert user_payout_event_manager.get_by_uuid(payout.uuid) == payout

        detail = user_payout_event_manager.get_payout_detail(payout.uuid)
        assert not detail.transaction_info
        assert detail.description == "PayPal"

    def test_user_request_redeem_paypal_complete(
        self,
        user_with_wallet: User,
        user_payout_event_manager: UserPayoutEventManager,
        cashout_method_manager: CashoutMethodManager,
        thl_ledger_manager: ThlLedgerManager,
        user_iphistory_manager: UserIpHistoryManager,
        geoip_info_manager: GeoIpInfoManager,
        ip_record_factory,
        thl_redis_client,
        create_main_accounts,
    ):
        create_main_accounts()
        user = user_with_wallet
        payout = self.request_paypal_payout(
            user=user,
            amount=USDCent(600),
            user_payout_event_manager=user_payout_event_manager,
            cashout_method_manager=cashout_method_manager,
            thl_ledger_manager=thl_ledger_manager,
            user_iphistory_manager=user_iphistory_manager,
            ip_record_factory=ip_record_factory,
            thl_redis_client=thl_redis_client,
        )

        user_payout_event_manager.update(
            payout_event=payout,
            status=PayoutStatus.APPROVED,
            ext_ref_id="paypal-batch-id",
        )
        thl_ledger_manager.create_tx_user_payout_complete(
            user=user,
            payout_event=payout,
            fee_amount=Decimal("0.25"),
        )
        user_payout_event_manager.update(
            payout_event=payout,
            status=PayoutStatus.COMPLETE,
            order_data={"transaction_id": "paypal-transaction-id"},
        )

        detail = user_payout_event_manager.get_payout_detail(pe_uuid=payout.uuid)
        assert detail.status == PayoutStatus.COMPLETE
        assert detail.transaction_info == {"transaction_id": "paypal-transaction-id"}

        assert thl_ledger_manager.get_user_wallet_balance(user_with_wallet) == 400
        bp_expense = thl_ledger_manager.get_account_or_create_bp_expense_by_uuid(
            product_uuid=user.to_user_ref().product_id, expense_name="paypal"
        )
        assert thl_ledger_manager.get_account_balance(bp_expense) == -25

    def test_user_request_redeem_tango_complete(
        self,
        user_with_wallet: User,
        user_payout_event_manager: UserPayoutEventManager,
        cashout_method_manager: CashoutMethodManager,
        thl_ledger_manager: ThlLedgerManager,
        user_iphistory_manager: UserIpHistoryManager,
        ip_record_factory,
        thl_redis_client,
        create_main_accounts,
        setup_cashoutmethod_db,
        example_tango_cashout_methods,
    ):
        create_main_accounts()
        setup_cashoutmethod_db()
        user = user_with_wallet
        # This is the italian Amazon.it gift card
        cashout_method = example_tango_cashout_methods[1]
        # This is an IP that we have hard-coded by the geoip_info_manager as Italy
        ip_record_factory(user=user, ip="2.2.2.2")

        thl_ledger_manager.create_tx_user_bonus(
            user=user,
            amount=Decimal("5.00"),
            ref_uuid=uuid4().hex,
            description="Fund Tango redemption test",
        )

        tango_client = Mock()
        tango_client.get_exchange_rates.return_value = {
            "exchangeRates": [
                {"rewardCurrency": "USD", "baseCurrency": "EUR", "baseFx": 1.14792}
            ]
        }
        tango_manager = TangoManager(
            tango_client=tango_client,
            tango_account_id="test-account",
            tango_customer_id="test-customer",
            cashout_method_manager=cashout_method_manager,
        )
        request = tango_manager.make_request(
            amount=USDCent(2_00),
            cashout_method=cashout_method,
            payout_event_id=uuid4().hex,
        )
        assert request.amount == Decimal(2.0 / 1.14792).quantize(
            Decimal("0.01")
        )  # ~ 1.74 EUR

        payout = user_payout_event_manager.user_request_redeem(
            user=user,
            cashout_method_id=cashout_method.id,
            amount=USDCent(2_00),
            tango_manager=tango_manager,
            cashout_method_manager=cashout_method_manager,
            ledger_manager=thl_ledger_manager,
            user_ip_history_manager=user_iphistory_manager,
            redis_client=thl_redis_client,
        )
        assert payout.request_data["amount"] == '1.74'
        assert payout.request_data["externalRefID"] == payout.uuid

        user_payout_event_manager.update(
            payout_event=payout,
            status=PayoutStatus.APPROVED,
            ext_ref_id="tango-order-id",
        )
        thl_ledger_manager.create_tx_user_payout_complete(
            user=user,
            payout_event=payout,
        )
        user_payout_event_manager.update(
            payout_event=payout,
            status=PayoutStatus.COMPLETE,
            order_data={
                "reward": {
                    "credentialList": [
                        {
                            "credentialType": "giftCardCode",
                            "credentialValue": "TEST-CODE",
                        }
                    ],
                    "redemptionInstructions": "Use this code at checkout.",
                }
            },
        )

        detail = user_payout_event_manager.get_payout_detail(pe_uuid=payout.uuid)
        assert payout.payout_type == PayoutType.TANGO
        assert detail.status == PayoutStatus.COMPLETE
        assert detail.transaction_info == {
            "credential_list": [
                {
                    "credentialType": "giftCardCode",
                    "credentialValue": "TEST-CODE",
                }
            ],
            "redemption_instructions": "Use this code at checkout.",
        }
        assert thl_ledger_manager.get_user_wallet_balance(user) == 300
        bp_expense = thl_ledger_manager.get_account_or_create_bp_expense_by_uuid(
            product_uuid=user.to_user_ref().product_id,
            expense_name="tango",
        )
        assert thl_ledger_manager.get_account_balance(bp_expense) == -7

    def test_user_request_redeem_paypal_insufficient_balance(
        self,
        user_with_wallet: User,
        user_payout_event_manager: UserPayoutEventManager,
        cashout_method_manager: CashoutMethodManager,
        thl_ledger_manager: ThlLedgerManager,
        user_iphistory_manager: UserIpHistoryManager,
        ip_record_factory,
        thl_redis_client,
    ):
        with pytest.raises(AssertionError, match="more than their redeemable balance"):
            self.request_paypal_payout(
                user=user_with_wallet,
                amount=USDCent(1_100),
                user_payout_event_manager=user_payout_event_manager,
                cashout_method_manager=cashout_method_manager,
                thl_ledger_manager=thl_ledger_manager,
                user_iphistory_manager=user_iphistory_manager,
                ip_record_factory=ip_record_factory,
                thl_redis_client=thl_redis_client,
            )

    def test_user_request_redeem_paypal_blocked_user(
        self,
        user_with_wallet: User,
        user_payout_event_manager: UserPayoutEventManager,
        cashout_method_manager: CashoutMethodManager,
        thl_ledger_manager: ThlLedgerManager,
        user_iphistory_manager: UserIpHistoryManager,
        ip_record_factory,
        thl_redis_client,
    ):
        user_with_wallet.blocked = True

        with pytest.raises(AssertionError, match="Blocked user"):
            self.request_paypal_payout(
                user=user_with_wallet,
                amount=USDCent(600),
                user_payout_event_manager=user_payout_event_manager,
                cashout_method_manager=cashout_method_manager,
                thl_ledger_manager=thl_ledger_manager,
                user_iphistory_manager=user_iphistory_manager,
                ip_record_factory=ip_record_factory,
                thl_redis_client=thl_redis_client,
            )

    def test_user_request_redeem_paypal_anonymous_user(
        self,
        user_with_wallet: User,
        user_payout_event_manager: UserPayoutEventManager,
        cashout_method_manager: CashoutMethodManager,
        thl_ledger_manager: ThlLedgerManager,
        user_iphistory_manager: UserIpHistoryManager,
        ip_record_factory,
        thl_redis_client,
    ):
        with pytest.raises(AssertionError, match="Anonymous user"):
            self.request_paypal_payout(
                user=user_with_wallet,
                amount=USDCent(600),
                user_payout_event_manager=user_payout_event_manager,
                cashout_method_manager=cashout_method_manager,
                thl_ledger_manager=thl_ledger_manager,
                user_iphistory_manager=user_iphistory_manager,
                ip_record_factory=ip_record_factory,
                thl_redis_client=thl_redis_client,
                ip="1.1.1.1",
            )