aboutsummaryrefslogtreecommitdiff
path: root/tests/managers/thl/test_cashout_method.py
blob: fc364f263d92deb7f139e0d02ec7338fed15c6a4 (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
from __future__ import annotations

from collections.abc import Callable
from typing import TYPE_CHECKING

import pytest

from generalresearch.models.thl.wallet.cashout_method import (
    CashMailCashoutMethodData,
    PaypalCashoutMethodData,
    USDeliveryAddress,
)
from generalresearch.models.thl.wallet.definitions import PayoutType

if TYPE_CHECKING:
    from generalresearch.config import GRLBaseSettings
    from generalresearch.managers.thl.cashout_method import (
        CashoutMethodManager,
    )
    from generalresearch.models.thl.user import User
    from generalresearch.models.thl.wallet.cashout_method import (
        CashoutMethod,
    )


class TestTangoCashoutMethods:

    def test_create_and_get(
        self,
        cashout_method_manager: CashoutMethodManager,
        setup_cashoutmethod_db: Callable[..., None],
        example_tango_cashout_methods: list[CashoutMethod],
    ):
        setup_cashoutmethod_db()

        res = cashout_method_manager.filter(payout_types=[PayoutType.TANGO])
        assert len(res) == 2
        cm = next(x for x in res if x.ext_id == "U025035")
        assert example_tango_cashout_methods[0] == cm

    def test_user(
        self,
        cashout_method_manager: CashoutMethodManager,
        user_with_wallet: User,
        setup_cashoutmethod_db: Callable[..., None],
    ):
        setup_cashoutmethod_db()

        res = cashout_method_manager.get_cashout_methods(user_with_wallet)
        # This user ONLY has the two tango cashout methods, no AMT
        assert len(res) == 2



class TestUserCashoutMethods:

    def test(
        self,
        cashout_method_manager: CashoutMethodManager,
        user_with_wallet: User,
        delete_cashoutmethod_db: Callable[..., None],
    ):
        delete_cashoutmethod_db()

        res = cashout_method_manager.get_cashout_methods(user_with_wallet)
        assert len(res) == 0

    def test_cash_in_mail(
        self,
        cashout_method_manager: CashoutMethodManager,
        user_with_wallet: User,
        delete_cashoutmethod_db: Callable[..., None],
    ):
        delete_cashoutmethod_db()

        data = CashMailCashoutMethodData(
            delivery_address=USDeliveryAddress.model_validate(
                {
                    "name_or_attn": "Josh Ackerman",
                    "address": "123 Fake St",
                    "city": "San Francisco",
                    "state": "CA",
                    "postal_code": "12345",
                }
            )
        )
        cashout_method_manager.create_cash_in_mail_cashout_method(
            data=data, user=user_with_wallet
        )

        res = cashout_method_manager.get_cashout_methods(user_with_wallet)
        assert len(res) == 1
        assert res[0].data.delivery_address.postal_code == "12345"

        # try to create the same one again. should just do nothing
        cashout_method_manager.create_cash_in_mail_cashout_method(
            data=data, user=user_with_wallet
        )
        res = cashout_method_manager.get_cashout_methods(user_with_wallet)
        assert len(res) == 1

        # Create with a new address, will create a new one
        data.delivery_address.postal_code = "99999"
        cashout_method_manager.create_cash_in_mail_cashout_method(
            data=data, user=user_with_wallet
        )
        res = cashout_method_manager.get_cashout_methods(user_with_wallet)
        assert len(res) == 2

    def test_paypal(
        self,
        cashout_method_manager: CashoutMethodManager,
        user_with_wallet: User,
        delete_cashoutmethod_db: Callable[..., None],
    ):
        delete_cashoutmethod_db()

        data = PaypalCashoutMethodData(email="test@example.com")
        cashout_method_manager.create_paypal_cashout_method(
            data=data, user=user_with_wallet
        )
        res = cashout_method_manager.get_cashout_methods(user_with_wallet)
        assert len(res) == 1
        assert res[0].data.email == "test@example.com"

        # try to create the same one again. should just do nothing
        cashout_method_manager.create_paypal_cashout_method(
            data=data, user=user_with_wallet
        )
        res = cashout_method_manager.get_cashout_methods(user_with_wallet)
        assert len(res) == 1

        # Create with a new email, will error! must delete the old one first.
        # We can only have one paypal active
        data.email = "test2@example.com"
        with pytest.raises(
            ValueError,
            match="User already has a cashout method of this type. Delete the existing one and try again.",
        ):
            cashout_method_manager.create_paypal_cashout_method(
                data=data, user=user_with_wallet
            )
        res = cashout_method_manager.get_cashout_methods(user_with_wallet)
        assert len(res) == 1

        cashout_method_manager.delete_cashout_method(res[0].id)
        res = cashout_method_manager.get_cashout_methods(user_with_wallet)
        assert len(res) == 0

        cashout_method_manager.create_paypal_cashout_method(
            data=data, user=user_with_wallet
        )
        res = cashout_method_manager.get_cashout_methods(user_with_wallet)
        assert len(res) == 1
        assert res[0].data.email == "test2@example.com"