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
|
from __future__ import annotations
import random
from collections import defaultdict
from collections.abc import Callable
from datetime import datetime
from typing import TYPE_CHECKING
from unittest.mock import Mock
from uuid import uuid4
import pytest
from grip_client.mmdb.models import (
GRIPAnonymousRecord,
GRIPAsnRecord,
GRIPCountryRecord,
GRIPMMDBLookupResult,
)
from generalresearch.managers.thl.cashout_method import (
CashoutMethodManager,
)
from generalresearch.managers.thl.ipinfo import GeoIpInfoManager
from generalresearch.managers.thl.user_streak import (
UserStreakManager,
)
from generalresearch.managers.thl.userhealth import (
AuditLogManager,
UserIpHistoryManager,
)
from generalresearch.models.definitions import Source
from generalresearch.models.thl.wallet.cashout_method import (
CashoutMethod,
TangoCashoutMethodData,
)
from generalresearch.models.thl.wallet.definitions import Currency, PayoutType
if TYPE_CHECKING:
from generalresearch.managers.spectrum.survey import SpectrumSurveyManager
from generalresearch.managers.thl.buyer import BuyerManager
from generalresearch.models.thl.user import User
from generalresearch.pg_helper import PostgresConfig
from generalresearch.redis_helper import RedisConfig
from generalresearch.sql_helper import SqlHelper
# === THL ===
@pytest.fixture(scope="session")
def audit_log_manager(thl_web_rw: PostgresConfig) -> AuditLogManager:
assert thl_web_rw.dsn.path
assert "/unittest-" in thl_web_rw.dsn.path
from generalresearch.managers.thl.userhealth import AuditLogManager
return AuditLogManager(pg_config=thl_web_rw)
@pytest.fixture
def user_iphistory_manager(
thl_web_rw: PostgresConfig,
thl_redis_config: RedisConfig,
geoip_info_manager: GeoIpInfoManager,
) -> UserIpHistoryManager:
assert thl_web_rw.dsn.path
assert "/unittest-" in thl_web_rw.dsn.path
return UserIpHistoryManager(
pg_config=thl_web_rw,
redis_config=thl_redis_config,
geoip_info_manager=geoip_info_manager,
)
@pytest.fixture(scope="function")
def user_iphistory_manager_clear_cache(user_iphistory_manager, user: User):
# On successive py-test/jenkins runs, the cache may contain
# the previous run's info (keyed under the same user_id)
user_iphistory_manager.delete_user_ip_history_cache(user_id=user.user_id)
yield
user_iphistory_manager.delete_user_ip_history_cache(user_id=user.user_id)
@pytest.fixture
def grip_lookup_results() -> dict[str, GRIPMMDBLookupResult]:
return defaultdict(
lambda: GRIPMMDBLookupResult(
country=GRIPCountryRecord(),
anonymous=GRIPAnonymousRecord(),
asn=GRIPAsnRecord(),
),
{
"8.8.8.8": GRIPMMDBLookupResult(
country=GRIPCountryRecord(country_iso="US"),
anonymous=GRIPAnonymousRecord(is_anonymous=False),
asn=GRIPAsnRecord(
asn=15169,
network_operator="Google",
),
),
"1.1.1.1": GRIPMMDBLookupResult(
country=GRIPCountryRecord(country_iso="AU"),
anonymous=GRIPAnonymousRecord(is_anonymous=True),
asn=GRIPAsnRecord(
asn=13335,
network_operator="Cloudflare",
),
),
"2.2.2.2": GRIPMMDBLookupResult(
country=GRIPCountryRecord(country_iso="IT"),
anonymous=GRIPAnonymousRecord(is_anonymous=False),
asn=GRIPAsnRecord(),
),
},
)
@pytest.fixture(scope="function")
def geoip_info_manager(
grip_lookup_results: dict[str, GRIPMMDBLookupResult],
) -> GeoIpInfoManager:
manager = GeoIpInfoManager(grip_token="test-token")
manager.grip_mmdb.lookup = Mock(side_effect=grip_lookup_results.__getitem__)
return manager
@pytest.fixture(scope="session")
def cashout_method_manager(thl_web_rw: PostgresConfig) -> CashoutMethodManager:
assert thl_web_rw.dsn.path
assert "/unittest-" in thl_web_rw.dsn.path
return CashoutMethodManager(pg_config=thl_web_rw)
@pytest.fixture(scope="session")
def event_manager(thl_redis_config: RedisConfig):
from generalresearch.managers.events import EventManager
return EventManager(redis_config=thl_redis_config)
@pytest.fixture(scope="session")
def user_streak_manager(thl_web_rw: PostgresConfig) -> UserStreakManager:
assert thl_web_rw.dsn.path
assert "/unittest-" in thl_web_rw.dsn.path
return UserStreakManager(pg_config=thl_web_rw)
@pytest.fixture(scope="session")
def uqa_db_index(thl_web_rw: PostgresConfig):
# There were some custom indices created not through django.
# Make sure the index used in the index hint exists
assert thl_web_rw.dsn.path
assert "/unittest-" in thl_web_rw.dsn.path
# query = f"""create index idx_user_id
# on `{thl_web_rw.db}`.marketplace_userquestionanswer (user_id);"""
# try:
# thl_web_rw.execute_sql_query(query, commit=True)
# except pymysql.OperationalError as e:
# if "Duplicate key name 'idx_user_id'" not in str(e):
# raise
@pytest.fixture(scope="session")
def delete_cashoutmethod_db(thl_web_rw: PostgresConfig) -> Callable[..., None]:
def _delete_cashoutmethod_db():
thl_web_rw.execute_write(
query="DELETE FROM accounting_cashoutmethod;",
)
return _delete_cashoutmethod_db
@pytest.fixture(scope="session")
def setup_cashoutmethod_db(
cashout_method_manager: CashoutMethodManager,
delete_cashoutmethod_db: Callable[..., None],
example_tango_cashout_methods: list[CashoutMethod],
) -> Callable[..., None]:
def _inner():
delete_cashoutmethod_db()
for x in example_tango_cashout_methods:
cashout_method_manager.create(x)
return _inner
@pytest.fixture(scope="session")
def random_ext_id_factory(base: str = "U02") -> Callable[..., str]:
def _inner() -> str:
suffix = random.randint(0, 99999)
return f"{base}{suffix:05d}"
return _inner
@pytest.fixture(scope="session")
def example_tango_cashout_methods(
) -> list[CashoutMethod]:
return [
CashoutMethod(
id=uuid4().hex,
last_updated=datetime.fromisoformat("2021-06-23T20:45:38.239182Z"),
is_live=True,
type=PayoutType.TANGO,
ext_id="U025035",
name="Safeway eGift Card $25",
data=TangoCashoutMethodData(
value_type="fixed", countries=["US"], utid="U025035"
),
user=None,
image_url="https://d30s7yzk2az89n.cloudfront.net/images/brands/b694446-1200w-326ppi.png",
original_currency=Currency.USD,
min_value=2500,
max_value=2500,
),
CashoutMethod(
id=uuid4().hex,
last_updated=datetime.fromisoformat("2021-06-23T20:45:38.239182Z"),
is_live=True,
type=PayoutType.TANGO,
ext_id="U006961",
name="Amazon.it Gift Certificate",
data=TangoCashoutMethodData(
value_type="variable", countries=["IT"], utid="U006961"
),
user=None,
image_url="https://d30s7yzk2az89n.cloudfront.net/images/brands/b405753-1200w-326ppi.png",
original_currency=Currency.EUR,
min_value=1,
max_value=10000,
),
]
# === THL: Marketplaces ===
@pytest.fixture(scope="session")
def spectrum_survey_manager(spectrum_rw: SqlHelper) -> SpectrumSurveyManager:
from generalresearch.managers.spectrum.survey import (
SpectrumSurveyManager,
)
return SpectrumSurveyManager(sql_helper=spectrum_rw)
@pytest.fixture(scope="session")
def delete_buyers_surveys(
thl_web_rw: PostgresConfig, buyer_manager: BuyerManager
) -> Callable[..., None]:
def _inner():
# assert "/unittest-" in thl_web_rw.dsn.path
thl_web_rw.execute_write(
"""
DELETE FROM marketplace_surveystat
WHERE survey_id IN (
SELECT id
FROM marketplace_survey
WHERE source = %(source)s
);""",
params={"source": Source.TESTING.value},
)
thl_web_rw.execute_write(
"""
DELETE FROM marketplace_survey
WHERE buyer_id IN (
SELECT id
FROM marketplace_buyer
WHERE source = %(source)s
);""",
params={"source": Source.TESTING.value},
)
thl_web_rw.execute_write(
"""
DELETE from marketplace_buyer
WHERE source=%(source)s;
""",
params={"source": Source.TESTING.value},
)
buyer_manager.populate_caches()
return _inner
|