conftest.py 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. @pytest.fixture
  320. async def test_engine():
  321. """Create a test database engine."""
  322. engine = create_async_engine(TEST_DATABASE_URL, echo=False)
  323. # Import all models to register them
  324. from backend.app.models import (
  325. active_print_session, # noqa: F401
  326. ams_history,
  327. ams_label,
  328. api_key,
  329. archive,
  330. auth_ephemeral,
  331. color_catalog,
  332. external_link,
  333. filament,
  334. group,
  335. kprofile_note,
  336. maintenance,
  337. notification,
  338. notification_template,
  339. oidc_provider,
  340. print_log,
  341. print_queue,
  342. printer,
  343. project,
  344. project_bom,
  345. scheduled_drying,
  346. settings,
  347. slot_preset,
  348. smart_plug,
  349. smart_plug_energy_snapshot, # noqa: F401
  350. sponsor_toast_state, # noqa: F401
  351. spool,
  352. spool_assignment,
  353. spool_catalog,
  354. spool_filament_preset,
  355. spool_k_profile,
  356. spool_usage_history,
  357. spoolbuddy_device,
  358. spoolman_k_profile,
  359. spoolman_slot_assignment,
  360. user,
  361. user_email_pref,
  362. user_otp_code,
  363. user_totp,
  364. virtual_printer,
  365. )
  366. if TEST_PG_URL is not None:
  367. # The same engine setup as the app's (UTC session, aware datetimes
  368. # made naive), on this worker's own database, emptied for each test.
  369. from backend.app.core.database import _create_engine
  370. await engine.dispose()
  371. engine = _create_engine(TEST_PG_URL)
  372. async with engine.begin() as conn:
  373. await _reset_pg_test_schema(conn)
  374. yield engine
  375. await engine.dispose()
  376. return
  377. async with engine.begin() as conn:
  378. await conn.run_sync(Base.metadata.create_all)
  379. yield engine
  380. async with engine.begin() as conn:
  381. await conn.run_sync(Base.metadata.drop_all)
  382. await engine.dispose()
  383. # Allow aiosqlite's background thread to finish processing the close
  384. # response before the per-function event loop shuts down, preventing
  385. # "RuntimeError: Event loop is closed" in call_soon_threadsafe.
  386. await asyncio.sleep(0.1)
  387. @pytest.fixture
  388. async def db_session(test_engine) -> AsyncGenerator[AsyncSession, None]:
  389. """Create a test database session."""
  390. async_session_maker = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
  391. async with async_session_maker() as session:
  392. yield session
  393. @pytest.fixture
  394. async def async_client(test_engine, db_session) -> AsyncGenerator[AsyncClient, None]:
  395. """Create an async test client."""
  396. from backend.app.core.database import async_session, get_db
  397. from backend.app.main import app
  398. # Create a new session maker for the test engine
  399. test_async_session = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
  400. async def override_get_db():
  401. # Mirror production get_db (core/database.py): commit on success,
  402. # rollback on error. Endpoints that rely on the request-scoped
  403. # implicit commit (e.g. create_project, which only flushes) would
  404. # otherwise silently lose their writes in tests (#1897).
  405. async with test_async_session() as session:
  406. try:
  407. yield session
  408. await session.commit()
  409. except BaseException:
  410. await session.rollback()
  411. raise
  412. app.dependency_overrides[get_db] = override_get_db
  413. # Mock init_printer_connections to prevent MQTT connection attempts during tests
  414. async def mock_init_printer_connections(db):
  415. pass # No-op - don't connect to real printers
  416. # Also patch the module-level async_session used by services, auth, and middleware
  417. with (
  418. patch("backend.app.core.database.async_session", test_async_session),
  419. patch("backend.app.core.auth.async_session", test_async_session),
  420. patch("backend.app.main.async_session", test_async_session),
  421. # Obico endpoints load settings through the service's module-level binding;
  422. # without this patch they'd read whatever DB the cwd resolves to (#1546).
  423. patch("backend.app.services.obico_detection.async_session", test_async_session),
  424. patch("backend.app.main.init_printer_connections", mock_init_printer_connections),
  425. ):
  426. # Seed default groups for tests that need them
  427. from backend.app.core.database import seed_default_groups
  428. await seed_default_groups()
  429. async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
  430. yield client
  431. # The app lifespan called init_db() which used the module-level engine
  432. # (not the test engine), creating aiosqlite connections. Dispose those
  433. # connections so their background threads finish before the event loop closes.
  434. from backend.app.core.database import engine as real_engine
  435. await real_engine.dispose()
  436. app.dependency_overrides.clear()
  437. # ============================================================================
  438. # Mock External Services
  439. # ============================================================================
  440. @pytest.fixture
  441. def mock_tasmota_service():
  442. """Mock the Tasmota service for smart plug tests."""
  443. # Patch both the module where it's defined and where it's imported
  444. with (
  445. patch("backend.app.services.tasmota.tasmota_service") as mock,
  446. patch("backend.app.api.routes.smart_plugs.tasmota_service") as mock2,
  447. ):
  448. mock.turn_on = AsyncMock(return_value=True)
  449. mock.turn_off = AsyncMock(return_value=True)
  450. mock.toggle = AsyncMock(return_value=True)
  451. mock.get_status = AsyncMock(return_value={"state": "ON", "reachable": True, "device_name": "Test Plug"})
  452. mock.get_energy = AsyncMock(
  453. return_value={
  454. "power": 150.5,
  455. "voltage": 120.0,
  456. "current": 1.25,
  457. "today": 2.5,
  458. "total": 100.0,
  459. "factor": 0.95,
  460. }
  461. )
  462. mock.test_connection = AsyncMock(return_value={"success": True, "state": "ON", "device_name": "Test Plug"})
  463. # Copy mocks to second patch target
  464. mock2.turn_on = mock.turn_on
  465. mock2.turn_off = mock.turn_off
  466. mock2.toggle = mock.toggle
  467. mock2.get_status = mock.get_status
  468. mock2.get_energy = mock.get_energy
  469. mock2.test_connection = mock.test_connection
  470. yield mock
  471. @pytest.fixture
  472. def mock_homeassistant_service():
  473. """Mock the Home Assistant service for smart plug tests."""
  474. # Patch both the module where it's defined and where it's imported
  475. with (
  476. patch("backend.app.services.homeassistant.homeassistant_service") as mock,
  477. patch("backend.app.api.routes.smart_plugs.homeassistant_service") as mock2,
  478. ):
  479. mock.turn_on = AsyncMock(return_value=True)
  480. mock.turn_off = AsyncMock(return_value=True)
  481. mock.toggle = AsyncMock(return_value=True)
  482. mock.get_status = AsyncMock(return_value={"state": "ON", "reachable": True, "device_name": "Test HA Entity"})
  483. mock.get_energy = AsyncMock(return_value=None) # Most HA entities don't have power monitoring
  484. mock.test_connection = AsyncMock(return_value={"success": True, "message": "API running", "error": None})
  485. mock.list_entities = AsyncMock(
  486. return_value=[
  487. {
  488. "entity_id": "switch.printer_plug",
  489. "friendly_name": "Printer Plug",
  490. "state": "on",
  491. "domain": "switch",
  492. },
  493. {"entity_id": "switch.test", "friendly_name": "Test Switch", "state": "off", "domain": "switch"},
  494. ]
  495. )
  496. mock.configure = MagicMock()
  497. # Copy mocks to second patch target
  498. mock2.turn_on = mock.turn_on
  499. mock2.turn_off = mock.turn_off
  500. mock2.toggle = mock.toggle
  501. mock2.get_status = mock.get_status
  502. mock2.get_energy = mock.get_energy
  503. mock2.test_connection = mock.test_connection
  504. mock2.list_entities = mock.list_entities
  505. mock2.configure = mock.configure
  506. yield mock
  507. @pytest.fixture
  508. def mock_mqtt_client():
  509. """Mock the MQTT client for printer communication tests."""
  510. with patch("backend.app.services.bambu_mqtt.BambuMQTTClient") as mock:
  511. instance = MagicMock()
  512. instance.state = MagicMock(connected=True, state="IDLE", progress=0, temperatures={"nozzle": 25, "bed": 25})
  513. instance.connect = MagicMock()
  514. instance.disconnect = MagicMock()
  515. mock.return_value = instance
  516. yield mock
  517. @pytest.fixture
  518. def mock_mqtt_smart_plug_service():
  519. """Mock the MQTT smart plug service for MQTT plug tests."""
  520. with patch("backend.app.api.routes.smart_plugs.mqtt_relay") as mock:
  521. # Create a mock smart_plug_service
  522. mock_service = MagicMock()
  523. mock_service.is_configured = MagicMock(return_value=True)
  524. mock_service.has_broker_settings = MagicMock(return_value=True)
  525. mock_service.configure = AsyncMock(return_value=True)
  526. mock_service.subscribe = MagicMock()
  527. mock_service.unsubscribe = MagicMock()
  528. mock_service.get_plug_data = MagicMock(return_value=None)
  529. mock_service.is_reachable = MagicMock(return_value=False)
  530. mock.smart_plug_service = mock_service
  531. yield mock
  532. @pytest.fixture
  533. def mock_ftp_client():
  534. """Mock the FTP client for file transfer tests."""
  535. with (
  536. patch("backend.app.services.bambu_ftp.download_file_async") as download_mock,
  537. patch("backend.app.services.bambu_ftp.list_files_async") as list_mock,
  538. ):
  539. download_mock.return_value = True
  540. list_mock.return_value = []
  541. yield {"download": download_mock, "list": list_mock}
  542. @pytest.fixture
  543. def mock_httpx_client():
  544. """Mock httpx for webhook/notification HTTP calls."""
  545. with patch("httpx.AsyncClient") as mock_class:
  546. mock_instance = AsyncMock()
  547. mock_response = MagicMock()
  548. mock_response.status_code = 200
  549. mock_response.text = "OK"
  550. mock_response.json.return_value = {}
  551. mock_instance.get = AsyncMock(return_value=mock_response)
  552. mock_instance.post = AsyncMock(return_value=mock_response)
  553. # ntfy switches to PUT when a photo is attached (e.g. the sample image on Test).
  554. mock_instance.put = AsyncMock(return_value=mock_response)
  555. mock_instance.__aenter__ = AsyncMock(return_value=mock_instance)
  556. mock_instance.__aexit__ = AsyncMock()
  557. mock_class.return_value = mock_instance
  558. yield mock_instance
  559. @pytest.fixture
  560. def mock_printer_manager():
  561. """Mock the printer manager for status checks."""
  562. with patch("backend.app.services.printer_manager.printer_manager") as mock:
  563. mock.get_status = MagicMock(
  564. return_value=MagicMock(
  565. connected=True,
  566. state="IDLE",
  567. progress=0,
  568. temperatures={"nozzle": 25, "bed": 25, "chamber": 25},
  569. raw_data={},
  570. )
  571. )
  572. mock.mark_printer_offline = MagicMock()
  573. yield mock
  574. # ============================================================================
  575. # Factory Fixtures for Test Data
  576. # ============================================================================
  577. @pytest.fixture
  578. def smart_plug_factory(db_session):
  579. """Factory to create test smart plugs."""
  580. async def _create_plug(**kwargs):
  581. from backend.app.models.smart_plug import SmartPlug
  582. # Determine defaults based on plug_type
  583. plug_type = kwargs.get("plug_type", "tasmota")
  584. defaults = {
  585. "name": "Test Plug",
  586. "plug_type": plug_type,
  587. "enabled": True,
  588. "auto_on": True,
  589. "auto_off": True,
  590. "off_delay_mode": "time",
  591. "off_delay_minutes": 5,
  592. "off_temp_threshold": 70,
  593. "schedule_enabled": False,
  594. "power_alert_enabled": False,
  595. }
  596. # Set required fields based on plug_type
  597. if plug_type == "homeassistant":
  598. defaults["ha_entity_id"] = "switch.test"
  599. defaults["ip_address"] = None
  600. elif plug_type == "mqtt":
  601. # Legacy fields (for backward compatibility tests)
  602. defaults["mqtt_topic"] = kwargs.get("mqtt_topic", "test/topic")
  603. defaults["mqtt_multiplier"] = kwargs.get("mqtt_multiplier", 1.0)
  604. # New separate topic/path/multiplier fields
  605. defaults["mqtt_power_topic"] = kwargs.get("mqtt_power_topic")
  606. defaults["mqtt_power_path"] = kwargs.get("mqtt_power_path", "power")
  607. defaults["mqtt_power_multiplier"] = kwargs.get("mqtt_power_multiplier", 1.0)
  608. defaults["mqtt_energy_topic"] = kwargs.get("mqtt_energy_topic")
  609. defaults["mqtt_energy_path"] = kwargs.get("mqtt_energy_path")
  610. defaults["mqtt_energy_multiplier"] = kwargs.get("mqtt_energy_multiplier", 1.0)
  611. defaults["mqtt_state_topic"] = kwargs.get("mqtt_state_topic")
  612. defaults["mqtt_state_path"] = kwargs.get("mqtt_state_path")
  613. defaults["mqtt_state_on_value"] = kwargs.get("mqtt_state_on_value")
  614. defaults["ip_address"] = None
  615. defaults["ha_entity_id"] = None
  616. elif plug_type == "rest":
  617. defaults["rest_on_url"] = kwargs.get("rest_on_url", "http://192.168.1.100/api/plug/on")
  618. defaults["rest_off_url"] = kwargs.get("rest_off_url", "http://192.168.1.100/api/plug/off")
  619. defaults["rest_method"] = kwargs.get("rest_method", "POST")
  620. defaults["ip_address"] = None
  621. defaults["ha_entity_id"] = None
  622. else:
  623. defaults["ip_address"] = "192.168.1.100"
  624. defaults["ha_entity_id"] = None
  625. defaults.update(kwargs)
  626. plug = SmartPlug(**defaults)
  627. db_session.add(plug)
  628. await db_session.commit()
  629. await db_session.refresh(plug)
  630. return plug
  631. return _create_plug
  632. @pytest.fixture
  633. async def printer_1(db_session):
  634. """A printer with id 1, for tests whose rows refer to printer 1 directly.
  635. Without it those rows point at nothing, which only SQLite (foreign keys
  636. off) accepts.
  637. """
  638. from sqlalchemy import text
  639. from backend.app.models.printer import Printer
  640. printer = Printer(
  641. id=1, name="Printer 1", serial_number="00M09A900000001", ip_address="192.168.1.250", access_code="12345678"
  642. )
  643. db_session.add(printer)
  644. await db_session.flush()
  645. if TEST_PG_URL is not None:
  646. # An explicit id does not move the sequence on.
  647. await db_session.execute(text("SELECT setval(pg_get_serial_sequence('printers', 'id'), 1)"))
  648. await db_session.commit()
  649. return printer
  650. @pytest.fixture
  651. def printer_factory(db_session):
  652. """Factory to create test printers."""
  653. _counter = [0] # Use list to allow mutation in nested function
  654. async def _create_printer(**kwargs):
  655. from backend.app.models.printer import Printer
  656. _counter[0] += 1
  657. counter = _counter[0]
  658. defaults = {
  659. "name": "Test Printer",
  660. "serial_number": f"00M09A{counter:09d}", # Unique serial per printer
  661. "ip_address": f"192.168.1.{100 + counter}", # Unique IP per printer
  662. "access_code": "12345678",
  663. "is_active": True,
  664. "auto_archive": True,
  665. "model": "X1C",
  666. }
  667. defaults.update(kwargs)
  668. printer = Printer(**defaults)
  669. db_session.add(printer)
  670. await db_session.commit()
  671. await db_session.refresh(printer)
  672. return printer
  673. return _create_printer
  674. @pytest.fixture
  675. def location_factory(db_session):
  676. _counter = [0]
  677. async def _create_location(**kwargs):
  678. from backend.app.models.location import Location
  679. _counter[0] += 1
  680. counter = _counter[0]
  681. name = kwargs.pop("name", f"Test Location {counter}")
  682. defaults = {
  683. "name": name,
  684. "name_key": name.strip().lower(),
  685. }
  686. defaults.update(kwargs)
  687. location = Location(**defaults)
  688. db_session.add(location)
  689. await db_session.commit()
  690. await db_session.refresh(location)
  691. return location
  692. return _create_location
  693. @pytest.fixture
  694. def notification_provider_factory(db_session):
  695. """Factory to create test notification providers."""
  696. async def _create_provider(**kwargs):
  697. from backend.app.models.notification import NotificationProvider
  698. config = kwargs.pop("config", {"server": "https://ntfy.sh", "topic": "test-topic"})
  699. if isinstance(config, dict):
  700. config = json.dumps(config)
  701. defaults = {
  702. "name": "Test Provider",
  703. "provider_type": "ntfy",
  704. "enabled": True,
  705. "config": config,
  706. "on_print_start": True,
  707. "on_print_complete": True,
  708. "on_print_failed": True,
  709. "on_print_stopped": True,
  710. "on_print_progress": False,
  711. "on_print_missing_spool_assignment": False,
  712. "on_billing_charge_failed": True,
  713. "on_printer_offline": False,
  714. "on_printer_error": False,
  715. "on_filament_low": False,
  716. "on_maintenance_due": False,
  717. "on_ams_humidity_high": False,
  718. "on_ams_temperature_high": False,
  719. "on_bed_cooled": False,
  720. "quiet_hours_enabled": False,
  721. "daily_digest_enabled": False,
  722. }
  723. defaults.update(kwargs)
  724. provider = NotificationProvider(**defaults)
  725. db_session.add(provider)
  726. await db_session.commit()
  727. await db_session.refresh(provider)
  728. return provider
  729. return _create_provider
  730. @pytest.fixture
  731. def archive_factory(db_session):
  732. """Factory to create test archives.
  733. Also synthesizes one PrintLogEntry per archive (matching the production
  734. flow where statistics are aggregated from PrintLogEntry, not PrintArchive,
  735. per #1378). Pass ``with_run=False`` to skip — useful for testing the
  736. "archived but never printed" state. Pass ``run_status=...`` to override
  737. the run's status independently of the archive's status field.
  738. """
  739. async def _create_archive(printer_id: int, **kwargs):
  740. from backend.app.models.archive import PrintArchive
  741. from backend.app.models.print_log import PrintLogEntry
  742. with_run = kwargs.pop("with_run", True)
  743. run_status = kwargs.pop("run_status", None)
  744. defaults = {
  745. "printer_id": printer_id,
  746. "filename": "test_print.gcode.3mf",
  747. "print_name": "Test Print",
  748. "file_path": "archives/test/test_print.gcode.3mf",
  749. "file_size": 1024000,
  750. "status": "completed",
  751. "filament_type": "PLA",
  752. "filament_used_grams": 50.0,
  753. "print_time_seconds": 3600,
  754. }
  755. defaults.update(kwargs)
  756. archive = PrintArchive(**defaults)
  757. db_session.add(archive)
  758. await db_session.commit()
  759. await db_session.refresh(archive)
  760. if with_run:
  761. duration = None
  762. if archive.started_at and archive.completed_at:
  763. duration = int((archive.completed_at - archive.started_at).total_seconds()) or None
  764. run = PrintLogEntry(
  765. archive_id=archive.id,
  766. printer_id=archive.printer_id,
  767. status=run_status or archive.status,
  768. started_at=archive.started_at,
  769. completed_at=archive.completed_at,
  770. duration_seconds=duration,
  771. filament_type=archive.filament_type,
  772. filament_color=archive.filament_color,
  773. filament_used_grams=archive.filament_used_grams,
  774. cost=archive.cost,
  775. energy_kwh=archive.energy_kwh,
  776. energy_cost=archive.energy_cost,
  777. failure_reason=archive.failure_reason,
  778. print_name=archive.print_name,
  779. created_by_id=archive.created_by_id,
  780. # Sync the event's created_at with the archive's so date-range
  781. # filtered tests that backdate an archive still find its event.
  782. created_at=archive.created_at,
  783. )
  784. db_session.add(run)
  785. await db_session.commit()
  786. return archive
  787. return _create_archive
  788. # ============================================================================
  789. # Sample Data Fixtures
  790. # ============================================================================
  791. @pytest.fixture
  792. def sample_mqtt_print_start():
  793. """Sample MQTT message for print start."""
  794. return {
  795. "print": {
  796. "command": "project_file",
  797. "param": "/sdcard/test.gcode.3mf",
  798. "subtask_name": "test_print",
  799. "gcode_state": "RUNNING",
  800. "mc_percent": 0,
  801. }
  802. }
  803. @pytest.fixture
  804. def sample_mqtt_print_complete():
  805. """Sample MQTT message for print complete."""
  806. return {
  807. "print": {
  808. "gcode_state": "FINISH",
  809. "mc_percent": 100,
  810. "subtask_name": "test_print",
  811. }
  812. }
  813. @pytest.fixture
  814. def sample_printer_status():
  815. """Sample printer status data."""
  816. return {
  817. "connected": True,
  818. "state": "IDLE",
  819. "progress": 0,
  820. "layer_num": 0,
  821. "total_layers": 0,
  822. "temperatures": {
  823. "nozzle": 25.0,
  824. "bed": 25.0,
  825. "chamber": 25.0,
  826. },
  827. "remaining_time": 0,
  828. "filename": None,
  829. }
  830. # ============================================================================
  831. # Log Capture Fixtures for Error Detection
  832. # ============================================================================
  833. class LogCapture(logging.Handler):
  834. """Handler that captures log records for testing."""
  835. def __init__(self):
  836. super().__init__()
  837. self.records: list[logging.LogRecord] = []
  838. def emit(self, record: logging.LogRecord):
  839. self.records.append(record)
  840. def clear(self):
  841. self.records.clear()
  842. def get_errors(self) -> list[logging.LogRecord]:
  843. """Get all ERROR and CRITICAL level records."""
  844. return [r for r in self.records if r.levelno >= logging.ERROR]
  845. def get_warnings(self) -> list[logging.LogRecord]:
  846. """Get all WARNING level records."""
  847. return [r for r in self.records if r.levelno == logging.WARNING]
  848. def has_errors(self) -> bool:
  849. """Check if any errors were logged."""
  850. return len(self.get_errors()) > 0
  851. def format_errors(self) -> str:
  852. """Format all errors as a string for assertion messages."""
  853. errors = self.get_errors()
  854. if not errors:
  855. return "No errors"
  856. formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
  857. return "\n".join(formatter.format(r) for r in errors)
  858. @pytest.fixture
  859. def capture_logs():
  860. """Fixture that captures log output during a test.
  861. Usage:
  862. def test_something(capture_logs):
  863. # Do something that might log errors
  864. some_function()
  865. # Check no errors were logged
  866. assert not capture_logs.has_errors(), capture_logs.format_errors()
  867. """
  868. handler = LogCapture()
  869. handler.setLevel(logging.DEBUG)
  870. # Attach to root logger to capture all logs
  871. root_logger = logging.getLogger()
  872. root_logger.addHandler(handler)
  873. yield handler
  874. root_logger.removeHandler(handler)
  875. @pytest.fixture
  876. def assert_no_log_errors(capture_logs):
  877. """Fixture that automatically asserts no errors were logged.
  878. Usage:
  879. def test_something(assert_no_log_errors):
  880. # If any ERROR logs occur during this test, it will fail
  881. some_function()
  882. """
  883. yield capture_logs
  884. errors = capture_logs.get_errors()
  885. if errors:
  886. pytest.fail(f"Unexpected log errors:\n{capture_logs.format_errors()}")