Change how subscription information is fetched (#148337)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Joakim Sørensen
2025-07-08 06:52:41 +02:00
committed by GitHub
co-authored by Franck Nijhof
parent dcf8d7f74d
commit 7a7e16bbb6
5 changed files with 27 additions and 26 deletions
+5 -1
View File
@@ -5,7 +5,7 @@ from pathlib import Path
from typing import Any
from unittest.mock import DEFAULT, AsyncMock, MagicMock, PropertyMock, patch
from hass_nabucasa import Cloud
from hass_nabucasa import Cloud, payments_api
from hass_nabucasa.auth import CognitoAuth
from hass_nabucasa.cloudhooks import Cloudhooks
from hass_nabucasa.const import DEFAULT_SERVERS, DEFAULT_VALUES, STATE_CONNECTED
@@ -71,6 +71,10 @@ async def cloud_fixture() -> AsyncGenerator[MagicMock]:
mock_cloud.voice = MagicMock(spec=Voice)
mock_cloud.files = MagicMock(spec=Files)
mock_cloud.started = None
mock_cloud.payments = MagicMock(
spec=payments_api.PaymentsApi,
subscription_info=AsyncMock(),
)
mock_cloud.ice_servers = MagicMock(
spec=IceServers,
async_register_ice_servers_listener=AsyncMock(
+6 -5
View File
@@ -18,6 +18,7 @@ from hass_nabucasa.auth import (
UnknownError,
)
from hass_nabucasa.const import STATE_CONNECTED
from hass_nabucasa.payments_api import PaymentsApiError
from hass_nabucasa.remote import CertificateStatus
import pytest
from syrupy.assertion import SnapshotAssertion
@@ -1008,16 +1009,14 @@ async def test_websocket_subscription_info(
cloud: MagicMock,
setup_cloud: None,
) -> None:
"""Test subscription info and connecting because valid account."""
aioclient_mock.get(SUBSCRIPTION_INFO_URL, json={"provider": "stripe"})
"""Test subscription info."""
cloud.payments.subscription_info.return_value = {"provider": "stripe"}
client = await hass_ws_client(hass)
mock_renew = cloud.auth.async_renew_access_token
await client.send_json({"id": 5, "type": "cloud/subscription"})
response = await client.receive_json()
assert response["result"] == {"provider": "stripe"}
assert mock_renew.call_count == 1
async def test_websocket_subscription_fail(
@@ -1028,7 +1027,9 @@ async def test_websocket_subscription_fail(
setup_cloud: None,
) -> None:
"""Test subscription info fail."""
aioclient_mock.get(SUBSCRIPTION_INFO_URL, status=HTTPStatus.INTERNAL_SERVER_ERROR)
cloud.payments.subscription_info.side_effect = PaymentsApiError(
"Failed to fetch subscription information"
)
client = await hass_ws_client(hass)
await client.send_json({"id": 5, "type": "cloud/subscription"})
+8 -5
View File
@@ -2,7 +2,7 @@
from unittest.mock import AsyncMock, Mock
from hass_nabucasa import Cloud
from hass_nabucasa import Cloud, payments_api
import pytest
from homeassistant.components.cloud.subscription import (
@@ -22,6 +22,10 @@ async def mocked_cloud_object(hass: HomeAssistant) -> Cloud:
accounts_server="accounts.nabucasa.com",
auth=Mock(async_check_token=AsyncMock()),
websession=async_get_clientsession(hass),
payments=Mock(
spec=payments_api.PaymentsApi,
subscription_info=AsyncMock(),
),
)
@@ -31,14 +35,13 @@ async def test_fetching_subscription_with_timeout_error(
mocked_cloud: Cloud,
) -> None:
"""Test that we handle timeout error."""
aioclient_mock.get(
"https://accounts.nabucasa.com/payments/subscription_info",
exc=TimeoutError(),
mocked_cloud.payments.subscription_info.side_effect = payments_api.PaymentsApiError(
"Timeout reached while calling API"
)
assert await async_subscription_info(mocked_cloud) is None
assert (
"A timeout of 10 was reached while trying to fetch subscription information"
"Failed to fetch subscription information - Timeout reached while calling API"
in caplog.text
)