| 1 |
"""Tests for Instance API.""" |
| 2 |
|
| 3 |
import re |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from aiohttp import ClientError |
| 7 |
import pytest |
| 8 |
from syrupy import SnapshotAssertion |
| 9 |
|
| 10 |
from hass_nabucasa import AccountsApiError, Cloud |
| 11 |
from tests.common import extract_log_messages |
| 12 |
from tests.utils.aiohttp import AiohttpClientMocker |
| 13 |
|
| 14 |
|
| 15 |
@pytest.mark.parametrize( |
| 16 |
"exception,mockargs,exception_msg", |
| 17 |
[ |
| 18 |
[ |
| 19 |
AccountsApiError, |
| 20 |
{"status": 500, "text": "Internal Server Error"}, |
| 21 |
"Failed to fetch: (500) ", |
| 22 |
], |
| 23 |
[ |
| 24 |
AccountsApiError, |
| 25 |
{"status": 429, "text": "Too fast"}, |
| 26 |
"Failed to fetch: (429) ", |
| 27 |
], |
| 28 |
[ |
| 29 |
AccountsApiError, |
| 30 |
{"exc": TimeoutError()}, |
| 31 |
"Timeout reached while calling API", |
| 32 |
], |
| 33 |
[ |
| 34 |
AccountsApiError, |
| 35 |
{"exc": ClientError("boom!")}, |
| 36 |
"Failed to fetch: boom!", |
| 37 |
], |
| 38 |
[ |
| 39 |
AccountsApiError, |
| 40 |
{"exc": Exception("boom!")}, |
| 41 |
"Unexpected error while calling API: boom!", |
| 42 |
], |
| 43 |
], |
| 44 |
ids=["500-error", "429-error", "timeout", "client-error", "unexpected-error"], |
| 45 |
) |
| 46 |
async def test_resolve_dns_cname_endpoint_problems( |
| 47 |
aioclient_mock: AiohttpClientMocker, |
| 48 |
cloud: Cloud, |
| 49 |
exception: Exception, |
| 50 |
mockargs: dict[str, Any], |
| 51 |
exception_msg: str, |
| 52 |
caplog: pytest.LogCaptureFixture, |
| 53 |
snapshot: SnapshotAssertion, |
| 54 |
): |
| 55 |
"""Test problems with resolve_dns_cname endpoint.""" |
| 56 |
aioclient_mock.post( |
| 57 |
f"https://{cloud.api_server}/account/instance/resolve_dns_cname", |
| 58 |
**mockargs, |
| 59 |
) |
| 60 |
|
| 61 |
with pytest.raises(exception, match=re.escape(exception_msg)): |
| 62 |
await cloud.accounts.instance_resolve_dns_cname(hostname="example.com") |
| 63 |
|
| 64 |
assert extract_log_messages(caplog) == snapshot |
| 65 |
|
| 66 |
|
| 67 |
async def test_resolve_dns_cname_endpoint_success( |
| 68 |
aioclient_mock: AiohttpClientMocker, |
| 69 |
cloud: Cloud, |
| 70 |
caplog: pytest.LogCaptureFixture, |
| 71 |
snapshot: SnapshotAssertion, |
| 72 |
): |
| 73 |
"""Test successful resolve_dns_cname endpoint.""" |
| 74 |
expected_result = ["alias1.example.com", "alias2.example.com"] |
| 75 |
|
| 76 |
aioclient_mock.post( |
| 77 |
f"https://{cloud.api_server}/account/instance/resolve_dns_cname", |
| 78 |
json=expected_result, |
| 79 |
) |
| 80 |
|
| 81 |
result = await cloud.accounts.instance_resolve_dns_cname(hostname="example.com") |
| 82 |
|
| 83 |
assert result == expected_result |
| 84 |
assert extract_log_messages(caplog) == snapshot |