Files
MeshChatX/tests/backend/test_rnpath_logic.py
Sudo-Ivan c4674992e0
Some checks failed
CI / test-backend (push) Successful in 4s
CI / build-frontend (push) Successful in 1m49s
CI / test-lang (push) Successful in 1m47s
CI / test-backend (pull_request) Successful in 24s
Build and Publish Docker Image / build (pull_request) Has been skipped
CI / test-lang (pull_request) Successful in 52s
OSV-Scanner PR Scan / scan-pr (pull_request) Successful in 24s
CI / lint (push) Failing after 5m14s
CI / lint (pull_request) Failing after 5m8s
Tests / test (push) Failing after 9m17s
CI / build-frontend (pull_request) Successful in 9m48s
Benchmarks / benchmark (push) Successful in 14m52s
Benchmarks / benchmark (pull_request) Successful in 15m9s
Build and Publish Docker Image / build-dev (pull_request) Successful in 13m47s
Tests / test (pull_request) Failing after 25m50s
Build Test / Build and Test (pull_request) Successful in 53m37s
Build Test / Build and Test (push) Successful in 56m30s
lots of fixes, changes, styling, fixing outbound calls, rnode-flasher.
2026-01-04 15:57:49 -06:00

129 lines
4.1 KiB
Python

import pytest
import json
from unittest.mock import MagicMock, patch, AsyncMock
from meshchatx.meshchat import ReticulumMeshChat
import RNS
@pytest.fixture
def temp_dir(tmp_path):
return str(tmp_path)
@pytest.fixture
def mock_rns_minimal():
with (
patch("RNS.Reticulum") as mock_rns,
patch("RNS.Transport") as mock_transport,
patch("LXMF.LXMRouter"),
patch("meshchatx.meshchat.get_file_path", return_value="/tmp/mock_path"),
):
mock_rns_instance = mock_rns.return_value
mock_rns_instance.configpath = "/tmp/mock_config"
mock_rns_instance.is_connected_to_shared_instance = False
mock_rns_instance.transport_enabled.return_value = True
# Setup RNS.Transport mock constants and tables
mock_transport.path_table = {}
mock_transport.path_states = {}
mock_transport.STATE_UNKNOWN = 0
mock_transport.STATE_RESPONSIVE = 1
mock_transport.STATE_UNRESPONSIVE = 2
# Path management mocks
mock_rns_instance.get_path_table.return_value = []
mock_rns_instance.get_rate_table.return_value = []
mock_rns_instance.drop_path.return_value = True
mock_rns_instance.drop_all_via.return_value = True
mock_rns_instance.drop_announce_queues = MagicMock()
mock_id = MagicMock(spec=RNS.Identity)
mock_id.hash = b"\x00" * 32
mock_id.hexhash = mock_id.hash.hex()
mock_id.get_private_key.return_value = b"test_private_key"
yield mock_id
@pytest.mark.asyncio
async def test_rnpath_table_endpoint(mock_rns_minimal, temp_dir):
with patch("meshchatx.meshchat.generate_ssl_certificate"):
app_instance = ReticulumMeshChat(
identity=mock_rns_minimal,
storage_dir=temp_dir,
reticulum_config_dir=temp_dir,
)
entry = {
"hash": b"\x01" * 32,
"hops": 1,
"via": b"\x02" * 32,
"interface": "UDP",
"expires": 1234567890,
}
app_instance.reticulum.get_path_table.return_value = [entry]
request = MagicMock()
request.query = {}
handler = next(
r.handler
for r in app_instance.get_routes()
if r.path == "/api/v1/rnpath/table"
)
response = await handler(request)
data = json.loads(response.body)
assert len(data["table"]) == 1
assert data["table"][0]["hash"] == entry["hash"].hex()
@pytest.mark.asyncio
async def test_rnpath_request_endpoint(mock_rns_minimal, temp_dir):
with (
patch("meshchatx.meshchat.generate_ssl_certificate"),
patch.object(RNS.Transport, "request_path") as mock_request_path,
):
app_instance = ReticulumMeshChat(
identity=mock_rns_minimal,
storage_dir=temp_dir,
reticulum_config_dir=temp_dir,
)
target_hash = "a" * 32
request = MagicMock()
request.json = AsyncMock(return_value={"destination_hash": target_hash})
handler = next(
r.handler
for r in app_instance.get_routes()
if r.path == "/api/v1/rnpath/request"
)
response = await handler(request)
mock_request_path.assert_called_with(bytes.fromhex(target_hash))
assert json.loads(response.body)["success"] is True
@pytest.mark.asyncio
async def test_rnpath_drop_endpoint(mock_rns_minimal, temp_dir):
with patch("meshchatx.meshchat.generate_ssl_certificate"):
app_instance = ReticulumMeshChat(
identity=mock_rns_minimal,
storage_dir=temp_dir,
reticulum_config_dir=temp_dir,
)
target_hash = "b" * 32
request = MagicMock()
request.json = AsyncMock(return_value={"destination_hash": target_hash})
handler = next(
r.handler
for r in app_instance.get_routes()
if r.path == "/api/v1/rnpath/drop"
)
response = await handler(request)
app_instance.reticulum.drop_path.assert_called_with(bytes.fromhex(target_hash))
assert json.loads(response.body)["success"] is True