aboutsummaryrefslogtreecommitdiff
path: root/tests/http/test_status.py
blob: d88ff658462fa70ec8c66efad778552bcb494031 (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
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}