1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
|
"""Tests for ActronAirAPI core client functionality."""
from unittest.mock import AsyncMock
import pytest
from actron_neo_api import ActronAirAPI
from actron_neo_api.exceptions import ActronAirAPIError, ActronAirAuthError
class TestActronAirAPIInitialization:
"""Test ActronAirAPI initialization and configuration."""
def test_init_default(self):
"""Test default initialization uses Neo platform."""
api = ActronAirAPI()
assert api.base_url == "https://nimbus.actronair.com.au"
assert api.platform == "neo"
assert api._auto_manage_base_url is True
assert api.oauth2_auth is not None
assert api.systems == []
assert not api._initialized
def test_init_with_refresh_token(self):
"""Test initialization with refresh token."""
api = ActronAirAPI(refresh_token="test_refresh_token")
assert api.oauth2_auth.refresh_token == "test_refresh_token"
def test_init_neo_platform_explicit(self):
"""Test explicit Neo platform selection."""
api = ActronAirAPI(platform="neo")
assert api.base_url == "https://nimbus.actronair.com.au"
assert api.platform == "neo"
assert api._auto_manage_base_url is False
def test_init_que_platform_explicit(self):
"""Test explicit Que platform selection."""
api = ActronAirAPI(platform="que")
assert api.base_url == "https://que.actronair.com.au"
assert api.platform == "que"
assert api._auto_manage_base_url is False
def test_init_custom_client_id(self):
"""Test initialization with custom OAuth2 client ID."""
api = ActronAirAPI(oauth2_client_id="custom_client")
assert api.oauth2_auth.client_id == "custom_client"
def test_authenticated_platform_property(self):
"""Test authenticated_platform property."""
api = ActronAirAPI()
api.oauth2_auth.authenticated_platform = "https://nimbus.actronair.com.au"
assert api.authenticated_platform == "https://nimbus.actronair.com.au"
class TestActronAirAPIPlatformManagement:
"""Test platform detection and switching."""
def test_is_nx_gen_system_true(self):
"""Test NX Gen system detection."""
api = ActronAirAPI()
assert api._is_nx_gen_system({"type": "NX-Gen"})
assert api._is_nx_gen_system({"type": "nx-gen"})
assert api._is_nx_gen_system({"type": "nxgen"})
def test_is_nx_gen_system_false(self):
"""Test non-NX Gen system detection."""
api = ActronAirAPI()
assert not api._is_nx_gen_system({"type": "standard"})
assert not api._is_nx_gen_system({"type": "other"})
assert not api._is_nx_gen_system({"type": None})
def test_set_base_url_changes_platform(self):
"""Test platform URL change."""
api = ActronAirAPI(platform="neo")
api._set_base_url("https://que.actronair.com.au")
assert api.base_url == "https://que.actronair.com.au"
assert api.platform == "que"
def test_set_base_url_preserves_tokens(self):
"""Test token preservation during platform switch."""
api = ActronAirAPI()
api.oauth2_auth.access_token = "old_token"
api.oauth2_auth.refresh_token = "old_refresh"
api.oauth2_auth.token_expiry = 1234567890.0
api._set_base_url("https://que.actronair.com.au")
assert api.oauth2_auth.access_token == "old_token"
assert api.oauth2_auth.refresh_token == "old_refresh"
assert api.oauth2_auth.token_expiry == 1234567890.0
def test_set_base_url_no_change(self):
"""Test no-op when setting same URL."""
api = ActronAirAPI(platform="neo")
original_oauth = api.oauth2_auth
api._set_base_url("https://nimbus.actronair.com.au")
# Should not recreate OAuth handler
assert api.oauth2_auth is original_oauth
def test_maybe_update_base_url_with_nx_gen(self, sample_system_que_nxgen):
"""Test auto-switch to Que platform for NX Gen systems."""
api = ActronAirAPI() # Auto-detect enabled
api._maybe_update_base_url_from_systems([sample_system_que_nxgen])
assert api.base_url == "https://que.actronair.com.au"
assert api.platform == "que"
def test_maybe_update_base_url_without_nx_gen(self, sample_system_neo):
"""Test stays on Neo platform for standard systems."""
api = ActronAirAPI() # Auto-detect enabled
api._maybe_update_base_url_from_systems([sample_system_neo])
assert api.base_url == "https://nimbus.actronair.com.au"
assert api.platform == "neo"
def test_maybe_update_base_url_disabled(self, sample_system_que_nxgen):
"""Test no auto-switch when platform explicitly set."""
api = ActronAirAPI(platform="neo") # Explicit, no auto-detect
api._maybe_update_base_url_from_systems([sample_system_que_nxgen])
assert api.base_url == "https://nimbus.actronair.com.au" # Should not change
def test_maybe_update_base_url_empty_systems(self):
"""Test no-op with empty systems list."""
api = ActronAirAPI()
original_url = api.base_url
api._maybe_update_base_url_from_systems([])
assert api.base_url == original_url
class TestActronAirAPISessionManagement:
"""Test aiohttp session lifecycle management."""
@pytest.mark.asyncio
async def test_get_session_creates_new(self):
"""Test session creation on first access."""
api = ActronAirAPI()
assert api._session is None
session = await api._get_session()
assert session is not None
assert api._session is session
@pytest.mark.asyncio
async def test_get_session_reuses_existing(self):
"""Test session reuse."""
api = ActronAirAPI()
session1 = await api._get_session()
session2 = await api._get_session()
assert session1 is session2
@pytest.mark.asyncio
async def test_get_session_recreates_if_closed(self):
"""Test session recreation if closed."""
from unittest.mock import PropertyMock
api = ActronAirAPI()
session1 = await api._get_session()
# Mock the closed property to return True
type(session1).closed = PropertyMock(return_value=True)
session2 = await api._get_session()
assert session2 is not session1
@pytest.mark.asyncio
async def test_close_handles_no_session(self):
"""Test close() with no active session."""
api = ActronAirAPI()
await api.close() # Should not raise
assert api._session is None
@pytest.mark.asyncio
async def test_context_manager_entry(self):
"""Test async context manager entry returns API instance."""
api = ActronAirAPI()
async with api as context_api:
assert context_api is api
session = await api._get_session()
assert session is not None
class TestActronAirAPISystemLinkResolution:
"""Test HAL link resolution for systems."""
def test_get_system_link_success(self, sample_system_neo):
"""Test successful link resolution."""
api = ActronAirAPI()
api.systems = [sample_system_neo]
link = api._get_system_link("abc123", "ac-status")
assert link == "api/v0/client/ac-systems/abc123/status"
def test_get_system_link_case_insensitive(self, sample_system_neo):
"""Test case-insensitive serial number matching."""
api = ActronAirAPI()
api.systems = [sample_system_neo]
link = api._get_system_link("ABC123", "ac-status") # Uppercase
assert link is not None
assert "abc123" in link
def test_get_system_link_not_found(self):
"""Test link not found returns None."""
api = ActronAirAPI()
api.systems = [{"serial": "abc123", "_links": {}}]
link = api._get_system_link("abc123", "missing-link")
assert link is None
def test_get_system_link_system_not_found(self):
"""Test system not found returns None."""
api = ActronAirAPI()
api.systems = [{"serial": "abc123"}]
link = api._get_system_link("xyz789", "ac-status")
assert link is None
def test_get_system_link_strips_leading_slash(self):
"""Test leading slash is stripped from href."""
api = ActronAirAPI()
api.systems = [
{
"serial": "abc123",
"_links": {"test": {"href": "/api/v0/test"}},
}
]
link = api._get_system_link("abc123", "test")
assert link == "api/v0/test"
def test_get_system_link_list_format(self):
"""Test link resolution with list format."""
api = ActronAirAPI()
api.systems = [
{
"serial": "abc123",
"_links": {"test": [{"href": "/api/v0/test"}]},
}
]
link = api._get_system_link("abc123", "test")
assert link == "api/v0/test"
class TestActronAirAPIGetSystems:
"""Test get_ac_systems method."""
@pytest.mark.asyncio
async def test_get_ac_systems_success(
self, mock_session, sample_systems_response_neo, mock_aiohttp_response, mock_oauth
):
"""Test successful systems retrieval."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=200, json_data=sample_systems_response_neo
)
systems = await api.get_ac_systems()
assert len(systems) == 1
assert systems[0]["serial"] == "abc123"
assert api.systems == systems
@pytest.mark.asyncio
async def test_get_ac_systems_triggers_platform_detection(
self, mock_session, sample_systems_response_que, mock_aiohttp_response, mock_oauth
):
"""Test platform auto-detection on systems retrieval."""
api = ActronAirAPI(refresh_token="test_token") # Auto-detect enabled
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=200, json_data=sample_systems_response_que
)
await api.get_ac_systems()
assert api.platform == "que"
@pytest.mark.asyncio
async def test_get_ac_systems_includes_neo_param(
self, mock_session, sample_systems_response_neo, mock_aiohttp_response, mock_oauth
):
"""Test includeNeo parameter is sent."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=200, json_data=sample_systems_response_neo
)
await api.get_ac_systems()
# Verify request was made with correct params
call_args = mock_session.request.call_args
assert call_args[1]["params"]["includeNeo"] == "true"
class TestActronAirAPIGetStatus:
"""Test get_ac_status method."""
@pytest.mark.asyncio
async def test_get_ac_status_success(
self, mock_session, sample_status_full, sample_system_neo, mock_aiohttp_response, mock_oauth
):
"""Test successful status retrieval."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
api.systems = [sample_system_neo]
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=200, json_data=sample_status_full
)
status = await api.get_ac_status("abc123")
assert status == sample_status_full
assert status["isOnline"] is True
@pytest.mark.asyncio
async def test_get_ac_status_normalizes_serial(
self, mock_session, sample_status_full, sample_system_neo, mock_aiohttp_response, mock_oauth
):
"""Test serial number normalization."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
api.systems = [sample_system_neo]
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=200, json_data=sample_status_full
)
status = await api.get_ac_status("ABC123") # Uppercase
assert status is not None
@pytest.mark.asyncio
async def test_get_ac_status_missing_link_raises(self):
"""Test error when status link is missing."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api.systems = [{"serial": "abc123", "_links": {}}]
with pytest.raises(ActronAirAPIError, match="No ac-status link found"):
await api.get_ac_status("abc123")
class TestActronAirAPISendCommand:
"""Test send_command method."""
@pytest.mark.asyncio
async def test_send_command_success(
self,
mock_session,
sample_command_response,
sample_system_neo,
mock_aiohttp_response,
mock_oauth,
):
"""Test successful command sending."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
api.systems = [sample_system_neo]
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=200, json_data=sample_command_response
)
command = {"command": {"type": "set-settings", "UserAirconSettings.isOn": True}}
response = await api.send_command("abc123", command)
assert response["success"] is True
@pytest.mark.asyncio
async def test_send_command_normalizes_serial(
self,
mock_session,
sample_command_response,
sample_system_neo,
mock_aiohttp_response,
mock_oauth,
):
"""Test serial number normalization in send_command."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
api.systems = [sample_system_neo]
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=200, json_data=sample_command_response
)
command = {"command": {"type": "set-settings"}}
response = await api.send_command("ABC123", command) # Uppercase
assert response is not None
@pytest.mark.asyncio
async def test_send_command_missing_link_raises(self):
"""Test error when commands link is missing."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api.systems = [{"serial": "abc123", "_links": {}}]
with pytest.raises(ActronAirAPIError, match="No commands link found"):
await api.send_command("abc123", {})
@pytest.mark.asyncio
async def test_send_command_sets_content_type(
self,
mock_session,
sample_command_response,
sample_system_neo,
mock_aiohttp_response,
mock_oauth,
):
"""Test Content-Type header is set."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
api.systems = [sample_system_neo]
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=200, json_data=sample_command_response
)
await api.send_command("abc123", {})
# Verify Content-Type was set
call_args = mock_session.request.call_args
assert call_args[1]["headers"]["Content-Type"] == "application/json"
class TestActronAirAPIErrorHandling:
"""Test error handling and retry logic."""
@pytest.mark.asyncio
async def test_make_request_401_triggers_refresh_and_retry(
self, mock_session, mock_aiohttp_response, mock_oauth
):
"""Test 401 response triggers token refresh and retry."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
api.oauth2_auth.refresh_access_token = AsyncMock()
# First call returns 401, second call succeeds
mock_session.request.return_value.__aenter__.side_effect = [
mock_aiohttp_response(status=401, text="Unauthorized"),
mock_aiohttp_response(status=200, json_data={"success": True}),
]
result = await api._make_request("get", "test/endpoint")
assert result["success"] is True
api.oauth2_auth.refresh_access_token.assert_called_once()
@pytest.mark.asyncio
async def test_make_request_401_without_refresh_token_raises(
self, mock_session, mock_aiohttp_response
):
"""Test 401 without refresh token raises immediately."""
api = ActronAirAPI() # No refresh token
api._initialized = True
api._session = mock_session
api.oauth2_auth.refresh_token = None
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=401, text="Unauthorized"
)
with pytest.raises(ActronAirAuthError, match="Refresh token is required"):
await api._make_request("get", "test/endpoint")
@pytest.mark.asyncio
async def test_make_request_401_refresh_fails_raises(self, mock_session, mock_aiohttp_response):
"""Test 401 with failed refresh raises ActronAirAuthError."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth.refresh_access_token = AsyncMock(
side_effect=ActronAirAuthError("Refresh failed")
)
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=401, text="Unauthorized"
)
with pytest.raises(ActronAirAuthError, match="Refresh failed"):
await api._make_request("get", "test/endpoint")
@pytest.mark.asyncio
async def test_make_request_non_200_raises(
self, mock_session, mock_aiohttp_response, mock_oauth
):
"""Test non-200 response raises ActronAirAPIError."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=500, text="Internal Server Error"
)
with pytest.raises(ActronAirAPIError, match="API request failed"):
await api._make_request("get", "test/endpoint")
@pytest.mark.asyncio
async def test_make_request_network_error_raises(self, mock_session, mock_oauth):
"""Test network error raises ActronAirAPIError."""
import aiohttp
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
mock_session.request.side_effect = aiohttp.ClientError("Network error")
with pytest.raises(ActronAirAPIError, match="Request failed"):
await api._make_request("get", "test/endpoint")
class TestActronAirAPITokenProperties:
"""Test token property accessors."""
def test_access_token_property(self):
"""Test access_token property."""
api = ActronAirAPI()
api.oauth2_auth.access_token = "test_token"
assert api.access_token == "test_token"
def test_refresh_token_value_property(self):
"""Test refresh_token_value property."""
api = ActronAirAPI()
api.oauth2_auth.refresh_token = "test_refresh"
assert api.refresh_token_value == "test_refresh"
def test_latest_event_id_property(self):
"""Test latest_event_id property."""
api = ActronAirAPI()
api.state_manager.latest_event_id = {"abc123": "event_1"}
assert api.latest_event_id == {"abc123": "event_1"}
class TestActronAirAPIUpdateStatus:
"""Test status update methods."""
@pytest.mark.asyncio
async def test_update_status_single_system(
self, mock_session, sample_status_full, sample_system_neo, mock_aiohttp_response, mock_oauth
):
"""Test updating single system status."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
api.systems = [sample_system_neo]
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=200, json_data=sample_status_full
)
result = await api.update_status("abc123")
assert "abc123" in result
assert result["abc123"] is not None
@pytest.mark.asyncio
async def test_update_status_all_systems(
self, mock_session, sample_status_full, sample_system_neo, mock_aiohttp_response, mock_oauth
):
"""Test updating all systems."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api._session = mock_session
api.oauth2_auth = mock_oauth
api.systems = [sample_system_neo]
mock_session.request.return_value.__aenter__.return_value = mock_aiohttp_response(
status=200, json_data=sample_status_full
)
result = await api.update_status()
assert len(result) == 1
assert "abc123" in result
@pytest.mark.asyncio
async def test_update_status_empty_systems(self):
"""Test update_status with no systems returns empty dict."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api.systems = []
result = await api.update_status()
assert result == {}
@pytest.mark.asyncio
async def test_ensure_initialized_with_refresh_token(self):
"""Test initialization triggers token refresh."""
api = ActronAirAPI(refresh_token="test_token")
api.oauth2_auth.access_token = None
api.oauth2_auth.refresh_access_token = AsyncMock()
await api._ensure_initialized()
api.oauth2_auth.refresh_access_token.assert_called_once()
assert api._initialized is True
@pytest.mark.asyncio
async def test_ensure_initialized_already_initialized(self):
"""Test ensure_initialized is idempotent."""
api = ActronAirAPI(refresh_token="test_token")
api._initialized = True
api.oauth2_auth.refresh_access_token = AsyncMock()
await api._ensure_initialized()
api.oauth2_auth.refresh_access_token.assert_not_called()
@pytest.mark.asyncio
async def test_ensure_initialized_failure_raises(self):
"""Test initialization failure raises ActronAirAuthError."""
import aiohttp
api = ActronAirAPI(refresh_token="test_token")
api.oauth2_auth.access_token = None
api.oauth2_auth.refresh_access_token = AsyncMock(
side_effect=aiohttp.ClientError("Network error")
)
with pytest.raises(ActronAirAuthError, match="Failed to initialize API"):
await api._ensure_initialized()
class TestActronAirAPIOAuth2Methods:
"""Test OAuth2 method proxies."""
@pytest.mark.asyncio
async def test_request_device_code_proxy(self):
"""Test request_device_code proxies to OAuth2 handler."""
api = ActronAirAPI()
api.oauth2_auth.request_device_code = AsyncMock(return_value={"device_code": "test"})
result = await api.request_device_code()
assert result["device_code"] == "test"
api.oauth2_auth.request_device_code.assert_called_once()
@pytest.mark.asyncio
async def test_poll_for_token_proxy(self):
"""Test poll_for_token proxies to OAuth2 handler."""
api = ActronAirAPI()
api.oauth2_auth.poll_for_token = AsyncMock(return_value={"access_token": "test"})
result = await api.poll_for_token("device_code")
assert result["access_token"] == "test"
api.oauth2_auth.poll_for_token.assert_called_once_with("device_code", 5, 600)
@pytest.mark.asyncio
async def test_get_user_info_proxy(self):
"""Test get_user_info proxies to OAuth2 handler."""
api = ActronAirAPI()
api.oauth2_auth.get_user_info = AsyncMock(return_value={"id": "test_user"})
result = await api.get_user_info()
assert result["id"] == "test_user"
api.oauth2_auth.get_user_info.assert_called_once()
|