aboutsummaryrefslogtreecommitdiff
path: root/tests/http/test_work.py
blob: 9eee15a36bf54f8c53d70c9481837386372dfb59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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