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/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 --------------------------------------- 6 files changed, 108 insertions(+), 317 deletions(-) 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 (limited to 'tests/http') 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 -- cgit v1.2.3