From 64e5c77aaa415ea70e291a44644855f7101ed395 Mon Sep 17 00:00:00 2001 From: fraviktor Date: Sun, 19 Jul 2026 13:30:55 +0200 Subject: [PATCH] Classify invalid paths as unknown paths --- sagemcom_api/client.py | 3 ++- sagemcom_api/const.py | 1 + tests/conftest.py | 6 +++++ tests/fixtures/xpath_invalid_path.json | 32 ++++++++++++++++++++++++++ tests/unit/test_client_basic.py | 26 ++++++++++++++++++++- 5 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/xpath_invalid_path.json diff --git a/sagemcom_api/client.py b/sagemcom_api/client.py index 3a16e64..f361a86 100644 --- a/sagemcom_api/client.py +++ b/sagemcom_api/client.py @@ -29,6 +29,7 @@ UINT_MAX, XMO_ACCESS_RESTRICTION_ERR, XMO_AUTHENTICATION_ERR, + XMO_INVALID_PATH_ERR, XMO_INVALID_SESSION_ERR, XMO_LOGIN_RETRY_ERR, XMO_MAX_SESSION_COUNT_ERR, @@ -261,7 +262,7 @@ async def __post(self, url, data): if action_error_desc == XMO_NON_WRITABLE_PARAMETER_ERR: raise NonWritableParameterException(action_error) - if action_error_desc == XMO_UNKNOWN_PATH_ERR: + if action_error_desc in (XMO_INVALID_PATH_ERR, XMO_UNKNOWN_PATH_ERR): raise UnknownPathException(action_error) if action_error_desc == XMO_MAX_SESSION_COUNT_ERR: diff --git a/sagemcom_api/const.py b/sagemcom_api/const.py index 4e35a4e..db6db6a 100644 --- a/sagemcom_api/const.py +++ b/sagemcom_api/const.py @@ -7,6 +7,7 @@ XMO_ACCESS_RESTRICTION_ERR = "XMO_ACCESS_RESTRICTION_ERR" XMO_AUTHENTICATION_ERR = "XMO_AUTHENTICATION_ERR" +XMO_INVALID_PATH_ERR = "XMO_INVALID_PATH_ERR" XMO_INVALID_SESSION_ERR = "XMO_INVALID_SESSION_ERR" XMO_NON_WRITABLE_PARAMETER_ERR = "XMO_NON_WRITABLE_PARAMETER_ERR" XMO_NO_ERR = "XMO_NO_ERR" diff --git a/tests/conftest.py b/tests/conftest.py index 031abd4..5b39653 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -64,6 +64,12 @@ def xpath_value_response() -> dict[str, Any]: return load_fixture("xpath_value.json") +@pytest.fixture +def xpath_invalid_path_response() -> dict[str, Any]: + """Mock response for an invalid XPath error.""" + return load_fixture("xpath_invalid_path.json") + + @pytest.fixture def mock_session_factory(): """Create a factory for mock aiohttp ClientSession. diff --git a/tests/fixtures/xpath_invalid_path.json b/tests/fixtures/xpath_invalid_path.json new file mode 100644 index 0000000..b01cb85 --- /dev/null +++ b/tests/fixtures/xpath_invalid_path.json @@ -0,0 +1,32 @@ +{ + "reply": { + "uid": 0, + "id": 0, + "error": { + "code": 16777236, + "description": "XMO_REQUEST_ACTION_ERR" + }, + "actions": [ + { + "uid": 1, + "id": 0, + "error": { + "code": 16777242, + "description": "XMO_INVALID_PATH_ERR" + }, + "callbacks": [ + { + "uid": 1, + "result": { + "code": 16777242, + "description": "XMO_INVALID_PATH_ERR" + }, + "xpath": "Device/Unsupported/Path", + "parameters": {} + } + ] + } + ], + "events": [] + } +} diff --git a/tests/unit/test_client_basic.py b/tests/unit/test_client_basic.py index d7ba7db..4aa3112 100644 --- a/tests/unit/test_client_basic.py +++ b/tests/unit/test_client_basic.py @@ -6,7 +6,7 @@ from sagemcom_api.client import SagemcomClient from sagemcom_api.enums import EncryptionMethod -from sagemcom_api.exceptions import AuthenticationException +from sagemcom_api.exceptions import AuthenticationException, UnknownPathException @pytest.mark.asyncio @@ -103,6 +103,30 @@ async def test_get_value_by_xpath_url_encoding(mock_session_factory, login_succe # will be covered later +@pytest.mark.asyncio +async def test_get_value_by_xpath_invalid_path(mock_session_factory, xpath_invalid_path_response): + """Test XMO_INVALID_PATH_ERR raises UnknownPathException with error details.""" + mock_session = mock_session_factory([xpath_invalid_path_response]) + client = SagemcomClient( + host="192.168.1.1", + username="admin", + password="admin", + authentication_method=EncryptionMethod.MD5, + session=mock_session, + ) + + with pytest.raises(UnknownPathException) as exc_info: + await client.get_value_by_xpath("Device/Unsupported/Path") + + assert exc_info.value.args == ( + { + "code": 16777242, + "description": "XMO_INVALID_PATH_ERR", + }, + ) + assert mock_session.post.call_count == 1 + + @pytest.mark.asyncio async def test_login_with_preconfigured_fixture(mock_client_sha512): """Test login using pre-configured client fixture.