| 1 |
"""Test the Alexa access token API.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import json |
| 6 |
import re |
| 7 |
from typing import TYPE_CHECKING, Any |
| 8 |
|
| 9 |
from aiohttp import ClientError |
| 10 |
import pytest |
| 11 |
from syrupy import SnapshotAssertion |
| 12 |
|
| 13 |
from hass_nabucasa.alexa_api import ( |
| 14 |
AlexaAccessTokenDetails, |
| 15 |
AlexaApiError, |
| 16 |
AlexaApiNeedsRelinkError, |
| 17 |
AlexaApiNoTokenError, |
| 18 |
) |
| 19 |
from tests.common import extract_log_messages |
| 20 |
|
| 21 |
if TYPE_CHECKING: |
| 22 |
from hass_nabucasa import Cloud |
| 23 |
from tests.utils.aiohttp import AiohttpClientMocker |
| 24 |
|
| 25 |
|
| 26 |
@pytest.mark.parametrize( |
| 27 |
"exception,getmockargs,exception_msg", |
| 28 |
[ |
| 29 |
[ |
| 30 |
AlexaApiError, |
| 31 |
{"status": 500, "text": "Internal Server Error"}, |
| 32 |
"Failed to fetch: (500) ", |
| 33 |
], |
| 34 |
[ |
| 35 |
AlexaApiError, |
| 36 |
{"status": 429, "text": "Too fast"}, |
| 37 |
"Failed to fetch: (429) ", |
| 38 |
], |
| 39 |
[ |
| 40 |
AlexaApiNeedsRelinkError, |
| 41 |
{"status": 400, "text": json.dumps({"reason": "RefreshTokenNotFound"})}, |
| 42 |
"RefreshTokenNotFound", |
| 43 |
], |
| 44 |
[ |
| 45 |
AlexaApiError, |
| 46 |
{"exc": TimeoutError()}, |
| 47 |
"Timeout reached while calling API", |
| 48 |
], |
| 49 |
[ |
| 50 |
AlexaApiError, |
| 51 |
{"exc": ClientError("boom!")}, |
| 52 |
"Failed to fetch: boom!", |
| 53 |
], |
| 54 |
[ |
| 55 |
AlexaApiError, |
| 56 |
{"exc": Exception("boom!")}, |
| 57 |
"Unexpected error while calling API: boom!", |
| 58 |
], |
| 59 |
], |
| 60 |
) |
| 61 |
async def test_problems_getting_access_token( |
| 62 |
aioclient_mock: AiohttpClientMocker, |
| 63 |
cloud: Cloud, |
| 64 |
exception: Exception, |
| 65 |
getmockargs, |
| 66 |
exception_msg, |
| 67 |
caplog: pytest.LogCaptureFixture, |
| 68 |
snapshot: SnapshotAssertion, |
| 69 |
): |
| 70 |
"""Test problems getting access token.""" |
| 71 |
aioclient_mock.post( |
| 72 |
f"https://{cloud.api_server}/alexa/access_token", |
| 73 |
**getmockargs, |
| 74 |
) |
| 75 |
|
| 76 |
with pytest.raises(exception, match=re.escape(exception_msg)): |
| 77 |
await cloud.alexa_api.access_token() |
| 78 |
|
| 79 |
assert extract_log_messages(caplog) == snapshot |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.parametrize( |
| 83 |
"response", |
| 84 |
[ |
| 85 |
{ |
| 86 |
"access_token": "test_key", |
| 87 |
"expires_in": 123, |
| 88 |
"event_endpoint": "http://example.com/events", |
| 89 |
} |
| 90 |
], |
| 91 |
) |
| 92 |
async def test_getting_access_token( |
| 93 |
aioclient_mock: AiohttpClientMocker, |
| 94 |
cloud: Cloud, |
| 95 |
response: dict[str, Any], |
| 96 |
caplog: pytest.LogCaptureFixture, |
| 97 |
snapshot: SnapshotAssertion, |
| 98 |
): |
| 99 |
"""Test getting access token.""" |
| 100 |
aioclient_mock.post( |
| 101 |
f"https://{cloud.api_server}/alexa/access_token", |
| 102 |
json=response, |
| 103 |
) |
| 104 |
|
| 105 |
details = await cloud.alexa_api.access_token() |
| 106 |
|
| 107 |
assert details == AlexaAccessTokenDetails(**response) |
| 108 |
assert extract_log_messages(caplog) == snapshot |
| 109 |
|
| 110 |
|
| 111 |
async def test_access_token_needs_relink_error( |
| 112 |
aioclient_mock: AiohttpClientMocker, |
| 113 |
cloud: Cloud, |
| 114 |
): |
| 115 |
"""Test that 400 with RefreshTokenNotFound raises AlexaApiNeedsRelinkError.""" |
| 116 |
aioclient_mock.post( |
| 117 |
f"https://{cloud.api_server}/alexa/access_token", |
| 118 |
status=400, |
| 119 |
json={"reason": "RefreshTokenNotFound"}, |
| 120 |
) |
| 121 |
|
| 122 |
with pytest.raises(AlexaApiNeedsRelinkError, match="RefreshTokenNotFound"): |
| 123 |
await cloud.alexa_api.access_token() |
| 124 |
|
| 125 |
|
| 126 |
async def test_access_token_needs_relink_error_unknown_region( |
| 127 |
aioclient_mock: AiohttpClientMocker, |
| 128 |
cloud: Cloud, |
| 129 |
): |
| 130 |
"""Test that 400 with UnknownRegion raises AlexaApiNeedsRelinkError.""" |
| 131 |
aioclient_mock.post( |
| 132 |
f"https://{cloud.api_server}/alexa/access_token", |
| 133 |
status=400, |
| 134 |
json={"reason": "UnknownRegion"}, |
| 135 |
) |
| 136 |
|
| 137 |
with pytest.raises(AlexaApiNeedsRelinkError, match="UnknownRegion"): |
| 138 |
await cloud.alexa_api.access_token() |
| 139 |
|
| 140 |
|
| 141 |
async def test_access_token_no_token_error( |
| 142 |
aioclient_mock: AiohttpClientMocker, |
| 143 |
cloud: Cloud, |
| 144 |
): |
| 145 |
"""Test that 400 with other reasons raises AlexaApiNoTokenError.""" |
| 146 |
aioclient_mock.post( |
| 147 |
f"https://{cloud.api_server}/alexa/access_token", |
| 148 |
status=400, |
| 149 |
json={"reason": "SomeOtherReason"}, |
| 150 |
) |
| 151 |
|
| 152 |
with pytest.raises(AlexaApiNoTokenError, match="No access token available"): |
| 153 |
await cloud.alexa_api.access_token() |
| 154 |
|
| 155 |
|
| 156 |
async def test_access_token_no_token_error_no_reason( |
| 157 |
aioclient_mock: AiohttpClientMocker, |
| 158 |
cloud: Cloud, |
| 159 |
): |
| 160 |
"""Test that 400 without reason raises AlexaApiNoTokenError.""" |
| 161 |
aioclient_mock.post( |
| 162 |
f"https://{cloud.api_server}/alexa/access_token", |
| 163 |
status=400, |
| 164 |
json={"error": "Bad Request"}, |
| 165 |
) |
| 166 |
|
| 167 |
with pytest.raises(AlexaApiNoTokenError, match="No access token available"): |
| 168 |
await cloud.alexa_api.access_token() |
| 169 |
|
| 170 |
|
| 171 |
async def test_access_token_raw_response_error_handling( |
| 172 |
aioclient_mock: AiohttpClientMocker, |
| 173 |
cloud: Cloud, |
| 174 |
): |
| 175 |
"""Test that non-400 errors are handled correctly with raw_response=True.""" |
| 176 |
aioclient_mock.post( |
| 177 |
f"https://{cloud.api_server}/alexa/access_token", |
| 178 |
status=500, |
| 179 |
text="Internal Server Error", |
| 180 |
) |
| 181 |
|
| 182 |
with pytest.raises(AlexaApiError, match=re.escape("Failed to fetch: (500) ")): |
| 183 |
await cloud.alexa_api.access_token() |