예제 #1
0
async def test_added_key() -> None:
    """Test added key."""
    key = generate_key()
    with aioresponses() as m:
        m.get(
            f"https://api.hypixel.net/key?key={str(key)}",
            status=200,
            headers={
                "RateLimit-Limit": "120",
                "RateLimit-Remaining": "119",
                "RateLimit-Reset": "8",
            },
            payload={
                "success": True,
                "record": {
                    "key": str(key),
                    "owner": "8ffb79fa-620e-45fe-8d62-381abd5bc60f",
                    "limit": 120,
                    "queriesInPastMin": 0,
                    "totalQueries": 849,
                },
            },
        )
        client = Hypixel(api_key=str(key))
        data = await client.key_data(str(key))

        assert data.key == key
        assert data.owner == uuid.UUID("8ffb79fa-620e-45fe-8d62-381abd5bc60f")
        assert data.limit == 120
        assert data.queries_in_past_min == 0
        assert data.total_queries == 849
        await client.close()
예제 #2
0
async def test_api_error() -> None:
    """Test bazaar."""
    key = generate_key()
    with aioresponses() as m:
        m.get(
            f"https://api.hypixel.net/test?key={str(key)}",
            status=502,
            payload={"success": False, "cause": "Error"},
        )
        client = Hypixel(api_key=str(key))
        with pytest.raises(ApiNoSuccess) as e:
            await client._get("test")
            assert str(e) == "The test endpoint encounted an error on the hypixel side."
        await client.close()
예제 #3
0
async def test_invalid_key() -> None:
    """Test bazaar."""
    key = generate_key()
    with aioresponses() as m:
        m.get(
            f"https://api.hypixel.net/test?key={str(key)}",
            status=400,
            payload={"success": False, "cause": "Invalid API key"},
        )
        client = Hypixel(api_key=str(key))
        with pytest.raises(InvalidApiKey) as e:
            await client._get("test")
            assert str(e) == "The hypixel API ratelimit was reached!"
        await client.close()
예제 #4
0
async def test_rate_limit() -> None:
    """Test bazaar."""
    key = generate_key()
    with aioresponses() as m:
        m.get(
            f"https://api.hypixel.net/test?key={str(key)}",
            status=429,
            headers={"Retry-After": "5"},
            payload={"success": False, "cause": "Key throttle!", "throttle": True},
        )
        client = Hypixel(api_key=str(key))
        with pytest.raises(RateLimitError) as e:
            await client._get("test")
            assert str(e) == "Entered API key is not valid"
        await client.close()
예제 #5
0
async def test_boosters() -> None:
    """Test to check the boosters method returns correct data."""
    key = generate_key()
    purchaseruuid1 = generate_key()
    purchaseruuid2 = generate_key()
    stacked1 = generate_key()
    stacked2 = generate_key()
    with aioresponses() as m:
        m.get(
            f"https://api.hypixel.net/boosters?key={str(key)}",
            status=200,
            headers={
                "RateLimit-Limit": "120",
                "RateLimit-Remaining": "119",
                "RateLimit-Reset": "8",
            },
            payload={
                "success":
                True,
                "boosters": [
                    {
                        "_id": "5c197fadc8f245280926413d",
                        "purchaserUuid": str(purchaseruuid1),
                        "amount": 2,
                        "originalLength": 3600,
                        "length": 3595,
                        "gameType": 24,
                        "dateActivated": 1545244519133,
                        "stacked": True,
                    },
                    {
                        "_id": "5de1bf590cf2544cd01a7e04",
                        "purchaserUuid": str(purchaseruuid2),
                        "amount": 2.2,
                        "originalLength": 3600,
                        "length": 2074,
                        "gameType": 13,
                        "dateActivated": 1590842991659,
                        "stacked": [
                            str(stacked1),
                            str(stacked2),
                        ],
                    },
                ],
                "boosterState": {
                    "decrementing": True
                },
            },
        )
        client = Hypixel(api_key=str(key))
        data = await client.boosters()

        assert len(data.boosters) == 2
        assert data.booster_state_decrementing is True

        assert data.boosters[0].id == "5c197fadc8f245280926413d"
        assert data.boosters[0].purchaser_uuid == purchaseruuid1
        assert data.boosters[0].amount == 2.0
        assert data.boosters[0].original_length == 3600
        assert data.boosters[0].length == 3595
        assert data.boosters[0].game_type == gametype("SUPER_SMASH",
                                                      "SuperSmash",
                                                      "Smash Heroes", 24)
        assert data.boosters[
            0].date_activated == datetime.datetime.fromtimestamp(
                1545244519.133, tz=datetime.timezone.utc)
        assert data.boosters[0].stacked is True

        assert data.boosters[1].id == "5de1bf590cf2544cd01a7e04"
        assert data.boosters[1].purchaser_uuid == purchaseruuid2
        assert data.boosters[1].amount == 2.2
        assert data.boosters[1].original_length == 3600
        assert data.boosters[1].length == 2074
        assert data.boosters[1].game_type == gametype("WALLS3", "Walls3",
                                                      "Mega Walls", 13)
        assert data.boosters[
            1].date_activated == datetime.datetime.fromtimestamp(
                1590842991.659, tz=datetime.timezone.utc)
        assert data.boosters[1].stacked == [
            stacked1,
            stacked2,
        ]
        await client.close()