test_virtual_printer.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 TestVirtualPrinterManager:
  9. """Tests for VirtualPrinterManager class."""
  10. @pytest.fixture
  11. def manager(self):
  12. """Create a VirtualPrinterManager instance."""
  13. from backend.app.services.virtual_printer.manager import VirtualPrinterManager
  14. return VirtualPrinterManager()
  15. # ========================================================================
  16. # Tests for configuration
  17. # ========================================================================
  18. @pytest.mark.asyncio
  19. async def test_configure_sets_parameters(self, manager):
  20. """Verify configure stores parameters correctly."""
  21. # Mock the start/stop methods to avoid actually starting services
  22. manager._start = AsyncMock()
  23. await manager.configure(
  24. enabled=True,
  25. access_code="12345678",
  26. mode="immediate",
  27. )
  28. assert manager._enabled is True
  29. assert manager._access_code == "12345678"
  30. assert manager._mode == "immediate"
  31. @pytest.mark.asyncio
  32. async def test_configure_disabled_stops_services(self, manager):
  33. """Verify disabling stops all services."""
  34. # First simulate enabled state
  35. manager._enabled = True
  36. manager._tasks = [MagicMock(done=MagicMock(return_value=False))]
  37. manager._stop = AsyncMock()
  38. await manager.configure(enabled=False, access_code="12345678")
  39. assert manager._enabled is False
  40. manager._stop.assert_called_once()
  41. @pytest.mark.asyncio
  42. async def test_configure_requires_access_code_when_enabling(self, manager):
  43. """Verify access code is required when enabling."""
  44. with pytest.raises(ValueError, match="Access code is required"):
  45. await manager.configure(enabled=True)
  46. @pytest.mark.asyncio
  47. async def test_configure_sets_model(self, manager):
  48. """Verify configure stores model correctly."""
  49. manager._start = AsyncMock()
  50. await manager.configure(
  51. enabled=True,
  52. access_code="12345678",
  53. mode="immediate",
  54. model="C11", # P1S model code
  55. )
  56. assert manager._model == "C11"
  57. @pytest.mark.asyncio
  58. async def test_configure_ignores_invalid_model(self, manager):
  59. """Verify configure ignores invalid model codes."""
  60. manager._start = AsyncMock()
  61. await manager.configure(
  62. enabled=True,
  63. access_code="12345678",
  64. model="INVALID",
  65. )
  66. # Should keep default model (3DPrinter-X1-Carbon = X1C)
  67. assert manager._model == "3DPrinter-X1-Carbon"
  68. @pytest.mark.asyncio
  69. async def test_configure_restarts_on_model_change(self, manager):
  70. """Verify model change restarts services when running."""
  71. # Simulate running state
  72. manager._enabled = True
  73. manager._model = "3DPrinter-X1-Carbon"
  74. manager._tasks = [MagicMock(done=MagicMock(return_value=False))]
  75. manager._stop = AsyncMock()
  76. manager._start = AsyncMock()
  77. await manager.configure(
  78. enabled=True,
  79. access_code="12345678",
  80. model="C11", # P1P
  81. )
  82. # Should have stopped and started
  83. manager._stop.assert_called_once()
  84. manager._start.assert_called_once()
  85. # ========================================================================
  86. # Tests for status
  87. # ========================================================================
  88. def test_get_status_returns_correct_format(self, manager):
  89. """Verify get_status returns expected fields."""
  90. manager._enabled = True
  91. manager._mode = "immediate"
  92. manager._model = "C11" # P1P
  93. manager._pending_files = {"file1.3mf": Path("/tmp/file1.3mf")}
  94. # Simulate running tasks
  95. manager._tasks = [MagicMock(done=MagicMock(return_value=False))]
  96. status = manager.get_status()
  97. assert status["enabled"] is True
  98. assert status["running"] is True
  99. assert status["mode"] == "immediate"
  100. assert status["name"] == "Bambuddy"
  101. assert status["serial"] == "01S00A391800001" # C11 (P1P) serial prefix
  102. assert status["model"] == "C11"
  103. assert status["model_name"] == "P1P"
  104. assert status["pending_files"] == 1
  105. def test_get_status_when_stopped(self, manager):
  106. """Verify get_status when not running."""
  107. manager._enabled = False
  108. manager._tasks = []
  109. status = manager.get_status()
  110. assert status["enabled"] is False
  111. assert status["running"] is False
  112. def test_is_running_with_active_tasks(self, manager):
  113. """Verify is_running is True when tasks are active."""
  114. mock_task = MagicMock()
  115. mock_task.done.return_value = False
  116. manager._tasks = [mock_task]
  117. assert manager.is_running is True
  118. def test_is_running_with_no_tasks(self, manager):
  119. """Verify is_running is False when no tasks."""
  120. manager._tasks = []
  121. assert manager.is_running is False
  122. # ========================================================================
  123. # Tests for file handling
  124. # ========================================================================
  125. @pytest.mark.asyncio
  126. async def test_on_file_received_adds_to_pending(self, manager):
  127. """Verify received file is added to pending list."""
  128. manager._mode = "queue"
  129. manager._session_factory = None # Disable actual archiving
  130. file_path = Path("/tmp/test.3mf")
  131. with patch.object(manager, "_queue_file", new_callable=AsyncMock) as mock_queue:
  132. await manager._on_file_received(file_path, "192.168.1.100")
  133. assert "test.3mf" in manager._pending_files
  134. mock_queue.assert_called_once()
  135. @pytest.mark.asyncio
  136. async def test_on_file_received_archives_immediately(self, manager):
  137. """Verify file is archived in immediate mode."""
  138. manager._mode = "immediate"
  139. manager._session_factory = None # Will prevent actual archiving
  140. file_path = Path("/tmp/test.3mf")
  141. with patch.object(manager, "_archive_file", new_callable=AsyncMock) as mock_archive:
  142. await manager._on_file_received(file_path, "192.168.1.100")
  143. mock_archive.assert_called_once_with(file_path, "192.168.1.100")
  144. @pytest.mark.asyncio
  145. async def test_archive_file_skips_non_3mf(self, manager):
  146. """Verify non-3MF files are skipped and cleaned up."""
  147. manager._session_factory = MagicMock()
  148. manager._pending_files["verify_job"] = Path("/tmp/verify_job")
  149. with patch("pathlib.Path.unlink"):
  150. await manager._archive_file(Path("/tmp/verify_job"), "192.168.1.100")
  151. # Should be removed from pending
  152. assert "verify_job" not in manager._pending_files
  153. class TestFTPSession:
  154. """Tests for FTP session handling."""
  155. @pytest.fixture
  156. def mock_reader(self):
  157. """Create a mock StreamReader."""
  158. reader = AsyncMock()
  159. return reader
  160. @pytest.fixture
  161. def mock_writer(self):
  162. """Create a mock StreamWriter."""
  163. writer = MagicMock()
  164. writer.get_extra_info = MagicMock(return_value=("192.168.1.100", 12345))
  165. writer.write = MagicMock()
  166. writer.drain = AsyncMock()
  167. writer.close = MagicMock()
  168. writer.wait_closed = AsyncMock()
  169. writer.is_closing = MagicMock(return_value=False)
  170. return writer
  171. @pytest.fixture
  172. def ssl_context(self):
  173. """Create a mock SSL context."""
  174. return MagicMock()
  175. @pytest.fixture
  176. def session(self, mock_reader, mock_writer, ssl_context, tmp_path):
  177. """Create an FTPSession instance."""
  178. from backend.app.services.virtual_printer.ftp_server import FTPSession
  179. return FTPSession(
  180. reader=mock_reader,
  181. writer=mock_writer,
  182. upload_dir=tmp_path,
  183. access_code="12345678",
  184. ssl_context=ssl_context,
  185. on_file_received=None,
  186. )
  187. # ========================================================================
  188. # Tests for authentication
  189. # ========================================================================
  190. @pytest.mark.asyncio
  191. async def test_user_command_accepts_bblp(self, session):
  192. """Verify USER command accepts bblp user."""
  193. await session.cmd_USER("bblp")
  194. assert session.username == "bblp"
  195. @pytest.mark.asyncio
  196. async def test_pass_command_authenticates(self, session):
  197. """Verify PASS command authenticates with correct code."""
  198. session.username = "bblp"
  199. await session.cmd_PASS("12345678")
  200. assert session.authenticated is True
  201. @pytest.mark.asyncio
  202. async def test_pass_command_rejects_wrong_code(self, session):
  203. """Verify PASS command rejects wrong access code."""
  204. session.username = "bblp"
  205. await session.cmd_PASS("wrongcode")
  206. assert session.authenticated is False
  207. # ========================================================================
  208. # Tests for FTP commands
  209. # ========================================================================
  210. @pytest.mark.asyncio
  211. async def test_syst_command(self, session):
  212. """Verify SYST returns UNIX type."""
  213. await session.cmd_SYST("")
  214. session.writer.write.assert_called()
  215. call_args = session.writer.write.call_args[0][0].decode()
  216. assert "215" in call_args
  217. assert "UNIX" in call_args
  218. @pytest.mark.asyncio
  219. async def test_pwd_command_requires_auth(self, session):
  220. """Verify PWD requires authentication."""
  221. session.authenticated = False
  222. await session.cmd_PWD("")
  223. call_args = session.writer.write.call_args[0][0].decode()
  224. assert "530" in call_args
  225. @pytest.mark.asyncio
  226. async def test_pwd_command_when_authenticated(self, session):
  227. """Verify PWD returns root directory when authenticated."""
  228. session.authenticated = True
  229. await session.cmd_PWD("")
  230. call_args = session.writer.write.call_args[0][0].decode()
  231. assert "257" in call_args
  232. @pytest.mark.asyncio
  233. async def test_type_command_sets_binary(self, session):
  234. """Verify TYPE I sets binary mode."""
  235. session.authenticated = True
  236. await session.cmd_TYPE("I")
  237. assert session.transfer_type == "I"
  238. @pytest.mark.asyncio
  239. async def test_pbsz_command(self, session):
  240. """Verify PBSZ returns success."""
  241. await session.cmd_PBSZ("0")
  242. call_args = session.writer.write.call_args[0][0].decode()
  243. assert "200" in call_args
  244. @pytest.mark.asyncio
  245. async def test_prot_command_accepts_p(self, session):
  246. """Verify PROT P is accepted."""
  247. await session.cmd_PROT("P")
  248. call_args = session.writer.write.call_args[0][0].decode()
  249. assert "200" in call_args
  250. @pytest.mark.asyncio
  251. async def test_quit_command(self, session):
  252. """Verify QUIT sends goodbye and raises CancelledError."""
  253. with pytest.raises(asyncio.CancelledError):
  254. await session.cmd_QUIT("")
  255. class TestSSDPServer:
  256. """Tests for Virtual Printer SSDP server."""
  257. @pytest.fixture
  258. def ssdp_server(self):
  259. """Create a VirtualPrinterSSDPServer instance."""
  260. from backend.app.services.virtual_printer.ssdp_server import VirtualPrinterSSDPServer
  261. return VirtualPrinterSSDPServer(
  262. serial="TEST123",
  263. name="TestPrinter",
  264. model="BL-P001",
  265. )
  266. # ========================================================================
  267. # Tests for SSDP response
  268. # ========================================================================
  269. def test_build_notify_message(self, ssdp_server):
  270. """Verify NOTIFY packet contains required headers."""
  271. # Set a known IP for testing
  272. ssdp_server._local_ip = "192.168.1.100"
  273. message = ssdp_server._build_notify_message()
  274. assert b"NOTIFY" in message
  275. assert b"DevName.bambu.com: TestPrinter" in message
  276. assert b"USN: TEST123" in message
  277. def test_build_response_message(self, ssdp_server):
  278. """Verify response packet contains required headers."""
  279. # Set a known IP for testing
  280. ssdp_server._local_ip = "192.168.1.100"
  281. message = ssdp_server._build_response_message()
  282. assert b"HTTP/1.1 200 OK" in message
  283. assert b"DevName.bambu.com: TestPrinter" in message
  284. assert b"USN: TEST123" in message
  285. def test_ssdp_server_uses_correct_model(self, ssdp_server):
  286. """Verify SSDP server uses the provided model."""
  287. ssdp_server._local_ip = "192.168.1.100"
  288. message = ssdp_server._build_notify_message()
  289. assert b"DevModel.bambu.com: BL-P001" in message
  290. class TestCertificateService:
  291. """Tests for TLS certificate generation."""
  292. @pytest.fixture
  293. def cert_service(self, tmp_path):
  294. """Create a CertificateService instance."""
  295. from backend.app.services.virtual_printer.certificate import CertificateService
  296. return CertificateService(cert_dir=tmp_path, serial="TEST123")
  297. def test_generate_certificates(self, cert_service, tmp_path):
  298. """Verify certificates are generated correctly."""
  299. cert_path, key_path = cert_service.generate_certificates()
  300. assert cert_path.exists()
  301. assert key_path.exists()
  302. # Verify certificate content
  303. cert_content = cert_path.read_text()
  304. assert "BEGIN CERTIFICATE" in cert_content
  305. key_content = key_path.read_text()
  306. assert "BEGIN" in key_content and "KEY" in key_content
  307. def test_certificates_reused_if_exist(self, cert_service):
  308. """Verify existing certificates are reused."""
  309. # First generation
  310. cert_path1, key_path1 = cert_service.generate_certificates()
  311. mtime1 = cert_path1.stat().st_mtime
  312. # Second call should reuse (via ensure_certificates)
  313. cert_path2, key_path2 = cert_service.ensure_certificates()
  314. mtime2 = cert_path2.stat().st_mtime
  315. assert mtime1 == mtime2 # File wasn't regenerated
  316. def test_delete_certificates(self, cert_service):
  317. """Verify certificates can be deleted."""
  318. cert_service.generate_certificates()
  319. assert cert_service.cert_path.exists()
  320. assert cert_service.key_path.exists()
  321. cert_service.delete_certificates()
  322. assert not cert_service.cert_path.exists()
  323. assert not cert_service.key_path.exists()
  324. def test_ensure_creates_if_not_exist(self, cert_service):
  325. """Verify ensure_certificates generates if not existing."""
  326. assert not cert_service.cert_path.exists()
  327. cert_path, key_path = cert_service.ensure_certificates()
  328. assert cert_path.exists()
  329. assert key_path.exists()