aboutsummaryrefslogtreecommitdiff
path: root/tests/http/test_statuses.py
blob: ffc98fd5684932fca4e8c303ece735cf76145ccc (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
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},
    ]