conftest.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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_auth_enabled_cache():
  227. """Drop the module-level auth-enabled cache between tests (issue #2572).
  228. ``is_auth_enabled`` caches an enabled=True result for a TTL. Without this
  229. reset a test that enables auth would leave ``True`` cached, so a later test
  230. running in auth-disabled mode (without going through ``set_auth_enabled``)
  231. would wrongly see auth as enabled until the TTL expired — order-dependent
  232. flakiness."""
  233. from backend.app.core.auth import invalidate_auth_enabled_cache
  234. invalidate_auth_enabled_cache()
  235. yield
  236. invalidate_auth_enabled_cache()
  237. @pytest.fixture(autouse=True)
  238. def disconnect_printers_registered_during_a_test():
  239. """Give every test an empty ``printer_manager`` singleton.
  240. ``POST /api/v1/printers`` really calls ``connect_printer``, so a test that
  241. creates a printer through the API parks a live client in the singleton --
  242. and the singleton outlives the per-test in-memory database. The next test
  243. on the same xdist worker gets a fresh database whose first printer is handed
  244. the same primary key, and reads that leftover client as its own live status.
  245. ``test_scheduled_drying_routes`` saw exactly that: an "online" printer with
  246. no firmware version, so scheduling a dry came back 400 instead of 200.
  247. Snapshotting the ids at test entry was insufficient: a client leaked by a
  248. previous module became part of that snapshot and therefore survived every
  249. later cleanup on the same xdist worker. Clear both before and after each
  250. test. ``disconnect_printer`` also clears model/printer-info caches and stops
  251. any paho thread owned by the leaked client.
  252. """
  253. from backend.app.services.printer_manager import printer_manager
  254. for printer_id in list(printer_manager._clients):
  255. printer_manager.disconnect_printer(printer_id)
  256. yield
  257. for printer_id in list(printer_manager._clients):
  258. printer_manager.disconnect_printer(printer_id)
  259. @pytest.fixture(scope="session")
  260. def event_loop():
  261. """Create an instance of the default event loop for each test session."""
  262. loop = asyncio.get_event_loop_policy().new_event_loop()
  263. yield loop
  264. # Dispose the module-level engine so aiosqlite worker threads finish
  265. # before the event loop closes, preventing "Event loop is closed" errors.
  266. from backend.app.core.database import engine
  267. loop.run_until_complete(engine.dispose())
  268. loop.run_until_complete(asyncio.sleep(0.05))
  269. loop.close()
  270. # The test database's schema as built, per worker: built once and emptied
  271. # between tests. Rebuilding it for every test (DROP SCHEMA + create_all, a few
  272. # hundred DDL statements) was 90% of a PostgreSQL run, with 30 workers and the
  273. # server competing for the same cores.
  274. _pg_schema: dict[str, object] = {}
  275. # Everything a test could change about the schema: relations (tables, indexes,
  276. # sequences, views) with their columns, types, nullability and defaults, and
  277. # every constraint.
  278. _PG_SCHEMA_FINGERPRINT = """
  279. SELECT md5(coalesce(string_agg(item, ',' ORDER BY item), '')) FROM (
  280. SELECT c.relname || ':' || c.relkind::text || ':' || coalesce(a.attname, '') || ':'
  281. || coalesce(format_type(a.atttypid, a.atttypmod), '') || ':' || coalesce(a.attnotnull::text, '')
  282. || ':' || coalesce(pg_get_expr(d.adbin, d.adrelid), '') AS item
  283. FROM pg_class c
  284. JOIN pg_namespace n ON n.oid = c.relnamespace
  285. LEFT JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped
  286. LEFT JOIN pg_attrdef d ON d.adrelid = a.attrelid AND d.adnum = a.attnum
  287. WHERE n.nspname = 'public'
  288. UNION ALL
  289. SELECT 'constraint:' || conname || ':' || pg_get_constraintdef(oid)
  290. FROM pg_constraint WHERE connamespace = 'public'::regnamespace
  291. ) AS schema_items
  292. """
  293. # Every row out and every id back to 1. DELETE rather than TRUNCATE: TRUNCATE
  294. # gives each table and index a new file, about three times the cost for the few
  295. # rows a test leaves. Foreign-key triggers are off for this transaction only
  296. # (the throwaway server's superuser), so the order does not matter.
  297. _PG_EMPTY_TABLES = """
  298. DO $$
  299. DECLARE r record;
  300. BEGIN
  301. PERFORM set_config('session_replication_role', 'replica', true);
  302. FOR r IN SELECT tablename FROM pg_tables WHERE schemaname = 'public' ORDER BY tablename LOOP
  303. EXECUTE format('DELETE FROM public.%I', r.tablename);
  304. END LOOP;
  305. FOR r IN SELECT c.oid::regclass AS seq FROM pg_class c
  306. WHERE c.relkind = 'S' AND c.relnamespace = 'public'::regnamespace LOOP
  307. PERFORM setval(r.seq, 1, false);
  308. END LOOP;
  309. END $$
  310. """
  311. async def _reset_pg_test_schema(conn) -> None:
  312. """An empty test database with the models' schema, as cheaply as possible.
  313. Emptied (ids restart at 1) when the schema is still the one built for this
  314. worker; a full rebuild the first time, after a test changed the schema (a
  315. migration test adding a column), and when a model loaded since adds a table.
  316. """
  317. from sqlalchemy import text
  318. from backend.app.core.database import load_all_models
  319. load_all_models()
  320. tables = set(Base.metadata.tables)
  321. fingerprint = (await conn.execute(text(_PG_SCHEMA_FINGERPRINT))).scalar_one()
  322. if _pg_schema.get("fingerprint") != fingerprint or _pg_schema.get("tables") != tables:
  323. await conn.execute(text("DROP SCHEMA public CASCADE"))
  324. await conn.execute(text("CREATE SCHEMA public"))
  325. await conn.run_sync(Base.metadata.create_all)
  326. _pg_schema["fingerprint"] = (await conn.execute(text(_PG_SCHEMA_FINGERPRINT))).scalar_one()
  327. _pg_schema["tables"] = tables
  328. return
  329. await conn.execute(text(_PG_EMPTY_TABLES))
  330. async def _finish_query_cancellations() -> None:
  331. """Wait for asyncpg to finish cancelling queries in this test's loop.
  332. A test that cancels a task mid-query (to stop the background work a
  333. handler started) awaits the task, but asyncpg cancels the query on the
  334. server in a task of its own. If the test ends first, that task is still
  335. pending when the loop closes and is reported as an unraisable
  336. "Connection._cancel" coroutine in some later test.
  337. """
  338. current = asyncio.current_task()
  339. pending = [
  340. task
  341. for task in asyncio.all_tasks()
  342. if task is not current
  343. and not task.done()
  344. and getattr(task.get_coro(), "__qualname__", "") == "Connection._cancel"
  345. ]
  346. if pending:
  347. await asyncio.wait(pending, timeout=5)
  348. async def _stop_background_tasks() -> None:
  349. """Cancel the background tasks this test's code spawned, and wait for them.
  350. A task still in a query when the loop closes gets its answer from
  351. aiosqlite's worker thread into the closed loop ("Event loop is closed"),
  352. reported as a thread exception in whatever test runs next on the worker.
  353. Cancelled here, its session closes while the loop still runs.
  354. """
  355. from backend.app.core.tasks import _background_tasks
  356. loop = asyncio.get_running_loop()
  357. pending = [task for task in _background_tasks if not task.done() and task.get_loop() is loop]
  358. for task in pending:
  359. task.cancel()
  360. if pending:
  361. await asyncio.wait(pending, timeout=5)
  362. async def _release_app_engine() -> None:
  363. """Close the app engine's pooled connections before this test's loop ends.
  364. Code that opens its own session (print completion, services) uses the
  365. app's module-level engine, shared by every test on the worker. Its pool
  366. would keep a connection opened in this test's event loop into the next
  367. test's, where SQLAlchemy closes it in a loop it does not belong to; on
  368. PostgreSQL that leaves asyncpg's cleanup unawaited, reported as an
  369. unraisable "Connection._cancel" coroutine in some later test.
  370. """
  371. # First: a task still holding a connection would return it to the pool
  372. # after the dispose below has replaced that pool, and it is never closed.
  373. await _stop_background_tasks()
  374. await _finish_query_cancellations()
  375. await _app_engine.dispose()
  376. @pytest.fixture(autouse=True)
  377. async def _app_engine_released_after_each_test():
  378. """Every test, not only those with test_engine: any of them can reach
  379. the app engine through code that opens its own session.
  380. On SQLite too: an aiosqlite connection belongs to the loop it was opened
  381. in, so one kept in the pool answers the next test's query into a closed
  382. loop ("Event loop is closed" from aiosqlite's worker thread).
  383. """
  384. yield
  385. await _release_app_engine()
  386. @pytest.fixture
  387. async def test_engine():
  388. """Create a test database engine."""
  389. engine = create_async_engine(TEST_DATABASE_URL, echo=False)
  390. # Every model, as the app's schema has them: a hand-kept list here fell
  391. # behind and left four tables out of the SQLite test database.
  392. from backend.app.core.database import load_all_models
  393. load_all_models()
  394. if TEST_PG_URL is not None:
  395. # The same engine setup as the app's (UTC session, aware datetimes
  396. # made naive), on this worker's own database, emptied for each test.
  397. from backend.app.core.database import _create_engine
  398. await engine.dispose()
  399. engine = _create_engine(TEST_PG_URL)
  400. async with engine.begin() as conn:
  401. await _reset_pg_test_schema(conn)
  402. yield engine
  403. await _release_app_engine()
  404. await engine.dispose()
  405. return
  406. async with engine.begin() as conn:
  407. await conn.run_sync(Base.metadata.create_all)
  408. yield engine
  409. await _release_app_engine()
  410. async with engine.begin() as conn:
  411. await conn.run_sync(Base.metadata.drop_all)
  412. await engine.dispose()
  413. # Allow aiosqlite's background thread to finish processing the close
  414. # response before the per-function event loop shuts down, preventing
  415. # "RuntimeError: Event loop is closed" in call_soon_threadsafe.
  416. await asyncio.sleep(0.1)
  417. @pytest.fixture
  418. async def db_session(test_engine) -> AsyncGenerator[AsyncSession, None]:
  419. """Create a test database session."""
  420. async_session_maker = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
  421. async with async_session_maker() as session:
  422. yield session
  423. @pytest.fixture
  424. async def async_client(test_engine, db_session) -> AsyncGenerator[AsyncClient, None]:
  425. """Create an async test client."""
  426. from backend.app.core.database import async_session, get_db
  427. from backend.app.main import app
  428. # Create a new session maker for the test engine
  429. test_async_session = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
  430. async def override_get_db():
  431. # Mirror production get_db (core/database.py): commit on success,
  432. # rollback on error. Endpoints that rely on the request-scoped
  433. # implicit commit (e.g. create_project, which only flushes) would
  434. # otherwise silently lose their writes in tests (#1897).
  435. async with test_async_session() as session:
  436. try:
  437. yield session
  438. await session.commit()
  439. except BaseException:
  440. await session.rollback()
  441. raise
  442. app.dependency_overrides[get_db] = override_get_db
  443. # Mock init_printer_connections to prevent MQTT connection attempts during tests
  444. async def mock_init_printer_connections(db):
  445. pass # No-op - don't connect to real printers
  446. # Also patch the module-level async_session used by services, auth, and middleware
  447. with (
  448. patch("backend.app.core.database.async_session", test_async_session),
  449. patch("backend.app.core.auth.async_session", test_async_session),
  450. patch("backend.app.main.async_session", test_async_session),
  451. # Obico endpoints load settings through the service's module-level binding;
  452. # without this patch they'd read whatever DB the cwd resolves to (#1546).
  453. patch("backend.app.services.obico_detection.async_session", test_async_session),
  454. patch("backend.app.main.init_printer_connections", mock_init_printer_connections),
  455. ):
  456. # Seed default groups for tests that need them
  457. from backend.app.core.database import seed_default_groups
  458. await seed_default_groups()
  459. async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
  460. yield client
  461. # The app lifespan called init_db() which used the module-level engine
  462. # (not the test engine), creating aiosqlite connections. Close those,
  463. # and stop background work still using them, before the loop closes.
  464. await _release_app_engine()
  465. app.dependency_overrides.clear()
  466. # ============================================================================
  467. # Mock External Services
  468. # ============================================================================
  469. @pytest.fixture
  470. def mock_tasmota_service():
  471. """Mock the Tasmota service for smart plug tests."""
  472. # Patch both the module where it's defined and where it's imported
  473. with (
  474. patch("backend.app.services.tasmota.tasmota_service") as mock,
  475. patch("backend.app.api.routes.smart_plugs.tasmota_service") as mock2,
  476. ):
  477. mock.turn_on = AsyncMock(return_value=True)
  478. mock.turn_off = AsyncMock(return_value=True)
  479. mock.toggle = AsyncMock(return_value=True)
  480. mock.get_status = AsyncMock(return_value={"state": "ON", "reachable": True, "device_name": "Test Plug"})
  481. mock.get_energy = AsyncMock(
  482. return_value={
  483. "power": 150.5,
  484. "voltage": 120.0,
  485. "current": 1.25,
  486. "today": 2.5,
  487. "total": 100.0,
  488. "factor": 0.95,
  489. }
  490. )
  491. mock.test_connection = AsyncMock(return_value={"success": True, "state": "ON", "device_name": "Test Plug"})
  492. # Copy mocks to second patch target
  493. mock2.turn_on = mock.turn_on
  494. mock2.turn_off = mock.turn_off
  495. mock2.toggle = mock.toggle
  496. mock2.get_status = mock.get_status
  497. mock2.get_energy = mock.get_energy
  498. mock2.test_connection = mock.test_connection
  499. yield mock
  500. @pytest.fixture
  501. def mock_homeassistant_service():
  502. """Mock the Home Assistant service for smart plug tests."""
  503. # Patch both the module where it's defined and where it's imported
  504. with (
  505. patch("backend.app.services.homeassistant.homeassistant_service") as mock,
  506. patch("backend.app.api.routes.smart_plugs.homeassistant_service") as mock2,
  507. ):
  508. mock.turn_on = AsyncMock(return_value=True)
  509. mock.turn_off = AsyncMock(return_value=True)
  510. mock.toggle = AsyncMock(return_value=True)
  511. mock.get_status = AsyncMock(return_value={"state": "ON", "reachable": True, "device_name": "Test HA Entity"})
  512. mock.get_energy = AsyncMock(return_value=None) # Most HA entities don't have power monitoring
  513. mock.test_connection = AsyncMock(return_value={"success": True, "message": "API running", "error": None})
  514. mock.list_entities = AsyncMock(
  515. return_value=[
  516. {
  517. "entity_id": "switch.printer_plug",
  518. "friendly_name": "Printer Plug",
  519. "state": "on",
  520. "domain": "switch",
  521. },
  522. {"entity_id": "switch.test", "friendly_name": "Test Switch", "state": "off", "domain": "switch"},
  523. ]
  524. )
  525. mock.configure = MagicMock()
  526. # Copy mocks to second patch target
  527. mock2.turn_on = mock.turn_on
  528. mock2.turn_off = mock.turn_off
  529. mock2.toggle = mock.toggle
  530. mock2.get_status = mock.get_status
  531. mock2.get_energy = mock.get_energy
  532. mock2.test_connection = mock.test_connection
  533. mock2.list_entities = mock.list_entities
  534. mock2.configure = mock.configure
  535. yield mock
  536. @pytest.fixture
  537. def mock_mqtt_client():
  538. """Mock the MQTT client for printer communication tests."""
  539. with patch("backend.app.services.bambu_mqtt.BambuMQTTClient") as mock:
  540. instance = MagicMock()
  541. instance.state = MagicMock(connected=True, state="IDLE", progress=0, temperatures={"nozzle": 25, "bed": 25})
  542. instance.connect = MagicMock()
  543. instance.disconnect = MagicMock()
  544. mock.return_value = instance
  545. yield mock
  546. @pytest.fixture
  547. def mock_mqtt_smart_plug_service():
  548. """Mock the MQTT smart plug service for MQTT plug tests."""
  549. with patch("backend.app.api.routes.smart_plugs.mqtt_relay") as mock:
  550. # Create a mock smart_plug_service
  551. mock_service = MagicMock()
  552. mock_service.is_configured = MagicMock(return_value=True)
  553. mock_service.configure = AsyncMock(return_value=True)
  554. mock_service.subscribe = MagicMock()
  555. mock_service.unsubscribe = MagicMock()
  556. mock_service.get_plug_data = MagicMock(return_value=None)
  557. mock_service.is_reachable = MagicMock(return_value=False)
  558. mock.smart_plug_service = mock_service
  559. yield mock
  560. @pytest.fixture
  561. def mock_ftp_client():
  562. """Mock the FTP client for file transfer tests."""
  563. with (
  564. patch("backend.app.services.bambu_ftp.download_file_async") as download_mock,
  565. patch("backend.app.services.bambu_ftp.list_files_async") as list_mock,
  566. ):
  567. download_mock.return_value = True
  568. list_mock.return_value = []
  569. yield {"download": download_mock, "list": list_mock}
  570. @pytest.fixture
  571. def mock_httpx_client():
  572. """Mock httpx for webhook/notification HTTP calls."""
  573. with patch("httpx.AsyncClient") as mock_class:
  574. mock_instance = AsyncMock()
  575. mock_response = MagicMock()
  576. mock_response.status_code = 200
  577. mock_response.text = "OK"
  578. mock_response.json.return_value = {}
  579. mock_instance.get = AsyncMock(return_value=mock_response)
  580. mock_instance.post = AsyncMock(return_value=mock_response)
  581. # ntfy switches to PUT when a photo is attached (e.g. the sample image on Test).
  582. mock_instance.put = AsyncMock(return_value=mock_response)
  583. mock_instance.__aenter__ = AsyncMock(return_value=mock_instance)
  584. mock_instance.__aexit__ = AsyncMock()
  585. mock_class.return_value = mock_instance
  586. yield mock_instance
  587. @pytest.fixture
  588. def mock_printer_manager():
  589. """Mock the printer manager for status checks."""
  590. with patch("backend.app.services.printer_manager.printer_manager") as mock:
  591. mock.get_status = MagicMock(
  592. return_value=MagicMock(
  593. connected=True,
  594. state="IDLE",
  595. progress=0,
  596. temperatures={"nozzle": 25, "bed": 25, "chamber": 25},
  597. raw_data={},
  598. )
  599. )
  600. mock.mark_printer_offline = MagicMock()
  601. yield mock
  602. # ============================================================================
  603. # Factory Fixtures for Test Data
  604. # ============================================================================
  605. @pytest.fixture
  606. def smart_plug_factory(db_session):
  607. """Factory to create test smart plugs."""
  608. async def _create_plug(**kwargs):
  609. from backend.app.models.smart_plug import SmartPlug
  610. # Determine defaults based on plug_type
  611. plug_type = kwargs.get("plug_type", "tasmota")
  612. defaults = {
  613. "name": "Test Plug",
  614. "plug_type": plug_type,
  615. "enabled": True,
  616. "auto_on": True,
  617. "auto_off": True,
  618. "off_delay_mode": "time",
  619. "off_delay_minutes": 5,
  620. "off_temp_threshold": 70,
  621. "schedule_enabled": False,
  622. "power_alert_enabled": False,
  623. }
  624. # Set required fields based on plug_type
  625. if plug_type == "homeassistant":
  626. defaults["ha_entity_id"] = "switch.test"
  627. defaults["ip_address"] = None
  628. elif plug_type == "mqtt":
  629. # Legacy fields (for backward compatibility tests)
  630. defaults["mqtt_topic"] = kwargs.get("mqtt_topic", "test/topic")
  631. defaults["mqtt_multiplier"] = kwargs.get("mqtt_multiplier", 1.0)
  632. # New separate topic/path/multiplier fields
  633. defaults["mqtt_power_topic"] = kwargs.get("mqtt_power_topic")
  634. defaults["mqtt_power_path"] = kwargs.get("mqtt_power_path", "power")
  635. defaults["mqtt_power_multiplier"] = kwargs.get("mqtt_power_multiplier", 1.0)
  636. defaults["mqtt_energy_topic"] = kwargs.get("mqtt_energy_topic")
  637. defaults["mqtt_energy_path"] = kwargs.get("mqtt_energy_path")
  638. defaults["mqtt_energy_multiplier"] = kwargs.get("mqtt_energy_multiplier", 1.0)
  639. defaults["mqtt_state_topic"] = kwargs.get("mqtt_state_topic")
  640. defaults["mqtt_state_path"] = kwargs.get("mqtt_state_path")
  641. defaults["mqtt_state_on_value"] = kwargs.get("mqtt_state_on_value")
  642. defaults["ip_address"] = None
  643. defaults["ha_entity_id"] = None
  644. elif plug_type == "rest":
  645. defaults["rest_on_url"] = kwargs.get("rest_on_url", "http://192.168.1.100/api/plug/on")
  646. defaults["rest_off_url"] = kwargs.get("rest_off_url", "http://192.168.1.100/api/plug/off")
  647. defaults["rest_method"] = kwargs.get("rest_method", "POST")
  648. defaults["ip_address"] = None
  649. defaults["ha_entity_id"] = None
  650. else:
  651. defaults["ip_address"] = "192.168.1.100"
  652. defaults["ha_entity_id"] = None
  653. defaults.update(kwargs)
  654. plug = SmartPlug(**defaults)
  655. db_session.add(plug)
  656. await db_session.commit()
  657. await db_session.refresh(plug)
  658. return plug
  659. return _create_plug
  660. @pytest.fixture
  661. async def printer_1(db_session):
  662. """A printer with id 1, for tests whose rows refer to printer 1 directly.
  663. Without it those rows point at nothing, which only SQLite (foreign keys
  664. off) accepts.
  665. """
  666. from sqlalchemy import text
  667. from backend.app.models.printer import Printer
  668. printer = Printer(
  669. id=1, name="Printer 1", serial_number="00M09A900000001", ip_address="192.168.1.250", access_code="12345678"
  670. )
  671. db_session.add(printer)
  672. await db_session.flush()
  673. if TEST_PG_URL is not None:
  674. # An explicit id does not move the sequence on.
  675. await db_session.execute(text("SELECT setval(pg_get_serial_sequence('printers', 'id'), 1)"))
  676. await db_session.commit()
  677. return printer
  678. @pytest.fixture
  679. def printer_factory(db_session):
  680. """Factory to create test printers."""
  681. _counter = [0] # Use list to allow mutation in nested function
  682. async def _create_printer(**kwargs):
  683. from backend.app.models.printer import Printer
  684. _counter[0] += 1
  685. counter = _counter[0]
  686. defaults = {
  687. "name": "Test Printer",
  688. "serial_number": f"00M09A{counter:09d}", # Unique serial per printer
  689. "ip_address": f"192.168.1.{100 + counter}", # Unique IP per printer
  690. "access_code": "12345678",
  691. "is_active": True,
  692. "auto_archive": True,
  693. "model": "X1C",
  694. }
  695. defaults.update(kwargs)
  696. printer = Printer(**defaults)
  697. db_session.add(printer)
  698. await db_session.commit()
  699. await db_session.refresh(printer)
  700. return printer
  701. return _create_printer
  702. @pytest.fixture
  703. def location_factory(db_session):
  704. _counter = [0]
  705. async def _create_location(**kwargs):
  706. from backend.app.models.location import Location
  707. _counter[0] += 1
  708. counter = _counter[0]
  709. name = kwargs.pop("name", f"Test Location {counter}")
  710. defaults = {
  711. "name": name,
  712. "name_key": name.strip().lower(),
  713. }
  714. defaults.update(kwargs)
  715. location = Location(**defaults)
  716. db_session.add(location)
  717. await db_session.commit()
  718. await db_session.refresh(location)
  719. return location
  720. return _create_location
  721. @pytest.fixture
  722. def notification_provider_factory(db_session):
  723. """Factory to create test notification providers."""
  724. async def _create_provider(**kwargs):
  725. from backend.app.models.notification import NotificationProvider
  726. config = kwargs.pop("config", {"server": "https://ntfy.sh", "topic": "test-topic"})
  727. if isinstance(config, dict):
  728. config = json.dumps(config)
  729. defaults = {
  730. "name": "Test Provider",
  731. "provider_type": "ntfy",
  732. "enabled": True,
  733. "config": config,
  734. "on_print_start": True,
  735. "on_print_complete": True,
  736. "on_print_failed": True,
  737. "on_print_stopped": True,
  738. "on_print_progress": False,
  739. "on_print_missing_spool_assignment": False,
  740. "on_billing_charge_failed": True,
  741. "on_printer_offline": False,
  742. "on_printer_error": False,
  743. "on_filament_low": False,
  744. "on_maintenance_due": False,
  745. "on_ams_humidity_high": False,
  746. "on_ams_temperature_high": False,
  747. "on_bed_cooled": False,
  748. "quiet_hours_enabled": False,
  749. "daily_digest_enabled": False,
  750. }
  751. defaults.update(kwargs)
  752. provider = NotificationProvider(**defaults)
  753. db_session.add(provider)
  754. await db_session.commit()
  755. await db_session.refresh(provider)
  756. return provider
  757. return _create_provider
  758. @pytest.fixture
  759. def archive_factory(db_session):
  760. """Factory to create test archives.
  761. Also synthesizes one PrintLogEntry per archive (matching the production
  762. flow where statistics are aggregated from PrintLogEntry, not PrintArchive,
  763. per #1378). Pass ``with_run=False`` to skip — useful for testing the
  764. "archived but never printed" state. Pass ``run_status=...`` to override
  765. the run's status independently of the archive's status field.
  766. """
  767. async def _create_archive(printer_id: int, **kwargs):
  768. from backend.app.models.archive import PrintArchive
  769. from backend.app.models.print_log import PrintLogEntry
  770. with_run = kwargs.pop("with_run", True)
  771. run_status = kwargs.pop("run_status", None)
  772. defaults = {
  773. "printer_id": printer_id,
  774. "filename": "test_print.gcode.3mf",
  775. "print_name": "Test Print",
  776. "file_path": "archives/test/test_print.gcode.3mf",
  777. "file_size": 1024000,
  778. "status": "completed",
  779. "filament_type": "PLA",
  780. "filament_used_grams": 50.0,
  781. "print_time_seconds": 3600,
  782. }
  783. defaults.update(kwargs)
  784. archive = PrintArchive(**defaults)
  785. db_session.add(archive)
  786. await db_session.commit()
  787. await db_session.refresh(archive)
  788. if with_run:
  789. duration = None
  790. if archive.started_at and archive.completed_at:
  791. duration = int((archive.completed_at - archive.started_at).total_seconds()) or None
  792. run = PrintLogEntry(
  793. archive_id=archive.id,
  794. printer_id=archive.printer_id,
  795. status=run_status or archive.status,
  796. started_at=archive.started_at,
  797. completed_at=archive.completed_at,
  798. duration_seconds=duration,
  799. filament_type=archive.filament_type,
  800. filament_color=archive.filament_color,
  801. filament_used_grams=archive.filament_used_grams,
  802. cost=archive.cost,
  803. energy_kwh=archive.energy_kwh,
  804. energy_cost=archive.energy_cost,
  805. failure_reason=archive.failure_reason,
  806. print_name=archive.print_name,
  807. created_by_id=archive.created_by_id,
  808. # Sync the event's created_at with the archive's so date-range
  809. # filtered tests that backdate an archive still find its event.
  810. created_at=archive.created_at,
  811. )
  812. db_session.add(run)
  813. await db_session.commit()
  814. return archive
  815. return _create_archive
  816. # ============================================================================
  817. # Sample Data Fixtures
  818. # ============================================================================
  819. @pytest.fixture
  820. def sample_mqtt_print_start():
  821. """Sample MQTT message for print start."""
  822. return {
  823. "print": {
  824. "command": "project_file",
  825. "param": "/sdcard/test.gcode.3mf",
  826. "subtask_name": "test_print",
  827. "gcode_state": "RUNNING",
  828. "mc_percent": 0,
  829. }
  830. }
  831. @pytest.fixture
  832. def sample_mqtt_print_complete():
  833. """Sample MQTT message for print complete."""
  834. return {
  835. "print": {
  836. "gcode_state": "FINISH",
  837. "mc_percent": 100,
  838. "subtask_name": "test_print",
  839. }
  840. }
  841. @pytest.fixture
  842. def sample_printer_status():
  843. """Sample printer status data."""
  844. return {
  845. "connected": True,
  846. "state": "IDLE",
  847. "progress": 0,
  848. "layer_num": 0,
  849. "total_layers": 0,
  850. "temperatures": {
  851. "nozzle": 25.0,
  852. "bed": 25.0,
  853. "chamber": 25.0,
  854. },
  855. "remaining_time": 0,
  856. "filename": None,
  857. }
  858. # ============================================================================
  859. # Log Capture Fixtures for Error Detection
  860. # ============================================================================
  861. class LogCapture(logging.Handler):
  862. """Handler that captures log records for testing."""
  863. def __init__(self):
  864. super().__init__()
  865. self.records: list[logging.LogRecord] = []
  866. def emit(self, record: logging.LogRecord):
  867. self.records.append(record)
  868. def clear(self):
  869. self.records.clear()
  870. def get_errors(self) -> list[logging.LogRecord]:
  871. """Get all ERROR and CRITICAL level records."""
  872. return [r for r in self.records if r.levelno >= logging.ERROR]
  873. def get_warnings(self) -> list[logging.LogRecord]:
  874. """Get all WARNING level records."""
  875. return [r for r in self.records if r.levelno == logging.WARNING]
  876. def has_errors(self) -> bool:
  877. """Check if any errors were logged."""
  878. return len(self.get_errors()) > 0
  879. def format_errors(self) -> str:
  880. """Format all errors as a string for assertion messages."""
  881. errors = self.get_errors()
  882. if not errors:
  883. return "No errors"
  884. formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
  885. return "\n".join(formatter.format(r) for r in errors)
  886. @pytest.fixture
  887. def capture_logs():
  888. """Fixture that captures log output during a test.
  889. Usage:
  890. def test_something(capture_logs):
  891. # Do something that might log errors
  892. some_function()
  893. # Check no errors were logged
  894. assert not capture_logs.has_errors(), capture_logs.format_errors()
  895. """
  896. handler = LogCapture()
  897. handler.setLevel(logging.DEBUG)
  898. # Attach to root logger to capture all logs
  899. root_logger = logging.getLogger()
  900. root_logger.addHandler(handler)
  901. yield handler
  902. root_logger.removeHandler(handler)
  903. @pytest.fixture
  904. def assert_no_log_errors(capture_logs):
  905. """Fixture that automatically asserts no errors were logged.
  906. Usage:
  907. def test_something(assert_no_log_errors):
  908. # If any ERROR logs occur during this test, it will fail
  909. some_function()
  910. """
  911. yield capture_logs
  912. errors = capture_logs.get_errors()
  913. if errors:
  914. pytest.fail(f"Unexpected log errors:\n{capture_logs.format_errors()}")