aboutsummaryrefslogtreecommitdiff
path: root/tests/http
diff options
context:
space:
mode:
authorMax Nanis2026-02-24 17:26:15 -0500
committerMax Nanis2026-02-24 17:26:15 -0500
commit8c1940445503fd6678d0961600f2be81622793a2 (patch)
treeb9173562b8824b5eaa805e446d9d780e1f23fb2a /tests/http
parent25d8c3c214baf10f6520cc1351f78473150e5d7a (diff)
downloadamt-jb-8c1940445503fd6678d0961600f2be81622793a2.tar.gz
amt-jb-8c1940445503fd6678d0961600f2be81622793a2.zip
Extensive use of type checking. Movement of pytest conf towards handling managers (for db agnostic unittest). Starting to organize pytests.
Diffstat (limited to 'tests/http')
-rw-r--r--tests/http/test_basic.py11
-rw-r--r--tests/http/test_notifications.py16
-rw-r--r--tests/http/test_preview.py41
-rw-r--r--tests/http/test_report.py0
-rw-r--r--tests/http/test_status.py78
-rw-r--r--tests/http/test_statuses.py102
-rw-r--r--tests/http/test_work.py38
7 files changed, 68 insertions, 218 deletions
diff --git a/tests/http/test_basic.py b/tests/http/test_basic.py
index 7b03a1e..18359da 100644
--- a/tests/http/test_basic.py
+++ b/tests/http/test_basic.py
@@ -22,14 +22,3 @@ async def test_static_file_alias(httpxclient: AsyncClient):
res = await client.get(p)
assert res.status_code == 200, p
assert res.json() == {}
-
-
-@pytest.mark.anyio
-async def test_health(httpxclient: AsyncClient):
- client = httpxclient
- """
- These are here for site crawlers and stuff..
- """
- res = await client.get("/health/")
- assert res.status_code == 200
- assert res.json() == {}
diff --git a/tests/http/test_notifications.py b/tests/http/test_notifications.py
index 70458b8..6770044 100644
--- a/tests/http/test_notifications.py
+++ b/tests/http/test_notifications.py
@@ -5,7 +5,6 @@ from httpx import AsyncClient
import secrets
from jb.config import JB_EVENTS_STREAM, settings
-from jb.decorators import REDIS
from jb.models.event import MTurkEvent
from tests import generate_amt_id
@@ -40,16 +39,17 @@ def example_mturk_event_body(amt_hit_id, amt_hit_type_id, amt_assignment_id):
@pytest.fixture()
-def clean_mturk_events_redis_stream():
- REDIS.xtrim(JB_EVENTS_STREAM, maxlen=0)
- assert REDIS.xlen(JB_EVENTS_STREAM) == 0
+def clean_mturk_events_redis_stream(redis):
+ redis.xtrim(JB_EVENTS_STREAM, maxlen=0)
+ assert redis.xlen(JB_EVENTS_STREAM) == 0
yield
- REDIS.xtrim(JB_EVENTS_STREAM, maxlen=0)
- assert REDIS.xlen(JB_EVENTS_STREAM) == 0
+ redis.xtrim(JB_EVENTS_STREAM, maxlen=0)
+ assert redis.xlen(JB_EVENTS_STREAM) == 0
@pytest.mark.anyio
async def test_mturk_notifications(
+ redis,
httpxclient: AsyncClient,
no_limit,
example_mturk_event_body,
@@ -61,10 +61,10 @@ async def test_mturk_notifications(
res = await client.post(url=f"/{settings.sns_path}/", json=example_mturk_event_body)
res.raise_for_status()
- msg_res = REDIS.xread(streams={JB_EVENTS_STREAM: 0}, count=1, block=100)
+ msg_res = redis.xread(streams={JB_EVENTS_STREAM: 0}, count=1, block=100)
msg_res = msg_res[0][1][0]
msg_id, msg = msg_res
- REDIS.xdel(JB_EVENTS_STREAM, msg_id)
+ redis.xdel(JB_EVENTS_STREAM, msg_id)
msg_json = msg["data"]
event = MTurkEvent.model_validate_json(msg_json)
diff --git a/tests/http/test_preview.py b/tests/http/test_preview.py
new file mode 100644
index 0000000..2bdf265
--- /dev/null
+++ b/tests/http/test_preview.py
@@ -0,0 +1,41 @@
+# 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.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
+ # the response is an html page
+
+ 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(
+ self, httpxclient: AsyncClient, amt_hit_id, amt_assignment_id
+ ):
+ client = httpxclient
+
+ 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"]
diff --git a/tests/http/test_report.py b/tests/http/test_report.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/http/test_report.py
diff --git a/tests/http/test_status.py b/tests/http/test_status.py
deleted file mode 100644
index d88ff65..0000000
--- a/tests/http/test_status.py
+++ /dev/null
@@ -1,78 +0,0 @@
-from uuid import uuid4
-
-import pytest
-from httpx import AsyncClient
-
-from jb.config import settings
-from tests import generate_amt_id
-
-
-@pytest.mark.anyio
-async def test_get_status_args(httpxclient: AsyncClient, no_limit):
- client = httpxclient
-
- # tsid misformatted
- res = await client.get(f"/status/{uuid4().hex[:-1]}/")
- assert res.status_code == 422
- assert "String should have at least 32 characters" in res.text
-
-
-@pytest.mark.anyio
-async def test_get_status_error(httpxclient: AsyncClient, no_limit):
- # Expects settings.fsb_host to point to a functional thl-fsb
- client = httpxclient
-
- # tsid doesn't exist
- res = await client.get(f"/status/{uuid4().hex}/")
- assert res.status_code == 502
- assert res.json()["detail"] == "Failed to fetch status"
-
-
-@pytest.mark.anyio
-async def test_get_status_complete(httpxclient: AsyncClient, no_limit, mock_requests):
- client = httpxclient
-
- tsid = uuid4().hex
- url = f"{settings.fsb_host}{settings.product_id}/status/{tsid}/"
-
- mock_response = {
- "tsid": tsid,
- "product_id": settings.product_id,
- "bpuid": generate_amt_id(length=21),
- "started": "2022-06-29T23:43:48.247777Z",
- "finished": "2022-06-29T23:56:57.632634Z",
- "status": 3,
- "payout": 81,
- "user_payout": 77,
- "payout_format": "${payout/100:.2f}",
- "user_payout_string": "$0.77",
- "kwargs": {},
- }
- mock_requests.get(url, json=mock_response, status_code=200)
- res = await client.get(f"/status/{tsid}/")
- assert res.status_code == 200
- assert res.json() == {"status": 3, "payout": "$0.77"}
-
-
-@pytest.mark.anyio
-async def test_get_status_failure(httpxclient: AsyncClient, no_limit, mock_requests):
- client = httpxclient
-
- tsid = uuid4().hex
- url = f"{settings.fsb_host}{settings.product_id}/status/{tsid}/"
-
- mock_response = {
- "tsid": tsid,
- "product_id": settings.product_id,
- "bpuid": "123ABC",
- "status": 2,
- "payout": 0,
- "user_payout": 0,
- "payout_format": "${payout/100:.2f}",
- "user_payout_string": None,
- "kwargs": {},
- }
- mock_requests.get(url, json=mock_response, status_code=200)
- res = await client.get(f"/status/{tsid}/")
- assert res.status_code == 200
- assert res.json() == {"status": 2, "payout": None}
diff --git a/tests/http/test_statuses.py b/tests/http/test_statuses.py
deleted file mode 100644
index ffc98fd..0000000
--- a/tests/http/test_statuses.py
+++ /dev/null
@@ -1,102 +0,0 @@
-from datetime import datetime, timezone, timedelta
-from urllib.parse import urlencode
-
-import pytest
-from uuid import uuid4
-from httpx import AsyncClient
-
-from jb.config import settings
-
-
-@pytest.mark.anyio
-async def test_get_statuses(httpxclient: AsyncClient, no_limit, amt_worker_id):
- # Expects settings.fsb_host to point to a functional thl-fsb
- client = httpxclient
- now = datetime.now(tz=timezone.utc)
-
- params = {"worker_id": amt_worker_id}
- res = await client.get(f"/statuses/", params=params)
- assert res.status_code == 200
- assert res.json() == []
-
- params = {"worker_id": amt_worker_id, "started_after": now.isoformat()}
- res = await client.get(f"/statuses/", params=params)
- assert res.status_code == 422
- assert "Input should be a valid integer" in res.text
-
-
-@pytest.fixture
-def fsb_get_statuses_example_response(amt_worker_id, tsid1, tsid2):
- return {
- "tasks_status": [
- {
- "tsid": tsid1,
- "product_id": settings.product_id,
- "bpuid": amt_worker_id,
- "started": "2025-06-12T03:27:24.902280Z",
- "finished": "2025-06-12T03:29:37.626481Z",
- "status": 2,
- "payout": 0,
- "user_payout": None,
- "payout_format": None,
- "user_payout_string": None,
- "kwargs": {},
- "status_code_1": "SESSION_START_QUALITY_FAIL",
- "status_code_2": "ENTRY_URL_MODIFICATION",
- },
- {
- "tsid": tsid2,
- "product_id": settings.product_id,
- "bpuid": amt_worker_id,
- "started": "2025-06-12T03:30:18.176826Z",
- "finished": "2025-06-12T03:36:58.789059Z",
- "status": 2,
- "payout": 0,
- "user_payout": None,
- "payout_format": None,
- "user_payout_string": None,
- "kwargs": {},
- "status_code_1": "BUYER_QUALITY_FAIL",
- "status_code_2": None,
- },
- ]
- }
-
-
-@pytest.mark.anyio
-async def test_get_statuses_mock(
- httpxclient: AsyncClient,
- no_limit,
- amt_worker_id,
- mock_requests,
- fsb_get_statuses_example_response,
- tsid1,
- tsid2,
-):
- client = httpxclient
- now = datetime.now(tz=timezone.utc)
- started_after = now - timedelta(minutes=5)
-
- # The fsb call we are mocking ------v
- params = {
- "bpuid": amt_worker_id,
- "started_after": round(started_after.timestamp()),
- "started_before": round(now.timestamp()),
- }
- url = f"{settings.fsb_host}{settings.product_id}/status/" + "?" + urlencode(params)
- mock_requests.get(url, json=fsb_get_statuses_example_response, status_code=200)
- # ---- end mock
-
- params = {
- "worker_id": amt_worker_id,
- "started_after": round(started_after.timestamp()),
- "started_before": round(now.timestamp()),
- }
- result = await client.get(f"/statuses/", params=params)
- assert result.status_code == 200
- res = result.json()
- assert len(res) == 2
- assert res == [
- {"status": 2, "tsid": tsid1},
- {"status": 2, "tsid": tsid2},
- ]
diff --git a/tests/http/test_work.py b/tests/http/test_work.py
index 59b8830..c69118b 100644
--- a/tests/http/test_work.py
+++ b/tests/http/test_work.py
@@ -1,24 +1,24 @@
import pytest
from httpx import AsyncClient
-from jb.models.hit import Hit
+class TestWork:
-@pytest.mark.skip(reason="hits live api, need to look at this")
-async def test_work(
- httpxclient: AsyncClient,
- no_limit,
- amt_worker_id,
- amt_assignment_id,
- hit_in_amt: Hit,
-):
- client = httpxclient
- params = {
- "workerId": amt_worker_id,
- "assignmentId": amt_assignment_id,
- "hitId": hit_in_amt.amt_hit_id,
- }
- res = await client.get(f"/work/", params=params)
- assert res.status_code == 200
- # the response is an html page
- assert res.headers["content-type"] == "text/html; charset=utf-8"
+ @pytest.mark.anyio
+ async def test_work(
+ self,
+ httpxclient: AsyncClient,
+ hit_record,
+ amt_assignment_id,
+ amt_worker_id,
+ ):
+ client = httpxclient
+
+ params = {
+ "workerId": amt_worker_id,
+ "assignmentId": amt_assignment_id,
+ "hitId": hit_record.amt_hit_id,
+ }
+ res = await client.get("/work/", params=params)
+
+ assert res.status_code == 200