Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet committed Dec 28, 2022
1 parent 1938ddd commit 59cd9d0
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
__pycache__/

# Virtual env
.venv
.venv
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/sfrbox_api/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Test suite for the sfrbox_api CLI."""
File renamed without changes.
4 changes: 4 additions & 0 deletions tests/fixtures/dsl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok" version="1.0">
<dsl status="up" linemode="ADSL2+" uptime="450796" counter="16" crc="0" 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" />
</rsp>
4 changes: 4 additions & 0 deletions tests/fixtures/ftth.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok" version="1.0">
<ftth status="down" wanfibre="out"/>
</rsp>
4 changes: 4 additions & 0 deletions tests/fixtures/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok" version="1.0">
<system product_id="NB6VAC-FXC-r0" serial_number="XU1001001001001001" 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" />
</rsp>
4 changes: 4 additions & 0 deletions tests/fixtures/wan.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok" version="1.0">
<wan status="up" uptime="2353338" ip_addr="88.219.146.196" infra="adsl" mode="adsl/routed" infra6="" status6="down" uptime6="" ipv6_addr="" />
</rsp>
56 changes: 56 additions & 0 deletions tests/test_bridge.py
Original file line number Diff line number Diff line change
@@ -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='')
12 changes: 12 additions & 0 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit 59cd9d0

Please sign in to comment.