Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 4 additions & 31 deletions tests/test_glinet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from gli4py.error_handling import NonZeroResponse
from gli4py.glinet import GLinet, NEW_VPN_CLIENT_VERSION

# All tests share one GLinet client (and one aiohttp session), so they must
# run on a single event loop.
pytestmark = pytest.mark.asyncio(loop_scope="module")

router = GLinet(base_url="http://192.168.0.1/rpc")
PERFORM_DISTRUPTIVE_TESTS = False

Expand Down Expand Up @@ -41,24 +45,13 @@
]


@pytest.fixture(scope="session")
def event_loop():
"""Create a new event loop for each test session."""
policy = asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
yield loop
loop.close()


@pytest.mark.asyncio
async def test_router_reachable() -> None:
"""Test if the router is reachable."""
response = await router.router_reachable()
assert response
print(response)


@pytest.mark.asyncio
async def test_login() -> None:
"""Test logging into the router."""
with open("router_pwd", "r", encoding="utf-8") as file:
Expand All @@ -69,7 +62,6 @@ async def test_login() -> None:
print(router.sid)


@pytest.mark.asyncio
async def test_router_info() -> None:
"""Test retrieving router information."""
response = await router.router_info()
Expand All @@ -79,7 +71,6 @@ async def test_router_info() -> None:
print(response)


@pytest.mark.asyncio
async def test_router_get_status() -> None:
"""Test retrieving router status."""
response = await router.router_get_status()
Expand All @@ -93,7 +84,6 @@ async def test_router_get_status() -> None:
print(response)


@pytest.mark.asyncio
async def test_router_get_load() -> None:
"""Test retrieving router load information."""
response = await router.router_get_load()
Expand All @@ -103,23 +93,20 @@ async def test_router_get_load() -> None:
print(response)


@pytest.mark.asyncio
async def test_router_mac() -> None:
"""Test retrieving the router's MAC address."""
response = await router.router_mac()
assert "factory_mac" in response
print(response)


@pytest.mark.asyncio
async def test_connected_clients() -> None:
"""Test retrieving connected clients."""
clients = await router.connected_clients()
print(len(clients))
assert len(clients) > 0


@pytest.mark.asyncio
async def test_wifi_ifaces_get() -> None:
"""Test retrieving WiFi interfaces."""
wifi_ifaces = await router.wifi_ifaces_get()
Expand All @@ -131,7 +118,6 @@ async def test_wifi_ifaces_get() -> None:
assert "key" in iface


@pytest.mark.asyncio
async def test_wifi_ifaces_set_enabled() -> None:
"""Test enabling/disabling a WiFi interface."""

Expand All @@ -151,7 +137,6 @@ async def test_wifi_ifaces_set_enabled() -> None:
assert iface_enabled_after != iface_enabled


@pytest.mark.asyncio
async def test_connected_to_internet() -> None:
"""Test checking if the router is connected to the internet."""
response = await router.connected_to_internet()
Expand All @@ -160,7 +145,6 @@ async def test_connected_to_internet() -> None:
assert "ip" in response


@pytest.mark.asyncio
async def test_ping() -> None:
"""Test pinging a host."""
response = await router.ping("google.com")
Expand All @@ -172,15 +156,13 @@ async def test_ping() -> None:
assert not response


@pytest.mark.asyncio
async def test_wireguard_client_list() -> None:
"""Test retrieving the list of WireGuard clients."""
response = await router.wireguard_client_list()
print(response)
# assert(response['enable'] in [True,False])


@pytest.mark.asyncio
async def test_wireguard_client_state() -> None:
"""Test retrieving the state of the WireGuard client."""
# We need to get the proper firmware version for this
Expand All @@ -198,7 +180,6 @@ async def test_wireguard_client_state() -> None:
assert first_status["status"] in [0, 1, 2]


@pytest.mark.asyncio
async def test_wireguard_start() -> None:
"""Test starting the WireGuard client."""
assert PERFORM_DISTRUPTIVE_TESTS, (
Expand Down Expand Up @@ -236,7 +217,6 @@ async def test_wireguard_start() -> None:
pytest.fail("WireGuard client took too long to connect.")


@pytest.mark.asyncio
async def test_wireguard_stop() -> None:
"""Test stopping the WireGuard client."""
assert PERFORM_DISTRUPTIVE_TESTS, (
Expand Down Expand Up @@ -278,39 +258,34 @@ async def test_wireguard_stop() -> None:
pytest.fail("WireGuard client took too long to disconnect.")


@pytest.mark.asyncio
async def test_tailscale_status() -> None:
"""Test retrieving the Tailscale status."""
response = await router._tailscale_status() # pylint: disable=protected-access
print(response)
assert dict(response).get("status", 0) in [1, 2, 3, 4] or response == []


@pytest.mark.asyncio
async def test_tailscale_connection() -> None:
"""Test retrieving the Tailscale connection state."""
response = await router.tailscale_connection_state()
print(response)
assert response in [TailscaleConnection.DISCONNECTED, TailscaleConnection.CONNECTED]


@pytest.mark.asyncio
async def test_tailscale_configured() -> None:
"""Test checking if Tailscale is configured."""
response = await router.tailscale_configured()
print("Tailscale configured:", response)
assert response in [True, False]


@pytest.mark.asyncio
async def test_tailscale_get_config() -> None:
"""Test retrieving the Tailscale configuration."""
response = await router._tailscale_get_config() # pylint: disable=protected-access
print(response["enabled"])
assert response["enabled"] in [True, False]


@pytest.mark.asyncio
async def test_tailscale_start() -> None:
"""Test starting Tailscale."""
assert PERFORM_DISTRUPTIVE_TESTS, (
Expand All @@ -321,7 +296,6 @@ async def test_tailscale_start() -> None:
assert result in [True, False]


@pytest.mark.asyncio
async def test_tailscale_stop() -> None:
"""Test stopping Tailscale."""
assert PERFORM_DISTRUPTIVE_TESTS, (
Expand All @@ -332,7 +306,6 @@ async def test_tailscale_stop() -> None:
assert result in [True, False]


@pytest.mark.asyncio
async def test_router_reboot() -> None:
"""Test rebooting the router."""
assert PERFORM_DISTRUPTIVE_TESTS, (
Expand Down