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()
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()
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()
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()
async def test_guild_create_object(hypixel_client: Hypixel) -> None: """Test to check the guild_create_object method returns correct data.""" data = hypixel_client._create_guild_object({ "success": True, "guild": { "_id": "52e57a1c0cf2e250d1cd00f8", "created": 1390770716373, "name": "The Sloths", "name_lower": "the sloths", "description": "The sloths", "tag": "SLOTH", "tagColor": "DARK_AQUA", "exp": 2238673, "members": [{ "uuid": "f7c77d999f154a66a87dc4a51ef30d19", "rank": "GUILDMASTER", "joined": 1390770716373, "expHistory": { "2020-05-25": 108, "2020-05-24": 404 }, "questParticipation": 4, "mutedTill": 1399507406038, }], "achievements": { "ONLINE_PLAYERS": 4, "EXPERIENCE_KINGS": 40062, "WINNERS": 2, }, "ranks": [{ "name": "Member", "default": True, "created": 1, "priority": 1, "tag": "Member", }], "joinable": True, "legacyRanking": 10446, "publiclyListed": False, "hideGmTag": True, "preferredGames": ["ARCADE", "SPEED_UHC", "UHC"], "chatMute": 1590703490783, "guildExpByGameType": { "TNTGAMES": 1312, "VAMPIREZ": 4495, "ARCADE": 10285, }, "banner": { "Base": "0", "Patterns": [ { "Pattern": "vhr", "Color": "15" }, { "Pattern": "vh", "Color": "15" }, ], }, }, }) assert data.id == "52e57a1c0cf2e250d1cd00f8" assert data.created == datetime.datetime.fromtimestamp( 1390770716.373, tz=datetime.timezone.utc) assert data.name == "The Sloths" assert data.name_lower == "the sloths" assert data.description == "The sloths" assert data.tag == "SLOTH" assert data.tag_color == "DARK_AQUA" assert data.exp == 2238673 assert len(data.members) == 1 assert data.members[0].uuid == uuid.UUID( "f7c77d999f154a66a87dc4a51ef30d19") assert data.members[0].rank == "GUILDMASTER" assert data.members[0].joined == datetime.datetime.fromtimestamp( 1390770716.373, tz=datetime.timezone.utc) assert data.members[0].exp_history == { "2020-05-25": 108, "2020-05-24": 404 } assert data.members[0].quest_participation == 4 assert data.members[0].muted_till == datetime.datetime.fromtimestamp( 1399507406.038, tz=datetime.timezone.utc) assert data.achievements == { "ONLINE_PLAYERS": 4, "EXPERIENCE_KINGS": 40062, "WINNERS": 2, } assert len(data.ranks) == 1 assert data.ranks[0].name == "Member" assert data.ranks[0].default is True assert data.ranks[0].created == 1 assert data.ranks[0].priority == 1 assert data.ranks[0].tag == "Member" assert data.joinable is True assert data.legacy_ranking == 10446 assert data.publicly_listed is False assert data.hide_gm_tag is True assert data.preferred_games == ["ARCADE", "SPEED_UHC", "UHC"] assert data.chat_mute == datetime.datetime.fromtimestamp( 1590703490.783, tz=datetime.timezone.utc) assert data.guild_exp_by_game_type == { "TNTGAMES": 1312, "VAMPIREZ": 4495, "ARCADE": 10285, } assert data.banner.base == 0 assert len(data.banner.patterns) == 2 assert data.banner.patterns[0].pattern == "vhr" assert data.banner.patterns[0].color == 15 assert data.banner.patterns[1].pattern == "vh" assert data.banner.patterns[1].color == 15
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()
async def hypixel_client_no_key() -> AsyncIterator[Hypixel]: """hypixel_client with no key.""" hypixel = Hypixel() yield hypixel await hypixel.close()
async def hypixel_client(key: uuid.UUID) -> AsyncIterator[Hypixel]: """Hypixel client fixture.""" hypixel = Hypixel(api_key=str(key)) yield hypixel await hypixel.close()
async def test_no_key() -> None: """Test no key.""" client = Hypixel() with pytest.raises(InvalidApiKey): await client.key_data() await client.close()