aboutsummaryrefslogtreecommitdiff
path: root/test_utils/managers/thl/conftest.py
blob: 355a39ddc53ea6b64e7a318d6fa45a2e42aca8f2 (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
from __future__ import annotations

import subprocess
from collections.abc import Callable, Generator
from random import randint
from typing import TYPE_CHECKING

import pytest
import redis
from pydantic import PostgresDsn

from generalresearch.managers.base import Permission
from generalresearch.managers.thl.user_manager.mysql_user_manager import (
    MysqlUserManager,
)
from generalresearch.managers.thl.user_manager.redis_user_manager import (
    RedisUserManager,
)
from generalresearch.pg_helper import PostgresConfig
from generalresearch.redis_helper import RedisConfig

if TYPE_CHECKING:
    from generalresearch.config import GRLBaseSettings
    from generalresearch.managers.thl.buyer import BuyerManager
    from generalresearch.managers.thl.category import CategoryManager
    from generalresearch.managers.thl.ipinfo import (
        IPGeonameManager,
        IPInformationManager,
    )
    from generalresearch.managers.thl.payout import (
        BrokerageProductPayoutEventManager,
        BusinessPayoutEventManager,
        PayoutEventManager,
        UserPayoutEventManager,
    )
    from generalresearch.managers.thl.product import ProductManager
    from generalresearch.managers.thl.session import SessionManager
    from generalresearch.managers.thl.task_adjustment import (
        TaskAdjustmentManager,
    )
    from generalresearch.managers.thl.user_manager.user_manager import (
        UserManager,
    )
    from generalresearch.managers.thl.user_manager.user_metadata_manager import (
        UserMetadataManager,
    )
    from generalresearch.managers.thl.userhealth import (
        AuditLogManager,
        IPRecordManager,
    )
    from generalresearch.managers.thl.wall import (
        WallCacheManager,
        WallManager,
    )

# === Msc ===


@pytest.fixture(scope="session")
def thl_redis_config_db() -> str:
    return str(randint(99, 1_023))


@pytest.fixture(scope="session")
def thl_redis_config(
    settings: GRLBaseSettings, thl_redis_config_db: str
) -> Generator[RedisConfig]:
    assert "unittest" in str(settings.testing_redis) or "127.0.0.1" in str(
        settings.testing_redis
    )

    uri = f"redis://{settings.testing_redis}/{thl_redis_config_db}"

    res = subprocess.run(
        ["redis-cli", "-u", uri, "SET", "jenkins_lock", "1", "NX", "EX", "3600"],
        check=True,
        text=True,
        capture_output=True,
    )

    if res.stdout.strip() != "OK":
        raise ValueError("Redis already locked... aborting.")

    yield RedisConfig(
        dsn=uri,
        decode_responses=True,
        socket_timeout=settings.redis_timeout,
        socket_connect_timeout=settings.redis_timeout,
    )

    r = redis.from_url(uri)
    r.flushdb()


@pytest.fixture(scope="session")
def thl_redis_client(thl_redis_config):
    return thl_redis_config.create_redis_client()


@pytest.fixture(scope="session")
def thl_web_rr(django_db_factory: Callable[..., PostgresDsn]) -> PostgresConfig:
    _dsn = django_db_factory("generalresearch.thl_django")

    return PostgresConfig(
        dsn=_dsn,
        connect_timeout=1,
        statement_timeout=5,
    )


@pytest.fixture(scope="session")
def thl_web_rw(thl_web_rr: PostgresConfig) -> PostgresConfig:
    return thl_web_rr


# === Managers ===


@pytest.fixture(scope="session")
def payout_event_manager(
    thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig
) -> PayoutEventManager:
    assert thl_web_rw.dsn.path
    assert "/unittest-" in thl_web_rw.dsn.path

    from generalresearch.managers.thl.payout import PayoutEventManager

    return PayoutEventManager(
        pg_config=thl_web_rw,
        permissions=[Permission.CREATE, Permission.READ],
        redis_config=thl_redis_config,
    )


@pytest.fixture(scope="session")
def user_payout_event_manager(
    thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig
) -> UserPayoutEventManager:
    assert thl_web_rw.dsn.path
    assert "/unittest-" in thl_web_rw.dsn.path

    from generalresearch.managers.thl.payout import UserPayoutEventManager

    return UserPayoutEventManager(
        pg_config=thl_web_rw,
        permissions=[Permission.CREATE, Permission.READ],
        redis_config=thl_redis_config,
    )


@pytest.fixture(scope="session")
def brokerage_product_payout_event_manager(
    thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig
) -> BrokerageProductPayoutEventManager:
    assert thl_web_rw.dsn.path
    assert "/unittest-" in thl_web_rw.dsn.path

    from generalresearch.managers.thl.payout import (
        BrokerageProductPayoutEventManager,
    )

    return BrokerageProductPayoutEventManager(
        pg_config=thl_web_rw,
        permissions=[Permission.CREATE, Permission.READ],
        redis_config=thl_redis_config,
    )


@pytest.fixture()
def audit_log_manager(thl_web_rw: PostgresConfig) -> AuditLogManager:
    from generalresearch.managers.thl.userhealth import AuditLogManager

    return AuditLogManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def business_payout_event_manager(
    thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig
) -> BusinessPayoutEventManager:
    assert thl_web_rw.dsn.path
    assert "/unittest-" in thl_web_rw.dsn.path

    from generalresearch.managers.thl.payout import (
        BusinessPayoutEventManager,
    )

    return BusinessPayoutEventManager(
        pg_config=thl_web_rw,
        permissions=[Permission.CREATE, Permission.READ],
        redis_config=thl_redis_config,
    )


@pytest.fixture(scope="session")
def product_manager(thl_web_rw: PostgresConfig) -> ProductManager:
    assert thl_web_rw.dsn
    assert thl_web_rw.dsn.path
    assert "/unittest-" in thl_web_rw.dsn.path

    from generalresearch.managers.thl.product import ProductManager

    return ProductManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def user_manager(
    settings: GRLBaseSettings,
    thl_web_rw: PostgresConfig,
    thl_web_rr: PostgresConfig,
    thl_redis_config: RedisConfig,
) -> UserManager:
    assert thl_web_rw.dsn
    assert thl_web_rw.dsn.path
    assert thl_web_rr.dsn
    assert thl_web_rr.dsn.path
    assert "/unittest-" in thl_web_rw.dsn.path
    assert "/unittest-" in thl_web_rr.dsn.path

    from generalresearch.managers.thl.user_manager.rate_limit import UserManagerLimiter
    from generalresearch.managers.thl.user_manager.user_manager import (
        UserManager,
    )

    um = UserManager(
        pg_config=thl_web_rw,
        pg_config_rr=thl_web_rr,
        redis=settings.redis,
    )

    # rc = thl_redis_config.create_redis_client()
    um.user_manager_limiter = UserManagerLimiter(redis=thl_redis_config.dsn)

    return um


@pytest.fixture(scope="session")
def mysql_user_manager(thl_web_rw: PostgresConfig) -> MysqlUserManager:
    return MysqlUserManager(pg_config=thl_web_rw, is_read_replica=False)


@pytest.fixture(scope="session")
def redis_user_manager(thl_redis_config: RedisConfig) -> RedisUserManager:
    return RedisUserManager(redis_dsn=thl_redis_config.dsn)


@pytest.fixture(scope="session")
def user_metadata_manager(thl_web_rw: PostgresConfig) -> UserMetadataManager:
    assert thl_web_rw.dsn
    assert thl_web_rw.dsn.path
    assert "/unittest-" in thl_web_rw.dsn.path

    from generalresearch.managers.thl.user_manager.user_metadata_manager import (
        UserMetadataManager,
    )

    return UserMetadataManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def session_manager(thl_web_rw: PostgresConfig) -> SessionManager:
    assert thl_web_rw.dsn
    assert thl_web_rw.dsn.path
    assert "/unittest-" in thl_web_rw.dsn.path

    from generalresearch.managers.thl.session import SessionManager

    return SessionManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def wall_manager(thl_web_rw: PostgresConfig) -> WallManager:
    assert thl_web_rw.dsn
    assert thl_web_rw.dsn.path
    assert "/unittest-" in thl_web_rw.dsn.path

    from generalresearch.managers.thl.wall import WallManager

    return WallManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def wall_cache_manager(
    thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig
) -> WallCacheManager:
    # assert "/unittest-" in thl_web_rw.dsn.path

    from generalresearch.managers.thl.wall import WallCacheManager

    return WallCacheManager(pg_config=thl_web_rw, redis_config=thl_redis_config)


@pytest.fixture(scope="session")
def task_adjustment_manager(thl_web_rw: PostgresConfig) -> TaskAdjustmentManager:
    # assert "/unittest-" in thl_web_rw.dsn.path

    from generalresearch.managers.thl.task_adjustment import (
        TaskAdjustmentManager,
    )

    return TaskAdjustmentManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def category_manager(thl_web_rw: PostgresConfig) -> CategoryManager:
    assert thl_web_rw.dsn
    assert thl_web_rw.dsn.path
    assert "/unittest-" in thl_web_rw.dsn.path
    from generalresearch.managers.thl.category import CategoryManager

    return CategoryManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def buyer_manager(thl_web_rw: PostgresConfig) -> BuyerManager:
    # assert "/unittest-" in thl_web_rw.dsn.path
    from generalresearch.managers.thl.buyer import BuyerManager

    return BuyerManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def survey_manager(thl_web_rw: PostgresConfig):
    # assert "/unittest-" in thl_web_rw.dsn.path
    from generalresearch.managers.thl.survey import SurveyManager

    return SurveyManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def surveystat_manager(thl_web_rw: PostgresConfig):
    # assert "/unittest-" in thl_web_rw.dsn.path
    from generalresearch.managers.thl.survey import SurveyStatManager

    return SurveyStatManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def surveypenalty_manager(thl_redis_config: RedisConfig):
    from generalresearch.managers.thl.survey_penalty import SurveyPenaltyManager

    return SurveyPenaltyManager(redis_config=thl_redis_config)


# --- IP Geolocation ---


@pytest.fixture
def ip_geoname_manager(thl_web_rw: PostgresConfig) -> IPGeonameManager:
    from generalresearch.managers.thl.ipinfo import IPGeonameManager

    return IPGeonameManager(pg_config=thl_web_rw)


# --- IP Information ---


@pytest.fixture(scope="session")
def ip_information_manager(thl_web_rw: PostgresConfig) -> IPInformationManager:
    assert thl_web_rw.dsn.path
    assert "/unittest-" in thl_web_rw.dsn.path

    from generalresearch.managers.thl.ipinfo import IPInformationManager

    return IPInformationManager(pg_config=thl_web_rw)


# --- IP Record ---


@pytest.fixture(scope="session")
def ip_record_manager(
    thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig
) -> IPRecordManager:
    assert thl_web_rw.dsn.path
    assert "/unittest-" in thl_web_rw.dsn.path

    from generalresearch.managers.thl.userhealth import IPRecordManager

    return IPRecordManager(pg_config=thl_web_rw, redis_config=thl_redis_config)