conftest.py 41 KB

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