From 671a7b1b02503cfa5c9aecd40c27a0c2729a285b Mon Sep 17 00:00:00 2001 From: stuppie Date: Wed, 4 Nov 2026 12:11:03 -0600 Subject: strip out AMT --- tests/__init__.py | 7 - tests/conftest.py | 319 +---------------------------------- tests/fixtures/amt.py | 264 ----------------------------- tests/fixtures/flow.py | 308 ---------------------------------- tests/fixtures/http.py | 50 +----- tests/fixtures/managers.py | 88 ---------- tests/fixtures/models.py | 296 -------------------------------- tests/flow/__init__.py | 16 -- tests/flow/test_tasks.py | 352 --------------------------------------- tests/http/test_auth.py | 14 +- tests/http/test_magic.py | 7 +- tests/http/test_notifications.py | 115 ------------- tests/http/test_preview.py | 71 -------- tests/http/test_wallet.py | 96 +++++++++++ tests/http/test_work.py | 122 -------------- tests/managers/__init__.py | 0 tests/managers/test_amt.py | 132 --------------- tests/managers/test_hit.py | 50 ------ tests/models/__init__.py | 0 tests/models/test_assignment.py | 42 ----- tests/models/test_event.py | 12 -- tests/models/test_hit.py | 11 -- tests/test_postgres.py | 69 -------- 23 files changed, 119 insertions(+), 2322 deletions(-) delete mode 100644 tests/fixtures/amt.py delete mode 100644 tests/fixtures/flow.py delete mode 100644 tests/fixtures/managers.py delete mode 100644 tests/fixtures/models.py delete mode 100644 tests/flow/__init__.py delete mode 100644 tests/flow/test_tasks.py delete mode 100644 tests/http/test_notifications.py delete mode 100644 tests/http/test_preview.py create mode 100644 tests/http/test_wallet.py delete mode 100644 tests/http/test_work.py delete mode 100644 tests/managers/__init__.py delete mode 100644 tests/managers/test_amt.py delete mode 100644 tests/managers/test_hit.py delete mode 100644 tests/models/__init__.py delete mode 100644 tests/models/test_assignment.py delete mode 100644 tests/models/test_event.py delete mode 100644 tests/models/test_hit.py delete mode 100644 tests/test_postgres.py (limited to 'tests') diff --git a/tests/__init__.py b/tests/__init__.py index 0166072..e69de29 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,7 +0,0 @@ -from random import choices as rand_choices -from string import ascii_uppercase, digits - - -def generate_amt_id(length: int = 30) -> str: - chars = ascii_uppercase + digits - return "".join(rand_choices(chars, k=length)) diff --git a/tests/conftest.py b/tests/conftest.py index 7a74fa0..ec5073c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,91 +1,23 @@ from __future__ import annotations -import os import subprocess -import sys -from collections.abc import Callable, Generator -from datetime import UTC, datetime -from pathlib import Path +from collections.abc import Generator from random import randint -from typing import TYPE_CHECKING, Any -from uuid import uuid4 +from typing import TYPE_CHECKING import pytest import redis -from fastapi.testclient import TestClient -from generalresearch.models.custom_types import InternalHostname, PostgresDict -from generalresearch.pg_helper import PostgresConfig, PostgresDsn from generalresearch.redis_helper import RedisConfig -from mypy_boto3_mturk import MTurkClient -from pydantic import TypeAdapter -from pytest import TempPathFactory - -from jb.decorators import CLIENT_CONFIG, get_redis_config -from jb.main import app -from tests import generate_amt_id if TYPE_CHECKING: from jb.settings import Settings pytest_plugins = [ - "tests.fixtures.amt", - "tests.fixtures.flow", "tests.fixtures.http", - "tests.fixtures.managers", - "tests.fixtures.models", ] -# --- IDs and Identifiers --- - - -@pytest.fixture -def amt_hit_id() -> str: - return generate_amt_id() - - -@pytest.fixture -def amt_hit_type_id() -> str: - return generate_amt_id() - - -@pytest.fixture -def amt_assignment_id() -> str: - return generate_amt_id() - - -@pytest.fixture -def amt_worker_id() -> str: - return generate_amt_id(length=21) - - -@pytest.fixture -def amt_group_id() -> str: - return generate_amt_id() - - -@pytest.fixture -def tsid() -> str: - return uuid4().hex - - -@pytest.fixture -def tsid1() -> str: - return uuid4().hex - - -@pytest.fixture -def tsid2() -> str: - return uuid4().hex - - -@pytest.fixture -def pe_id() -> str: - # payout event / cashout request UUID - return uuid4().hex - - # --- Settings --- @@ -96,233 +28,6 @@ def settings() -> Settings: return JBSettings() -# --- Database Connectors --- - - -@pytest.fixture(scope="session") -def postgres_instance(settings: Settings) -> Generator[PostgresDsn]: - """Create a ephemeral postgresql instance for us to use during pytest. - - This does not create any tables, or schema definitions within the instance. - What this does is simply: - - 1. Create a database on a known, consistent, staging or unittest - defined Postgres server. - - 2. Return the PostgresDsn of that table - - 3. On shutdown, go ahead and delete that database after the - tests have finished. - """ - - msg = "Must define Postgres test settings" - assert settings.testing_postgres, msg - assert settings.testing_postgres_user, msg - assert settings.testing_postgres_pass, msg - - db_uri, db_user, db_pass = ( - settings.testing_postgres, - settings.testing_postgres_user, - settings.testing_postgres_pass, - ) - - # Connect to default DB to create the new one - from psycopg import connect - from psycopg.sql import SQL, Identifier - - now = datetime.now(UTC) - ts: str = now.strftime("%Y-%m-%d") - db_name = f"unittest-{ts}-{uuid4().hex[:6]}" - - db_path_connect = f"postgres://{db_user}:{db_pass}@{db_uri}" - db_path = f"{db_path_connect}/{db_name}" - - # The DATABASE does NOT yet exist on the Postgres SERVER, thus - # we first must connect only to the SERVER (eg: default postgres path used) - conn = connect(f"{db_path_connect}/postgres") - conn.autocommit = True - cur = conn.cursor() - cur.execute(SQL("CREATE DATABASE {}").format(Identifier(db_name))) - cur.close() - conn.close() - - yield PostgresDsn(db_path) - - # Teardown: drop the DB after the session - conn = connect(f"{db_path_connect}/postgres") - conn.autocommit = True - cur = conn.cursor() - cur.execute(SQL("DROP DATABASE {} WITH (FORCE)").format(Identifier(db_name))) - cur.close() - conn.close() - - -@pytest.fixture(scope="session") -def django_settings_file( - postgres_instance_dict: PostgresDict, - tmp_path_factory: TempPathFactory, -) -> Callable[..., tuple[str, Path]]: - - def _inner(extra_installed_apps: list[str] | None = None) -> tuple[str, Path]: - installed_apps = [ - "django.contrib.postgres", - "django.contrib.contenttypes", - ] + (extra_installed_apps or []) - - settings_dir = tmp_path_factory.mktemp("django-settings") - settings_module = "test_settings" - - settings_content = f"""DATABASES = {{ - "default": {{ - "ENGINE": "django.db.backends.postgresql", - "NAME": {postgres_instance_dict["name"]!r}, - "USER": {postgres_instance_dict["username"]!r}, - "PASSWORD": {postgres_instance_dict["password"]!r}, - "HOST": {postgres_instance_dict["host"]!r}, - "PORT": {postgres_instance_dict["port"]!r}, - }} -}} -INSTALLED_APPS = {installed_apps!r} -DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" -LANGUAGE_CODE = "en-us" -TIME_ZONE = "UTC" -USE_I18N = True -USE_L10N = True -USE_TZ = True -""" - settings_file_path = settings_dir / f"{settings_module}.py" - settings_file_path.write_text(settings_content, encoding="utf-8") - - return settings_module, settings_dir - - return _inner - - -@pytest.fixture(scope="session") -def postgres_instance_dict( - postgres_instance: PostgresDsn, -) -> Generator[PostgresDict]: - host = postgres_instance.hosts()[0] - assert host is not None - - msg = "Must have full Postgres details" - assert host["host"], msg - assert host["username"], msg - assert host["password"], msg - - assert postgres_instance.path - - yield PostgresDict( - username=host["username"], - password=host["password"], - host=host["host"], - name=postgres_instance.path.lstrip("/"), - port=5432, - ) - - -@pytest.fixture(scope="session") -def postgres_instance_host( - postgres_instance_dict: PostgresDict, -) -> Generator[InternalHostname]: - adapter = TypeAdapter(InternalHostname) - value = adapter.validate_python(postgres_instance_dict["host"]) - yield value - - -@pytest.fixture(scope="session") -def django_db_factory( - postgres_instance: PostgresDsn, - django_settings_file: Callable[..., tuple[str, Path]], - postgres_instance_dict: PostgresDict, - tmp_path_factory: TempPathFactory, -) -> Callable[..., PostgresDsn | None]: - - _ran = {} - - def _inner( - django_project: str = "carer.mtwerk", - ) -> PostgresDsn | None: - - if _ran.get(django_project, False): - print(f"Already ran django_db_factory:{django_project}") - return postgres_instance - _ran[django_project] = True - - _cwd = None - _manage_path = "generalresearch.thl_django.app.manage" - _settings_module, _settings_dir = django_settings_file( - extra_installed_apps=[ - "carer.mtwerk", - ], - ) - - pythonpath = str(_settings_dir) - if existing_pythonpath := os.environ.get("PYTHONPATH"): - pythonpath += os.pathsep + existing_pythonpath - - env = { - **os.environ, - "DJANGO_SETTINGS_MODULE": _settings_module, - "PYTHONPATH": pythonpath, - } - - # we check right after. if we check now, we won't print if bad - res1 = subprocess.run( # noqa: PLW1510 - [ - sys.executable, - "-m", - _manage_path, - "makemigrations", - f"--settings={_settings_module}", - ], - cwd=str(_cwd) if _cwd is not None else None, - env=env, - capture_output=True, - text=True, - ) - - if res1.returncode != 0: - print("STDOUT:", res1.stdout) - print("STDERR:", res1.stderr) - res1.check_returncode() - - res2 = subprocess.run( # noqa: PLW1510 - [ - sys.executable, - "-m", - _manage_path, - "migrate", - f"--settings={_settings_module}", - ], - env=env, - cwd=str(_cwd) if _cwd is not None else None, - capture_output=True, - text=True, - ) - - if res2.returncode != 0: - print("STDOUT:", res2.stdout) - print("STDERR:", res2.stderr) - res2.check_returncode() - - # 3. Return the Dsn so the factory gives a way to connect - return postgres_instance - - return _inner - - -@pytest.fixture(scope="session") -def pg_config(django_db_factory: Callable[..., PostgresDsn]) -> PostgresConfig: - _dsn = django_db_factory() - - return PostgresConfig( - dsn=_dsn, - connect_timeout=1, - statement_timeout=5, - ) - - # --- Redis --- @@ -360,23 +65,3 @@ def redis_config(settings: Settings, redis_config_db: str) -> Generator[RedisCon r = redis.from_url(uri) r.flushdb() - - -# --- Connectors --- -@pytest.fixture(scope="session") -def amt_client(settings: Settings) -> MTurkClient: - import boto3 - - client = boto3.client( - service_name="mturk", - region_name="us-east-1", - endpoint_url=str(settings.amt_endpoint), - aws_access_key_id=settings.amt_access_id, - aws_secret_access_key=settings.amt_secret_key, - config=CLIENT_CONFIG, - ) - - # Confirm we're only using the Sandbox for any unittests - assert "mturk-requester-sandbox" in client.meta.endpoint_url - - return client diff --git a/tests/fixtures/amt.py b/tests/fixtures/amt.py deleted file mode 100644 index d7cc03d..0000000 --- a/tests/fixtures/amt.py +++ /dev/null @@ -1,264 +0,0 @@ -import copy -from collections.abc import Callable -from datetime import datetime, timedelta -from uuid import uuid4 - -import pytest -from dateutil.tz import tzlocal -from mypy_boto3_mturk.type_defs import ( - CreateHITTypeResponseTypeDef, - CreateHITWithHITTypeResponseTypeDef, - GetAssignmentResponseTypeDef, - GetHITResponseTypeDef, - ResponseMetadataTypeDef, -) - -from jb.managers.amt import APPROVAL_MESSAGE, NO_WORK_APPROVAL_MESSAGE -from tests import generate_amt_id - -# --- Generic Boto --- - - -@pytest.fixture -def amt_response_metadata() -> ResponseMetadataTypeDef: - req_id = str(uuid4()) - return { - "RequestId": req_id, - "HTTPStatusCode": 200, - "HTTPHeaders": { - "x-amzn-requestid": req_id, - "content-type": "application/x-amz-json-1.1", - "content-length": "46", - "date": "Wed, 15 Oct 2025 02:16:16 GMT", - }, - "RetryAttempts": 0, - } - - -# --- Assignment --- - - -@pytest.fixture -def assignment_response( - amt_hit_id: str, - amt_assignment_id: str, - amt_worker_id: str, - hit_response: GetHITResponseTypeDef, - amt_response_metadata: ResponseMetadataTypeDef, - tsid: str, -) -> GetAssignmentResponseTypeDef: - - hit_response = hit_response["HIT"] - local_now = datetime.now(tz=tzlocal()) - - return { - "Assignment": { - "AssignmentId": amt_assignment_id, - "WorkerId": amt_worker_id, - "HITId": amt_hit_id, - "AssignmentStatus": "Submitted", - "AutoApprovalTime": local_now + timedelta(days=7), - "AcceptTime": local_now - timedelta(minutes=10), - "SubmitTime": local_now, - "Deadline": local_now + timedelta(minutes=90), - "Answer": '\n' - '\n ' - "\n amt_worker_id\n " - f" {amt_worker_id}\n \n \n " - " amt_assignment_id\n " - f" {amt_assignment_id}\n \n \n " - f" tsid\n {tsid}\n " - " \n", - "RequesterFeedback": "Good work", - }, - "HIT": hit_response, - "ResponseMetadata": amt_response_metadata, - } - - -@pytest.fixture -def assignment_response_bad_tsid( - assignment_response: GetAssignmentResponseTypeDef, - amt_worker_id: str, - amt_assignment_id: str, -) -> GetAssignmentResponseTypeDef: - res = copy.deepcopy(assignment_response) - res["Assignment"]["Answer"] = ( - '\n' - '\n ' - "\n amt_worker_id\n " - f" {amt_worker_id}\n \n \n " - " amt_assignment_id\n " - f" {amt_assignment_id}\n \n " - f" \n tsid\n abc123 \n \n" - f"" - ) - return res - - -@pytest.fixture -def assignment_response_no_tsid( - assignment_response: GetAssignmentResponseTypeDef, - amt_worker_id: str, - amt_assignment_id: str, -) -> GetAssignmentResponseTypeDef: - res = copy.deepcopy(assignment_response) - res["Assignment"]["Answer"] = ( - '\n' - '\n ' - "\n amt_worker_id\n " - f" {amt_worker_id}\n \n \n " - " amt_assignment_id\n " - f" {amt_assignment_id}\n \n " - # f"\n tsid\n {tsid}\n \n" - f"" - ) - return res - - -@pytest.fixture -def assignment_response_approved_no_tsid( - assignment_response_no_tsid: GetAssignmentResponseTypeDef, -) -> GetAssignmentResponseTypeDef: - res = copy.deepcopy(assignment_response_no_tsid) - res["Assignment"]["AssignmentStatus"] = "Approved" - res["Assignment"]["RequesterFeedback"] = NO_WORK_APPROVAL_MESSAGE - res["Assignment"]["ApprovalTime"] = res["Assignment"]["SubmitTime"] - return res - - -@pytest.fixture -def assignment_response_factory_approved( - assignment_response: GetAssignmentResponseTypeDef, -) -> Callable[[str], GetAssignmentResponseTypeDef]: - - def inner(feedback: str = APPROVAL_MESSAGE) -> GetAssignmentResponseTypeDef: - res = copy.deepcopy(assignment_response) - res["Assignment"]["AssignmentStatus"] = "Approved" - res["Assignment"]["RequesterFeedback"] = feedback - res["Assignment"]["ApprovalTime"] = res["Assignment"]["SubmitTime"] - return res - - return inner - - -@pytest.fixture -def assignment_response_factory_rejected( - assignment_response: GetAssignmentResponseTypeDef, -) -> Callable[[str], GetAssignmentResponseTypeDef]: - - def inner(reject_reason: str = "reject reason") -> GetAssignmentResponseTypeDef: - res = copy.deepcopy(assignment_response) - res["Assignment"]["AssignmentStatus"] = "Rejected" - res["Assignment"]["RequesterFeedback"] = reject_reason - res["Assignment"]["RejectionTime"] = res["Assignment"]["SubmitTime"] - return res - - return inner - - -@pytest.fixture -def assignment_response_factory_rejected_no_tsid( - assignment_response_no_tsid: GetAssignmentResponseTypeDef, -) -> Callable[[str], GetAssignmentResponseTypeDef]: - - def inner(reject_reason: str = "reject reason") -> GetAssignmentResponseTypeDef: - res = copy.deepcopy(assignment_response_no_tsid) - res["Assignment"]["AssignmentStatus"] = "Rejected" - res["Assignment"]["RequesterFeedback"] = reject_reason - res["Assignment"]["RejectionTime"] = res["Assignment"]["SubmitTime"] - return res - - return inner - - -# --- HITType --- - - -@pytest.fixture -def create_hit_type_response( - amt_hit_type_id: str, amt_response_metadata: ResponseMetadataTypeDef -) -> CreateHITTypeResponseTypeDef: - return { - "HITTypeId": amt_hit_type_id, - "ResponseMetadata": amt_response_metadata, - } - - -# --- HIT --- - - -@pytest.fixture -def hit_response_with_hit_type( - amt_hit_type_id: str, - amt_hit_id: str, - amt_response_metadata: ResponseMetadataTypeDef, -) -> CreateHITWithHITTypeResponseTypeDef: - amt_group_id = generate_amt_id(length=30) - return { - "HIT": { - "HITId": amt_hit_id, - "HITTypeId": amt_hit_type_id, - "HITGroupId": amt_group_id, - "CreationTime": datetime(2025, 10, 14, 20, 22, tzinfo=tzlocal()), - "Title": "Test", - "Description": "test", - "Question": '\n\n https://jamesbillings67.com/work/\n 1200\n ', - "HITStatus": "Assignable", - "MaxAssignments": 1, - "Reward": "0.05", - "AutoApprovalDelayInSeconds": 2_592_000, - "Expiration": datetime(2025, 10, 14, 20, 24, 3, tzinfo=tzlocal()), - "AssignmentDurationInSeconds": 123, - "QualificationRequirements": [], - "HITReviewStatus": "NotReviewed", - "NumberOfAssignmentsPending": 0, - "NumberOfAssignmentsAvailable": 1, - "NumberOfAssignmentsCompleted": 0, - }, - "ResponseMetadata": amt_response_metadata, - } - - -@pytest.fixture -def hit_response( - amt_hit_type_id: str, - amt_hit_id: str, - amt_response_metadata: ResponseMetadataTypeDef, -) -> GetHITResponseTypeDef: - amt_group_id = generate_amt_id(length=30) - return { - "HIT": { - "HITId": amt_hit_id, - "HITTypeId": amt_hit_type_id, - "HITGroupId": amt_group_id, - "CreationTime": datetime(2025, 10, 13, 23, 0, 3, tzinfo=tzlocal()), - "Title": "Awesome Surveys!", - "Description": "Give us your opinion", - "Question": '\n\n https://jamesbillings67.com/work/\n 1200\n ', - "Keywords": "market,research,amazing", - "HITStatus": "Assignable", - "MaxAssignments": 1, - "Reward": "0.05", - "AutoApprovalDelayInSeconds": 604_800, - "Expiration": datetime(2025, 10, 27, 23, 0, 3, tzinfo=tzlocal()), - "AssignmentDurationInSeconds": 5_400, - "QualificationRequirements": [], - "HITReviewStatus": "NotReviewed", - "NumberOfAssignmentsPending": 0, - "NumberOfAssignmentsAvailable": 1, - "NumberOfAssignmentsCompleted": 0, - }, - "ResponseMetadata": amt_response_metadata, - } - - -@pytest.fixture -def hit_response_reviewing( - hit_response: GetHITResponseTypeDef, -) -> GetHITResponseTypeDef: - res = copy.deepcopy(hit_response) - res["HIT"]["NumberOfAssignmentsAvailable"] = 0 - res["HIT"]["NumberOfAssignmentsCompleted"] = 1 - res["HIT"]["HITStatus"] = "Reviewing" - return res diff --git a/tests/fixtures/flow.py b/tests/fixtures/flow.py deleted file mode 100644 index 7bf8b4f..0000000 --- a/tests/fixtures/flow.py +++ /dev/null @@ -1,308 +0,0 @@ -from collections.abc import Callable -from datetime import datetime, timezone -from typing import Any -from uuid import uuid4 - -import pytest -import requests -from generalresearch.currency import USDCent -from generalresearch.models.thl.definitions import PayoutStatus -from generalresearch.models.thl.payout import UserPayoutEvent -from generalresearch.models.thl.wallet.cashout_method import ( - CashoutRequestInfo, - CashoutRequestResponse, -) -from generalresearch.models.thl.wallet.definitions import PayoutType -from mypy_boto3_mturk.type_defs import ( - GetAssignmentResponseTypeDef, - GetHITResponseTypeDef, -) - -from jb.config import settings -from jb.managers.amt import ( - APPROVAL_MESSAGE, - BONUS_MESSAGE, -) - - -@pytest.fixture -def approved_assignment_stubs( - assignment_response: GetAssignmentResponseTypeDef, - assignment_response_factory_approved: Callable[[str], GetAssignmentResponseTypeDef], - amt_assignment_id: str, - amt_hit_id: str, - hit_response_reviewing: GetHITResponseTypeDef, -) -> Callable[..., list[dict[str, Any]]]: - - # These are the AMT_CLIENT stubs/mocks that need to be set when running - # process_assignment_submitted() which will result in an approved - # assignment and sent bonus - def _inner( - feedback: str = APPROVAL_MESSAGE, - override_response: str | None = None, - override_approve_response: str | None = None, - ) -> list[dict[str, Any]]: - - response = override_response or assignment_response - approve_response = ( - override_approve_response or assignment_response_factory_approved(feedback) - ) - - return [ - { - "operation": "get_assignment", - "response": response, - "expected_params": {"AssignmentId": amt_assignment_id}, - }, - { - "operation": "approve_assignment", - "response": {}, - "expected_params": { - "AssignmentId": amt_assignment_id, - "RequesterFeedback": feedback, - "OverrideRejection": False, - }, - }, - { - "operation": "get_assignment", - "response": approve_response, - "expected_params": {"AssignmentId": amt_assignment_id}, - }, - { - "operation": "update_hit_review_status", - "response": {}, - "expected_params": {"HITId": amt_hit_id, "Revert": False}, - }, - { - "operation": "get_hit", - "response": hit_response_reviewing, - "expected_params": {"HITId": amt_hit_id}, - }, - ] - - return _inner - - -@pytest.fixture -def approved_assignment_stubs_w_bonus( - approved_assignment_stubs: Callable[..., list[dict[str, Any]]], - amt_worker_id: str, - amt_assignment_id: str, - pe_id: str, -) -> list[dict[str, Any]]: - - now = datetime.now(tz=timezone.utc) - stubs = approved_assignment_stubs().copy() - stubs.append( - { - "operation": "send_bonus", - "response": {}, - "expected_params": { - "WorkerId": amt_worker_id, - "BonusAmount": "0.07", - "AssignmentId": amt_assignment_id, - "Reason": BONUS_MESSAGE, - "UniqueRequestToken": pe_id, - }, - } - ) - stubs.append( - { - "operation": "list_bonus_payments", - "response": { - "BonusPayments": [ - { - "WorkerId": amt_worker_id, - "BonusAmount": "0.07", - "AssignmentId": amt_assignment_id, - "Reason": BONUS_MESSAGE, - "GrantTime": now, - } - ] - }, - "expected_params": {"AssignmentId": amt_assignment_id}, - } - ) - return stubs - - -@pytest.fixture -def rejected_assignment_stubs( - assignment_response: GetAssignmentResponseTypeDef, - assignment_response_factory_rejected: Callable[[str], GetAssignmentResponseTypeDef], - amt_assignment_id: str, - amt_hit_id: str, - hit_response_reviewing: GetHITResponseTypeDef, -) -> Callable[..., list[dict[str, Any]]]: - - # These are the AMT_CLIENT stubs/mocks that need to be set when running - # process_assignment_submitted() which will result in a rejected - # assignment - def _inner( - reject_reason: str, - override_response: str | None = None, - override_reject_response: str | None = None, - ) -> list[dict[str, Any]]: - - response = override_response or assignment_response - reject_response = ( - override_reject_response - or assignment_response_factory_rejected(reject_reason) - ) - - return [ - { - "operation": "get_assignment", - "response": response, - "expected_params": {"AssignmentId": amt_assignment_id}, - }, - { - "operation": "reject_assignment", - "response": {}, - "expected_params": { - "AssignmentId": amt_assignment_id, - "RequesterFeedback": reject_reason, - }, - }, - { - "operation": "get_assignment", - "response": reject_response, - "expected_params": {"AssignmentId": amt_assignment_id}, - }, - { - "operation": "update_hit_review_status", - "response": {}, - "expected_params": {"HITId": amt_hit_id, "Revert": False}, - }, - { - "operation": "get_hit", - "response": hit_response_reviewing, - "expected_params": {"HITId": amt_hit_id}, - }, - ] - - return _inner - - -@pytest.fixture -def mock_thl_responses( - monkeypatch: pytest.MonkeyPatch, amt_worker_id: str, tsid: str, pe_id: str -) -> Callable[..., None]: - - original_get = requests.get - original_post = requests.post - - class MockThlCashoutRequestResponse: - def json(self): - return CashoutRequestResponse( - status="success", - cashout=CashoutRequestInfo( - id=pe_id, - description="amt something", - status=PayoutStatus.PENDING, - ), - ).model_dump(mode="json") - - def _inner( - user_blocked: bool = False, - status_finished: bool = True, - status_complete: bool = False, - wallet_redeemable_amount: int = 10, - ): - - def mock_get(url, *args, **kwargs): # type: ignore - profile_url = f"{settings.fsb_host}{settings.product_id}/user/{amt_worker_id}/profile/" - status_url = f"{settings.fsb_host}{settings.product_id}/status/{tsid}/" - cashout_request_url = f"{settings.fsb_host}{settings.product_id}/cashout/" - wallet_url = f"{settings.fsb_host}{settings.product_id}/wallet/" - if url == profile_url: - - class MockThlProfileResponse: - def json(self): - return { - "user_profile": { - "user": { - "product_id": settings.product_id, - "product_user_id": amt_worker_id, - "blocked": user_blocked, - } - } - } - - return MockThlProfileResponse() - - elif url == wallet_url: - - class MockThlWalletResponse: - def json(self) -> dict[str, Any]: - return { - "wallet": { - "amount": wallet_redeemable_amount, - "redeemable_amount": wallet_redeemable_amount, - } - } - - return MockThlWalletResponse() - - elif url == status_url: - - class MockThlStatusResponse: - def json(self) -> dict[str, Any]: - return { - "tsid": tsid, - "product_id": str(settings.product_id), - "product_user_id": amt_worker_id, - "started": "2020-06-02T00:30:35.036398Z", - "finished": ( - "2020-06-02T00:31:35.036398Z" - if status_finished - else None - ), - "status": 3 if status_complete else 2, - "payout": 10 if status_complete else 0, - "user_payout": 10 if status_complete else 0, - "status_code_1": "BUYER_FAIL", - "status_code_2": None, - } - - return MockThlStatusResponse() - - elif url == cashout_request_url: - return MockThlCashoutRequestResponse() - - else: - raise ValueError(f"unhandled call: {url=} {args=} {kwargs=}") - - return original_get(url, *args, **kwargs) - - def mock_post(url, *args, **kwargs): # type: ignore - cashout_request_url = f"{settings.fsb_host}{settings.product_id}/cashout/" - manage_cashout_request_url = f"{settings.fsb_host}{settings.fsb_host_private_route}/thl/manage_cashout/" - - if url == cashout_request_url: - return MockThlCashoutRequestResponse() - - elif url == manage_cashout_request_url: - json = kwargs["json"] - print(json) - payout_id = json["payout_id"] - new_status = json["new_status"] - - class MockThlManageCashoutResponse: - def json(self): - return UserPayoutEvent( - uuid=payout_id, - status=new_status, - amount=USDCent(5), - debit_account_uuid=uuid4().hex, - cashout_method_uuid=uuid4().hex, - payout_type=PayoutType.AMT, - ).model_dump(mode="json") - - return MockThlManageCashoutResponse() - return original_post(url, *args, **kwargs) - - monkeypatch.setattr(requests, "get", mock_get) - monkeypatch.setattr(requests, "post", mock_post) - - return _inner diff --git a/tests/fixtures/http.py b/tests/fixtures/http.py index e38c853..0f88fb8 100644 --- a/tests/fixtures/http.py +++ b/tests/fixtures/http.py @@ -1,7 +1,6 @@ -import json import secrets from collections.abc import AsyncGenerator -from typing import Any +from uuid import uuid4 import httpx import pytest @@ -10,12 +9,8 @@ from asgi_lifespan import LifespanManager from generalresearch.redis_helper import RedisConfig from httpx import ASGITransport, AsyncClient -from jb.config import JB_EVENTS_STREAM, settings from jb.decorators import get_redis_config from jb.main import app -from jb.models.assignment import AssignmentStub -from jb.models.hit import Hit -from tests import generate_amt_id @pytest.fixture(scope="session") @@ -64,43 +59,16 @@ def mock_requests(): yield m -def generate_hex_id(length: int = 40) -> str: - # length is number of hex chars, so we need length//2 bytes - return secrets.token_hex(length // 2) +@pytest.fixture +def email() -> str: + return "unittest@generalresearch.com" @pytest.fixture -def mturk_event_body_record( - hit_record: Hit, assignment_stub_record: AssignmentStub -) -> dict[str, Any]: - return { - "Type": "Notification", - "Message": json.dumps( - { - "Events": [ - { - "EventType": "AssignmentSubmitted", - "EventTimestamp": "2025-10-16T18:45:51.000000Z", - "HITId": hit_record.amt_hit_id, - "AssignmentId": assignment_stub_record.amt_assignment_id, - "HITTypeId": hit_record.amt_hit_type_id, - } - ], - "EventDocId": generate_hex_id(), - "SourceAccount": settings.aws_owner_id, - "CustomerId": generate_amt_id(length=14), - "EventDocVersion": "2006-05-05", - } - ), - } +def amt_worker_id() -> str: + return uuid4().hex -@pytest.fixture() -def clean_mturk_events_redis_stream(redis_config: RedisConfig): - redis_client = redis_config.create_redis_client() - - redis_client.xtrim(JB_EVENTS_STREAM, maxlen=0) - assert redis_client.xlen(JB_EVENTS_STREAM) == 0 - yield - redis_client.xtrim(JB_EVENTS_STREAM, maxlen=0) - assert redis_client.xlen(JB_EVENTS_STREAM) == 0 +def generate_hex_id(length: int = 40) -> str: + # length is number of hex chars, so we need length//2 bytes + return secrets.token_hex(length // 2) diff --git a/tests/fixtures/managers.py b/tests/fixtures/managers.py deleted file mode 100644 index 8f87e5d..0000000 --- a/tests/fixtures/managers.py +++ /dev/null @@ -1,88 +0,0 @@ -from typing import TYPE_CHECKING - -import pytest -from generalresearch.managers.base import Permission -from generalresearch.pg_helper import PostgresConfig -from mypy_boto3_mturk import MTurkClient - -if TYPE_CHECKING: - from jb.managers.amt import AMTManager - from jb.managers.assignment import AssignmentManager - from jb.managers.bonus import BonusManager - from jb.managers.hit import HitManager, HitQuestionManager, HitTypeManager - - -# --- Managers --- -@pytest.fixture(scope="session") -def amtm(amt_client: MTurkClient) -> "AMTManager": - from jb.managers.amt import AMTManager - - amtm = AMTManager(amt_client=amt_client) - assert "mturk-requester-sandbox" in amtm.amt_client.meta.endpoint_url - - return amtm - - -@pytest.fixture(scope="session") -def hqm(pg_config: PostgresConfig) -> "HitQuestionManager": - assert ( - pg_config.dsn.path and "/unittest-" in pg_config.dsn.path - ), "pg_config must point to a unittest database (dsn path must contain '/unittest-')" - - from jb.managers.hit import HitQuestionManager - - return HitQuestionManager( - pg_config=pg_config, permissions=[Permission.READ, Permission.CREATE] - ) - - -@pytest.fixture(scope="session") -def htm(pg_config: PostgresConfig) -> "HitTypeManager": - assert ( - pg_config.dsn.path and "/unittest-" in pg_config.dsn.path - ), "pg_config must point to a unittest database (dsn path must contain '/unittest-')" - - from jb.managers.hit import HitTypeManager - - return HitTypeManager( - pg_config=pg_config, permissions=[Permission.READ, Permission.CREATE] - ) - - -@pytest.fixture(scope="session") -def hm(pg_config: PostgresConfig) -> "HitManager": - assert ( - pg_config.dsn.path and "/unittest-" in pg_config.dsn.path - ), "pg_config must point to a unittest database (dsn path must contain '/unittest-')" - - from jb.managers.hit import HitManager - - return HitManager( - pg_config=pg_config, permissions=[Permission.READ, Permission.CREATE] - ) - - -@pytest.fixture(scope="session") -def am(pg_config: PostgresConfig) -> "AssignmentManager": - assert ( - pg_config.dsn.path and "/unittest-" in pg_config.dsn.path - ), "pg_config must point to a unittest database (dsn path must contain '/unittest-')" - - from jb.managers.assignment import AssignmentManager - - return AssignmentManager( - pg_config=pg_config, permissions=[Permission.READ, Permission.CREATE] - ) - - -@pytest.fixture(scope="session") -def bm(pg_config: PostgresConfig) -> "BonusManager": - assert ( - pg_config.dsn.path and "/unittest-" in pg_config.dsn.path - ), "pg_config must point to a unittest database (dsn path must contain '/unittest-')" - - from jb.managers.bonus import BonusManager - - return BonusManager( - pg_config=pg_config, permissions=[Permission.READ, Permission.CREATE] - ) diff --git a/tests/fixtures/models.py b/tests/fixtures/models.py deleted file mode 100644 index 157daec..0000000 --- a/tests/fixtures/models.py +++ /dev/null @@ -1,296 +0,0 @@ -from collections.abc import Callable, Generator -from datetime import datetime, timedelta, timezone -from typing import TYPE_CHECKING - -import pytest -from generalresearch.currency import USDCent -from generalresearch.pg_helper import PostgresConfig -from psycopg.errors import ForeignKeyViolation - -from jb.managers.amt import AMTManager -from jb.models.assignment import Assignment, AssignmentStub -from jb.models.definitions import AssignmentStatus, HitReviewStatus, HitStatus -from jb.models.event import MTurkEvent -from jb.models.hit import Hit, HitQuestion, HitType -from tests import generate_amt_id - -if TYPE_CHECKING: - from jb.managers.assignment import AssignmentManager - from jb.managers.hit import HitManager, HitQuestionManager, HitTypeManager - - -# --- MTurk Event --- - - -@pytest.fixture -def mturk_event( - amt_assignment_id: str, amt_hit_id: str, amt_hit_type_id: str -) -> MTurkEvent: - now = datetime.now(tz=timezone.utc) - return MTurkEvent( - event_type="AssignmentSubmitted", - event_timestamp=now, - amt_assignment_id=amt_assignment_id, - amt_hit_type_id=amt_hit_type_id, - amt_hit_id=amt_hit_id, - ) - - -# --- Question --- - - -@pytest.fixture -def question() -> HitQuestion: - return HitQuestion(url="https://jamesbillings67.com/work/", height=1200) - - -@pytest.fixture -def question_record(hqm: "HitQuestionManager", question: HitQuestion) -> HitQuestion: - return hqm.get_or_create(question) - - -# --- HITType --- - - -@pytest.fixture -def hit_type() -> HitType: - return HitType( - title="Awesome Surveys!", - description="Give us your opinion", - reward=USDCent(5), - keywords="market,research,amazing", - min_active=10, - ) - - -@pytest.fixture -def hit_type_record( - pg_config: PostgresConfig, htm: "HitTypeManager", hit_type: HitType -) -> Generator[HitType, None, None]: - - hit_type.amt_hit_type_id = generate_amt_id() - - ht = htm.get_or_create(hit_type) - - yield ht - - try: - with pg_config.make_connection() as conn, conn.cursor() as c: - c.execute("DELETE FROM mtwerk_hittype WHERE id=%s", (ht.id,)) - conn.commit() - - except ForeignKeyViolation: - pass # DB gets dropped anyway, don't care - - -@pytest.fixture -def hit_type_record_with_amt_id( - pg_config: PostgresConfig, htm: "HitTypeManager", hit_type: HitType -) -> Generator[HitType, None, None]: - # This is a real hit type I've previously registered with amt (sandbox). - # It will always exist - hit_type.amt_hit_type_id = "3217B3DC4P5YW9DRV9R3X8O56V041J" - - # Get or create our db - ht = htm.get_or_create(hit_type) - - yield ht - - try: - with pg_config.make_connection() as conn, conn.cursor() as c: - c.execute("DELETE FROM mtwerk_hittype WHERE id=%s", (ht.id,)) - conn.commit() - - except ForeignKeyViolation: - pass # DB gets dropped anyway, don't care - - -# --- HIT --- - - -@pytest.fixture -def hit( - amt_hit_id: str, amt_hit_type_id: str, amt_group_id: str, question: HitQuestion -) -> Hit: - now = datetime.now(tz=timezone.utc) - - return Hit.model_validate( - dict( - amt_hit_id=amt_hit_id, - amt_hit_type_id=amt_hit_type_id, - amt_group_id=amt_group_id, - status=HitStatus.Assignable, - review_status=HitReviewStatus.NotReviewed, - creation_time=now, - expiration=now + timedelta(days=3), - hit_question_xml=question.xml, - qualification_requirements=[], - max_assignments=1, - assignment_pending_count=0, - assignment_available_count=1, - assignment_completed_count=0, - description="Description", - keywords="Keywords", - reward=USDCent(5), - title="Title", - question_id=question.id, - hit_type_id=None, - ) - ) - - -@pytest.fixture -def hit_record( - pg_config: PostgresConfig, - hm: "HitManager", - question_record: HitQuestion, - hit_type_record: HitType, - hit: Hit, - amt_hit_id: str, -) -> Generator[Hit, None, None]: - """ - Returns a hit that exists in our db, but does not in amazon (the amt ids - are random). The mtwerk_hittype and mtwerk_question records will also - exist (in the db) - """ - - hit.hit_type_id = hit_type_record.id - hit.amt_hit_id = amt_hit_id - hit.question_id = question_record.id - - hm.create(hit) - - yield hit - - try: - with pg_config.make_connection() as conn, conn.cursor() as c: - c.execute("DELETE FROM mtwerk_hit WHERE id=%s", (hit.id,)) - conn.commit() - - except ForeignKeyViolation: - pass # DB gets dropped anyway, don't care - - -@pytest.fixture -def hit_in_amt( - amtm: AMTManager, - hm: "HitManager", - question_record: HitQuestion, - hit_type_record_with_amt_id: HitType, -) -> Hit: - # Actually create a new HIT in amt (sandbox) - hit = amtm.create_hit_with_hit_type( - hit_type=hit_type_record_with_amt_id, question=question_record - ) - - # Create it in the DB - hm.create(hit) - return hit - - -# --- Assignment --- - - -@pytest.fixture -def assignment_stub( - hit: Hit, amt_assignment_id: str, amt_worker_id: str -) -> AssignmentStub: - now = datetime.now(tz=timezone.utc) - return AssignmentStub( - amt_assignment_id=amt_assignment_id, - amt_hit_id=hit.amt_hit_id, - amt_worker_id=amt_worker_id, - status=AssignmentStatus.Submitted, - modified_at=now, - created_at=now, - ) - - -@pytest.fixture -def assignment_stub_record( - pg_config: PostgresConfig, - am: "AssignmentManager", - hit_record: Hit, - assignment_stub: AssignmentStub, -) -> Generator[AssignmentStub, None, None]: - """ - Returns an AssignmentStub that exists in our db, but does not in - amazon (the amt ids are random). The mtwerk_hit, mtwerk_hittype, and - mtwerk_question records will also exist (in the db) - """ - assignment_stub.hit_id = hit_record.id - am.create_stub(stub=assignment_stub) - - yield assignment_stub - - try: - with pg_config.make_connection() as conn, conn.cursor() as c: - c.execute( - "DELETE FROM mtwerk_assignment WHERE id=%s", (assignment_stub.id,) - ) - conn.commit() - - except ForeignKeyViolation: - pass # DB gets dropped anyway, don't care - - -@pytest.fixture -def assignment(assignment_factory: Callable[..., Assignment]) -> Assignment: - return assignment_factory() - - -@pytest.fixture -def assignment_record( - pg_config: PostgresConfig, - hit_record: Hit, - assignment_record_factory: Callable[..., Assignment], -) -> Generator[Assignment, None, None]: - assignment = assignment_record_factory(hit_id=hit_record.id) - - yield assignment - - try: - with pg_config.make_connection() as conn, conn.cursor() as c: - c.execute("DELETE FROM mtwerk_assignment WHERE id=%s", (assignment.id,)) - conn.commit() - - except ForeignKeyViolation: - pass # DB gets dropped anyway, don't care - - -@pytest.fixture -def assignment_factory(hit_record: Hit) -> Callable[[str | None], Assignment]: - - def _inner(amt_worker_id: str | None = None) -> Assignment: - now = datetime.now(tz=timezone.utc) - amt_assignment_id = generate_amt_id() - amt_worker_id = amt_worker_id or generate_amt_id() - - return Assignment( - amt_assignment_id=amt_assignment_id, - amt_hit_id=hit_record.amt_hit_id, - amt_worker_id=amt_worker_id, - status=AssignmentStatus.Submitted, - modified_at=now, - created_at=now, - accept_time=now, - auto_approval_time=now, - submit_time=now, - ) - - return _inner - - -@pytest.fixture -def assignment_record_factory( - am: "AssignmentManager", assignment_factory: Callable[..., Assignment] -) -> Callable[..., Assignment]: - - def _inner(hit_id: int, amt_worker_id: str | None = None) -> Assignment: - a = assignment_factory(amt_worker_id=amt_worker_id) - a.hit_id = hit_id - am.create_stub(a) - am.update_answer(a) - return a - - return _inner diff --git a/tests/flow/__init__.py b/tests/flow/__init__.py deleted file mode 100644 index 3c88c7e..0000000 --- a/tests/flow/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -# def test_a_hit(): -# hit_type = HitType( -# title="Sandbox Test 2026-02-04", -# description="test description", -# reward=USDCent(5), -# keywords="a,b,c", -# min_active=10, -# ) -# AMTManager.create_hit_type(hit_type=hit_type) -# HTM.create(hit_type) - -# hit_type = HTM.get(amt_hit_type_id=hit_type.amt_hit_type_id) -# hit = create_hit_from_hittype(hit_type) - -# hit_type = HTM.get(amt_hit_type_id="3QMCR5SRRVWV0O3UDSC64VSB8KL4GL") -# hit = create_hit_from_hittype(hit_type) diff --git a/tests/flow/test_tasks.py b/tests/flow/test_tasks.py deleted file mode 100644 index f28a6aa..0000000 --- a/tests/flow/test_tasks.py +++ /dev/null @@ -1,352 +0,0 @@ -import logging -from collections.abc import Callable -from contextlib import contextmanager -from typing import Any - -import pytest -from botocore.stub import Stubber -from generalresearch.currency import USDCent -from mypy_boto3_mturk import MTurkClient -from mypy_boto3_mturk.type_defs import ( - GetAssignmentResponseTypeDef, -) - -from jb.flow.assignment_tasks import process_assignment_submitted -from jb.managers.amt import ( - APPROVAL_MESSAGE, - NO_WORK_APPROVAL_MESSAGE, - REJECT_MESSAGE_BADDIE, - REJECT_MESSAGE_NO_WORK, - REJECT_MESSAGE_UNKNOWN_ASSIGNMENT, - AMTManager, -) -from jb.managers.assignment import AssignmentManager -from jb.managers.bonus import BonusManager -from jb.managers.hit import HitManager -from jb.models.assignment import Assignment, AssignmentStub -from jb.models.definitions import AssignmentStatus -from jb.models.event import MTurkEvent -from jb.models.hit import Hit - - -@contextmanager -def amt_stub_context(amt_client: MTurkClient, responses: list[dict[str, Any]]): - - # ty chatgpt for this - with Stubber(amt_client) as stub: - for r in responses: - stub.add_response( - r["operation"], - r.get("response", {}), - r.get("expected_params", {}), - ) - yield stub - - -class TestHITTasks: - - def test_fake_get_assignment( - self, - amt_client: MTurkClient, - amtm: AMTManager, - amt_assignment_id: str, - amt_worker_id: str, - assignment_response: GetAssignmentResponseTypeDef, - ): - # Testing just that this boto stubber works (we fake a response - # using the real boto client) - fake_response = assignment_response.copy() - - with Stubber(amt_client) as stub: - expected_params = {"AssignmentId": amt_assignment_id} - stub.add_response("get_assignment", fake_response, expected_params) - - assignment = amtm.get_assignment_if_exists( - amt_assignment_id=amt_assignment_id - ) - assert assignment is not None - assert assignment.amt_assignment_id == amt_assignment_id - assert assignment.amt_worker_id == amt_worker_id - - # Optionally, ensure all queued responses were used: - stub.assert_no_pending_responses() - - -class TestProcessAssignmentSubmitted: - - def test_no_assignment_in_db( - self, - amtm: AMTManager, - hm: HitManager, - am: AssignmentManager, - bm: BonusManager, - amt_client: MTurkClient, - hit_record: Hit, - mturk_event: MTurkEvent, - amt_assignment_id: str, - caplog: pytest.LogCaptureFixture, - rejected_assignment_stubs: Callable[..., list[dict[str, Any]]], - ): - - # These records are auto cleaned up, so we need to explicitly create - # a HIT record in the DB so the process_assignment_submitted task - # doesn't error when we try to process the Request - _ = hit_record - - # An assignment is submitted. The hit exists in the DB. The amt - # assignment id is valid, but the assignment stub is not in our - # db. Reject it and write the assignment to the db. - - amt_stubs = rejected_assignment_stubs( - reject_reason=REJECT_MESSAGE_UNKNOWN_ASSIGNMENT - ) - - with amt_stub_context(amt_client, amt_stubs) as stub, caplog.at_level( - logging.WARNING - ): - process_assignment_submitted( - amtm=amtm, hm=hm, am=am, bm=bm, event=mturk_event - ) - stub.assert_no_pending_responses() - - # assert f"No assignment found in DB: {amt_assignment_id}" in caplog.text - assert "Rejected assignment doesn't exist in DB. Creating ... " in caplog.text - assert "Rejected assignment: " in caplog.text - stub.assert_no_pending_responses() - - ass = am.get(amt_assignment_id=amt_assignment_id) - assert ass.status == AssignmentStatus.Rejected - assert ass.requester_feedback == REJECT_MESSAGE_UNKNOWN_ASSIGNMENT - - def test_assignment_in_db_user_doesnt_exist( - self, - amtm: AMTManager, - am: AssignmentManager, - hm: HitManager, - bm: BonusManager, - amt_client: MTurkClient, - mturk_event: MTurkEvent, - amt_assignment_id: str, - assignment_stub_record: AssignmentStub, - caplog: pytest.LogCaptureFixture, - mock_thl_responses: Callable[..., None], - rejected_assignment_stubs: Callable[..., list[dict[str, Any]]], - ): - # An assignment is submitted. The hit and AssignmentStub exist in the - # DB. We think we're going to approve the Assignment, but the - # user-profile / check blocked call on THL shows the user doesn't - # exist (same thing would happen if the user does exist and is - # blocked). So we reject. - - # We need this to make the assignment stub in the db - _ = assignment_stub_record - - amt_stubs = rejected_assignment_stubs(reject_reason=REJECT_MESSAGE_BADDIE) - mock_thl_responses(user_blocked=True) - - with amt_stub_context(amt_client, amt_stubs) as stub, caplog.at_level( - logging.WARNING - ): - process_assignment_submitted( - amtm=amtm, am=am, hm=hm, bm=bm, event=mturk_event - ) - stub.assert_no_pending_responses() - - # assert f"No assignment found in DB: {amt_assignment_id}" not in caplog.text - assert "blocked or not exists" in caplog.text - assert "Rejected assignment: " in caplog.text - - ass = am.get(amt_assignment_id=amt_assignment_id) - assert ass.status == AssignmentStatus.Rejected - assert ass.requester_feedback == REJECT_MESSAGE_BADDIE - - def test_no_work_w_warning( - self, - amtm: AMTManager, - am: AssignmentManager, - hm: HitManager, - bm: BonusManager, - mturk_event: MTurkEvent, - amt_client: MTurkClient, - amt_assignment_id: str, - assignment_stub_record: AssignmentStub, - caplog: pytest.LogCaptureFixture, - mock_thl_responses: Callable[..., None], - approved_assignment_stubs: Callable[..., list[dict[str, Any]]], - assignment_response_approved_no_tsid: GetAssignmentResponseTypeDef, - assignment_response_no_tsid: GetAssignmentResponseTypeDef, - ): - # An Assignment is submitted. The hit and AssignmentStub exist in - # the DB. The assignment has no tsid. - # We APPROVE this assignment b/c we are very nice and give users a - # couple chances, with an explanation, before rejecting. - - # We need this to make the assignment stub in the db - _ = assignment_stub_record - - # Simulate that the AMT.get_assignment call returns the assignment, - # but the answers XML has no tsid. - amt_stubs = approved_assignment_stubs( - feedback=NO_WORK_APPROVAL_MESSAGE, - override_response=assignment_response_no_tsid, - override_approve_response=assignment_response_approved_no_tsid, - ) - - mock_thl_responses(user_blocked=False) - - with amt_stub_context(amt_client, amt_stubs) as stub, caplog.at_level( - logging.WARNING - ): - process_assignment_submitted( - amtm=amtm, hm=hm, am=am, bm=bm, event=mturk_event - ) - stub.assert_no_pending_responses() - - assert f"No assignment found in DB: {amt_assignment_id}" not in caplog.text - assert "Assignment submitted with no tsid" in caplog.text - assert "Approved assignment: " in caplog.text - - ass = am.get(amt_assignment_id=amt_assignment_id) - assert ass.status == AssignmentStatus.Approved - assert ass.requester_feedback == NO_WORK_APPROVAL_MESSAGE - assert am.missing_tsid_count(amt_worker_id=ass.amt_worker_id) == 1 - - def test_no_work_no_warning( - self, - amtm: AMTManager, - am: AssignmentManager, - hm: HitManager, - bm: BonusManager, - mturk_event: MTurkEvent, - amt_client: MTurkClient, - amt_assignment_id: str, - assignment_stub_record: AssignmentStub, - caplog: pytest.LogCaptureFixture, - mock_thl_responses: Callable[..., None], - rejected_assignment_stubs: Callable[..., list[dict[str, Any]]], - assignment_response_factory_rejected_no_tsid: Callable[ - ..., GetAssignmentResponseTypeDef - ], - assignment_response_no_tsid: GetAssignmentResponseTypeDef, - assignment_record_factory: Callable[..., Assignment], - hit_record: Hit, # used in assignment_factory - amt_worker_id: str, - ): - # An assignment is submitted. The hit and assignment stub exist in the DB. - # The assignment has no tsid. - - # Going to create and submit 3 assignments w no work - # (all on the same hit, which we don't do in JB for real, - # but doesn't matter here) - _a1 = assignment_record_factory( - hit_id=hit_record.id, amt_worker_id=amt_worker_id - ) - _a2 = assignment_record_factory( - hit_id=hit_record.id, amt_worker_id=amt_worker_id - ) - _a3 = assignment_record_factory( - hit_id=hit_record.id, amt_worker_id=amt_worker_id - ) - - assert am.missing_tsid_count(amt_worker_id=amt_worker_id) == 3 - # So now, we'll reject, b/c they've already gotten 3 warnings - - _ = assignment_stub_record # we need this to make the assignment stub in the db - - # Simulate that the AMT.get_assignment call returns the assignment, but the answers xml - # has no tsid. - amt_stubs = rejected_assignment_stubs( - reject_reason=REJECT_MESSAGE_NO_WORK, - override_response=assignment_response_no_tsid, - override_reject_response=assignment_response_factory_rejected_no_tsid( - REJECT_MESSAGE_NO_WORK - ), - ) - - mock_thl_responses(user_blocked=False) - - with amt_stub_context(amt_client, amt_stubs) as stub, caplog.at_level( - logging.WARNING - ): - process_assignment_submitted( - amtm=amtm, hm=hm, am=am, bm=bm, event=mturk_event - ) - stub.assert_no_pending_responses() - - assert f"No assignment found in DB: {amt_assignment_id}" not in caplog.text - assert "Assignment submitted with no tsid" in caplog.text - assert "Rejected assignment: " in caplog.text - - # It will exist in the db since we can validate the model. - ass = am.get(amt_assignment_id=amt_assignment_id) - assert ass.status == AssignmentStatus.Rejected - assert ass.requester_feedback == REJECT_MESSAGE_NO_WORK - - def test_assignment_submitted_no_bonus( - self, - amtm: AMTManager, - am: AssignmentManager, - hm: HitManager, - bm: BonusManager, - amt_client: MTurkClient, - mturk_event: MTurkEvent, - amt_assignment_id: str, - assignment_stub_record: Assignment, - caplog: pytest.LogCaptureFixture, - mock_thl_responses: Callable[..., None], - approved_assignment_stubs: Callable[..., list[dict[str, Any]]], - ): - - _ = assignment_stub_record # we need this to make the assignment stub in the db - - # The "send bonus" stuff will still run, even if the user didn't get - # a complete, because all we do is check the user's wallet balance (if - # an assignment is approved) and they may have money in their wallet - # from a prev event or bribe - # - # So mock the wallet balance as 1cent, so no bonus will be triggered - - mock_thl_responses(status_complete=False, wallet_redeemable_amount=1) - with amt_stub_context( - amt_client, approved_assignment_stubs() - ) as stub, caplog.at_level(logging.WARNING): - process_assignment_submitted( - amtm=amtm, hm=hm, am=am, bm=bm, event=mturk_event - ) - stub.assert_no_pending_responses() - - ass = am.get(amt_assignment_id=amt_assignment_id) - assert ass.status == AssignmentStatus.Approved - assert ass.requester_feedback == APPROVAL_MESSAGE - - def test_assignment_submitted_w_bonus( - self, - amtm: AMTManager, - am: AssignmentManager, - hm: HitManager, - bm: BonusManager, - amt_client: MTurkClient, - mturk_event: MTurkEvent, - amt_assignment_id: str, - assignment_stub_record: Assignment, - caplog: pytest.LogCaptureFixture, - mock_thl_responses: Callable[..., None], - approved_assignment_stubs_w_bonus: list[dict[str, Any]], - ): - _ = assignment_stub_record # we need this to make the assignment stub in the db - mock_thl_responses(status_complete=True, wallet_redeemable_amount=10) - - with amt_stub_context( - amt_client, approved_assignment_stubs_w_bonus - ) as stub, caplog.at_level(logging.WARNING): - process_assignment_submitted( - amtm=amtm, hm=hm, am=am, bm=bm, event=mturk_event - ) - stub.assert_no_pending_responses() - - ass = am.get(amt_assignment_id=amt_assignment_id) - assert ass.status == AssignmentStatus.Approved - assert ass.requester_feedback == APPROVAL_MESSAGE - - bonus = bm.filter(amt_assignment_id=amt_assignment_id)[0] - assert bonus.amount == USDCent(7) diff --git a/tests/http/test_auth.py b/tests/http/test_auth.py index b44a36a..40f0366 100644 --- a/tests/http/test_auth.py +++ b/tests/http/test_auth.py @@ -1,4 +1,5 @@ from urllib.parse import parse_qs, urlparse +from uuid import uuid4 import pytest from httpx import AsyncClient @@ -35,9 +36,6 @@ class FakeGRApiManager: return user -@pytest.fixture -def email() -> str: - return "unittest@generalresearch.com" @pytest.fixture @@ -58,20 +56,20 @@ class TestAuth: ): client = httpxclient - res = await client.post("/auth/magic-link/request", json={"email": email}) + res = await client.post("/auth/magic-link/request/", json={"email": email}) d = res.json() assert res.status_code == 200 assert d["magic_link"] token = parse_qs(urlparse(d["magic_link"]).query)["token"][0] - url = "/auth/magic-link/exchange" + url = "/auth/magic-link/exchange/" body = {"token": token} res = await client.post(url, json=body) assert res.status_code == 204 assert client.cookies.get(SESSION_COOKIE_NAME) - res = await client.get("/auth/session") + res = await client.get("/auth/session/") assert res.status_code == 200 assert res.json()["email"] == email @@ -87,7 +85,7 @@ class TestAuth: fake_gr_api_manager.add_amt_user(amt_worker_id) res = await client.post( - "/auth/link-amt/request", + "/auth/link-amt/request/", json={"email": email, "amt_worker_id": amt_worker_id}, ) d = res.json() @@ -110,7 +108,7 @@ class TestAuth: assert transitioned_amt_worker_id == amt_worker_id assert amt_worker_id not in fake_gr_api_manager.amt_worker_ids - res = await client.get("/auth/session") + res = await client.get("/auth/session/") assert res.status_code == 200 assert res.json()["email"] == email diff --git a/tests/http/test_magic.py b/tests/http/test_magic.py index f645677..278a194 100644 --- a/tests/http/test_magic.py +++ b/tests/http/test_magic.py @@ -1,5 +1,6 @@ from uuid import uuid4 +import pytest from generalresearch.redis_helper import RedisConfig from jb.api.magic_token import ( @@ -11,8 +12,12 @@ from jb.api.magic_token import ( from jb.models.auth import AmtAccountLink -class TestViewFunctions: +@pytest.fixture +def amt_worker_id() -> str: + return uuid4().hex + +class TestViewFunctions: def test_create_amt_account_link_token( self, redis_config: RedisConfig, amt_worker_id: str ): diff --git a/tests/http/test_notifications.py b/tests/http/test_notifications.py deleted file mode 100644 index 3df2423..0000000 --- a/tests/http/test_notifications.py +++ /dev/null @@ -1,115 +0,0 @@ -import json -from typing import Any -from uuid import uuid4 - -import pytest -from generalresearch.redis_helper import RedisConfig -from httpx import AsyncClient - -from jb.config import JB_EVENTS_STREAM, settings -from jb.models.assignment import AssignmentStub -from jb.models.event import MTurkEvent -from jb.models.hit import Hit - - -class TestNotifications: - - @pytest.mark.anyio - async def test_no_post_keys( - self, - httpxclient: AsyncClient, - ): - client = httpxclient - - res = await client.post(url=f"/{settings.sns_path}/", json={}) - assert res.status_code == 500 - assert res.json() == {"detail": "Invalid JSON"} - - @pytest.mark.anyio - async def test_no_post_data( - self, - httpxclient: AsyncClient, - ): - client = httpxclient - - res = await client.post(url=f"/{settings.sns_path}/") - assert res.status_code == 500 - assert res.json() == {"detail": "No POST data"} - - @pytest.mark.anyio - async def test_invalid_post_data( - self, - httpxclient: AsyncClient, - ): - client = httpxclient - - res = await client.post( - url=f"/{settings.sns_path}/", json={uuid4().hex: uuid4().hex} - ) - assert res.status_code == 500 - assert res.json() == {"detail": "Invalid JSON"} - - @pytest.mark.anyio - async def test_mturk_notifications( - self, - redis_config: RedisConfig, - httpxclient: AsyncClient, - hit_record: Hit, - assignment_stub_record: AssignmentStub, - mturk_event_body_record: dict[str, Any], - ): - client = httpxclient - redis_client = redis_config.create_redis_client() - - json_msg = json.loads(mturk_event_body_record["Message"]) - # Assert the mturk event is owned by the correct account - assert json_msg["SourceAccount"] == settings.aws_owner_id - - # Assert the HIT and Assignment are "connected" - assert assignment_stub_record.hit_id == hit_record.id - assert assignment_stub_record.amt_hit_id == hit_record.amt_hit_id - - # Assert the event body and the Assignment/HIT are "connected" - assert json_msg["Events"][0]["HITId"] == hit_record.amt_hit_id - assert json_msg["Events"][0]["HITId"] == assignment_stub_record.amt_hit_id - assert ( - json_msg["Events"][0]["AssignmentId"] - == assignment_stub_record.amt_assignment_id - ) - - # Confirm the stream is empty - assert redis_client.xlen(JB_EVENTS_STREAM) == 0 - - res = await client.post( - url=f"/{settings.sns_path}/", json=mturk_event_body_record - ) - res.raise_for_status() - - # Now that we POSTed, confirm the stream has 1 event in it - # Confirm the stream is empty - assert redis_client.xlen(JB_EVENTS_STREAM) == 1 - - # AMT SNS needs to receive a 200 response to stop retrying the notification - assert res.status_code == 200 - assert res.json() == {"status": "ok"} - - # Check that the event was enqueued in Redis - msg_res = redis_client.xread(streams={JB_EVENTS_STREAM: 0}, count=1, block=100) - msg_res = msg_res[0][1][0] - msg_id, msg = msg_res - redis_client.xdel(JB_EVENTS_STREAM, msg_id) - - # After running xdel, we can confirm the stream is empty - assert redis_client.xlen(JB_EVENTS_STREAM) == 0 - - msg_json = msg["data"] - event = MTurkEvent.model_validate_json(msg_json) - - # Confirm that the event that we got from redis is what was POSTED by - # AMT SNS. We're using the fixture() so the values are the same as - # what was in the example_mturk_event_body - assert event.event_type == "AssignmentSubmitted" - - assert event.amt_assignment_id == assignment_stub_record.amt_assignment_id - assert event.amt_hit_id == hit_record.amt_hit_id - assert event.amt_hit_type_id == hit_record.amt_hit_type_id diff --git a/tests/http/test_preview.py b/tests/http/test_preview.py deleted file mode 100644 index 39a6f5b..0000000 --- a/tests/http/test_preview.py +++ /dev/null @@ -1,71 +0,0 @@ -# There are two types of "preview" - one is where we navigate direclty -# to it and one is where we redirect possibly - -import pytest -from httpx import AsyncClient - -from jb.models.assignment import AssignmentStub -from jb.models.hit import Hit - - -class TestPreview: - - @pytest.mark.anyio - async def test_preview_direct(self, httpxclient: AsyncClient): - client = httpxclient - res = await client.get("/preview/") - - assert res.status_code == 200 - assert res.headers["content-type"] == "text/html; charset=utf-8" - assert res.num_bytes_downloaded == 507 - - assert "James Billings loves you." in res.text - assert "https://cdn.jamesbillings67.com/james-has-style.css" in res.text - assert "https://cdn.jamesbillings67.com/james-billings.js" in res.text - - @pytest.mark.anyio - async def test_preview_redirect_from_work_random_str( - self, httpxclient: AsyncClient, amt_hit_id: str, amt_assignment_id: str - ): - client = httpxclient - - """ - The redirect occurs regardless of any parameter validation. This is - because the query params will be used for record lookup on the work - page itself. - """ - - params = { - "workerId": None, - "assignmentId": amt_assignment_id, - "hitId": amt_hit_id, - } - res = await client.get("/work/", params=params) - - assert res.status_code == 302 - assert "/preview/" in res.headers["location"] - - @pytest.mark.anyio - async def test_preview_redirect_from_work_records( - self, - httpxclient: AsyncClient, - hit_record: Hit, - assignment_stub_record: AssignmentStub, - ): - client = httpxclient - - """ - The redirect occurs regardless of any parameter validation. This is - because the query params will be used for record lookup on the work - page itself. - """ - - params = { - "workerId": None, - "assignmentId": assignment_stub_record.amt_assignment_id, - "hitId": hit_record.amt_hit_id, - } - res = await client.get("/work/", params=params) - - assert res.status_code == 302 - assert "/preview/" in res.headers["location"] diff --git a/tests/http/test_wallet.py b/tests/http/test_wallet.py new file mode 100644 index 0000000..4ba5ba6 --- /dev/null +++ b/tests/http/test_wallet.py @@ -0,0 +1,96 @@ +from urllib.parse import parse_qs, urlparse +from uuid import uuid4 + +import pytest +from generalresearch.redis_helper import RedisConfig +from httpx import AsyncClient + +from jb.api.auth import SESSION_COOKIE_NAME, create_session +from jb.api.cashout_token import CASHOUT_TOKEN_PREFIX +from jb.api.magic_token import redis_token_key +from jb.dependencies import get_gr_api_manager +from jb.main import app +from jb.models.auth import User +from jb.models.wallet import PendingCashout + + +class FakeGRApiManager: + def __init__(self, user: User): + self.user = user + + def get_user(self, product_user_id: str) -> User: + assert product_user_id == self.user.product_user_id + return self.user + + +@pytest.fixture +def authenticated_client(httpxclient: AsyncClient, email: str): + user = User(email=email) + app.dependency_overrides[get_gr_api_manager] = lambda: FakeGRApiManager(user) + httpxclient.cookies.set( + SESSION_COOKIE_NAME, create_session(user.product_user_id), path="/" + ) + yield httpxclient, user + httpxclient.cookies.delete(SESSION_COOKIE_NAME, path="/") + app.dependency_overrides.pop(get_gr_api_manager, None) + + +class TestCashout: + @pytest.mark.anyio + async def test_request_requires_authentication(self, httpxclient: AsyncClient): + httpxclient.cookies.delete(SESSION_COOKIE_NAME, path="/") + + response = await httpxclient.post( + "/wallet/cashout/request/", json={"amount": 500} + ) + + assert response.status_code == 401 + + @pytest.mark.anyio + @pytest.mark.skip("needs a cashout method mocked. this wont work") + async def test_request_binds_user_and_amount_to_single_use_token( + self, + authenticated_client: tuple[AsyncClient, User], + redis_config: RedisConfig, + ): + client, user = authenticated_client + cashout_method_id = uuid4().hex + + response = await client.post( + "/wallet/cashout/request/", + json={"amount": 500, "cashout_method_id": cashout_method_id}, + ) + + assert response.status_code == 200, response.text + token = parse_qs(urlparse(response.json()["confirmation_link"]).query)["token"][ + 0 + ] + stored = redis_config.create_redis_client().get( + redis_token_key(token, CASHOUT_TOKEN_PREFIX) + ) + assert PendingCashout.model_validate_json(stored) == PendingCashout( + product_user_id=user.product_user_id, + amount=500, + cashout_method_id=cashout_method_id, + ) + + response = await client.post("/wallet/cashout/confirm/", json={"token": token}) + assert response.status_code == 204 + + response = await client.post("/wallet/cashout/confirm/", json={"token": token}) + assert response.status_code == 401 + + @pytest.mark.anyio + @pytest.mark.parametrize("amount", [0, -1, 1000]) + async def test_request_rejects_invalid_amount( + self, + authenticated_client: tuple[AsyncClient, User], + amount: int, + ): + client, _ = authenticated_client + + response = await client.post( + "/wallet/cashout/request/", json={"amount": amount} + ) + + assert response.status_code == 422 diff --git a/tests/http/test_work.py b/tests/http/test_work.py deleted file mode 100644 index 9eee15a..0000000 --- a/tests/http/test_work.py +++ /dev/null @@ -1,122 +0,0 @@ -import pytest -from httpx import AsyncClient - -from jb.managers.assignment import AssignmentManager -from jb.models.assignment import AssignmentStub -from jb.models.hit import Hit - - -class TestWork: - - @pytest.mark.anyio - async def test_work( - self, - httpxclient: AsyncClient, - hit_record: Hit, - amt_assignment_id: str, - amt_worker_id: str, - ): - - assert isinstance(hit_record.id, int) - - params = { - "workerId": amt_worker_id, - "assignmentId": amt_assignment_id, - "hitId": hit_record.amt_hit_id, - } - res = await httpxclient.get("/work/", params=params) - assert res.status_code == 200 - - @pytest.mark.anyio - async def test_work_no_hit_record( - self, - httpxclient: AsyncClient, - hit: Hit, - amt_assignment_id: str, - amt_worker_id: str, - ): - # Because no AssignmentStub record is created, and we're just using - # random strings as IDs, we should also confirm that the Hit record - # is not a saved record. - assert hit.id is None - - params = { - "workerId": amt_worker_id, - "assignmentId": amt_assignment_id, - "hitId": hit.amt_hit_id, - } - res = await httpxclient.get("/work/", params=params) - - # This either results a 302 redirect to the Preview page, - # or a 200. In previous tests, it expected a 500 but is - # unclear what that behavior was intended for, but does - # not seem to be the expected response anyway. - assert res.status_code == 200 - - @pytest.mark.anyio - async def test_work_assignment_stub_existing( - self, - httpxclient: AsyncClient, - am: AssignmentManager, - hit: Hit, - assignment_stub_record: AssignmentStub, - amt_assignment_id: str, - amt_worker_id: str, - ): - - # Because the AssignmentStub is created with a reference to the Hit, - # the Hit is actually a "Hit Record" (with a primary key), so it's - # saved in the database - assert isinstance(hit.id, int) - - # Confirm that it exists in the database before the call - res = am.get_stub_if_exists(amt_assignment_id=amt_assignment_id) - assert isinstance(res, AssignmentStub) - assert isinstance(res.id, int) - - params = { - "workerId": amt_worker_id, - "assignmentId": assignment_stub_record.amt_assignment_id, - "hitId": hit.amt_hit_id, - } - res = await httpxclient.get("/work/", params=params) - assert res.status_code == 200 - - # Confirm that it exists in the database - res = am.get_stub_if_exists(amt_assignment_id=amt_assignment_id) - assert isinstance(res, AssignmentStub) - assert isinstance(res.id, int) - - @pytest.mark.anyio - async def test_work_assignment_stub_created( - self, - httpxclient: AsyncClient, - am: AssignmentManager, - hit_record: Hit, - assignment_stub: AssignmentStub, - amt_assignment_id: str, - amt_worker_id: str, - ): - - # Confirm that it exists in the database before the call - res = am.get_stub_if_exists(amt_assignment_id=amt_assignment_id) - assert res is None - - params = { - "workerId": amt_worker_id, - "assignmentId": assignment_stub.amt_assignment_id, - "hitId": hit_record.amt_hit_id, - } - res = await httpxclient.get("/work/", params=params) - assert res.status_code == 200 - - # Confirm that it exists in the database - res = am.get_stub_if_exists(amt_assignment_id=amt_assignment_id) - # assert isinstance(res, AssignmentStub) - # assert isinstance(res.id, int) - - # As of Sep 10th, 2026 - I don't see any logic where the /work/ - # would go ahead and create the Assignment Stub. Maybe it was moved - # somewhere else, but it would continue to be None as the /work/ - # page only returns back the template or a redirect.. - Max - assert res is None diff --git a/tests/managers/__init__.py b/tests/managers/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/managers/test_amt.py b/tests/managers/test_amt.py deleted file mode 100644 index 6a944a8..0000000 --- a/tests/managers/test_amt.py +++ /dev/null @@ -1,132 +0,0 @@ -from mypy_boto3_mturk import MTurkClient -from mypy_boto3_mturk.type_defs import ( - GetAccountBalanceResponseTypeDef, - ListHITsResponseTypeDef, -) - -from jb.managers.amt import AMTManager -from jb.managers.hit import HitManager, HitTypeManager -from jb.models.hit import HitQuestion, HitType - -# from jb.decorators import HM -# from jb.flow.tasks import refill_hits, check_stale_hits, check_expired_hits - - -# def test_refill_hits( -# set_hit_types_in_db_min_active_0, -# hit_type_record, expire_all_hits, amt_manager -# ): - -# assert HM.get_active_count(hit_type_in_db.id) == 0 -# assert hit_type_in_db.min_active > 0 -# refill_hits() -# assert HM.get_active_count(hit_type_in_db.id) == hit_type_in_db.min_active - -# amt_hit_ids = HM.filter_active_ids(hit_type_id=hit_type_in_db.id) -# amt_hit_id = list(amt_hit_ids)[0] -# hit, _ = amt_manager.get_hit_if_exists(amt_hit_id=amt_hit_id) -# assert hit - - -# def test_check_stale_hits(): -# # todo: I'd have to create some purposely stale hits. -# # just make sure it runs for now -# check_stale_hits() - - -# def test_check_expired_hits(): -# # todo: I'd have to create some purposely expired hits. -# # just make sure it runs for now -# check_expired_hits() - - -class TestMTurkClient: - - def test_sandbox(self, amt_client: MTurkClient): - assert amt_client is not None - assert "mturk-requester-sandbox" in amt_client.meta.endpoint_url - - balance: GetAccountBalanceResponseTypeDef = amt_client.get_account_balance() - assert balance["AvailableBalance"] == "10000.00" - - # def test_create_hit() - - # def test_delete_hit() - - # def test_get_assignment(self, amt_client: MTurkClient): - - # def test_get_hit - - def test_list_hits(self, amt_client: MTurkClient, amtm: AMTManager): - res: ListHITsResponseTypeDef = amt_client.list_hits() - assert isinstance(res, dict) - assert isinstance(res.get("HITs"), list) - - for hit in res["HITs"]: - assert isinstance(hit, dict) - - assert isinstance(hit.get("HITId"), str) - assert isinstance(hit.get("HITTypeId"), str) - assert isinstance(hit.get("HITGroupId"), str) - assert isinstance(hit.get("Title"), str) - - # botocore.errorfactory.RequestError: An error occurred - # (RequestError) when calling the DeleteHIT operation: This - # HIT is currently in the state 'Assignable'. This operation - # can be called with a status of: Reviewing, Reviewable - # (1772146028380 s) - - if hit["HITStatus"] in ["Reviewing", "Reviewable"]: - print( - f"Deleting HIT with id {hit['HITId']} and status {hit['HITStatus']}" - ) - amt_client.delete_hit(HITId=hit["HITId"]) - - elif hit["HITStatus"] == "Assignable": - # I don't know... just let expire I guess. To changes it's - # status you need to do manage Assignments (I think) - - # amt_client.update_hit_review_status(HITId=hit["HITId"], Revert=True) - pass - - amtm.expire_all_hits() - - -class TestAMTManager: - - def test_create_hit_type(self, amtm: AMTManager, hit_type: HitType): - assert hit_type.amt_hit_type_id is None - - amtm.create_hit_type(hit_type=hit_type) - assert hit_type.amt_hit_type_id is not None - - def test_create_hit_with_hit_type( - self, - amtm: AMTManager, - htm: HitTypeManager, - hm: HitManager, - hit_type_record_with_amt_id: HitType, - question_record: HitQuestion, - ): - - sandbox_amt_hit_type_id = hit_type_record_with_amt_id.amt_hit_type_id - assert isinstance(sandbox_amt_hit_type_id, str) - - res = [ - x - for x in htm.filter_active() - if x.amt_hit_type_id == sandbox_amt_hit_type_id - ] - - assert len(res) == 1 - sandbox_hit_type = res[0] - - hit = amtm.create_hit_with_hit_type( - hit_type=sandbox_hit_type, question=question_record - ) - - assert hit.amt_hit_id is not None - assert hit.id is None - - hm.create(hit) - assert hit.id is not None diff --git a/tests/managers/test_hit.py b/tests/managers/test_hit.py deleted file mode 100644 index 7227e4d..0000000 --- a/tests/managers/test_hit.py +++ /dev/null @@ -1,50 +0,0 @@ -from jb.managers.hit import HitManager, HitTypeManager -from jb.models.hit import Hit, HitQuestion, HitType - - -class TestHitQuestionManager: - - def test_base(self, question_record: HitQuestion): - assert isinstance(question_record, HitQuestion) - assert isinstance(question_record.id, int) - - -class TestHitTypeManager: - - def test_create(self, htm: HitTypeManager, hit_type_record: HitType): - - assert isinstance(hit_type_record.id, int) - assert isinstance(hit_type_record.amt_hit_type_id, str) - - count1 = len(htm.filter_active()) - # assert count1 == 1 - - hit_type_record.min_active = 0 - htm.set_min_active(hit_type=hit_type_record) - - count2 = len(htm.filter_active()) - assert count1 - 1 == count2 - - -class TestHitManager: - - # def test_create - - # def update_status - - # def update_hit - - # def get_from_amt_id - - # get_from_amt_id_if_exists - - def test_get_active_count(self, hm: HitManager, hit_record: Hit): - - count = hm.get_active_count(hit_type_id=999_999_999) - assert isinstance(count, int) - assert count == 0 - - count = hm.get_active_count(hit_type_id=hit_record.hit_type_id) - assert count == 1 - - # filter_active_ids diff --git a/tests/models/__init__.py b/tests/models/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/models/test_assignment.py b/tests/models/test_assignment.py deleted file mode 100644 index ecaafe9..0000000 --- a/tests/models/test_assignment.py +++ /dev/null @@ -1,42 +0,0 @@ -from mypy_boto3_mturk.type_defs import ( - GetAssignmentResponseTypeDef, -) - -from jb.models.assignment import Assignment, AssignmentStub - - -class TestAssignmentStub: - - def test_init(self, assignment_stub: AssignmentStub): - assert isinstance(assignment_stub, AssignmentStub) - assert assignment_stub.id is None - - def test_record(self, assignment_stub_record: AssignmentStub): - assert isinstance(assignment_stub_record, AssignmentStub) - assert assignment_stub_record.id is not None - - -class TestAssignment: - - def test_get_assignment(self, assignment_record: Assignment): - assert isinstance(assignment_record, Assignment) - assert assignment_record.id is not None - - -class TestAssignmentResponse: - - def test_get_assignment_no_tsid( - self, assignment_response_no_tsid: GetAssignmentResponseTypeDef - ): - assignment = Assignment.from_amt_get_assignment( - assignment_response_no_tsid["Assignment"] - ) - assert assignment.tsid is None - - def test_get_assignment_bad_tsid( - self, assignment_response_bad_tsid: GetAssignmentResponseTypeDef - ): - assignment = Assignment.from_amt_get_assignment( - assignment_response_bad_tsid["Assignment"] - ) - assert assignment.tsid is None diff --git a/tests/models/test_event.py b/tests/models/test_event.py deleted file mode 100644 index a4c591d..0000000 --- a/tests/models/test_event.py +++ /dev/null @@ -1,12 +0,0 @@ -import pytest - -from jb.models.event import MTurkEvent - - -class TestMTurkEvent: - - @pytest.mark.anyio - def test_init(self, mturk_event: MTurkEvent): - assert isinstance(mturk_event, MTurkEvent) - assert isinstance(mturk_event.amt_assignment_id, str) - assert isinstance(mturk_event.amt_hit_type_id, str) diff --git a/tests/models/test_hit.py b/tests/models/test_hit.py deleted file mode 100644 index aa48f00..0000000 --- a/tests/models/test_hit.py +++ /dev/null @@ -1,11 +0,0 @@ -import pytest - -from jb.models.hit import Hit - - -class TestAssignment: - - @pytest.mark.anyio - def test_get_assignment(self, hit_record: Hit): - assert isinstance(hit_record, Hit) - assert hit_record.id is not None diff --git a/tests/test_postgres.py b/tests/test_postgres.py deleted file mode 100644 index 244c132..0000000 --- a/tests/test_postgres.py +++ /dev/null @@ -1,69 +0,0 @@ -import socket -import subprocess -from collections.abc import Callable -from typing import TYPE_CHECKING - -from generalresearch.pg_helper import PostgresConfig -from pydantic import PostgresDsn - -if TYPE_CHECKING: - from generalresearch.models.custom_types import InternalHostname, PostgresDict - - -def is_port_open(host: InternalHostname, port: int = 5432, timeout: int = 3): - try: - with socket.create_connection((host, port), timeout=timeout): - return True - except (TimeoutError, ConnectionRefusedError, OSError): - return False - - -def can_ping(host: InternalHostname): - return ( - subprocess.call( - ["ping", "-c", "1", str(host)], - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - ) - == 0 - ) - - -class TestPostgresDSN: - - def test_ping(self, postgres_instance_host: InternalHostname): - assert can_ping(host=postgres_instance_host) - - def test_port(self, postgres_instance_host: InternalHostname): - assert is_port_open(host=postgres_instance_host) - - def test_conn(self, postgres_instance: PostgresDsn): - config = PostgresConfig( - dsn=postgres_instance, - connect_timeout=1, - statement_timeout=1, - ) - res = config.execute_sql_query(query="SELECT 1;") - assert len(res) == 1 - - -class TestPostgresDjangoCreation: - - def test_ping(self, postgres_instance_dict: PostgresDict): - assert can_ping(host=postgres_instance_dict["host"]) - - def test_django_creation( - self, - django_db_factory: Callable[..., None], - ): - dsn = django_db_factory() - assert isinstance(dsn, PostgresDsn) - - def test_django_tables(self, pg_config: PostgresConfig): - res = pg_config.execute_sql_query(query=""" - SELECT COUNT(*) - FROM information_schema.tables - WHERE table_schema = 'public'; - """) - assert len(res) == 1 - assert res[0]["count"] == 7 -- cgit v1.2.3