Compare commits

..

2 Commits

Author SHA1 Message Date
Josiah Baldwin
fe4c2fe874 Fixed connection errors not raising immediately 2025-01-08 13:26:04 -08:00
Josiah Baldwin
bb7cf17cd3 Added test for invalid auth 2025-01-08 13:23:21 -08:00
3 changed files with 29 additions and 24 deletions

View File

@@ -2,7 +2,9 @@ class MeshCtrlError(Exception):
"""
Base class for Meshctrl errors
"""
pass
def __init__(self, message, *args, **kwargs):
self.message = message
super().__init__(message, *args, **kwargs)
class ServerError(MeshCtrlError):
"""
@@ -25,6 +27,7 @@ class FileTransferError(MeshCtrlError):
"""
def __init__(self, message, stats):
self.stats = stats
super().__init__(message)
class FileTransferCancelled(FileTransferError):
"""

View File

@@ -140,17 +140,20 @@ def compare_dict(dict1, dict2):
return False
def _check_socket(f):
async def _check_errs(self):
if not self.alive and self._main_loop_error is not None:
raise self._main_loop_error
elif not self.alive and self.initialized.is_set():
raise exceptions.SocketError("Socket Closed")
@functools.wraps(f)
async def wrapper(self, *args, **kwargs):
try:
async with asyncio.TaskGroup() as tg:
tg.create_task(asyncio.wait_for(self.initialized.wait(), 10))
tg.create_task(asyncio.wait_for(self._socket_open.wait(), 10))
await asyncio.wait_for(self.initialized.wait(), 10)
await _check_errs(self)
await asyncio.wait_for(self._socket_open.wait(), 10)
finally:
if not self.alive and self._main_loop_error is not None:
raise self._main_loop_error
elif not self.alive and self.initialized.is_set():
raise exceptions.SocketError("Socket Closed")
await _check_errs(self)
return await f(self, *args, **kwargs)
return wrapper

View File

@@ -5,6 +5,8 @@ import meshctrl
import requests
import random
import io
import traceback
import time
thisdir = os.path.dirname(os.path.realpath(__file__))
async def test_admin(env):
@@ -77,6 +79,17 @@ async def test_users(env):
pass
else:
raise Exception("Connected with no password")
start = time.time()
try:
async with meshctrl.Session(env.mcurl, user="admin", password="The wrong password", ignore_ssl=True) as admin_session:
pass
except* meshctrl.exceptions.ServerError as eg:
assert str(eg.exceptions[0]) == "Invalid Auth" or eg.exceptions[0].message == "Invalid Auth", "Didn't get invalid auth message"
assert time.time() - start < 10, "Invalid auth wasn't raised until after timeout"
pass
else:
raise Exception("Connected with bad password")
async with meshctrl.Session(env.mcurl+"/", user="admin", password=env.users["admin"], ignore_ssl=True) as admin_session,\
meshctrl.Session(env.mcurl, user="privileged", password=env.users["privileged"], ignore_ssl=True) as privileged_session,\
meshctrl.Session(env.mcurl, user="unprivileged", password=env.users["unprivileged"], ignore_ssl=True) as unprivileged_session:
@@ -206,30 +219,17 @@ async def test_mesh_device(env):
r = await admin_session.list_devices(details=True, timeout=10)
print("\ninfo list_devices_details: {}\n".format(r))
assert len(r), "No devices found"
assert r[0].mesh is not None, "No mesh found"
assert r[0].mesh.name is not None, "Mesh details not filled correctly"
r = await admin_session.list_devices(group=mesh.name, timeout=10)
print("\ninfo list_devices_group: {}\n".format(r))
assert len(r), "No devices found"
assert r[0].mesh is not None, "No mesh found"
assert r[0].mesh.name is not None, "Mesh details not filled correctly"
r = await admin_session.list_devices(meshid=mesh.meshid, timeout=10)
print("\ninfo list_devices_meshid: {}\n".format(r))
assert len(r), "No devices found"
assert r[0].mesh is not None, "No mesh found"
assert r[0].mesh.name is not None, "Mesh details not filled correctly"
# Test editing device info propagating correctly
assert await admin_session.edit_device(agent.nodeid, name="new_name", description="New Description", tags="device", consent=meshctrl.constants.ConsentFlags.all, timeout=10), "Failed to edit device info"
r = await privileged_session.device_info(agent.nodeid, timeout=10)
print("\ninfo privileged_device_info: {}\n".format(r))
assert r.name == "new_name", "New name did not propagate to other sessions"
assert r.mesh is not None, "No mesh found"
assert r.mesh.name is not None, "Mesh details not filled correctly"
assert (await privileged_session.device_info(agent.nodeid, timeout=10)).name == "new_name", "New name did not propagate to other sessions"
assert await admin_session.edit_device(agent.nodeid, consent=meshctrl.constants.ConsentFlags.none, timeout=10), "Failed to edit device info"
@@ -265,8 +265,7 @@ async def test_mesh_device(env):
# Test getting individual device info
r = await unprivileged_session.device_info(agent.nodeid, timeout=10)
print("\ninfo unprivileged_device_info: {}\n".format(r))
assert r.mesh is None or r.mesh.name is None, "Unprivileged user can see mesh"
print("\ninfo device_info: {}\n".format(r))
# This device info includes the mesh ID of the device, even though the user doesn't have acces to that mesh. That's odd.
# assert r.meshid is None, "Individual device is exposing its meshid"