diff --git a/.gitignore b/.gitignore index 21a4183..ab7cd1c 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,4 @@ __pycache__/ # Virtual env -.venv \ No newline at end of file +.venv diff --git a/noxfile.py b/noxfile.py index ec03439..b69a05a 100644 --- a/noxfile.py +++ b/noxfile.py @@ -161,7 +161,7 @@ def mypy(session: Session) -> None: def tests(session: Session) -> None: """Run the test suite.""" session.install(".[cli]") - session.install("coverage[toml]", "pytest", "pygments") + session.install("coverage[toml]", "pytest", "pygments", "pytest-asyncio", "respx") try: session.run("coverage", "run", "--parallel", "-m", "pytest", *session.posargs) finally: diff --git a/src/sfrbox_api/bridge.py b/src/sfrbox_api/bridge.py index 8d1d3d9..725f21b 100644 --- a/src/sfrbox_api/bridge.py +++ b/src/sfrbox_api/bridge.py @@ -30,8 +30,8 @@ async def _send_get(self, method: str, **kwargs: str) -> ET.Element: raise Exception(f"Query failed: {err.get('msg')}") if stat != "ok": raise Exception(f"Response was not ok: {stat}") - result = element.find(method.rsplit(method)[0]) - assert result + result = element.find(method.split(".")[0]) + assert result is not None return result async def dsl_getInfo(self) -> DslInfo: diff --git a/tests/cli/__init__.py b/tests/cli/__init__.py new file mode 100644 index 0000000..46909e2 --- /dev/null +++ b/tests/cli/__init__.py @@ -0,0 +1 @@ +"""Test suite for the sfrbox_api CLI.""" diff --git a/tests/test_main.py b/tests/cli/test_main.py similarity index 100% rename from tests/test_main.py rename to tests/cli/test_main.py diff --git a/tests/fixtures/dsl.xml b/tests/fixtures/dsl.xml new file mode 100644 index 0000000..1b36179 --- /dev/null +++ b/tests/fixtures/dsl.xml @@ -0,0 +1,4 @@ + + + + diff --git a/tests/fixtures/ftth.xml b/tests/fixtures/ftth.xml new file mode 100644 index 0000000..83e2497 --- /dev/null +++ b/tests/fixtures/ftth.xml @@ -0,0 +1,4 @@ + + + + diff --git a/tests/fixtures/system.xml b/tests/fixtures/system.xml new file mode 100644 index 0000000..0ede808 --- /dev/null +++ b/tests/fixtures/system.xml @@ -0,0 +1,4 @@ + + + + diff --git a/tests/fixtures/wan.xml b/tests/fixtures/wan.xml new file mode 100644 index 0000000..9f1d28f --- /dev/null +++ b/tests/fixtures/wan.xml @@ -0,0 +1,4 @@ + + + + diff --git a/tests/test_bridge.py b/tests/test_bridge.py new file mode 100644 index 0000000..404cb7d --- /dev/null +++ b/tests/test_bridge.py @@ -0,0 +1,56 @@ +"""Test cases for the __main__ module.""" +import httpx +import pathlib +import pytest +import respx + +from sfrbox_api.bridge import SFRBox +from sfrbox_api.models import DslInfo, FtthInfo, SystemInfo, WanInfo + +def _load_fixture(filename:str) -> str: + return pathlib.Path(__file__).parent.joinpath("fixtures", filename).read_text() + +@respx.mock +@pytest.mark.asyncio +async def test_bridge_dsl() -> None: + """It exits with a status code of zero.""" + respx.get("http://192.168.0.1/api/1.0/?method=dsl.getInfo").respond(text=_load_fixture("dsl.xml")) + async with httpx.AsyncClient() as client: + + box = SFRBox(ip="192.168.0.1", client=client) + dsl = await box.dsl_getInfo() + assert dsl == DslInfo(linemode='ADSL2+', uptime='450796', counter='16', crc='0', status='up', noise_down='5.8', noise_up='6.0', attenuation_down='28.5', attenuation_up='20.8', rate_down='5549', rate_up='187', line_status='No Defect', training='Showtime') + +@respx.mock +@pytest.mark.asyncio +async def test_bridge_ftth() -> None: + """It exits with a status code of zero.""" + respx.get("http://192.168.0.1/api/1.0/?method=ftth.getInfo").respond(text=_load_fixture("ftth.xml")) + async with httpx.AsyncClient() as client: + + box = SFRBox(ip="192.168.0.1", client=client) + dsl = await box.ftth_getInfo() + assert dsl == FtthInfo(status='down', wanfibre='out') + +@respx.mock +@pytest.mark.asyncio +async def test_bridge_system() -> None: + """It exits with a status code of zero.""" + respx.get("http://192.168.0.1/api/1.0/?method=system.getInfo").respond(text=_load_fixture("system.xml")) + async with httpx.AsyncClient() as client: + + box = SFRBox(ip="192.168.0.1", client=client) + dsl = await box.system_getInfo() + print(dsl) + assert dsl == SystemInfo(product_id='NB6VAC-FXC-r0', mac_addr='e4:5d:51:00:11:22', net_mode='router', net_infra='adsl', uptime='2353575', version_mainfirmware='NB6VAC-MAIN-R4.0.44k', version_rescuefirmware='NB6VAC-MAIN-R4.0.44k', version_bootloader='NB6VAC-BOOTLOADER-R4.0.8', version_dsldriver='NB6VAC-XDSL-A2pv6F039p', current_datetime='202212282233', refclient='', idur='RP3P85K', alimvoltage='12251', temperature='27560', serial_number='XU1001001001001001') + +@respx.mock +@pytest.mark.asyncio +async def test_bridge_wan() -> None: + """It exits with a status code of zero.""" + respx.get("http://192.168.0.1/api/1.0/?method=wan.getInfo").respond(text=_load_fixture("wan.xml")) + async with httpx.AsyncClient() as client: + + box = SFRBox(ip="192.168.0.1", client=client) + dsl = await box.wan_getInfo() + assert dsl == WanInfo(status='up', uptime='2353338', ip_addr='88.219.146.196', infra='adsl', mode='adsl/routed', infra6='', status6='down', uptime6='', ipv6_addr='') diff --git a/tests/test_helper.py b/tests/test_helper.py new file mode 100644 index 0000000..4b9b256 --- /dev/null +++ b/tests/test_helper.py @@ -0,0 +1,12 @@ +"""Test cases for the __main__ module.""" +from sfrbox_api.helpers import compute_hash + + +def test_hash() -> None: + """It matches expected hash.""" + result = compute_hash("afd1baa4cb261bfc08ec2dc0ade3b4", "admin", "password") + assert len(result) == 64 * 2 + assert result == ( + "3e89f9170f9e64e5132aa6f72a520ffd45f952f259872a60e9acde5dba45ff64" + "88cc72099f52b8414e5b182b8e1c2b4b87863bd67b0134904adfe00ae6c6499e" + )