|
| 1 | +"""Test AsyncDashboardAPI context manager and session lifecycle.""" |
| 2 | + |
| 3 | +from unittest.mock import patch, AsyncMock, MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from meraki.aio import AsyncDashboardAPI |
| 8 | +from meraki.exceptions import APIKeyError |
| 9 | + |
| 10 | + |
| 11 | +class TestAsyncDashboardAPILifecycle: |
| 12 | + def test_missing_api_key_raises(self, monkeypatch): |
| 13 | + monkeypatch.delenv("MERAKI_DASHBOARD_API_KEY", raising=False) |
| 14 | + with pytest.raises(APIKeyError): |
| 15 | + AsyncDashboardAPI(api_key=None, suppress_logging=True) |
| 16 | + |
| 17 | + def test_instantiation_with_valid_key(self): |
| 18 | + with patch("meraki.session.base.check_python_version"): |
| 19 | + api = AsyncDashboardAPI( |
| 20 | + api_key="fake_key_1234567890123456789012345678901234567890", |
| 21 | + suppress_logging=True, |
| 22 | + ) |
| 23 | + assert api._session is not None |
| 24 | + |
| 25 | + @pytest.mark.asyncio |
| 26 | + async def test_context_manager_enters_and_exits(self): |
| 27 | + with patch("meraki.session.base.check_python_version"): |
| 28 | + api = AsyncDashboardAPI( |
| 29 | + api_key="fake_key_1234567890123456789012345678901234567890", |
| 30 | + suppress_logging=True, |
| 31 | + ) |
| 32 | + |
| 33 | + api._session._client = MagicMock() |
| 34 | + api._session._client.aclose = AsyncMock() |
| 35 | + |
| 36 | + async with api as dashboard: |
| 37 | + assert dashboard is api |
| 38 | + |
| 39 | + api._session._client.aclose.assert_called_once() |
| 40 | + |
| 41 | + def test_all_api_sections_assigned(self): |
| 42 | + with patch("meraki.session.base.check_python_version"): |
| 43 | + api = AsyncDashboardAPI( |
| 44 | + api_key="fake_key_1234567890123456789012345678901234567890", |
| 45 | + suppress_logging=True, |
| 46 | + ) |
| 47 | + |
| 48 | + sections = [ |
| 49 | + "administered", |
| 50 | + "appliance", |
| 51 | + "camera", |
| 52 | + "campusGateway", |
| 53 | + "cellularGateway", |
| 54 | + "devices", |
| 55 | + "insight", |
| 56 | + "licensing", |
| 57 | + "networks", |
| 58 | + "organizations", |
| 59 | + "sensor", |
| 60 | + "sm", |
| 61 | + "spaces", |
| 62 | + "switch", |
| 63 | + "wireless", |
| 64 | + "wirelessController", |
| 65 | + ] |
| 66 | + for section in sections: |
| 67 | + assert hasattr(api, section), f"Missing API section: {section}" |
| 68 | + assert getattr(api, section) is not None |
0 commit comments