Increase test coverage of Habitica (#150671)

This commit is contained in:
Manu
2025-08-15 05:45:42 +02:00
committed by GitHub
parent 9f36b2dcde
commit a785f3d509
2 changed files with 159 additions and 2 deletions

View File

@@ -0,0 +1,74 @@
{
"success": true,
"data": {
"leaderOnly": {
"challenges": false,
"getGems": false
},
"quest": {
"progress": {
"collect": {},
"hp": 100
},
"key": "dustbunnies",
"active": true,
"leader": "d69833ef-4542-4259-ba50-9b4a1a841bcf",
"members": {
"d69833ef-4542-4259-ba50-9b4a1a841bcf": true
},
"extra": {}
},
"tasksOrder": {
"habits": [],
"dailys": [],
"todos": [],
"rewards": []
},
"purchased": {
"plan": {
"consecutive": {
"count": 0,
"offset": 0,
"gemCapExtra": 0,
"trinkets": 0
},
"quantity": 1,
"extraMonths": 0,
"gemsBought": 0,
"cumulativeCount": 0,
"mysteryItems": []
}
},
"cron": {},
"_id": "1e87097c-4c03-4f8c-a475-67cc7da7f409",
"name": "test-user's Party",
"type": "party",
"privacy": "private",
"chat": [],
"memberCount": 2,
"challengeCount": 0,
"balance": 0,
"managers": {},
"categories": [],
"leader": {
"auth": {
"local": {
"username": "test-username"
}
},
"flags": {
"verifiedUsername": true
},
"profile": {
"name": "test-user"
},
"_id": "af36e2a8-7927-4dec-a258-400ade7f0ae3",
"id": "af36e2a8-7927-4dec-a258-400ade7f0ae3"
},
"summary": "test-user's Party",
"id": "1e87097c-4c03-4f8c-a475-67cc7da7f409"
},
"notifications": [],
"userV": 0,
"appVersion": "5.38.0"
}

View File

@@ -8,12 +8,13 @@ import sys
from unittest.mock import AsyncMock, patch
from freezegun.api import FrozenDateTimeFactory
from habiticalib import HabiticaUserResponse
from habiticalib import HabiticaGroupsResponse, HabiticaUserResponse
import pytest
import respx
from syrupy.assertion import SnapshotAssertion
from syrupy.extensions.image import PNGImageSnapshotExtension
from homeassistant.components.habitica.const import DOMAIN
from homeassistant.components.habitica.const import ASSETS_URL, DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
@@ -97,3 +98,85 @@ async def test_image_platform(
assert (await resp.read()) == snapshot(
extension_class=PNGImageSnapshotExtension
)
@pytest.mark.usefixtures("habitica")
@respx.mock
async def test_load_image_from_url(
hass: HomeAssistant,
config_entry: MockConfigEntry,
habitica: AsyncMock,
hass_client: ClientSessionGenerator,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test loading of image from URL."""
freezer.move_to("2024-09-20T22:00:00.000")
call1 = respx.get(f"{ASSETS_URL}quest_atom1.png").respond(content=b"\x89PNG")
call2 = respx.get(f"{ASSETS_URL}quest_dustbunnies.png").respond(content=b"\x89PNG")
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
assert (state := hass.states.get("image.test_user_s_party_quest"))
assert state.state == "2024-09-20T22:00:00+00:00"
client = await hass_client()
resp = await client.get(state.attributes["entity_picture"])
assert resp.status == HTTPStatus.OK
assert (await resp.read()) == b"\x89PNG"
assert call1.call_count == 1
habitica.get_group.return_value = HabiticaGroupsResponse.from_json(
await async_load_fixture(hass, "party_2.json", DOMAIN)
)
freezer.tick(timedelta(minutes=15))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert (state := hass.states.get("image.test_user_s_party_quest"))
assert state.state == "2024-09-20T22:15:00+00:00"
client = await hass_client()
resp = await client.get(state.attributes["entity_picture"])
assert resp.status == HTTPStatus.OK
assert (await resp.read()) == b"\x89PNG"
assert call2.call_count == 1
@pytest.mark.usefixtures("habitica")
@respx.mock
async def test_load_image_not_found(
hass: HomeAssistant,
config_entry: MockConfigEntry,
hass_client: ClientSessionGenerator,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test NotFound error."""
freezer.move_to("2024-09-20T22:00:00.000")
call1 = respx.get(f"{ASSETS_URL}quest_atom1.png").respond(status_code=404)
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
assert (state := hass.states.get("image.test_user_s_party_quest"))
assert state.state == "2024-09-20T22:00:00+00:00"
client = await hass_client()
resp = await client.get(state.attributes["entity_picture"])
assert resp.status == HTTPStatus.INTERNAL_SERVER_ERROR
assert call1.call_count == 1