Files
archive-clients/tests/test_routes.py
T

94 lines
3.7 KiB
Python

import tempfile
import unittest
from datetime import datetime, timezone
from pathlib import Path, PurePosixPath
from archive_clients.config import RootMapping
from archive_clients.routes import discover_routes
from archive_control.v1 import route_pb2
class RouteDiscoveryTests(unittest.TestCase):
def test_pairwise_routes_are_discovered_and_unsafe_folders_ignored(self):
with tempfile.TemporaryDirectory() as directory:
roots = RootMapping(PurePosixPath("/sync"), Path(directory))
configuration = {"folders": [
{
"id": "route-b", "path": "/sync/routes/b",
"type": "sendreceive",
"devices": [{"deviceID": "LOCAL"}, {"deviceID": "PEER"}],
"label": "Archive Control route-b",
},
{
"id": "route-a", "path": "/sync/routes/a",
"type": "sendonly",
"devices": [{"deviceID": "LOCAL"}, {"deviceID": "PEER"}],
},
{
"id": "outside", "path": "/other/path",
"type": "sendreceive", "devices": [],
},
]}
routes = discover_routes(
configuration, "LOCAL", roots, True,
datetime(2026, 1, 2, tzinfo=timezone.utc),
)
self.assertEqual([route.route_id for route in routes], ["route-a", "route-b"])
self.assertEqual(routes[0].state, route_pb2.ROUTE_STATE_UNSUPPORTED)
self.assertEqual(routes[1].state, route_pb2.ROUTE_STATE_DISCOVERED)
self.assertEqual(list(routes[1].peer_syncthing_device_ids), ["PEER"])
self.assertEqual(routes[1].local_relative_path, "routes/b")
self.assertFalse(routes[1].archive_control_created)
def test_home_relative_paths_are_normalized_under_api_root(self):
with tempfile.TemporaryDirectory() as directory:
roots = RootMapping(PurePosixPath("/var/syncthing"), Path(directory))
routes = discover_routes(
{"folders": [{
"id": "DownloadsSync",
"path": "~/DownloadsSync",
"type": "sendreceive",
"devices": [
{"deviceID": "LOCAL"},
{"deviceID": "ARCHIVE"},
],
}]},
"LOCAL",
roots,
True,
)
self.assertEqual(len(routes), 1)
self.assertEqual(routes[0].route_id, "DownloadsSync")
self.assertEqual(routes[0].local_relative_path, "DownloadsSync")
self.assertEqual(routes[0].state, route_pb2.ROUTE_STATE_DISCOVERED)
def test_discovery_accepts_a_safe_local_folder_override(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
override = root / "qb/Sync"
override.mkdir(parents=True)
roots = RootMapping(
PurePosixPath("/var/syncthing"),
root / "sync",
((PurePosixPath("/var/syncthing/DownloadsSync"), override),),
)
routes = discover_routes(
{"folders": [{
"id": "DownloadsSync",
"path": "~/DownloadsSync",
"type": "sendreceive",
"devices": [
{"deviceID": "LOCAL"},
{"deviceID": "ARCHIVE"},
],
}]},
"LOCAL",
roots,
True,
)
self.assertEqual([route.route_id for route in routes], ["DownloadsSync"])
if __name__ == "__main__":
unittest.main()