aboutsummaryrefslogtreecommitdiff
path: root/test_utils
diff options
context:
space:
mode:
Diffstat (limited to 'test_utils')
-rw-r--r--test_utils/managers/conftest.py112
-rw-r--r--test_utils/managers/thl/conftest.py42
-rw-r--r--test_utils/models/ledger/conftest.py16
-rw-r--r--test_utils/models/thl/conftest.py181
-rw-r--r--test_utils/models/upk/conftest.py7
5 files changed, 113 insertions, 245 deletions
diff --git a/test_utils/managers/conftest.py b/test_utils/managers/conftest.py
index 391e6bf..a9f92a2 100644
--- a/test_utils/managers/conftest.py
+++ b/test_utils/managers/conftest.py
@@ -1,19 +1,32 @@
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,
@@ -24,15 +37,6 @@ 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.managers.thl.ipinfo import (
- GeoIpInfoManager,
- IPGeonameManager,
- )
- from generalresearch.managers.thl.userhealth import (
- AuditLogManager,
- IPRecordManager,
- UserIpHistoryManager,
- )
from generalresearch.models.thl.user import User
from generalresearch.pg_helper import PostgresConfig
from generalresearch.redis_helper import RedisConfig
@@ -51,41 +55,21 @@ def audit_log_manager(thl_web_rw: PostgresConfig) -> AuditLogManager:
return AuditLogManager(pg_config=thl_web_rw)
-@pytest.fixture(scope="session")
-def ip_geoname_manager(thl_web_rw: PostgresConfig) -> IPGeonameManager:
- assert thl_web_rw.dsn.path
- assert "/unittest-" in thl_web_rw.dsn.path
-
- from generalresearch.managers.thl.ipinfo import IPGeonameManager
-
- return IPGeonameManager(pg_config=thl_web_rw)
-
-
-@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)
-
-
-@pytest.fixture(scope="session")
+@pytest.fixture
def user_iphistory_manager(
- thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig
+ 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
- from generalresearch.managers.thl.userhealth import (
- UserIpHistoryManager,
+ return UserIpHistoryManager(
+ pg_config=thl_web_rw,
+ redis_config=thl_redis_config,
+ geoip_info_manager=geoip_info_manager,
)
- return UserIpHistoryManager(pg_config=thl_web_rw, redis_config=thl_redis_config)
-
@pytest.fixture(scope="function")
def user_iphistory_manager_clear_cache(user_iphistory_manager, user: User):
@@ -96,16 +80,47 @@ def user_iphistory_manager_clear_cache(user_iphistory_manager, user: User):
user_iphistory_manager.delete_user_ip_history_cache(user_id=user.user_id)
-@pytest.fixture(scope="session")
-def geoipinfo_manager(
- thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig
-) -> GeoIpInfoManager:
- assert thl_web_rw.dsn.path
- assert "/unittest-" in thl_web_rw.dsn.path
+@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(),
+ ),
+ },
+ )
- from generalresearch.managers.thl.ipinfo import GeoIpInfoManager
- return GeoIpInfoManager(pg_config=thl_web_rw, redis_config=thl_redis_config)
+@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")
@@ -185,7 +200,6 @@ def random_ext_id_factory(base: str = "U02") -> Callable[..., str]:
@pytest.fixture(scope="session")
def example_tango_cashout_methods(
- random_ext_id_factory: Callable[..., str],
) -> list[CashoutMethod]:
return [
CashoutMethod(
@@ -193,10 +207,10 @@ def example_tango_cashout_methods(
last_updated=datetime.fromisoformat("2021-06-23T20:45:38.239182Z"),
is_live=True,
type=PayoutType.TANGO,
- ext_id='U025035',
+ ext_id="U025035",
name="Safeway eGift Card $25",
data=TangoCashoutMethodData(
- value_type="fixed", countries=["US"], utid='U025035'
+ value_type="fixed", countries=["US"], utid="U025035"
),
user=None,
image_url="https://d30s7yzk2az89n.cloudfront.net/images/brands/b694446-1200w-326ppi.png",
@@ -209,7 +223,7 @@ def example_tango_cashout_methods(
last_updated=datetime.fromisoformat("2021-06-23T20:45:38.239182Z"),
is_live=True,
type=PayoutType.TANGO,
- ext_id='U006961',
+ ext_id="U006961",
name="Amazon.it Gift Certificate",
data=TangoCashoutMethodData(
value_type="variable", countries=["IT"], utid="U006961"
diff --git a/test_utils/managers/thl/conftest.py b/test_utils/managers/thl/conftest.py
index 3392559..2dce648 100644
--- a/test_utils/managers/thl/conftest.py
+++ b/test_utils/managers/thl/conftest.py
@@ -23,15 +23,10 @@ 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
@@ -45,13 +40,13 @@ if TYPE_CHECKING:
UserMetadataManager,
)
from generalresearch.managers.thl.userhealth import (
- AuditLogManager,
IPRecordManager,
)
from generalresearch.managers.thl.wall import (
WallCacheManager,
WallManager,
)
+ from generalresearch.managers.thl.wallet.user_payout import UserPayoutEventManager
# === Msc ===
@@ -139,7 +134,7 @@ def user_payout_event_manager(
assert thl_web_rw.dsn.path
assert "/unittest-" in thl_web_rw.dsn.path
- from generalresearch.managers.thl.payout import UserPayoutEventManager
+ from generalresearch.managers.thl.wallet.user_payout import UserPayoutEventManager
return UserPayoutEventManager(
pg_config=thl_web_rw,
@@ -166,13 +161,6 @@ def brokerage_product_payout_event_manager(
)
-@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
@@ -342,32 +330,6 @@ def surveypenalty_manager(thl_redis_config: RedisConfig):
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
diff --git a/test_utils/models/ledger/conftest.py b/test_utils/models/ledger/conftest.py
index 9ee0df2..e9ddbc9 100644
--- a/test_utils/models/ledger/conftest.py
+++ b/test_utils/models/ledger/conftest.py
@@ -39,7 +39,6 @@ if TYPE_CHECKING:
ThlLedgerManager,
)
from generalresearch.managers.thl.payout import (
- BrokerageProductPayoutEventManager,
BusinessPayoutEventManager,
)
from generalresearch.managers.thl.session import SessionManager
@@ -202,7 +201,7 @@ def bp_payout_event(
@pytest.fixture
def bp_payout_event_factory(
- brokerage_product_payout_event_manager: BrokerageProductPayoutEventManager,
+ business_payout_event_manager: BusinessPayoutEventManager,
thl_ledger_manager: ThlLedgerManager,
) -> Callable[..., BrokerageProductPayoutEvent]:
@@ -210,14 +209,13 @@ def bp_payout_event_factory(
product: Product, usd_cent: USDCent, ext_ref_id: str | None = None
) -> BrokerageProductPayoutEvent:
- return brokerage_product_payout_event_manager.create_bp_payout_event(
+ bus_pe = business_payout_event_manager.create_bp_payout_event(
thl_ledger_manager=thl_ledger_manager,
product=product,
amount=usd_cent,
- ext_ref_id=ext_ref_id,
- skip_wallet_balance_check=True,
- skip_one_per_day_check=True,
+ ext_ref_id=ext_ref_id or uuid4().hex,
)
+ return bus_pe.bp_payouts[0]
return _inner
@@ -225,9 +223,9 @@ def bp_payout_event_factory(
@pytest.fixture
def currency(ledger_manager: LedgerManager) -> LedgerCurrency:
# return request.param if hasattr(request, "currency") else LedgerCurrency.TEST
- assert (
- ledger_manager.currency
- ), "LedgerManager must have a currency specified for these tests"
+ assert ledger_manager.currency, (
+ "LedgerManager must have a currency specified for these tests"
+ )
return ledger_manager.currency
diff --git a/test_utils/models/thl/conftest.py b/test_utils/models/thl/conftest.py
index d20fdc4..a8ad037 100644
--- a/test_utils/models/thl/conftest.py
+++ b/test_utils/models/thl/conftest.py
@@ -14,10 +14,9 @@ from grip_client.enums import AccessType
from pydantic import PositiveInt
from generalresearch.currency import USDCent
-from generalresearch.managers.thl.payout import (
- BusinessPayoutEventManager,
- UserPayoutEventManager,
-)
+from generalresearch.managers.thl.ipinfo import GeoIpInfoManager
+from generalresearch.managers.thl.payout import BusinessPayoutEventManager
+from generalresearch.managers.thl.wallet.user_payout import UserPayoutEventManager
from generalresearch.models.custom_types import (
AwareDatetimeISO,
IPvAnyAddressStr,
@@ -29,6 +28,7 @@ from generalresearch.models.thl.definitions import (
PayoutStatus,
Status,
)
+from generalresearch.models.thl.ipinfo import GeoIPInformation
from generalresearch.models.thl.payout import UserPayoutEvent
from generalresearch.models.thl.user import User
from generalresearch.models.thl.userhealth import AuditLogLevel
@@ -36,10 +36,6 @@ from generalresearch.models.thl.wallet.definitions import PayoutType
from generalresearch.pg_helper import PostgresConfig
if TYPE_CHECKING:
- from generalresearch.managers.thl.ipinfo import (
- IPGeonameManager,
- IPInformationManager,
- )
from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
from generalresearch.managers.thl.product import ProductManager
from generalresearch.managers.thl.session import SessionManager
@@ -50,7 +46,6 @@ if TYPE_CHECKING:
from generalresearch.models.gr.business import Business
from generalresearch.models.gr.team import Team
from generalresearch.models.legacy.bucket import Bucket
- from generalresearch.models.thl.ipinfo import IPGeoname, IPInformation
from generalresearch.models.thl.payout import BrokerageProductPayoutEvent
from generalresearch.models.thl.product import (
PayoutConfig,
@@ -438,143 +433,44 @@ def unsaved_product(product_factory: Callable[..., Product]) -> Product:
return product_factory(save=False)
-# --- IP Geoname ---
-
-
-@pytest.fixture
-def ip_geoname_factory(
- ip_geoname_manager: IPGeonameManager,
-) -> Callable[..., IPGeoname]:
-
- def _inner(
- save: bool = True,
- geoname_id: PositiveInt | None = None,
- continent_code: str | None = None,
- continent_name: str | None = None,
- country_iso: str | None = None,
- country_name: str | None = None,
- subdivision_1_iso: str | None = None,
- subdivision_1_name: str | None = None,
- subdivision_2_iso: str | None = None,
- subdivision_2_name: str | None = None,
- city_name: str | None = None,
- metro_code: int | None = None,
- time_zone: str | None = None,
- is_in_european_union: bool | None = None,
- ) -> IPGeoname:
- if save:
- return ip_geoname_manager.create(
- geoname_id=geoname_id or randint(1, 999_999_999),
- continent_code=continent_code or "na",
- continent_name=continent_name or "North America",
- country_iso=country_iso or "us",
- country_name=country_name or "United States",
- subdivision_1_iso=subdivision_1_iso or "fl",
- subdivision_1_name=subdivision_1_name or "Florida",
- subdivision_2_iso=subdivision_2_iso,
- subdivision_2_name=subdivision_2_name,
- city_name=city_name,
- metro_code=metro_code,
- time_zone=time_zone,
- is_in_european_union=is_in_european_union,
- )
- else:
- raise ValueError("Unsaved IPGeoname not yet supported")
-
- return _inner
-
-
-@pytest.fixture()
-def ip_geoname(ip_geoname_factory: Callable[..., IPGeoname]) -> IPGeoname:
- return ip_geoname_factory(save=True)
-
-
-@pytest.fixture()
-def unsaved_ip_geoname(ip_geoname_factory: Callable[..., IPGeoname]) -> IPGeoname:
- return ip_geoname_factory(save=True)
-
-
-# --- IP Information ---
+# --- GeoIP Information ---
@pytest.fixture
-def ip_information_factory(
- ip_information_manager: IPInformationManager,
-) -> Callable[..., IPInformation]:
+def geoip_information_factory(
+ geoip_info_manager: GeoIpInfoManager,
+) -> Callable[..., GeoIPInformation]:
def _inner(
- save: bool = True,
ip: IPvAnyAddressStr | None = None,
- geoname_id: PositiveInt | None = None,
country_iso: str | None = None,
- registered_country_iso: str | None = None,
+ country_name: str | None = None,
is_anonymous: bool | None = None,
- is_anonymous_vpn: bool | None = None,
- is_hosting_provider: bool | None = None,
- is_public_proxy: bool | None = None,
- is_tor_exit_node: bool | None = None,
- is_residential_proxy: bool | None = None,
autonomous_system_number: PositiveInt | None = None,
autonomous_system_organization: str | None = None,
- domain: str | None = None,
- isp: str | None = None,
- mobile_country_code: str | None = None,
- mobile_network_code: str | None = None,
- network: str | None = None,
- organization: str | None = None,
- static_ip_score: float | None = None,
- user_type: AccessType | None = None,
- postal_code: str | None = None,
- latitude: Decimal | None = None,
- longitude: Decimal | None = None,
- accuracy_radius: int | None = None,
- ) -> IPInformation:
-
- if save:
- return ip_information_manager.create(
- ip=ip or fake.ipv4_public(),
- geoname_id=geoname_id,
- country_iso=country_iso or fake.country_code(),
- registered_country_iso=registered_country_iso,
- is_anonymous=is_anonymous,
- is_anonymous_vpn=is_anonymous_vpn,
- is_hosting_provider=is_hosting_provider,
- is_public_proxy=is_public_proxy,
- is_tor_exit_node=is_tor_exit_node,
- is_residential_proxy=is_residential_proxy,
- autonomous_system_number=autonomous_system_number,
- autonomous_system_organization=autonomous_system_organization,
- domain=domain,
- isp=isp,
- mobile_country_code=mobile_country_code,
- mobile_network_code=mobile_network_code,
- network=network,
- organization=organization,
- static_ip_score=static_ip_score,
- user_type=user_type,
- postal_code=postal_code,
- latitude=latitude,
- longitude=longitude,
- accuracy_radius=accuracy_radius,
- )
- else:
- raise ValueError("Unsaved IP Information not supported yet")
+ access_type: AccessType | None = None,
+ ) -> GeoIPInformation:
+
+ return GeoIPInformation(
+ country_iso=country_iso or fake.country_code("alpha-2"),
+ access_type=access_type,
+ is_anonymous=is_anonymous,
+ autonomous_system_number=autonomous_system_number,
+ autonomous_system_organization=autonomous_system_organization,
+ country_name=country_name,
+ ip=ip or fake.ipv4_public(),
+ subdivision_1_iso=None,
+ subdivision_1_name=None,
+ )
return _inner
@pytest.fixture
-def ip_information(
- ip_information_factory: Callable[..., IPInformation],
-) -> IPInformation:
- return ip_information_factory(save=True)
-
-
-@pytest.fixture()
-def unsaved_ip_information(
- ip_information_factory: Callable[..., IPInformation],
-) -> IPInformation:
- return ip_information_factory(save=False)
+def geoip_information(
+ geoip_information_factory: Callable[..., GeoIPInformation],
+) -> GeoIPInformation:
+ return geoip_information_factory()
# --- IP Record ---
@@ -584,7 +480,7 @@ def unsaved_ip_information(
def ip_record_factory(ip_record_manager: IPRecordManager) -> Callable[..., IPRecord]:
def _inner(
- user_id: PositiveInt,
+ user: User,
save: bool = True,
ip: IPvAnyAddressStr | None = None,
forwarded_ip1: IPvAnyAddressStr | None = None,
@@ -597,15 +493,13 @@ def ip_record_factory(ip_record_manager: IPRecordManager) -> Callable[..., IPRec
if save:
return ip_record_manager.create(
- user_id=user_id,
+ user_id=user.to_user_ref().user_id,
ip=ip or fake.ipv4_public(),
- forwarded_ip1=(forwarded_ip1 or fake.ipv4_public()),
- forwarded_ip2=(
- forwarded_ip2 or fake.ipv6() if random() < 0.5 else None
- ),
- forwarded_ip3=(
- forwarded_ip3 or fake.ipv4_public() if random() < 0.25 else None
- ),
+ forwarded_ip1=forwarded_ip1 or fake.ipv4_public(),
+ forwarded_ip2=forwarded_ip2 or fake.ipv6() if random() < 0.5 else None,
+ forwarded_ip3=forwarded_ip3 or fake.ipv4_public()
+ if random() < 0.25
+ else None,
forwarded_ip4=forwarded_ip4,
forwarded_ip5=forwarded_ip5,
forwarded_ip6=forwarded_ip6,
@@ -734,13 +628,9 @@ def user_payout_event_factory(
debit_account_uuid = debit_account_uuid or uuid4().hex
cashout_method_uuid = cashout_method_uuid or uuid4().hex
- # account_reference_type = account_reference_type or f"acct-ref-{uuid4().hex}"
- # account_reference_uuid = account_reference_uuid or uuid4().hex
- # cashout_method_uuid = cashout_method_uuid or uuid4().hex
amount = amount or randint(a=99, b=9_999)
status = status or rand_choice(list(PayoutStatus))
- description = description or f"desc-{uuid4().hex[:12]}"
# ext_ref_id = ext_ref_id or f"ext-ref-{uuid4().hex[:8]}"
payout_type = payout_type or rand_choice(list(PayoutType))
request_data = request_data or {}
@@ -749,10 +639,7 @@ def user_payout_event_factory(
return user_payout_event_manager.create(
uuid=uuid,
debit_account_uuid=debit_account_uuid,
- account_reference_type=account_reference_type,
- account_reference_uuid=account_reference_uuid,
cashout_method_uuid=cashout_method_uuid,
- description=description,
created=created,
amount=amount,
status=status,
diff --git a/test_utils/models/upk/conftest.py b/test_utils/models/upk/conftest.py
index ad96bbb..520d566 100644
--- a/test_utils/models/upk/conftest.py
+++ b/test_utils/models/upk/conftest.py
@@ -33,6 +33,13 @@ def insert_data_from_csv(
df["id"] = df["id"].map(lambda x: UUID(x).hex)
args = df.to_dict("tight")["data"]
+ # Fix issue with nullable cols
+ if table_name == 'marketplace_question':
+ for arg in args:
+ for idx, col in enumerate(arg):
+ if pd.isnull(col):
+ arg[idx] = None
+
with thl_web_rw.make_connection() as conn:
with conn.cursor() as c:
if disable_fk_checks: