Use the async_migrate_paypal_agreement function to get the migration URL (#83469)

* Use the async_migrate_paypal_agreement function to get the migration URL

* Update URL

* Handle timeout error
This commit is contained in:
Joakim Sørensen 2022-12-07 20:02:22 +01:00 committed by GitHub
parent dfb0887765
commit f5cfd0329c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 5 deletions

View file

@ -88,6 +88,10 @@ async def test_legacy_subscription_repair_flow(
"https://accounts.nabucasa.com/payments/subscription_info",
json={"provider": None},
)
aioclient_mock.post(
"https://accounts.nabucasa.com/payments/migrate_paypal_agreement",
json={"url": "https://paypal.com"},
)
cloud_repairs.async_manage_legacy_subscription_issue(hass, {"provider": "legacy"})
repair_issue = issue_registry.async_get_issue(
@ -133,7 +137,7 @@ async def test_legacy_subscription_repair_flow(
"flow_id": flow_id,
"handler": DOMAIN,
"step_id": "change_plan",
"url": "https://account.nabucasa.com/",
"url": "https://paypal.com",
"description_placeholders": None,
}
@ -161,8 +165,15 @@ async def test_legacy_subscription_repair_flow(
async def test_legacy_subscription_repair_flow_timeout(
hass: HomeAssistant,
hass_client: Callable[..., Awaitable[ClientSession]],
mock_auth: Generator[None, AsyncMock, None],
aioclient_mock: AiohttpClientMocker,
):
"""Test timeout flow of the fix flow for legacy subscription."""
aioclient_mock.post(
"https://accounts.nabucasa.com/payments/migrate_paypal_agreement",
status=403,
)
issue_registry: ir.IssueRegistry = ir.async_get(hass)
cloud_repairs.async_manage_legacy_subscription_issue(hass, {"provider": "legacy"})

View file

@ -0,0 +1,61 @@
"""Test cloud subscription functions."""
import asyncio
from unittest.mock import AsyncMock, Mock
from hass_nabucasa import Cloud
import pytest
from homeassistant.components.cloud.subscription import (
async_migrate_paypal_agreement,
async_subscription_info,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from tests.test_util.aiohttp import AiohttpClientMocker
@pytest.fixture(name="mocked_cloud")
def mocked_cloud_object(hass: HomeAssistant) -> Cloud:
"""Mock cloud object."""
return Mock(
accounts_server="accounts.nabucasa.com",
auth=Mock(async_check_token=AsyncMock()),
websession=async_get_clientsession(hass),
)
async def test_fetching_subscription_with_timeout_error(
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
mocked_cloud: Cloud,
):
"""Test that we handle timeout error."""
aioclient_mock.get(
"https://accounts.nabucasa.com/payments/subscription_info",
exc=asyncio.TimeoutError(),
)
assert await async_subscription_info(mocked_cloud) is None
assert (
"A timeout of 10 was reached while trying to fetch subscription information"
in caplog.text
)
async def test_migrate_paypal_agreement_with_timeout_error(
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
mocked_cloud: Cloud,
):
"""Test that we handle timeout error."""
aioclient_mock.post(
"https://accounts.nabucasa.com/payments/migrate_paypal_agreement",
exc=asyncio.TimeoutError(),
)
assert await async_migrate_paypal_agreement(mocked_cloud) is None
assert (
"A timeout of 10 was reached while trying to start agreement migration"
in caplog.text
)