conftest.py 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. """Shared test fixtures for BamBuddy backend tests."""
  2. import asyncio
  3. import atexit
  4. import json
  5. import logging
  6. import os
  7. import shutil
  8. import tempfile
  9. from collections.abc import AsyncGenerator
  10. from pathlib import Path
  11. from unittest.mock import AsyncMock, MagicMock, patch
  12. import pytest
  13. # IMPORTANT: Set environment variables BEFORE any app imports
  14. # This must happen before settings/config are loaded
  15. os.environ["LOG_TO_FILE"] = "false"
  16. os.environ["DEBUG"] = "false"
  17. # Point the app's own engine at a throwaway database before anything reads
  18. # DATABASE_URL.
  19. #
  20. # The fixtures below build their own SQLite engine, but that is not the only
  21. # engine in play: `core/config.py` snapshots ``DATABASE_URL`` at import time and
  22. # `core/database.py` builds a module-level ``engine`` / ``async_session`` from
  23. # it. Any app code that opens its own session rather than receiving the fixture
  24. # one therefore talks to whatever database the developer's `.env` names. The
  25. # clearest example is ``run_with_retry`` (used by the print-completion path),
  26. # whose sessions come from ``backend.app.core.database`` — so the widespread
  27. # ``patch("backend.app.main.async_session")`` does not intercept them.
  28. #
  29. # Left alone that is not a hypothetical: on a plain checkout it means the suite
  30. # writes to the developer's real SQLite file, and with a PostgreSQL `.env` it
  31. # means a live install. A completion test calling ``on_print_complete(1, ...)``
  32. # closed a queue item belonging to an actual running print that way.
  33. _TEST_APP_DB_DIR = Path(tempfile.mkdtemp(prefix="bambuddy_test_appdb_"))
  34. APP_DATABASE_URL = f"sqlite+aiosqlite:///{_TEST_APP_DB_DIR / 'app.db'}"
  35. # Opt-in PostgreSQL run. TEST_POSTGRES_URL names a throwaway local server
  36. # (postgresql+asyncpg://user:password@127.0.0.1:port/postgres). Each xdist
  37. # worker gets two fresh databases there, under a prefix this file owns: one for
  38. # the app's own engine, one for the fixtures, kept apart as they are on SQLite.
  39. # Nothing else on that server is touched.
  40. _PG_DB_PREFIX = "bambuddy_testpg_"
  41. TEST_PG_URL: str | None = None
  42. if os.environ.get("TEST_POSTGRES_URL"):
  43. from sqlalchemy.engine import make_url
  44. _pg_server = make_url(os.environ["TEST_POSTGRES_URL"])
  45. if _pg_server.drivername != "postgresql+asyncpg" or _pg_server.host not in ("127.0.0.1", "localhost"):
  46. raise RuntimeError("TEST_POSTGRES_URL must be a postgresql+asyncpg URL of a local throwaway server")
  47. _pg_worker = os.environ.get("PYTEST_XDIST_WORKER", "main")
  48. _pg_app_db = f"{_PG_DB_PREFIX}{_pg_worker}_app"
  49. _pg_test_db = f"{_PG_DB_PREFIX}{_pg_worker}_test"
  50. async def _recreate_pg_databases() -> None:
  51. import asyncpg
  52. conn = await asyncpg.connect(
  53. user=_pg_server.username,
  54. password=_pg_server.password,
  55. host=_pg_server.host,
  56. port=_pg_server.port or 5432,
  57. database=_pg_server.database or "postgres",
  58. )
  59. try:
  60. for name in (_pg_app_db, _pg_test_db):
  61. await conn.execute(f'DROP DATABASE IF EXISTS "{name}" WITH (FORCE)')
  62. await conn.execute(f'CREATE DATABASE "{name}"')
  63. finally:
  64. await conn.close()
  65. asyncio.run(_recreate_pg_databases())
  66. APP_DATABASE_URL = _pg_server.set(database=_pg_app_db).render_as_string(hide_password=False)
  67. TEST_PG_URL = _pg_server.set(database=_pg_test_db).render_as_string(hide_password=False)
  68. os.environ["DATABASE_URL"] = APP_DATABASE_URL
  69. def _cleanup_test_app_db_dir():
  70. shutil.rmtree(_TEST_APP_DB_DIR, ignore_errors=True)
  71. atexit.register(_cleanup_test_app_db_dir)
  72. def _assert_disposable_database(url, source: str) -> None:
  73. """Abort the run unless *url* is the throwaway database created above.
  74. A guard rather than a comment because the failure it prevents is silent and
  75. destructive: the suite would appear to pass while having mutated real print
  76. history. Anything that reintroduces a real ``DATABASE_URL`` — an `.env` read
  77. later in the import order, a fixture rebuilding the engine — trips this
  78. instead of reaching the database.
  79. """
  80. database = str(getattr(url, "database", "") or "")
  81. if TEST_PG_URL is not None:
  82. if (
  83. str(getattr(url, "drivername", "")) == "postgresql+asyncpg"
  84. and getattr(url, "host", None) in ("127.0.0.1", "localhost")
  85. and database.startswith(_PG_DB_PREFIX)
  86. ):
  87. return
  88. raise RuntimeError(f"Refusing to run tests: {source} resolves to {url!r}, not a {_PG_DB_PREFIX}* database.")
  89. if not str(getattr(url, "drivername", "")).startswith("sqlite") or not database.startswith(str(_TEST_APP_DB_DIR)):
  90. raise RuntimeError(
  91. f"Refusing to run tests: {source} resolves to {url!r}, which is not the "
  92. f"disposable SQLite database under {_TEST_APP_DB_DIR}. Tests must never "
  93. f"open a session against a real Bambuddy database."
  94. )
  95. from httpx import ASGITransport, AsyncClient # noqa: E402
  96. from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine # noqa: E402
  97. # Ensure settings use our env vars - import and override before database import
  98. from backend.app.core.config import settings # noqa: E402
  99. settings.log_to_file = False
  100. if settings.database_url != APP_DATABASE_URL:
  101. raise RuntimeError(
  102. f"Refusing to run tests: settings.database_url is {settings.database_url!r} "
  103. f"rather than the disposable test database. Something read DATABASE_URL "
  104. f"before conftest could override it."
  105. )
  106. # Use a temp directory for plate calibration to avoid deleting real calibration files
  107. _test_plate_cal_dir = Path(tempfile.mkdtemp(prefix="bambuddy_test_plate_cal_"))
  108. settings.plate_calibration_dir = _test_plate_cal_dir
  109. # Clean up temp directory when tests finish
  110. def _cleanup_test_plate_cal_dir():
  111. if _test_plate_cal_dir.exists():
  112. shutil.rmtree(_test_plate_cal_dir, ignore_errors=True)
  113. atexit.register(_cleanup_test_plate_cal_dir)
  114. from backend.app.core.database import Base, engine as _app_engine # noqa: E402
  115. # The engine is built at import time from the URL above, so this catches the
  116. # case where that override did not take effect for whatever reason.
  117. _assert_disposable_database(_app_engine.url, "backend.app.core.database.engine")
  118. if TEST_PG_URL is not None:
  119. # The app's own database gets the schema too, once per worker, as a real
  120. # install's has it: code that opens its own session (the backup export,
  121. # print completion) reads and writes it. A short-lived engine, so the app
  122. # engine's pool holds no connection from this event loop.
  123. async def _create_app_schema() -> None:
  124. from backend.app.core.database import _create_engine, load_all_models
  125. load_all_models()
  126. schema_engine = _create_engine(APP_DATABASE_URL)
  127. try:
  128. async with schema_engine.begin() as conn:
  129. await conn.run_sync(Base.metadata.create_all)
  130. finally:
  131. await schema_engine.dispose()
  132. asyncio.run(_create_app_schema())
  133. # Use in-memory SQLite for tests
  134. TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
  135. def pytest_collection_modifyitems(config, items):
  136. """On PostgreSQL, skip the tests of SQLite-specific behaviour."""
  137. if TEST_PG_URL is None:
  138. return
  139. skip = pytest.mark.skip(reason="SQLite-specific behaviour; the suite runs on PostgreSQL")
  140. for item in items:
  141. if item.get_closest_marker("sqlite_only"):
  142. item.add_marker(skip)
  143. @pytest.fixture(autouse=True)
  144. def mfa_encryption_isolation(monkeypatch, tmp_path):
  145. """Per-test isolation for MFA encryption state.
  146. - Sets ``DATA_DIR`` to an isolated tmp path so the auto-bootstrap can
  147. never write ``.mfa_encryption_key`` into the repo or share state
  148. across tests / xdist workers.
  149. - Removes any inherited ``MFA_ENCRYPTION_KEY`` env var.
  150. - With ``DATA_DIR`` pointing at a writable ``tmp_path``, the default
  151. bootstrap path on first ``_get_fernet()`` call is **auto-generation**
  152. (key_source='generated'), NOT plaintext fallback. Tests that need the
  153. plaintext fallback path must monkeypatch ``_load_or_generate_key`` to
  154. return ``(None, 'none')`` (or 'none_write_failed' / 'none_corrupted')
  155. explicitly — see ``test_plaintext_passthrough_without_key`` for an
  156. example.
  157. - Resets the ``encryption`` module-level singletons before AND after the
  158. test so reorder doesn't leak cached Fernet instances.
  159. Tests that want to exercise an active key should call
  160. ``monkeypatch.setenv("MFA_ENCRYPTION_KEY", valid_key)`` and
  161. ``enc_mod._fernet_instance = None`` inside the test body — the autouse
  162. fixture only sets defaults, it doesn't lock them in.
  163. """
  164. from backend.app.core import encryption as enc_mod
  165. monkeypatch.setenv("DATA_DIR", str(tmp_path))
  166. monkeypatch.delenv("MFA_ENCRYPTION_KEY", raising=False)
  167. enc_mod._fernet_instance = None
  168. enc_mod._warn_shown = False
  169. enc_mod._key_source = None
  170. yield
  171. enc_mod._fernet_instance = None
  172. enc_mod._warn_shown = False
  173. enc_mod._key_source = None
  174. @pytest.fixture(autouse=True)
  175. def reset_archive_work():
  176. """Drop archives held or being merged between tests (services/archive_work).
  177. Every test database hands out the same archive ids, and a dispatch driven
  178. without ``_dispatch_one`` never releases its hold."""
  179. from backend.app.services import archive_work
  180. archive_work._held.clear()
  181. archive_work._merging.clear()
  182. yield
  183. archive_work._held.clear()
  184. archive_work._merging.clear()
  185. @pytest.fixture(autouse=True)
  186. def reset_printer_file_cache():
  187. """A fresh file-browser preview cache per test.
  188. Test databases start their printer ids at 1 again, so a preview kept by one
  189. test would be served to the next one's printer 1."""
  190. from backend.app.services.printer_file_cache import printer_file_cache
  191. printer_file_cache.clear()
  192. yield
  193. printer_file_cache.clear()
  194. @pytest.fixture(autouse=True)
  195. def reset_spoolman_location_sync_cache():
  196. """Drop the per-URL Spoolman location-sync TTL cache between tests.
  197. Without this, a test that runs the sync against `http://localhost:7912`
  198. will skip the sync in any later test that uses the same URL within 60
  199. real seconds — test ordering would then leak assertions across runs."""
  200. from backend.app.services.location_service import _spoolman_location_sync_cache_clear
  201. _spoolman_location_sync_cache_clear()
  202. yield
  203. _spoolman_location_sync_cache_clear()
  204. @pytest.fixture(autouse=True)
  205. def reset_slot_unlink_grace():
  206. """Drop held slot unlinks and cancel their re-checks between tests (#3186).
  207. The holds live in a module-level dict keyed by printer id, and every test
  208. database hands out the same ids -- a hold left by one test would let the
  209. next one's first blank report count as already confirmed."""
  210. from backend.app.services import slot_unlink_grace
  211. slot_unlink_grace.reset()
  212. yield
  213. slot_unlink_grace.reset()
  214. @pytest.fixture(autouse=True)
  215. def reset_usage_tracker_sessions():
  216. """Drop the usage tracker's in-flight print sessions between tests.
  217. They live in a module-level dict keyed by printer id. Any test that runs
  218. ``main.on_print_start(1, ...)`` against a mocked printer manager leaves a
  219. session for printer 1 whose fields are MagicMocks, and a later test on the
  220. same worker that completes a print for printer 1 picks it up as its own."""
  221. from backend.app.services.usage_tracker import _active_sessions
  222. _active_sessions.clear()
  223. yield
  224. _active_sessions.clear()
  225. @pytest.fixture(autouse=True)
  226. def reset_ftp_stall_gate():
  227. """Drop the FTP client's per-printer stall cool-off and cleartext replies.
  228. Class-level dicts keyed by IP, and most tests share one printer IP: a
  229. listing that timed out in one test would keep the next one's timelapse
  230. scan from connecting at all (#3474)."""
  231. from backend.app.services.bambu_ftp import BambuFTPClient, _stalled_until
  232. _stalled_until.clear()
  233. BambuFTPClient._cleartext_reply.clear()
  234. yield
  235. _stalled_until.clear()
  236. BambuFTPClient._cleartext_reply.clear()
  237. @pytest.fixture(autouse=True)
  238. def reset_auth_enabled_cache():
  239. """Drop the module-level auth-enabled cache between tests (issue #2572).
  240. ``is_auth_enabled`` caches an enabled=True result for a TTL. Without this
  241. reset a test that enables auth would leave ``True`` cached, so a later test
  242. running in auth-disabled mode (without going through ``set_auth_enabled``)
  243. would wrongly see auth as enabled until the TTL expired — order-dependent
  244. flakiness."""
  245. from backend.app.core.auth import invalidate_auth_enabled_cache
  246. invalidate_auth_enabled_cache()
  247. yield
  248. invalidate_auth_enabled_cache()
  249. @pytest.fixture(autouse=True)
  250. def disconnect_printers_registered_during_a_test():
  251. """Give every test an empty ``printer_manager`` singleton.
  252. ``POST /api/v1/printers`` really calls ``connect_printer``, so a test that
  253. creates a printer through the API parks a live client in the singleton --
  254. and the singleton outlives the per-test in-memory database. The next test
  255. on the same xdist worker gets a fresh database whose first printer is handed
  256. the same primary key, and reads that leftover client as its own live status.
  257. ``test_scheduled_drying_routes`` saw exactly that: an "online" printer with
  258. no firmware version, so scheduling a dry came back 400 instead of 200.
  259. Snapshotting the ids at test entry was insufficient: a client leaked by a
  260. previous module became part of that snapshot and therefore survived every
  261. later cleanup on the same xdist worker. Clear both before and after each
  262. test. ``disconnect_printer`` also clears model/printer-info caches and stops
  263. any paho thread owned by the leaked client.
  264. """
  265. from backend.app.services.printer_manager import printer_manager
  266. for printer_id in list(printer_manager._clients):
  267. printer_manager.disconnect_printer(printer_id)
  268. yield
  269. for printer_id in list(printer_manager._clients):
  270. printer_manager.disconnect_printer(printer_id)
  271. @pytest.fixture(scope="session")
  272. def event_loop():
  273. """Create an instance of the default event loop for each test session."""
  274. loop = asyncio.get_event_loop_policy().new_event_loop()
  275. yield loop
  276. # Dispose the module-level engine so aiosqlite worker threads finish
  277. # before the event loop closes, preventing "Event loop is closed" errors.
  278. from backend.app.core.database import engine
  279. loop.run_until_complete(engine.dispose())
  280. loop.run_until_complete(asyncio.sleep(0.05))
  281. loop.close()
  282. # The test database's schema as built, per worker: built once and emptied
  283. # between tests. Rebuilding it for every test (DROP SCHEMA + create_all, a few
  284. # hundred DDL statements) was 90% of a PostgreSQL run, with 30 workers and the
  285. # server competing for the same cores.
  286. _pg_schema: dict[str, object] = {}
  287. # Everything a test could change about the schema: relations (tables, indexes,
  288. # sequences, views) with their columns, types, nullability and defaults, and
  289. # every constraint.
  290. _PG_SCHEMA_FINGERPRINT = """
  291. SELECT md5(coalesce(string_agg(item, ',' ORDER BY item), '')) FROM (
  292. SELECT c.relname || ':' || c.relkind::text || ':' || coalesce(a.attname, '') || ':'
  293. || coalesce(format_type(a.atttypid, a.atttypmod), '') || ':' || coalesce(a.attnotnull::text, '')
  294. || ':' || coalesce(pg_get_expr(d.adbin, d.adrelid), '') AS item
  295. FROM pg_class c
  296. JOIN pg_namespace n ON n.oid = c.relnamespace
  297. LEFT JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped
  298. LEFT JOIN pg_attrdef d ON d.adrelid = a.attrelid AND d.adnum = a.attnum
  299. WHERE n.nspname = 'public'
  300. UNION ALL
  301. SELECT 'constraint:' || conname || ':' || pg_get_constraintdef(oid)
  302. FROM pg_constraint WHERE connamespace = 'public'::regnamespace
  303. ) AS schema_items
  304. """
  305. # Every row out and every id back to 1. DELETE rather than TRUNCATE: TRUNCATE
  306. # gives each table and index a new file, about three times the cost for the few
  307. # rows a test leaves. Foreign-key triggers are off for this transaction only
  308. # (the throwaway server's superuser), so the order does not matter.
  309. _PG_EMPTY_TABLES = """
  310. DO $$
  311. DECLARE r record;
  312. BEGIN
  313. PERFORM set_config('session_replication_role', 'replica', true);
  314. FOR r IN SELECT tablename FROM pg_tables WHERE schemaname = 'public' ORDER BY tablename LOOP
  315. EXECUTE format('DELETE FROM public.%I', r.tablename);
  316. END LOOP;
  317. FOR r IN SELECT c.oid::regclass AS seq FROM pg_class c
  318. WHERE c.relkind = 'S' AND c.relnamespace = 'public'::regnamespace LOOP
  319. PERFORM setval(r.seq, 1, false);
  320. END LOOP;
  321. END $$
  322. """
  323. async def _reset_pg_test_schema(conn) -> None:
  324. """An empty test database with the models' schema, as cheaply as possible.
  325. Emptied (ids restart at 1) when the schema is still the one built for this
  326. worker; a full rebuild the first time, after a test changed the schema (a
  327. migration test adding a column), and when a model loaded since adds a table.
  328. """
  329. from sqlalchemy import text
  330. from backend.app.core.database import load_all_models
  331. load_all_models()
  332. tables = set(Base.metadata.tables)
  333. fingerprint = (await conn.execute(text(_PG_SCHEMA_FINGERPRINT))).scalar_one()
  334. if _pg_schema.get("fingerprint") != fingerprint or _pg_schema.get("tables") != tables:
  335. await conn.execute(text("DROP SCHEMA public CASCADE"))
  336. await conn.execute(text("CREATE SCHEMA public"))
  337. await conn.run_sync(Base.metadata.create_all)
  338. _pg_schema["fingerprint"] = (await conn.execute(text(_PG_SCHEMA_FINGERPRINT))).scalar_one()
  339. _pg_schema["tables"] = tables
  340. return
  341. await conn.execute(text(_PG_EMPTY_TABLES))
  342. async def _finish_query_cancellations() -> None:
  343. """Wait for asyncpg to finish cancelling queries in this test's loop.
  344. A test that cancels a task mid-query (to stop the background work a
  345. handler started) awaits the task, but asyncpg cancels the query on the
  346. server in a task of its own. If the test ends first, that task is still
  347. pending when the loop closes and is reported as an unraisable
  348. "Connection._cancel" coroutine in some later test.
  349. """
  350. current = asyncio.current_task()
  351. pending = [
  352. task
  353. for task in asyncio.all_tasks()
  354. if task is not current
  355. and not task.done()
  356. and getattr(task.get_coro(), "__qualname__", "") == "Connection._cancel"
  357. ]
  358. if pending:
  359. await asyncio.wait(pending, timeout=5)
  360. async def _stop_background_tasks() -> None:
  361. """Cancel the background tasks this test's code spawned, and wait for them.
  362. A task still in a query when the loop closes gets its answer from
  363. aiosqlite's worker thread into the closed loop ("Event loop is closed"),
  364. reported as a thread exception in whatever test runs next on the worker.
  365. Cancelled here, its session closes while the loop still runs.
  366. """
  367. from backend.app.core.tasks import _background_tasks
  368. loop = asyncio.get_running_loop()
  369. pending = [task for task in _background_tasks if not task.done() and task.get_loop() is loop]
  370. for task in pending:
  371. task.cancel()
  372. if pending:
  373. await asyncio.wait(pending, timeout=5)
  374. async def _release_app_engine() -> None:
  375. """Close the app engine's pooled connections before this test's loop ends.
  376. Code that opens its own session (print completion, services) uses the
  377. app's module-level engine, shared by every test on the worker. Its pool
  378. would keep a connection opened in this test's event loop into the next
  379. test's, where SQLAlchemy closes it in a loop it does not belong to; on
  380. PostgreSQL that leaves asyncpg's cleanup unawaited, reported as an
  381. unraisable "Connection._cancel" coroutine in some later test.
  382. """
  383. # First: a task still holding a connection would return it to the pool
  384. # after the dispose below has replaced that pool, and it is never closed.
  385. await _stop_background_tasks()
  386. await _finish_query_cancellations()
  387. await _app_engine.dispose()
  388. @pytest.fixture(autouse=True)
  389. async def _app_engine_released_after_each_test():
  390. """Every test, not only those with test_engine: any of them can reach
  391. the app engine through code that opens its own session.
  392. On SQLite too: an aiosqlite connection belongs to the loop it was opened
  393. in, so one kept in the pool answers the next test's query into a closed
  394. loop ("Event loop is closed" from aiosqlite's worker thread).
  395. """
  396. yield
  397. await _release_app_engine()
  398. @pytest.fixture
  399. async def test_engine():
  400. """Create a test database engine."""
  401. engine = create_async_engine(TEST_DATABASE_URL, echo=False)
  402. # Every model, as the app's schema has them: a hand-kept list here fell
  403. # behind and left four tables out of the SQLite test database.
  404. from backend.app.core.database import load_all_models
  405. load_all_models()
  406. if TEST_PG_URL is not None:
  407. # The same engine setup as the app's (UTC session, aware datetimes
  408. # made naive), on this worker's own database, emptied for each test.
  409. from backend.app.core.database import _create_engine
  410. await engine.dispose()
  411. engine = _create_engine(TEST_PG_URL)
  412. async with engine.begin() as conn:
  413. await _reset_pg_test_schema(conn)
  414. yield engine
  415. await _release_app_engine()
  416. await engine.dispose()
  417. return
  418. async with engine.begin() as conn:
  419. await conn.run_sync(Base.metadata.create_all)
  420. yield engine
  421. await _release_app_engine()
  422. async with engine.begin() as conn:
  423. await conn.run_sync(Base.metadata.drop_all)
  424. await engine.dispose()
  425. # Allow aiosqlite's background thread to finish processing the close
  426. # response before the per-function event loop shuts down, preventing
  427. # "RuntimeError: Event loop is closed" in call_soon_threadsafe.
  428. await asyncio.sleep(0.1)
  429. @pytest.fixture
  430. async def db_session(test_engine) -> AsyncGenerator[AsyncSession, None]:
  431. """Create a test database session."""
  432. async_session_maker = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
  433. async with async_session_maker() as session:
  434. yield session
  435. @pytest.fixture
  436. async def async_client(test_engine, db_session) -> AsyncGenerator[AsyncClient, None]:
  437. """Create an async test client."""
  438. from backend.app.core.database import async_session, get_db
  439. from backend.app.main import app
  440. # Create a new session maker for the test engine
  441. test_async_session = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
  442. async def override_get_db():
  443. # Mirror production get_db (core/database.py): commit on success,
  444. # rollback on error. Endpoints that rely on the request-scoped
  445. # implicit commit (e.g. create_project, which only flushes) would
  446. # otherwise silently lose their writes in tests (#1897).
  447. async with test_async_session() as session:
  448. try:
  449. yield session
  450. await session.commit()
  451. except BaseException:
  452. await session.rollback()
  453. raise
  454. app.dependency_overrides[get_db] = override_get_db
  455. # Mock init_printer_connections to prevent MQTT connection attempts during tests
  456. async def mock_init_printer_connections(db):
  457. pass # No-op - don't connect to real printers
  458. # Also patch the module-level async_session used by services, auth, and middleware
  459. with (
  460. patch("backend.app.core.database.async_session", test_async_session),
  461. patch("backend.app.core.auth.async_session", test_async_session),
  462. patch("backend.app.main.async_session", test_async_session),
  463. # Obico endpoints load settings through the service's module-level binding;
  464. # without this patch they'd read whatever DB the cwd resolves to (#1546).
  465. patch("backend.app.services.obico_detection.async_session", test_async_session),
  466. patch("backend.app.services.octoeverywhere_detection.async_session", test_async_session),
  467. patch("backend.app.main.init_printer_connections", mock_init_printer_connections),
  468. ):
  469. # Seed default groups for tests that need them
  470. from backend.app.core.database import seed_default_groups
  471. await seed_default_groups()
  472. async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
  473. yield client
  474. # The app lifespan called init_db() which used the module-level engine
  475. # (not the test engine), creating aiosqlite connections. Close those,
  476. # and stop background work still using them, before the loop closes.
  477. await _release_app_engine()
  478. app.dependency_overrides.clear()
  479. # ============================================================================
  480. # Mock External Services
  481. # ============================================================================
  482. @pytest.fixture
  483. def mock_tasmota_service():
  484. """Mock the Tasmota service for smart plug tests."""
  485. # Patch both the module where it's defined and where it's imported
  486. with (
  487. patch("backend.app.services.tasmota.tasmota_service") as mock,
  488. patch("backend.app.api.routes.smart_plugs.tasmota_service") as mock2,
  489. ):
  490. mock.turn_on = AsyncMock(return_value=True)
  491. mock.turn_off = AsyncMock(return_value=True)
  492. mock.toggle = AsyncMock(return_value=True)
  493. mock.get_status = AsyncMock(return_value={"state": "ON", "reachable": True, "device_name": "Test Plug"})
  494. mock.get_energy = AsyncMock(
  495. return_value={
  496. "power": 150.5,
  497. "voltage": 120.0,
  498. "current": 1.25,
  499. "today": 2.5,
  500. "total": 100.0,
  501. "factor": 0.95,
  502. }
  503. )
  504. mock.test_connection = AsyncMock(return_value={"success": True, "state": "ON", "device_name": "Test Plug"})
  505. # Copy mocks to second patch target
  506. mock2.turn_on = mock.turn_on
  507. mock2.turn_off = mock.turn_off
  508. mock2.toggle = mock.toggle
  509. mock2.get_status = mock.get_status
  510. mock2.get_energy = mock.get_energy
  511. mock2.test_connection = mock.test_connection
  512. yield mock
  513. @pytest.fixture
  514. def mock_homeassistant_service():
  515. """Mock the Home Assistant service for smart plug tests."""
  516. # Patch both the module where it's defined and where it's imported
  517. with (
  518. patch("backend.app.services.homeassistant.homeassistant_service") as mock,
  519. patch("backend.app.api.routes.smart_plugs.homeassistant_service") as mock2,
  520. ):
  521. mock.turn_on = AsyncMock(return_value=True)
  522. mock.turn_off = AsyncMock(return_value=True)
  523. mock.toggle = AsyncMock(return_value=True)
  524. mock.get_status = AsyncMock(return_value={"state": "ON", "reachable": True, "device_name": "Test HA Entity"})
  525. mock.get_energy = AsyncMock(return_value=None) # Most HA entities don't have power monitoring
  526. mock.test_connection = AsyncMock(return_value={"success": True, "message": "API running", "error": None})
  527. mock.list_entities = AsyncMock(
  528. return_value=[
  529. {
  530. "entity_id": "switch.printer_plug",
  531. "friendly_name": "Printer Plug",
  532. "state": "on",
  533. "domain": "switch",
  534. },
  535. {"entity_id": "switch.test", "friendly_name": "Test Switch", "state": "off", "domain": "switch"},
  536. ]
  537. )
  538. mock.configure = MagicMock()
  539. # Copy mocks to second patch target
  540. mock2.turn_on = mock.turn_on
  541. mock2.turn_off = mock.turn_off
  542. mock2.toggle = mock.toggle
  543. mock2.get_status = mock.get_status
  544. mock2.get_energy = mock.get_energy
  545. mock2.test_connection = mock.test_connection
  546. mock2.list_entities = mock.list_entities
  547. mock2.configure = mock.configure
  548. yield mock
  549. @pytest.fixture
  550. def mock_mqtt_client():
  551. """Mock the MQTT client for printer communication tests."""
  552. with patch("backend.app.services.bambu_mqtt.BambuMQTTClient") as mock:
  553. instance = MagicMock()
  554. instance.state = MagicMock(connected=True, state="IDLE", progress=0, temperatures={"nozzle": 25, "bed": 25})
  555. instance.connect = MagicMock()
  556. instance.disconnect = MagicMock()
  557. mock.return_value = instance
  558. yield mock
  559. @pytest.fixture
  560. def mock_mqtt_smart_plug_service():
  561. """Mock the MQTT smart plug service for MQTT plug tests."""
  562. with patch("backend.app.api.routes.smart_plugs.mqtt_relay") as mock:
  563. # Create a mock smart_plug_service
  564. mock_service = MagicMock()
  565. mock_service.is_configured = MagicMock(return_value=True)
  566. mock_service.configure = AsyncMock(return_value=True)
  567. mock_service.subscribe = MagicMock()
  568. mock_service.unsubscribe = MagicMock()
  569. mock_service.get_plug_data = MagicMock(return_value=None)
  570. mock_service.is_reachable = MagicMock(return_value=False)
  571. mock.smart_plug_service = mock_service
  572. yield mock
  573. @pytest.fixture
  574. def mock_ftp_client():
  575. """Mock the FTP client for file transfer tests."""
  576. with (
  577. patch("backend.app.services.bambu_ftp.download_file_async") as download_mock,
  578. patch("backend.app.services.bambu_ftp.list_files_async") as list_mock,
  579. ):
  580. download_mock.return_value = True
  581. list_mock.return_value = []
  582. yield {"download": download_mock, "list": list_mock}
  583. @pytest.fixture
  584. def mock_httpx_client():
  585. """Mock httpx for webhook/notification HTTP calls."""
  586. with patch("httpx.AsyncClient") as mock_class:
  587. mock_instance = AsyncMock()
  588. mock_response = MagicMock()
  589. mock_response.status_code = 200
  590. mock_response.text = "OK"
  591. mock_response.json.return_value = {}
  592. mock_instance.get = AsyncMock(return_value=mock_response)
  593. mock_instance.post = AsyncMock(return_value=mock_response)
  594. # ntfy switches to PUT when a photo is attached (e.g. the sample image on Test).
  595. mock_instance.put = AsyncMock(return_value=mock_response)
  596. mock_instance.__aenter__ = AsyncMock(return_value=mock_instance)
  597. mock_instance.__aexit__ = AsyncMock()
  598. mock_class.return_value = mock_instance
  599. yield mock_instance
  600. @pytest.fixture
  601. def mock_printer_manager():
  602. """Mock the printer manager for status checks."""
  603. with patch("backend.app.services.printer_manager.printer_manager") as mock:
  604. mock.get_status = MagicMock(
  605. return_value=MagicMock(
  606. connected=True,
  607. state="IDLE",
  608. progress=0,
  609. temperatures={"nozzle": 25, "bed": 25, "chamber": 25},
  610. raw_data={},
  611. )
  612. )
  613. mock.mark_printer_offline = MagicMock()
  614. yield mock
  615. # ============================================================================
  616. # Factory Fixtures for Test Data
  617. # ============================================================================
  618. @pytest.fixture
  619. def smart_plug_factory(db_session):
  620. """Factory to create test smart plugs."""
  621. async def _create_plug(**kwargs):
  622. from backend.app.models.smart_plug import SmartPlug
  623. # Determine defaults based on plug_type
  624. plug_type = kwargs.get("plug_type", "tasmota")
  625. defaults = {
  626. "name": "Test Plug",
  627. "plug_type": plug_type,
  628. "enabled": True,
  629. "auto_on": True,
  630. "auto_off": True,
  631. "off_delay_mode": "time",
  632. "off_delay_minutes": 5,
  633. "off_temp_threshold": 70,
  634. "schedule_enabled": False,
  635. "power_alert_enabled": False,
  636. }
  637. # Set required fields based on plug_type
  638. if plug_type == "homeassistant":
  639. defaults["ha_entity_id"] = "switch.test"
  640. defaults["ip_address"] = None
  641. elif plug_type == "mqtt":
  642. # Legacy fields (for backward compatibility tests)
  643. defaults["mqtt_topic"] = kwargs.get("mqtt_topic", "test/topic")
  644. defaults["mqtt_multiplier"] = kwargs.get("mqtt_multiplier", 1.0)
  645. # New separate topic/path/multiplier fields
  646. defaults["mqtt_power_topic"] = kwargs.get("mqtt_power_topic")
  647. defaults["mqtt_power_path"] = kwargs.get("mqtt_power_path", "power")
  648. defaults["mqtt_power_multiplier"] = kwargs.get("mqtt_power_multiplier", 1.0)
  649. defaults["mqtt_energy_topic"] = kwargs.get("mqtt_energy_topic")
  650. defaults["mqtt_energy_path"] = kwargs.get("mqtt_energy_path")
  651. defaults["mqtt_energy_multiplier"] = kwargs.get("mqtt_energy_multiplier", 1.0)
  652. defaults["mqtt_state_topic"] = kwargs.get("mqtt_state_topic")
  653. defaults["mqtt_state_path"] = kwargs.get("mqtt_state_path")
  654. defaults["mqtt_state_on_value"] = kwargs.get("mqtt_state_on_value")
  655. defaults["ip_address"] = None
  656. defaults["ha_entity_id"] = None
  657. elif plug_type == "rest":
  658. defaults["rest_on_url"] = kwargs.get("rest_on_url", "http://192.168.1.100/api/plug/on")
  659. defaults["rest_off_url"] = kwargs.get("rest_off_url", "http://192.168.1.100/api/plug/off")
  660. defaults["rest_method"] = kwargs.get("rest_method", "POST")
  661. defaults["ip_address"] = None
  662. defaults["ha_entity_id"] = None
  663. else:
  664. defaults["ip_address"] = "192.168.1.100"
  665. defaults["ha_entity_id"] = None
  666. defaults.update(kwargs)
  667. plug = SmartPlug(**defaults)
  668. db_session.add(plug)
  669. await db_session.commit()
  670. await db_session.refresh(plug)
  671. return plug
  672. return _create_plug
  673. @pytest.fixture
  674. async def printer_1(db_session):
  675. """A printer with id 1, for tests whose rows refer to printer 1 directly.
  676. Without it those rows point at nothing, which only SQLite (foreign keys
  677. off) accepts.
  678. """
  679. from sqlalchemy import text
  680. from backend.app.models.printer import Printer
  681. printer = Printer(
  682. id=1, name="Printer 1", serial_number="00M09A900000001", ip_address="192.168.1.250", access_code="12345678"
  683. )
  684. db_session.add(printer)
  685. await db_session.flush()
  686. if TEST_PG_URL is not None:
  687. # An explicit id does not move the sequence on.
  688. await db_session.execute(text("SELECT setval(pg_get_serial_sequence('printers', 'id'), 1)"))
  689. await db_session.commit()
  690. return printer
  691. @pytest.fixture
  692. def printer_factory(db_session):
  693. """Factory to create test printers."""
  694. _counter = [0] # Use list to allow mutation in nested function
  695. async def _create_printer(**kwargs):
  696. from backend.app.models.printer import Printer
  697. _counter[0] += 1
  698. counter = _counter[0]
  699. defaults = {
  700. "name": "Test Printer",
  701. "serial_number": f"00M09A{counter:09d}", # Unique serial per printer
  702. "ip_address": f"192.168.1.{100 + counter}", # Unique IP per printer
  703. "access_code": "12345678",
  704. "is_active": True,
  705. "auto_archive": True,
  706. "model": "X1C",
  707. }
  708. defaults.update(kwargs)
  709. printer = Printer(**defaults)
  710. db_session.add(printer)
  711. await db_session.commit()
  712. await db_session.refresh(printer)
  713. return printer
  714. return _create_printer
  715. @pytest.fixture
  716. def location_factory(db_session):
  717. _counter = [0]
  718. async def _create_location(**kwargs):
  719. from backend.app.models.location import Location
  720. _counter[0] += 1
  721. counter = _counter[0]
  722. name = kwargs.pop("name", f"Test Location {counter}")
  723. defaults = {
  724. "name": name,
  725. "name_key": name.strip().lower(),
  726. }
  727. defaults.update(kwargs)
  728. location = Location(**defaults)
  729. db_session.add(location)
  730. await db_session.commit()
  731. await db_session.refresh(location)
  732. return location
  733. return _create_location
  734. @pytest.fixture
  735. def notification_provider_factory(db_session):
  736. """Factory to create test notification providers."""
  737. async def _create_provider(**kwargs):
  738. from backend.app.models.notification import NotificationProvider
  739. config = kwargs.pop("config", {"server": "https://ntfy.sh", "topic": "test-topic"})
  740. if isinstance(config, dict):
  741. config = json.dumps(config)
  742. defaults = {
  743. "name": "Test Provider",
  744. "provider_type": "ntfy",
  745. "enabled": True,
  746. "config": config,
  747. "on_print_start": True,
  748. "on_print_complete": True,
  749. "on_print_failed": True,
  750. "on_print_stopped": True,
  751. "on_print_progress": False,
  752. "on_print_missing_spool_assignment": False,
  753. "on_billing_charge_failed": True,
  754. "on_printer_offline": False,
  755. "on_printer_error": False,
  756. "on_filament_low": False,
  757. "on_maintenance_due": False,
  758. "on_ams_humidity_high": False,
  759. "on_ams_temperature_high": False,
  760. "on_bed_cooled": False,
  761. "quiet_hours_enabled": False,
  762. "daily_digest_enabled": False,
  763. }
  764. defaults.update(kwargs)
  765. provider = NotificationProvider(**defaults)
  766. db_session.add(provider)
  767. await db_session.commit()
  768. await db_session.refresh(provider)
  769. return provider
  770. return _create_provider
  771. @pytest.fixture
  772. def archive_factory(db_session):
  773. """Factory to create test archives.
  774. Also synthesizes one PrintLogEntry per archive (matching the production
  775. flow where statistics are aggregated from PrintLogEntry, not PrintArchive,
  776. per #1378). Pass ``with_run=False`` to skip — useful for testing the
  777. "archived but never printed" state. Pass ``run_status=...`` to override
  778. the run's status independently of the archive's status field.
  779. """
  780. async def _create_archive(printer_id: int, **kwargs):
  781. from backend.app.models.archive import PrintArchive
  782. from backend.app.models.print_log import PrintLogEntry
  783. with_run = kwargs.pop("with_run", True)
  784. run_status = kwargs.pop("run_status", None)
  785. defaults = {
  786. "printer_id": printer_id,
  787. "filename": "test_print.gcode.3mf",
  788. "print_name": "Test Print",
  789. "file_path": "archives/test/test_print.gcode.3mf",
  790. "file_size": 1024000,
  791. "status": "completed",
  792. "filament_type": "PLA",
  793. "filament_used_grams": 50.0,
  794. "print_time_seconds": 3600,
  795. }
  796. defaults.update(kwargs)
  797. archive = PrintArchive(**defaults)
  798. db_session.add(archive)
  799. await db_session.commit()
  800. await db_session.refresh(archive)
  801. if with_run:
  802. duration = None
  803. if archive.started_at and archive.completed_at:
  804. duration = int((archive.completed_at - archive.started_at).total_seconds()) or None
  805. run = PrintLogEntry(
  806. archive_id=archive.id,
  807. printer_id=archive.printer_id,
  808. status=run_status or archive.status,
  809. started_at=archive.started_at,
  810. completed_at=archive.completed_at,
  811. duration_seconds=duration,
  812. filament_type=archive.filament_type,
  813. filament_color=archive.filament_color,
  814. filament_used_grams=archive.filament_used_grams,
  815. cost=archive.cost,
  816. energy_kwh=archive.energy_kwh,
  817. energy_cost=archive.energy_cost,
  818. failure_reason=archive.failure_reason,
  819. print_name=archive.print_name,
  820. created_by_id=archive.created_by_id,
  821. # Sync the event's created_at with the archive's so date-range
  822. # filtered tests that backdate an archive still find its event.
  823. created_at=archive.created_at,
  824. )
  825. db_session.add(run)
  826. await db_session.commit()
  827. return archive
  828. return _create_archive
  829. # ============================================================================
  830. # Sample Data Fixtures
  831. # ============================================================================
  832. @pytest.fixture
  833. def sample_mqtt_print_start():
  834. """Sample MQTT message for print start."""
  835. return {
  836. "print": {
  837. "command": "project_file",
  838. "param": "/sdcard/test.gcode.3mf",
  839. "subtask_name": "test_print",
  840. "gcode_state": "RUNNING",
  841. "mc_percent": 0,
  842. }
  843. }
  844. @pytest.fixture
  845. def sample_mqtt_print_complete():
  846. """Sample MQTT message for print complete."""
  847. return {
  848. "print": {
  849. "gcode_state": "FINISH",
  850. "mc_percent": 100,
  851. "subtask_name": "test_print",
  852. }
  853. }
  854. @pytest.fixture
  855. def sample_printer_status():
  856. """Sample printer status data."""
  857. return {
  858. "connected": True,
  859. "state": "IDLE",
  860. "progress": 0,
  861. "layer_num": 0,
  862. "total_layers": 0,
  863. "temperatures": {
  864. "nozzle": 25.0,
  865. "bed": 25.0,
  866. "chamber": 25.0,
  867. },
  868. "remaining_time": 0,
  869. "filename": None,
  870. }
  871. # ============================================================================
  872. # Log Capture Fixtures for Error Detection
  873. # ============================================================================
  874. class LogCapture(logging.Handler):
  875. """Handler that captures log records for testing."""
  876. def __init__(self):
  877. super().__init__()
  878. self.records: list[logging.LogRecord] = []
  879. def emit(self, record: logging.LogRecord):
  880. self.records.append(record)
  881. def clear(self):
  882. self.records.clear()
  883. def get_errors(self) -> list[logging.LogRecord]:
  884. """Get all ERROR and CRITICAL level records."""
  885. return [r for r in self.records if r.levelno >= logging.ERROR]
  886. def get_warnings(self) -> list[logging.LogRecord]:
  887. """Get all WARNING level records."""
  888. return [r for r in self.records if r.levelno == logging.WARNING]
  889. def has_errors(self) -> bool:
  890. """Check if any errors were logged."""
  891. return len(self.get_errors()) > 0
  892. def format_errors(self) -> str:
  893. """Format all errors as a string for assertion messages."""
  894. errors = self.get_errors()
  895. if not errors:
  896. return "No errors"
  897. formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
  898. return "\n".join(formatter.format(r) for r in errors)
  899. @pytest.fixture
  900. def capture_logs():
  901. """Fixture that captures log output during a test.
  902. Usage:
  903. def test_something(capture_logs):
  904. # Do something that might log errors
  905. some_function()
  906. # Check no errors were logged
  907. assert not capture_logs.has_errors(), capture_logs.format_errors()
  908. """
  909. handler = LogCapture()
  910. handler.setLevel(logging.DEBUG)
  911. # Attach to root logger to capture all logs
  912. root_logger = logging.getLogger()
  913. root_logger.addHandler(handler)
  914. yield handler
  915. root_logger.removeHandler(handler)
  916. @pytest.fixture
  917. def assert_no_log_errors(capture_logs):
  918. """Fixture that automatically asserts no errors were logged.
  919. Usage:
  920. def test_something(assert_no_log_errors):
  921. # If any ERROR logs occur during this test, it will fail
  922. some_function()
  923. """
  924. yield capture_logs
  925. errors = capture_logs.get_errors()
  926. if errors:
  927. pytest.fail(f"Unexpected log errors:\n{capture_logs.format_errors()}")