test_virtual_printer.py 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  1. """Unit tests for Virtual Printer services.
  2. Tests the virtual printer manager, FTP server, and SSDP server components.
  3. """
  4. import asyncio
  5. from pathlib import Path
  6. from unittest.mock import AsyncMock, MagicMock, patch
  7. import pytest
  8. class TestVirtualPrinterInstance:
  9. """Tests for VirtualPrinterInstance class."""
  10. @pytest.fixture
  11. def instance(self, tmp_path):
  12. """Create a VirtualPrinterInstance with test defaults."""
  13. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  14. return VirtualPrinterInstance(
  15. vp_id=1,
  16. name="TestPrinter",
  17. mode="immediate",
  18. model="C11",
  19. access_code="12345678",
  20. serial_suffix="391800001",
  21. base_dir=tmp_path,
  22. )
  23. # ========================================================================
  24. # Tests for instance properties
  25. # ========================================================================
  26. def test_instance_stores_parameters(self, instance):
  27. """Verify constructor stores parameters correctly."""
  28. assert instance.id == 1
  29. assert instance.name == "TestPrinter"
  30. assert instance.mode == "immediate"
  31. assert instance.model == "C11"
  32. assert instance.access_code == "12345678"
  33. assert instance.serial_suffix == "391800001"
  34. def test_instance_serial_property(self, instance):
  35. """Verify serial is generated from model prefix + suffix."""
  36. # C11 = P1P, prefix = 01S00A
  37. assert instance.serial == "01S00A391800001"
  38. def test_instance_serial_x1c(self, tmp_path):
  39. """Verify X1C serial uses correct prefix."""
  40. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  41. inst = VirtualPrinterInstance(
  42. vp_id=2,
  43. name="X1C",
  44. mode="immediate",
  45. model="BL-P001",
  46. access_code="12345678",
  47. serial_suffix="391800002",
  48. base_dir=tmp_path,
  49. )
  50. assert inst.serial == "00M00A391800002"
  51. def test_instance_is_proxy_false(self, instance):
  52. """Verify is_proxy is False for non-proxy mode."""
  53. assert instance.is_proxy is False
  54. def test_instance_is_proxy_true(self, tmp_path):
  55. """Verify is_proxy is True for proxy mode."""
  56. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  57. inst = VirtualPrinterInstance(
  58. vp_id=3,
  59. name="Proxy",
  60. mode="proxy",
  61. model="C11",
  62. access_code="",
  63. serial_suffix="391800003",
  64. target_printer_ip="192.168.1.100",
  65. base_dir=tmp_path,
  66. )
  67. assert inst.is_proxy is True
  68. def test_instance_is_running_with_active_tasks(self, instance):
  69. """Verify is_running is True when tasks are active."""
  70. mock_task = MagicMock()
  71. mock_task.done.return_value = False
  72. instance._tasks = [mock_task]
  73. assert instance.is_running is True
  74. def test_instance_is_running_with_no_tasks(self, instance):
  75. """Verify is_running is False when no tasks."""
  76. assert instance.is_running is False
  77. def test_instance_creates_directories(self, instance, tmp_path):
  78. """Verify instance creates upload and cert directories."""
  79. assert (tmp_path / "uploads" / "1").exists()
  80. assert (tmp_path / "uploads" / "1" / "cache").exists()
  81. assert (tmp_path / "certs" / "1").exists()
  82. # ========================================================================
  83. # Tests for status
  84. # ========================================================================
  85. def test_get_status_returns_correct_format(self, instance):
  86. """Verify get_status returns expected fields."""
  87. instance._pending_files = {"file1.3mf": Path("/tmp/file1.3mf")} # nosec B108
  88. mock_task = MagicMock(done=MagicMock(return_value=False))
  89. instance._tasks = [mock_task]
  90. status = instance.get_status()
  91. assert status["running"] is True
  92. assert status["pending_files"] == 1
  93. def test_get_status_not_running(self, instance):
  94. """Verify get_status when no tasks."""
  95. status = instance.get_status()
  96. assert status["running"] is False
  97. assert status["pending_files"] == 0
  98. # ========================================================================
  99. # Tests for file handling
  100. # ========================================================================
  101. @pytest.mark.asyncio
  102. async def test_on_file_received_adds_to_pending(self, instance):
  103. """Verify received file is added to pending list in review mode."""
  104. instance.mode = "review"
  105. file_path = Path("/tmp/test.3mf") # nosec B108
  106. with patch.object(instance, "_queue_file", new_callable=AsyncMock) as mock_queue:
  107. await instance.on_file_received(file_path, "192.168.1.100")
  108. assert "test.3mf" in instance._pending_files
  109. mock_queue.assert_called_once()
  110. @pytest.mark.asyncio
  111. async def test_on_file_received_archives_immediately(self, instance):
  112. """Verify file is archived in immediate mode."""
  113. file_path = Path("/tmp/test.3mf") # nosec B108
  114. with patch.object(instance, "_archive_file", new_callable=AsyncMock) as mock_archive:
  115. await instance.on_file_received(file_path, "192.168.1.100")
  116. mock_archive.assert_called_once_with(file_path, "192.168.1.100")
  117. @pytest.mark.asyncio
  118. async def test_archive_file_skips_non_3mf(self, instance):
  119. """Verify non-3MF files are skipped and cleaned up."""
  120. instance._session_factory = MagicMock()
  121. instance._pending_files["verify_job"] = Path("/tmp/verify_job") # nosec B108
  122. with patch("pathlib.Path.unlink"):
  123. await instance._archive_file(Path("/tmp/verify_job"), "192.168.1.100") # nosec B108
  124. assert "verify_job" not in instance._pending_files
  125. # ========================================================================
  126. # Tests for auto_dispatch
  127. # ========================================================================
  128. def test_auto_dispatch_defaults_to_true(self, tmp_path):
  129. """Verify auto_dispatch defaults to True when not specified."""
  130. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  131. inst = VirtualPrinterInstance(
  132. vp_id=10,
  133. name="DefaultDispatch",
  134. mode="print_queue",
  135. model="C11",
  136. access_code="12345678",
  137. serial_suffix="391800010",
  138. base_dir=tmp_path,
  139. )
  140. assert inst.auto_dispatch is True
  141. @pytest.mark.asyncio
  142. async def test_add_to_print_queue_with_auto_dispatch_on(self, tmp_path):
  143. """Verify queue items have manual_start=False when auto_dispatch=True."""
  144. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  145. mock_db = AsyncMock()
  146. added_items = []
  147. def capture_add(item):
  148. added_items.append(item)
  149. mock_db.add = MagicMock(side_effect=capture_add)
  150. mock_db.commit = AsyncMock()
  151. mock_session_factory = MagicMock()
  152. mock_session_ctx = AsyncMock()
  153. mock_session_ctx.__aenter__ = AsyncMock(return_value=mock_db)
  154. mock_session_ctx.__aexit__ = AsyncMock(return_value=False)
  155. mock_session_factory.return_value = mock_session_ctx
  156. inst = VirtualPrinterInstance(
  157. vp_id=11,
  158. name="AutoDispatchOn",
  159. mode="print_queue",
  160. model="C11",
  161. access_code="12345678",
  162. serial_suffix="391800011",
  163. auto_dispatch=True,
  164. base_dir=tmp_path,
  165. session_factory=mock_session_factory,
  166. )
  167. # Create a temp 3mf file
  168. file_path = tmp_path / "test.3mf"
  169. file_path.write_bytes(b"fake3mf")
  170. mock_archive = MagicMock()
  171. mock_archive.id = 1
  172. mock_archive.print_name = "test"
  173. with patch(
  174. "backend.app.services.archive.ArchiveService.archive_print",
  175. new_callable=AsyncMock,
  176. return_value=mock_archive,
  177. ):
  178. await inst._add_to_print_queue(file_path, "192.168.1.100")
  179. assert len(added_items) == 1
  180. queue_item = added_items[0]
  181. assert queue_item.manual_start is False
  182. @pytest.mark.asyncio
  183. async def test_add_to_print_queue_with_auto_dispatch_off(self, tmp_path):
  184. """Verify queue items have manual_start=True when auto_dispatch=False."""
  185. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  186. mock_db = AsyncMock()
  187. added_items = []
  188. def capture_add(item):
  189. added_items.append(item)
  190. mock_db.add = MagicMock(side_effect=capture_add)
  191. mock_db.commit = AsyncMock()
  192. mock_session_factory = MagicMock()
  193. mock_session_ctx = AsyncMock()
  194. mock_session_ctx.__aenter__ = AsyncMock(return_value=mock_db)
  195. mock_session_ctx.__aexit__ = AsyncMock(return_value=False)
  196. mock_session_factory.return_value = mock_session_ctx
  197. inst = VirtualPrinterInstance(
  198. vp_id=12,
  199. name="AutoDispatchOff",
  200. mode="print_queue",
  201. model="C11",
  202. access_code="12345678",
  203. serial_suffix="391800012",
  204. auto_dispatch=False,
  205. base_dir=tmp_path,
  206. session_factory=mock_session_factory,
  207. )
  208. # Create a temp 3mf file
  209. file_path = tmp_path / "test.3mf"
  210. file_path.write_bytes(b"fake3mf")
  211. mock_archive = MagicMock()
  212. mock_archive.id = 1
  213. mock_archive.print_name = "test"
  214. with patch(
  215. "backend.app.services.archive.ArchiveService.archive_print",
  216. new_callable=AsyncMock,
  217. return_value=mock_archive,
  218. ):
  219. await inst._add_to_print_queue(file_path, "192.168.1.100")
  220. assert len(added_items) == 1
  221. queue_item = added_items[0]
  222. assert queue_item.manual_start is True
  223. class TestVirtualPrinterManager:
  224. """Tests for VirtualPrinterManager orchestrator."""
  225. @pytest.fixture
  226. def manager(self):
  227. """Create a VirtualPrinterManager instance."""
  228. from backend.app.services.virtual_printer.manager import VirtualPrinterManager
  229. return VirtualPrinterManager()
  230. def test_manager_starts_empty(self, manager):
  231. """Verify manager starts with no instances."""
  232. assert len(manager._instances) == 0
  233. assert manager.is_enabled is False
  234. def test_manager_get_status_empty(self, manager):
  235. """Verify get_status returns disabled state when no instances."""
  236. status = manager.get_status()
  237. assert status["enabled"] is False
  238. assert status["running"] is False
  239. assert status["mode"] == "immediate"
  240. def test_manager_is_enabled_with_instance(self, manager, tmp_path):
  241. """Verify is_enabled is True when instances exist."""
  242. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  243. inst = VirtualPrinterInstance(
  244. vp_id=1,
  245. name="Test",
  246. mode="immediate",
  247. model="C11",
  248. access_code="12345678",
  249. serial_suffix="391800001",
  250. base_dir=tmp_path,
  251. )
  252. manager._instances[1] = inst
  253. assert manager.is_enabled is True
  254. @pytest.mark.asyncio
  255. async def test_manager_remove_instance_server(self, manager, tmp_path):
  256. """Verify remove_instance stops and removes a server-mode instance."""
  257. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  258. inst = VirtualPrinterInstance(
  259. vp_id=1,
  260. name="Test",
  261. mode="immediate",
  262. model="C11",
  263. access_code="12345678",
  264. serial_suffix="391800001",
  265. base_dir=tmp_path,
  266. )
  267. inst.stop_server = AsyncMock()
  268. manager._instances[1] = inst
  269. await manager.remove_instance(1)
  270. assert 1 not in manager._instances
  271. inst.stop_server.assert_called_once()
  272. @pytest.mark.asyncio
  273. async def test_manager_remove_instance_proxy(self, manager, tmp_path):
  274. """Verify remove_instance stops proxy-mode instance."""
  275. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  276. inst = VirtualPrinterInstance(
  277. vp_id=2,
  278. name="Proxy",
  279. mode="proxy",
  280. model="C11",
  281. access_code="",
  282. serial_suffix="391800002",
  283. target_printer_ip="192.168.1.100",
  284. base_dir=tmp_path,
  285. )
  286. inst.stop_proxy = AsyncMock()
  287. manager._instances[2] = inst
  288. await manager.remove_instance(2)
  289. assert 2 not in manager._instances
  290. inst.stop_proxy.assert_called_once()
  291. def test_manager_get_status_with_instance(self, manager, tmp_path):
  292. """Verify legacy get_status returns first instance data."""
  293. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  294. inst = VirtualPrinterInstance(
  295. vp_id=1,
  296. name="Bambuddy",
  297. mode="immediate",
  298. model="C11",
  299. access_code="12345678",
  300. serial_suffix="391800001",
  301. base_dir=tmp_path,
  302. )
  303. mock_task = MagicMock(done=MagicMock(return_value=False))
  304. inst._tasks = [mock_task]
  305. inst._pending_files = {"file1.3mf": Path("/tmp/file1.3mf")} # nosec B108
  306. manager._instances[1] = inst
  307. status = manager.get_status()
  308. assert status["enabled"] is True
  309. assert status["running"] is True
  310. assert status["mode"] == "immediate"
  311. assert status["name"] == "Bambuddy"
  312. assert status["serial"] == "01S00A391800001"
  313. assert status["model"] == "C11"
  314. assert status["model_name"] == "P1P"
  315. assert status["pending_files"] == 1
  316. def test_manager_get_all_status(self, manager, tmp_path):
  317. """Verify get_all_status returns status for all instances."""
  318. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  319. for i in range(1, 3):
  320. inst = VirtualPrinterInstance(
  321. vp_id=i,
  322. name=f"VP{i}",
  323. mode="immediate",
  324. model="C11",
  325. access_code="12345678",
  326. serial_suffix=f"39180000{i}",
  327. base_dir=tmp_path,
  328. )
  329. manager._instances[i] = inst
  330. statuses = manager.get_all_status()
  331. assert len(statuses) == 2
  332. assert statuses[0]["name"] == "VP1"
  333. assert statuses[1]["name"] == "VP2"
  334. @pytest.mark.asyncio
  335. async def test_manager_stop_all(self, manager, tmp_path):
  336. """Verify stop_all removes all instances."""
  337. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  338. for i in range(1, 3):
  339. inst = VirtualPrinterInstance(
  340. vp_id=i,
  341. name=f"VP{i}",
  342. mode="immediate",
  343. model="C11",
  344. access_code="12345678",
  345. serial_suffix=f"39180000{i}",
  346. base_dir=tmp_path,
  347. )
  348. inst.stop_server = AsyncMock()
  349. manager._instances[i] = inst
  350. await manager.stop_all()
  351. assert len(manager._instances) == 0
  352. # ========================================================================
  353. # Tests for sync_from_db config change detection
  354. # ========================================================================
  355. def _make_db_vp(self, **overrides):
  356. """Create a mock VirtualPrinter DB object."""
  357. defaults = {
  358. "id": 1,
  359. "name": "TestVP",
  360. "enabled": True,
  361. "mode": "immediate",
  362. "model": "C11",
  363. "access_code": "12345678",
  364. "serial_suffix": "391800001",
  365. "bind_ip": "",
  366. "remote_interface_ip": "",
  367. "target_printer_id": None,
  368. "auto_dispatch": True,
  369. "position": 0,
  370. }
  371. defaults.update(overrides)
  372. vp = MagicMock()
  373. for k, v in defaults.items():
  374. setattr(vp, k, v)
  375. return vp
  376. def _setup_sync_mocks(self, manager, enabled_vps, tmp_path):
  377. """Wire up session_factory mock for sync_from_db."""
  378. mock_result = MagicMock()
  379. mock_result.scalars.return_value.all.return_value = enabled_vps
  380. mock_db = AsyncMock()
  381. mock_db.execute = AsyncMock(return_value=mock_result)
  382. mock_db.__aenter__ = AsyncMock(return_value=mock_db)
  383. mock_db.__aexit__ = AsyncMock(return_value=False)
  384. manager._session_factory = MagicMock(return_value=mock_db)
  385. manager._base_dir = tmp_path
  386. @pytest.mark.asyncio
  387. async def test_sync_from_db_restarts_on_mode_change(self, manager, tmp_path):
  388. """Verify sync_from_db restarts VP when mode changes."""
  389. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  390. inst = VirtualPrinterInstance(
  391. vp_id=1,
  392. name="TestVP",
  393. mode="immediate",
  394. model="C11",
  395. access_code="12345678",
  396. serial_suffix="391800001",
  397. base_dir=tmp_path,
  398. )
  399. inst.stop_server = AsyncMock()
  400. manager._instances[1] = inst
  401. # DB says mode changed to "archive"
  402. db_vp = self._make_db_vp(mode="archive")
  403. self._setup_sync_mocks(manager, [db_vp], tmp_path)
  404. with patch.object(manager, "remove_instance", new_callable=AsyncMock) as mock_remove:
  405. # Patch VirtualPrinterInstance to prevent actual start
  406. with patch("backend.app.services.virtual_printer.manager.VirtualPrinterInstance") as MockInst:
  407. mock_new = MagicMock()
  408. mock_new.start_server = AsyncMock()
  409. MockInst.return_value = mock_new
  410. await manager.sync_from_db()
  411. mock_remove.assert_called_once_with(1)
  412. @pytest.mark.asyncio
  413. async def test_sync_from_db_restarts_on_access_code_change(self, manager, tmp_path):
  414. """Verify sync_from_db restarts VP when access_code changes."""
  415. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  416. inst = VirtualPrinterInstance(
  417. vp_id=1,
  418. name="TestVP",
  419. mode="immediate",
  420. model="C11",
  421. access_code="12345678",
  422. serial_suffix="391800001",
  423. base_dir=tmp_path,
  424. )
  425. inst.stop_server = AsyncMock()
  426. manager._instances[1] = inst
  427. db_vp = self._make_db_vp(access_code="newcode99")
  428. self._setup_sync_mocks(manager, [db_vp], tmp_path)
  429. with patch.object(manager, "remove_instance", new_callable=AsyncMock) as mock_remove:
  430. with patch("backend.app.services.virtual_printer.manager.VirtualPrinterInstance") as MockInst:
  431. mock_new = MagicMock()
  432. mock_new.start_server = AsyncMock()
  433. MockInst.return_value = mock_new
  434. await manager.sync_from_db()
  435. mock_remove.assert_called_once_with(1)
  436. @pytest.mark.asyncio
  437. async def test_sync_from_db_skips_unchanged_instance(self, manager, tmp_path):
  438. """Verify sync_from_db does NOT restart when config is identical."""
  439. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  440. inst = VirtualPrinterInstance(
  441. vp_id=1,
  442. name="TestVP",
  443. mode="immediate",
  444. model="C11",
  445. access_code="12345678",
  446. serial_suffix="391800001",
  447. base_dir=tmp_path,
  448. )
  449. manager._instances[1] = inst
  450. # DB matches running config exactly
  451. db_vp = self._make_db_vp()
  452. self._setup_sync_mocks(manager, [db_vp], tmp_path)
  453. with patch.object(manager, "remove_instance", new_callable=AsyncMock) as mock_remove:
  454. await manager.sync_from_db()
  455. mock_remove.assert_not_called()
  456. @pytest.mark.asyncio
  457. async def test_sync_from_db_restarts_on_bind_ip_change(self, manager, tmp_path):
  458. """Verify sync_from_db restarts VP when bind_ip changes."""
  459. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  460. inst = VirtualPrinterInstance(
  461. vp_id=1,
  462. name="TestVP",
  463. mode="immediate",
  464. model="C11",
  465. access_code="12345678",
  466. serial_suffix="391800001",
  467. bind_ip="192.168.1.10",
  468. base_dir=tmp_path,
  469. )
  470. inst.stop_server = AsyncMock()
  471. manager._instances[1] = inst
  472. db_vp = self._make_db_vp(bind_ip="192.168.1.20")
  473. self._setup_sync_mocks(manager, [db_vp], tmp_path)
  474. with patch.object(manager, "remove_instance", new_callable=AsyncMock) as mock_remove:
  475. with patch("backend.app.services.virtual_printer.manager.VirtualPrinterInstance") as MockInst:
  476. mock_new = MagicMock()
  477. mock_new.start_server = AsyncMock()
  478. MockInst.return_value = mock_new
  479. await manager.sync_from_db()
  480. mock_remove.assert_called_once_with(1)
  481. @pytest.mark.asyncio
  482. async def test_sync_from_db_restarts_on_model_change(self, manager, tmp_path):
  483. """Verify sync_from_db restarts VP when model changes."""
  484. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  485. inst = VirtualPrinterInstance(
  486. vp_id=1,
  487. name="TestVP",
  488. mode="immediate",
  489. model="C11",
  490. access_code="12345678",
  491. serial_suffix="391800001",
  492. base_dir=tmp_path,
  493. )
  494. inst.stop_server = AsyncMock()
  495. manager._instances[1] = inst
  496. db_vp = self._make_db_vp(model="C12")
  497. self._setup_sync_mocks(manager, [db_vp], tmp_path)
  498. with patch.object(manager, "remove_instance", new_callable=AsyncMock) as mock_remove:
  499. with patch("backend.app.services.virtual_printer.manager.VirtualPrinterInstance") as MockInst:
  500. mock_new = MagicMock()
  501. mock_new.start_server = AsyncMock()
  502. MockInst.return_value = mock_new
  503. await manager.sync_from_db()
  504. mock_remove.assert_called_once_with(1)
  505. class TestFTPSession:
  506. """Tests for FTP session handling."""
  507. @pytest.fixture
  508. def mock_reader(self):
  509. """Create a mock StreamReader."""
  510. reader = AsyncMock()
  511. return reader
  512. @pytest.fixture
  513. def mock_writer(self):
  514. """Create a mock StreamWriter."""
  515. writer = MagicMock()
  516. writer.get_extra_info = MagicMock(return_value=("192.168.1.100", 12345))
  517. writer.write = MagicMock()
  518. writer.drain = AsyncMock()
  519. writer.close = MagicMock()
  520. writer.wait_closed = AsyncMock()
  521. writer.is_closing = MagicMock(return_value=False)
  522. return writer
  523. @pytest.fixture
  524. def ssl_context(self):
  525. """Create a mock SSL context."""
  526. return MagicMock()
  527. @pytest.fixture
  528. def session(self, mock_reader, mock_writer, ssl_context, tmp_path):
  529. """Create an FTPSession instance."""
  530. from backend.app.services.virtual_printer.ftp_server import FTPSession
  531. return FTPSession(
  532. reader=mock_reader,
  533. writer=mock_writer,
  534. upload_dir=tmp_path,
  535. access_code="12345678",
  536. ssl_context=ssl_context,
  537. on_file_received=None,
  538. )
  539. # ========================================================================
  540. # Tests for authentication
  541. # ========================================================================
  542. @pytest.mark.asyncio
  543. async def test_user_command_accepts_bblp(self, session):
  544. """Verify USER command accepts bblp user."""
  545. await session.cmd_USER("bblp")
  546. assert session.username == "bblp"
  547. @pytest.mark.asyncio
  548. async def test_pass_command_authenticates(self, session):
  549. """Verify PASS command authenticates with correct code."""
  550. session.username = "bblp"
  551. await session.cmd_PASS("12345678")
  552. assert session.authenticated is True
  553. @pytest.mark.asyncio
  554. async def test_pass_command_rejects_wrong_code(self, session):
  555. """Verify PASS command rejects wrong access code."""
  556. session.username = "bblp"
  557. await session.cmd_PASS("wrongcode")
  558. assert session.authenticated is False
  559. # ========================================================================
  560. # Tests for FTP commands
  561. # ========================================================================
  562. @pytest.mark.asyncio
  563. async def test_syst_command(self, session):
  564. """Verify SYST returns UNIX type."""
  565. await session.cmd_SYST("")
  566. session.writer.write.assert_called()
  567. call_args = session.writer.write.call_args[0][0].decode()
  568. assert "215" in call_args
  569. assert "UNIX" in call_args
  570. @pytest.mark.asyncio
  571. async def test_pwd_command_requires_auth(self, session):
  572. """Verify PWD requires authentication."""
  573. session.authenticated = False
  574. await session.cmd_PWD("")
  575. call_args = session.writer.write.call_args[0][0].decode()
  576. assert "530" in call_args
  577. @pytest.mark.asyncio
  578. async def test_pwd_command_when_authenticated(self, session):
  579. """Verify PWD returns root directory when authenticated."""
  580. session.authenticated = True
  581. await session.cmd_PWD("")
  582. call_args = session.writer.write.call_args[0][0].decode()
  583. assert "257" in call_args
  584. @pytest.mark.asyncio
  585. async def test_type_command_sets_binary(self, session):
  586. """Verify TYPE I sets binary mode."""
  587. session.authenticated = True
  588. await session.cmd_TYPE("I")
  589. assert session.transfer_type == "I"
  590. @pytest.mark.asyncio
  591. async def test_pbsz_command(self, session):
  592. """Verify PBSZ returns success."""
  593. await session.cmd_PBSZ("0")
  594. call_args = session.writer.write.call_args[0][0].decode()
  595. assert "200" in call_args
  596. @pytest.mark.asyncio
  597. async def test_prot_command_accepts_p(self, session):
  598. """Verify PROT P is accepted."""
  599. await session.cmd_PROT("P")
  600. call_args = session.writer.write.call_args[0][0].decode()
  601. assert "200" in call_args
  602. @pytest.mark.asyncio
  603. async def test_quit_command(self, session):
  604. """Verify QUIT sends goodbye and raises CancelledError."""
  605. with pytest.raises(asyncio.CancelledError):
  606. await session.cmd_QUIT("")
  607. class TestSSDPServer:
  608. """Tests for Virtual Printer SSDP server."""
  609. @pytest.fixture
  610. def ssdp_server(self):
  611. """Create a VirtualPrinterSSDPServer instance."""
  612. from backend.app.services.virtual_printer.ssdp_server import VirtualPrinterSSDPServer
  613. return VirtualPrinterSSDPServer(
  614. serial="TEST123",
  615. name="TestPrinter",
  616. model="BL-P001",
  617. )
  618. # ========================================================================
  619. # Tests for SSDP response
  620. # ========================================================================
  621. def test_build_notify_message(self, ssdp_server):
  622. """Verify NOTIFY packet contains required headers."""
  623. # Set a known IP for testing
  624. ssdp_server._local_ip = "192.168.1.100"
  625. message = ssdp_server._build_notify_message()
  626. assert b"NOTIFY" in message
  627. assert b"DevName.bambu.com: TestPrinter" in message
  628. assert b"USN: TEST123" in message
  629. def test_build_response_message(self, ssdp_server):
  630. """Verify response packet contains required headers."""
  631. # Set a known IP for testing
  632. ssdp_server._local_ip = "192.168.1.100"
  633. message = ssdp_server._build_response_message()
  634. assert b"HTTP/1.1 200 OK" in message
  635. assert b"DevName.bambu.com: TestPrinter" in message
  636. assert b"USN: TEST123" in message
  637. def test_ssdp_server_uses_correct_model(self, ssdp_server):
  638. """Verify SSDP server uses the provided model."""
  639. ssdp_server._local_ip = "192.168.1.100"
  640. message = ssdp_server._build_notify_message()
  641. assert b"DevModel.bambu.com: BL-P001" in message
  642. # ========================================================================
  643. # Tests for advertise_ip parameter
  644. # ========================================================================
  645. def test_advertise_ip_sets_local_ip(self):
  646. """Verify advertise_ip overrides auto-detection."""
  647. from backend.app.services.virtual_printer.ssdp_server import VirtualPrinterSSDPServer
  648. server = VirtualPrinterSSDPServer(
  649. serial="TEST123",
  650. name="TestPrinter",
  651. model="BL-P001",
  652. advertise_ip="10.0.0.50",
  653. )
  654. assert server._local_ip == "10.0.0.50"
  655. def test_advertise_ip_empty_string_uses_auto_detect(self):
  656. """Verify empty advertise_ip falls back to auto-detection."""
  657. from backend.app.services.virtual_printer.ssdp_server import VirtualPrinterSSDPServer
  658. server = VirtualPrinterSSDPServer(
  659. serial="TEST123",
  660. name="TestPrinter",
  661. model="BL-P001",
  662. advertise_ip="",
  663. )
  664. assert server._local_ip is None
  665. def test_advertise_ip_in_notify_message(self):
  666. """Verify NOTIFY message uses the advertise_ip."""
  667. from backend.app.services.virtual_printer.ssdp_server import VirtualPrinterSSDPServer
  668. server = VirtualPrinterSSDPServer(
  669. serial="TEST123",
  670. name="TestPrinter",
  671. model="BL-P001",
  672. advertise_ip="10.0.0.50",
  673. )
  674. message = server._build_notify_message()
  675. assert b"Location: 10.0.0.50" in message
  676. def test_advertise_ip_in_response_message(self):
  677. """Verify M-SEARCH response uses the advertise_ip."""
  678. from backend.app.services.virtual_printer.ssdp_server import VirtualPrinterSSDPServer
  679. server = VirtualPrinterSSDPServer(
  680. serial="TEST123",
  681. name="TestPrinter",
  682. model="BL-P001",
  683. advertise_ip="10.0.0.50",
  684. )
  685. message = server._build_response_message()
  686. assert b"Location: 10.0.0.50" in message
  687. def test_default_no_advertise_ip(self):
  688. """Verify default constructor has None local_ip (auto-detect)."""
  689. from backend.app.services.virtual_printer.ssdp_server import VirtualPrinterSSDPServer
  690. server = VirtualPrinterSSDPServer()
  691. assert server._local_ip is None
  692. class TestCertificateService:
  693. """Tests for TLS certificate generation."""
  694. @pytest.fixture
  695. def cert_service(self, tmp_path):
  696. """Create a CertificateService instance."""
  697. from backend.app.services.virtual_printer.certificate import CertificateService
  698. return CertificateService(cert_dir=tmp_path, serial="TEST123")
  699. def test_generate_certificates(self, cert_service, tmp_path):
  700. """Verify certificates are generated correctly."""
  701. cert_path, key_path = cert_service.generate_certificates()
  702. assert cert_path.exists()
  703. assert key_path.exists()
  704. # Verify certificate content
  705. cert_content = cert_path.read_text()
  706. assert "BEGIN CERTIFICATE" in cert_content
  707. key_content = key_path.read_text()
  708. assert "BEGIN" in key_content and "KEY" in key_content
  709. def test_certificates_reused_if_exist(self, cert_service):
  710. """Verify existing certificates are reused."""
  711. # First generation
  712. cert_path1, key_path1 = cert_service.generate_certificates()
  713. mtime1 = cert_path1.stat().st_mtime
  714. # Second call should reuse (via ensure_certificates)
  715. cert_path2, key_path2 = cert_service.ensure_certificates()
  716. mtime2 = cert_path2.stat().st_mtime
  717. assert mtime1 == mtime2 # File wasn't regenerated
  718. def test_delete_certificates(self, cert_service):
  719. """Verify certificates can be deleted."""
  720. cert_service.generate_certificates()
  721. assert cert_service.cert_path.exists()
  722. assert cert_service.key_path.exists()
  723. cert_service.delete_certificates()
  724. assert not cert_service.cert_path.exists()
  725. assert not cert_service.key_path.exists()
  726. def test_ensure_creates_if_not_exist(self, cert_service):
  727. """Verify ensure_certificates generates if not existing."""
  728. assert not cert_service.cert_path.exists()
  729. cert_path, key_path = cert_service.ensure_certificates()
  730. assert cert_path.exists()
  731. assert key_path.exists()
  732. class TestBindServer:
  733. """Tests for BindServer (port 3002 bind/detect protocol)."""
  734. @pytest.fixture
  735. def bind_server(self):
  736. """Create a BindServer instance."""
  737. from backend.app.services.virtual_printer.bind_server import BindServer
  738. return BindServer(
  739. serial="09400A391800001",
  740. model="O1D",
  741. name="Bambuddy",
  742. )
  743. def test_build_frame(self, bind_server):
  744. """Verify frame building produces correct format."""
  745. payload = {"login": {"command": "detect"}}
  746. frame = bind_server._build_frame(payload)
  747. # Header: 0xA5A5
  748. assert frame[:2] == b"\xa5\xa5"
  749. # Trailer: 0xA7A7
  750. assert frame[-2:] == b"\xa7\xa7"
  751. # Length field is total message size (LE uint16)
  752. import struct
  753. total_len = struct.unpack_from("<H", frame, 2)[0]
  754. assert total_len == len(frame)
  755. # JSON payload is between header and trailer
  756. import json
  757. json_bytes = frame[4:-2]
  758. parsed = json.loads(json_bytes)
  759. assert parsed == payload
  760. def test_parse_frame_valid(self, bind_server):
  761. """Verify valid frame parsing extracts JSON correctly."""
  762. import json
  763. import struct
  764. payload = {"login": {"command": "detect", "sequence_id": "20000"}}
  765. json_bytes = json.dumps(payload, separators=(",", ":")).encode()
  766. total_len = 4 + len(json_bytes) + 2
  767. frame = b"\xa5\xa5" + struct.pack("<H", total_len) + json_bytes + b"\xa7\xa7"
  768. result = bind_server._parse_frame(frame)
  769. assert result is not None
  770. assert result["login"]["command"] == "detect"
  771. assert result["login"]["sequence_id"] == "20000"
  772. def test_parse_frame_invalid_header(self, bind_server):
  773. """Verify invalid header returns None."""
  774. result = bind_server._parse_frame(b"\xbb\xbb\x06\x00{}\xa7\xa7")
  775. assert result is None
  776. def test_parse_frame_invalid_trailer(self, bind_server):
  777. """Verify invalid trailer returns None."""
  778. result = bind_server._parse_frame(b"\xa5\xa5\x06\x00{}\xbb\xbb")
  779. assert result is None
  780. def test_parse_frame_too_short(self, bind_server):
  781. """Verify short data returns None."""
  782. result = bind_server._parse_frame(b"\xa5\xa5\x00")
  783. assert result is None
  784. def test_parse_frame_invalid_json(self, bind_server):
  785. """Verify invalid JSON returns None."""
  786. import struct
  787. bad_json = b"not json"
  788. total_len = 4 + len(bad_json) + 2
  789. frame = b"\xa5\xa5" + struct.pack("<H", total_len) + bad_json + b"\xa7\xa7"
  790. result = bind_server._parse_frame(frame)
  791. assert result is None
  792. def test_build_frame_roundtrip(self, bind_server):
  793. """Verify build_frame output can be parsed back."""
  794. payload = {
  795. "login": {
  796. "bind": "free",
  797. "command": "detect",
  798. "connect": "lan",
  799. "dev_cap": 1,
  800. "id": "09400A391800001",
  801. "model": "O1D",
  802. "name": "Bambuddy",
  803. "sequence_id": 3021,
  804. "version": "01.00.00.00",
  805. }
  806. }
  807. frame = bind_server._build_frame(payload)
  808. parsed = bind_server._parse_frame(frame)
  809. assert parsed is not None
  810. assert parsed["login"]["id"] == "09400A391800001"
  811. assert parsed["login"]["model"] == "O1D"
  812. assert parsed["login"]["name"] == "Bambuddy"
  813. assert parsed["login"]["bind"] == "free"
  814. def test_bind_server_stores_config(self, bind_server):
  815. """Verify bind server stores serial, model, name."""
  816. assert bind_server.serial == "09400A391800001"
  817. assert bind_server.model == "O1D"
  818. assert bind_server.name == "Bambuddy"
  819. assert bind_server.version == "01.00.00.00"
  820. def test_bind_server_custom_version(self):
  821. """Verify custom firmware version is stored."""
  822. from backend.app.services.virtual_printer.bind_server import BindServer
  823. server = BindServer(
  824. serial="TEST123",
  825. model="C13",
  826. name="Test",
  827. version="02.03.04.05",
  828. )
  829. assert server.version == "02.03.04.05"
  830. def test_bind_ports_constant(self):
  831. """Verify BIND_PORTS includes both 3000 and 3002 for slicer compatibility."""
  832. from backend.app.services.virtual_printer.bind_server import BIND_PORTS
  833. assert 3000 in BIND_PORTS
  834. assert 3002 in BIND_PORTS
  835. def test_bind_server_initializes_empty_servers_list(self, bind_server):
  836. """Verify bind server starts with empty servers list."""
  837. assert bind_server._servers == []
  838. assert bind_server._running is False
  839. class TestSlicerProxyManager:
  840. """Tests for SlicerProxyManager (proxy mode)."""
  841. @pytest.fixture
  842. def proxy_manager(self, tmp_path):
  843. """Create a SlicerProxyManager instance."""
  844. from backend.app.services.virtual_printer.tcp_proxy import SlicerProxyManager
  845. # Create dummy cert files
  846. cert_path = tmp_path / "cert.pem"
  847. key_path = tmp_path / "key.pem"
  848. cert_path.write_text("-----BEGIN CERTIFICATE-----\ntest\n-----END CERTIFICATE-----")
  849. # Split string to avoid pre-commit hook false positive on test data
  850. key_path.write_text("-----BEGIN " + "PRIVATE KEY-----\ntest\n-----END " + "PRIVATE KEY-----")
  851. return SlicerProxyManager(
  852. target_host="192.168.1.100",
  853. cert_path=cert_path,
  854. key_path=key_path,
  855. )
  856. def test_proxy_manager_initializes_ports(self, proxy_manager):
  857. """Verify proxy manager has correct port constants."""
  858. # FTP proxy uses privileged port 990 to match what Bambu Studio expects
  859. assert proxy_manager.LOCAL_FTP_PORT == 990
  860. assert proxy_manager.LOCAL_MQTT_PORT == 8883
  861. assert proxy_manager.PRINTER_FTP_PORT == 990
  862. assert proxy_manager.PRINTER_MQTT_PORT == 8883
  863. # Bind ports: both 3000 and 3002 for slicer compatibility
  864. assert proxy_manager.PRINTER_BIND_PORTS == [3000, 3002]
  865. def test_proxy_manager_stores_target_host(self, proxy_manager):
  866. """Verify proxy manager stores target host."""
  867. assert proxy_manager.target_host == "192.168.1.100"
  868. def test_get_status_before_start(self, proxy_manager):
  869. """Verify get_status returns zeros before start."""
  870. status = proxy_manager.get_status()
  871. assert status["running"] is False
  872. assert status["ftp_connections"] == 0
  873. assert status["mqtt_connections"] == 0
  874. class TestSSDPProxy:
  875. """Tests for SSDPProxy (cross-network SSDP relay)."""
  876. @pytest.fixture
  877. def ssdp_proxy(self):
  878. """Create an SSDPProxy instance."""
  879. from backend.app.services.virtual_printer.ssdp_server import SSDPProxy
  880. return SSDPProxy(
  881. local_interface_ip="192.168.1.100",
  882. remote_interface_ip="10.0.0.100",
  883. target_printer_ip="192.168.1.50",
  884. )
  885. def test_ssdp_proxy_stores_interface_ips(self, ssdp_proxy):
  886. """Verify SSDPProxy stores interface IPs correctly."""
  887. assert ssdp_proxy.local_interface_ip == "192.168.1.100"
  888. assert ssdp_proxy.remote_interface_ip == "10.0.0.100"
  889. assert ssdp_proxy.target_printer_ip == "192.168.1.50"
  890. def test_rewrite_ssdp_location(self, ssdp_proxy):
  891. """Verify SSDP Location header is rewritten to remote interface IP."""
  892. original_packet = b"NOTIFY * HTTP/1.1\r\nLocation: 192.168.1.50\r\nDevName.bambu.com: TestPrinter\r\n\r\n"
  893. rewritten = ssdp_proxy._rewrite_ssdp(original_packet)
  894. # Location should be changed to remote interface IP
  895. assert b"Location: 10.0.0.100" in rewritten
  896. assert b"Location: 192.168.1.50" not in rewritten
  897. # Other headers should be preserved
  898. assert b"DevName.bambu.com: TestPrinter" in rewritten
  899. def test_rewrite_ssdp_location_case_insensitive(self, ssdp_proxy):
  900. """Verify SSDP Location rewrite is case insensitive."""
  901. original_packet = b"NOTIFY * HTTP/1.1\r\nlocation: 192.168.1.50\r\n\r\n"
  902. rewritten = ssdp_proxy._rewrite_ssdp(original_packet)
  903. assert b"10.0.0.100" in rewritten
  904. def test_rewrite_ssdp_location_no_match(self, ssdp_proxy):
  905. """Verify packet without Location header is returned unchanged."""
  906. original_packet = b"NOTIFY * HTTP/1.1\r\nDevName.bambu.com: Test\r\n\r\n"
  907. rewritten = ssdp_proxy._rewrite_ssdp(original_packet)
  908. # No Location header, but _rewrite_ssdp logs a warning and returns as-is
  909. assert b"DevName.bambu.com: Test" in rewritten
  910. def test_parse_ssdp_message(self, ssdp_proxy):
  911. """Verify SSDP message parsing extracts headers."""
  912. packet = (
  913. b"NOTIFY * HTTP/1.1\r\n"
  914. b"Location: 192.168.1.50\r\n"
  915. b"DevName.bambu.com: TestPrinter\r\n"
  916. b"DevModel.bambu.com: BL-P001\r\n"
  917. b"\r\n"
  918. )
  919. headers = ssdp_proxy._parse_ssdp_message(packet)
  920. assert headers["location"] == "192.168.1.50"
  921. assert headers["devname.bambu.com"] == "TestPrinter"
  922. assert headers["devmodel.bambu.com"] == "BL-P001"
  923. class TestVirtualPrinterManagerDirectories:
  924. """Tests for VirtualPrinterManager directory management."""
  925. def test_ensure_base_directories_creates_subdirs(self, tmp_path):
  926. """Verify _ensure_base_directories creates required base directories."""
  927. from backend.app.services.virtual_printer.manager import VirtualPrinterManager
  928. manager = VirtualPrinterManager()
  929. manager._base_dir = tmp_path / "virtual_printer"
  930. manager._ensure_base_directories()
  931. assert (tmp_path / "virtual_printer").exists()
  932. assert (tmp_path / "virtual_printer" / "uploads").exists()
  933. assert (tmp_path / "virtual_printer" / "certs").exists()
  934. def test_ensure_base_directories_handles_permission_error(self, tmp_path, caplog):
  935. """Verify _ensure_base_directories logs error on permission failure."""
  936. import logging
  937. from backend.app.services.virtual_printer.manager import VirtualPrinterManager
  938. manager = VirtualPrinterManager()
  939. vp_dir = tmp_path / "virtual_printer"
  940. manager._base_dir = vp_dir
  941. original_mkdir = type(vp_dir).mkdir
  942. def mock_mkdir(self, *args, **kwargs):
  943. if "virtual_printer" in str(self):
  944. raise PermissionError("Permission denied")
  945. return original_mkdir(self, *args, **kwargs)
  946. with caplog.at_level(logging.ERROR), patch.object(type(vp_dir), "mkdir", mock_mkdir):
  947. manager._ensure_base_directories()
  948. assert "Permission denied" in caplog.text
  949. def test_instance_creates_per_vp_directories(self, tmp_path):
  950. """Verify VirtualPrinterInstance creates per-VP upload and cert dirs."""
  951. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  952. VirtualPrinterInstance(
  953. vp_id=42,
  954. name="Test",
  955. mode="immediate",
  956. model="C11",
  957. access_code="12345678",
  958. serial_suffix="391800042",
  959. base_dir=tmp_path,
  960. )
  961. assert (tmp_path / "uploads" / "42").exists()
  962. assert (tmp_path / "uploads" / "42" / "cache").exists()
  963. assert (tmp_path / "certs" / "42").exists()
  964. class TestVirtualPrinterInstanceProxyMode:
  965. """Tests for VirtualPrinterInstance proxy mode."""
  966. @pytest.fixture
  967. def proxy_instance(self, tmp_path):
  968. """Create a proxy-mode VirtualPrinterInstance."""
  969. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  970. return VirtualPrinterInstance(
  971. vp_id=10,
  972. name="ProxyTest",
  973. mode="proxy",
  974. model="C11",
  975. access_code="",
  976. serial_suffix="391800010",
  977. target_printer_ip="192.168.1.100",
  978. target_printer_serial="01P00A000000001",
  979. base_dir=tmp_path,
  980. )
  981. def test_proxy_instance_properties(self, proxy_instance):
  982. """Verify proxy instance stores config correctly."""
  983. assert proxy_instance.is_proxy is True
  984. assert proxy_instance.mode == "proxy"
  985. assert proxy_instance.target_printer_ip == "192.168.1.100"
  986. assert proxy_instance.target_printer_serial == "01P00A000000001"
  987. def test_proxy_instance_does_not_require_access_code(self, proxy_instance):
  988. """Verify proxy mode can have empty access code."""
  989. assert proxy_instance.access_code == ""
  990. def test_get_status_proxy_includes_proxy_fields(self, proxy_instance):
  991. """Verify get_status includes proxy fields when proxy is active."""
  992. mock_proxy = MagicMock()
  993. mock_proxy.get_status.return_value = {
  994. "running": True,
  995. "ftp_port": 990,
  996. "mqtt_port": 8883,
  997. "ftp_connections": 1,
  998. "mqtt_connections": 2,
  999. "target_host": "192.168.1.100",
  1000. }
  1001. proxy_instance._proxy = mock_proxy
  1002. status = proxy_instance.get_status()
  1003. assert "proxy" in status
  1004. assert status["proxy"]["ftp_port"] == 990
  1005. assert status["proxy"]["mqtt_connections"] == 2
  1006. def test_proxy_instance_stores_remote_interface(self, tmp_path):
  1007. """Verify proxy instance stores remote_interface_ip."""
  1008. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  1009. inst = VirtualPrinterInstance(
  1010. vp_id=11,
  1011. name="Proxy2",
  1012. mode="proxy",
  1013. model="C11",
  1014. access_code="",
  1015. serial_suffix="391800011",
  1016. target_printer_ip="192.168.1.100",
  1017. remote_interface_ip="10.0.0.50",
  1018. base_dir=tmp_path,
  1019. )
  1020. assert inst.remote_interface_ip == "10.0.0.50"
  1021. class TestVirtualPrinterInstanceIPOverride:
  1022. """Tests for remote_interface_ip and bind_ip on VirtualPrinterInstance."""
  1023. @pytest.fixture
  1024. def instance_with_remote_ip(self, tmp_path):
  1025. """Create an instance with remote_interface_ip set."""
  1026. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  1027. return VirtualPrinterInstance(
  1028. vp_id=20,
  1029. name="IPTest",
  1030. mode="immediate",
  1031. model="BL-P001",
  1032. access_code="12345678",
  1033. serial_suffix="391800020",
  1034. bind_ip="192.168.1.50",
  1035. remote_interface_ip="10.0.0.50",
  1036. base_dir=tmp_path,
  1037. )
  1038. def test_instance_stores_bind_ip(self, instance_with_remote_ip):
  1039. """Verify bind_ip is stored."""
  1040. assert instance_with_remote_ip.bind_ip == "192.168.1.50"
  1041. def test_instance_stores_remote_interface_ip(self, instance_with_remote_ip):
  1042. """Verify remote_interface_ip is stored."""
  1043. assert instance_with_remote_ip.remote_interface_ip == "10.0.0.50"
  1044. def test_generate_certificates_includes_remote_and_bind_ip(self, instance_with_remote_ip):
  1045. """Verify generate_certificates passes remote_interface_ip and bind_ip as SANs."""
  1046. with (
  1047. patch.object(instance_with_remote_ip._cert_service, "delete_printer_certificate"),
  1048. patch.object(
  1049. instance_with_remote_ip._cert_service,
  1050. "generate_certificates",
  1051. return_value=(Path("/tmp/cert.pem"), Path("/tmp/key.pem")), # nosec B108
  1052. ) as mock_gen,
  1053. ):
  1054. instance_with_remote_ip.generate_certificates()
  1055. mock_gen.assert_called_once_with(additional_ips=["10.0.0.50", "192.168.1.50"])
  1056. def test_generate_certificates_no_remote_ip(self, tmp_path):
  1057. """Verify generate_certificates passes only bind_ip when no remote_interface_ip."""
  1058. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  1059. inst = VirtualPrinterInstance(
  1060. vp_id=21,
  1061. name="NoRemote",
  1062. mode="immediate",
  1063. model="BL-P001",
  1064. access_code="12345678",
  1065. serial_suffix="391800021",
  1066. bind_ip="192.168.1.50",
  1067. base_dir=tmp_path,
  1068. )
  1069. with (
  1070. patch.object(inst._cert_service, "delete_printer_certificate"),
  1071. patch.object(
  1072. inst._cert_service,
  1073. "generate_certificates",
  1074. return_value=(Path("/tmp/cert.pem"), Path("/tmp/key.pem")), # nosec B108
  1075. ) as mock_gen,
  1076. ):
  1077. inst.generate_certificates()
  1078. mock_gen.assert_called_once_with(additional_ips=["192.168.1.50"])
  1079. def test_generate_certificates_no_ips(self, tmp_path):
  1080. """Verify generate_certificates passes None when no IPs configured."""
  1081. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  1082. inst = VirtualPrinterInstance(
  1083. vp_id=22,
  1084. name="NoIPs",
  1085. mode="immediate",
  1086. model="BL-P001",
  1087. access_code="12345678",
  1088. serial_suffix="391800022",
  1089. base_dir=tmp_path,
  1090. )
  1091. with (
  1092. patch.object(inst._cert_service, "delete_printer_certificate"),
  1093. patch.object(
  1094. inst._cert_service,
  1095. "generate_certificates",
  1096. return_value=(Path("/tmp/cert.pem"), Path("/tmp/key.pem")), # nosec B108
  1097. ) as mock_gen,
  1098. ):
  1099. inst.generate_certificates()
  1100. mock_gen.assert_called_once_with(additional_ips=None)
  1101. class TestBindServer:
  1102. """Tests for the BindServer (port 3002 bind/detect protocol)."""
  1103. @pytest.fixture
  1104. def bind_server(self):
  1105. """Create a BindServer instance."""
  1106. from backend.app.services.virtual_printer.bind_server import BindServer
  1107. return BindServer(
  1108. serial="01S00C000000001",
  1109. model="BL-P001",
  1110. name="Bambuddy",
  1111. )
  1112. def test_build_frame(self, bind_server):
  1113. """Verify frame format: 0xA5A5 + len(u16le) + JSON + 0xA7A7."""
  1114. payload = {"login": {"command": "detect"}}
  1115. frame = bind_server._build_frame(payload)
  1116. assert frame[:2] == b"\xa5\xa5"
  1117. assert frame[-2:] == b"\xa7\xa7"
  1118. # Length field is total message size
  1119. import struct
  1120. total_len = struct.unpack_from("<H", frame, 2)[0]
  1121. assert total_len == len(frame)
  1122. # JSON payload is between header and trailer
  1123. import json
  1124. json_bytes = frame[4:-2]
  1125. parsed = json.loads(json_bytes)
  1126. assert parsed == payload
  1127. def test_parse_frame_valid(self, bind_server):
  1128. """Verify valid frame parsing."""
  1129. frame = bind_server._build_frame({"login": {"command": "detect", "sequence_id": "20000"}})
  1130. result = bind_server._parse_frame(frame)
  1131. assert result is not None
  1132. assert result["login"]["command"] == "detect"
  1133. assert result["login"]["sequence_id"] == "20000"
  1134. def test_parse_frame_invalid_header(self, bind_server):
  1135. """Verify invalid header returns None."""
  1136. frame = b"\xb5\xb5\x10\x00" + b'{"login":{}}' + b"\xa7\xa7"
  1137. assert bind_server._parse_frame(frame) is None
  1138. def test_parse_frame_invalid_trailer(self, bind_server):
  1139. """Verify invalid trailer returns None."""
  1140. frame = b"\xa5\xa5\x10\x00" + b'{"login":{}}' + b"\xb7\xb7"
  1141. assert bind_server._parse_frame(frame) is None
  1142. def test_parse_frame_too_short(self, bind_server):
  1143. """Verify short data returns None."""
  1144. assert bind_server._parse_frame(b"\xa5\xa5\x00") is None
  1145. assert bind_server._parse_frame(b"") is None
  1146. def test_parse_frame_invalid_json(self, bind_server):
  1147. """Verify invalid JSON returns None."""
  1148. import struct
  1149. bad_json = b"not json"
  1150. total_len = 4 + len(bad_json) + 2
  1151. frame = b"\xa5\xa5" + struct.pack("<H", total_len) + bad_json + b"\xa7\xa7"
  1152. assert bind_server._parse_frame(frame) is None
  1153. def test_build_frame_roundtrip(self, bind_server):
  1154. """Verify build then parse roundtrip."""
  1155. original = {"login": {"bind": "free", "command": "detect", "id": "01S00C000000001"}}
  1156. frame = bind_server._build_frame(original)
  1157. parsed = bind_server._parse_frame(frame)
  1158. assert parsed == original
  1159. def test_bind_server_stores_config(self, bind_server):
  1160. """Verify config is stored correctly."""
  1161. assert bind_server.serial == "01S00C000000001"
  1162. assert bind_server.model == "BL-P001"
  1163. assert bind_server.name == "Bambuddy"
  1164. assert bind_server.version == "01.00.00.00"
  1165. def test_bind_server_custom_version(self):
  1166. """Verify custom firmware version is stored."""
  1167. from backend.app.services.virtual_printer.bind_server import BindServer
  1168. server = BindServer(
  1169. serial="01S00C000000001",
  1170. model="BL-P001",
  1171. name="Bambuddy",
  1172. version="01.09.00.10",
  1173. )
  1174. assert server.version == "01.09.00.10"
  1175. def test_bind_ports_includes_both(self):
  1176. """Verify BIND_PORTS includes both 3000 and 3002 for slicer compatibility."""
  1177. from backend.app.services.virtual_printer.bind_server import BIND_PORTS
  1178. assert 3000 in BIND_PORTS
  1179. assert 3002 in BIND_PORTS
  1180. def test_bind_server_initializes_empty_servers_list(self, bind_server):
  1181. """Verify bind server starts with empty servers list."""
  1182. assert bind_server._servers == []
  1183. assert bind_server._running is False
  1184. @pytest.mark.asyncio
  1185. async def test_start_server_creates_bind_server(self, tmp_path):
  1186. """Verify start_server creates BindServer with correct params."""
  1187. from backend.app.services.virtual_printer.manager import VirtualPrinterInstance
  1188. inst = VirtualPrinterInstance(
  1189. vp_id=99,
  1190. name="Bambuddy",
  1191. mode="immediate",
  1192. model="BL-P001",
  1193. access_code="12345678",
  1194. serial_suffix="391800099",
  1195. bind_ip="192.168.1.50",
  1196. base_dir=tmp_path,
  1197. )
  1198. with (
  1199. patch("backend.app.services.virtual_printer.manager.VirtualPrinterSSDPServer"),
  1200. patch("backend.app.services.virtual_printer.manager.VirtualPrinterFTPServer"),
  1201. patch("backend.app.services.virtual_printer.manager.SimpleMQTTServer"),
  1202. patch("backend.app.services.virtual_printer.manager.BindServer") as mock_bind_cls,
  1203. patch.object(inst._cert_service, "delete_printer_certificate"),
  1204. patch.object(
  1205. inst._cert_service,
  1206. "generate_certificates",
  1207. return_value=(Path("/tmp/cert.pem"), Path("/tmp/key.pem")), # nosec B108
  1208. ),
  1209. ):
  1210. await inst.start_server()
  1211. mock_bind_cls.assert_called_once_with(
  1212. serial=inst.serial,
  1213. model="BL-P001",
  1214. name="Bambuddy",
  1215. bind_address="192.168.1.50",
  1216. cert_path=Path("/tmp/cert.pem"), # nosec B108
  1217. key_path=Path("/tmp/key.pem"), # nosec B108
  1218. )
  1219. class TestResolveModelCodes:
  1220. """Tests for model code resolution (display name → SSDP code)."""
  1221. def test_display_name_to_model_code_maps_all_models(self):
  1222. """Verify reverse mapping covers all VIRTUAL_PRINTER_MODELS entries."""
  1223. from backend.app.services.virtual_printer.manager import DISPLAY_NAME_TO_MODEL_CODE, VIRTUAL_PRINTER_MODELS
  1224. for _code, display_name in VIRTUAL_PRINTER_MODELS.items():
  1225. assert display_name in DISPLAY_NAME_TO_MODEL_CODE
  1226. # For non-duplicate display names, should map back to a valid code
  1227. assert DISPLAY_NAME_TO_MODEL_CODE[display_name] in VIRTUAL_PRINTER_MODELS
  1228. def test_resolve_printer_model_with_ssdp_code(self):
  1229. """SSDP codes pass through unchanged."""
  1230. from backend.app.api.routes.virtual_printers import _resolve_printer_model
  1231. assert _resolve_printer_model("BL-P001") == "BL-P001"
  1232. assert _resolve_printer_model("O1D") == "O1D"
  1233. assert _resolve_printer_model("N2S") == "N2S"
  1234. def test_resolve_printer_model_with_display_name(self):
  1235. """Display names resolve to SSDP codes."""
  1236. from backend.app.api.routes.virtual_printers import _resolve_printer_model
  1237. assert _resolve_printer_model("X1C") == "BL-P001"
  1238. assert _resolve_printer_model("H2D") == "O1D"
  1239. assert _resolve_printer_model("A1") == "N2S"
  1240. assert _resolve_printer_model("P1S") == "C12"
  1241. def test_resolve_printer_model_with_none_or_unknown(self):
  1242. """None and unknown values return None."""
  1243. from backend.app.api.routes.virtual_printers import _resolve_printer_model
  1244. assert _resolve_printer_model(None) is None
  1245. assert _resolve_printer_model("UnknownModel") is None