Skip to content

Commit 2f3acc3

Browse files
authored
Add support for factory reset (#60)
* Add support for factory reset * Fix incorrectly named test * Format code
1 parent c93161e commit 2f3acc3

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

python_otbr_api/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class OTBRError(Exception):
1717
"""Raised on error."""
1818

1919

20+
class FactoryResetNotSupportedError(OTBRError):
21+
"""Raised when attempting to factory reset a router which does not support it."""
22+
23+
2024
class ThreadNetworkActiveError(OTBRError):
2125
"""Raised on attempts to modify the active dataset when thread network is active."""
2226

@@ -32,6 +36,20 @@ def __init__(
3236
self._url = url
3337
self._timeout = timeout
3438

39+
async def factory_reset(self) -> None:
40+
"""Factory reset the router."""
41+
42+
response = await self._session.delete(
43+
f"{self._url}/node",
44+
timeout=aiohttp.ClientTimeout(total=10),
45+
)
46+
47+
if response.status == HTTPStatus.METHOD_NOT_ALLOWED:
48+
raise FactoryResetNotSupportedError
49+
50+
if response.status != HTTPStatus.OK:
51+
raise OTBRError(f"unexpected http status {response.status}")
52+
3553
async def set_enabled(self, enabled: bool) -> None:
3654
"""Enable or disable the router."""
3755

tests/test_init.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,39 @@
3838
}
3939

4040

41+
async def test_factory_reset(aioclient_mock: AiohttpClientMocker) -> None:
42+
"""Test factory_reset."""
43+
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())
44+
45+
aioclient_mock.delete(f"{BASE_URL}/node", status=HTTPStatus.OK)
46+
47+
await otbr.factory_reset()
48+
assert aioclient_mock.call_count == 1
49+
assert aioclient_mock.mock_calls[-1][0] == "DELETE"
50+
assert aioclient_mock.mock_calls[-1][1].path == "/node"
51+
assert aioclient_mock.mock_calls[-1][2] is None
52+
53+
54+
async def test_factory_reset_unsupported(aioclient_mock: AiohttpClientMocker) -> None:
55+
"""Test factory_reset is unsupported."""
56+
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())
57+
58+
aioclient_mock.delete(f"{BASE_URL}/node", status=HTTPStatus.METHOD_NOT_ALLOWED)
59+
60+
with pytest.raises(python_otbr_api.FactoryResetNotSupportedError):
61+
await otbr.factory_reset()
62+
63+
64+
async def test_factory_reset_201(aioclient_mock: AiohttpClientMocker) -> None:
65+
"""Test factory_reset with error."""
66+
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())
67+
68+
aioclient_mock.delete(f"{BASE_URL}/node", status=HTTPStatus.CREATED)
69+
70+
with pytest.raises(python_otbr_api.OTBRError):
71+
await otbr.factory_reset()
72+
73+
4174
async def test_set_enabled(aioclient_mock: AiohttpClientMocker) -> None:
4275
"""Test set_enabled."""
4376
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())

0 commit comments

Comments
 (0)