test_makerworld_routes.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. """Tests for the /makerworld/* route handlers.
  2. Mocks ``MakerWorldService`` so tests don't hit the real MakerWorld API. We
  3. still cover: URL validation, metadata passthrough, already-imported detection,
  4. source-URL-based dedupe on import, auto-creation of the MakerWorld default
  5. folder, canonical URL shape, filename basenaming, and the ``/recent-imports``
  6. listing endpoint.
  7. """
  8. from __future__ import annotations
  9. from datetime import datetime, timedelta
  10. from unittest.mock import AsyncMock, patch
  11. import pytest
  12. from backend.app.api.routes.makerworld import _canonical_url
  13. from backend.app.models.library import LibraryFile, LibraryFolder
  14. def _fake_service(**stubs):
  15. """Build an AsyncMock MakerWorldService with the given async method stubs."""
  16. svc = AsyncMock()
  17. svc.close = AsyncMock()
  18. for name, value in stubs.items():
  19. if callable(value) and not isinstance(value, AsyncMock):
  20. setattr(svc, name, AsyncMock(side_effect=value))
  21. else:
  22. setattr(svc, name, AsyncMock(return_value=value))
  23. return svc
  24. def _default_design(alphanumeric: str = "US2bb73b106683e5", model_id: int = 1400373):
  25. """Shape the backend needs from ``/design/{id}``: the alphanumeric
  26. ``modelId`` field that iot-service requires, plus at least one instance
  27. so the importer has a ``profile_id`` to fall back on."""
  28. return {
  29. "id": model_id,
  30. "modelId": alphanumeric,
  31. "title": "Seed Starter",
  32. "instances": [{"profileId": 298919107, "title": "9 cells"}],
  33. }
  34. def _default_manifest(name: str = "benchy.3mf"):
  35. return {
  36. "name": name,
  37. "url": "https://makerworld.bblmw.com/makerworld/model/X/Y/f.3mf?exp=1&key=k",
  38. }
  39. class TestCanonicalUrl:
  40. """Unit test the dedupe-key builder directly — regressions break dedupe
  41. silently so it's worth pinning the exact shape."""
  42. def test_without_profile_id(self):
  43. assert _canonical_url(1400373) == "https://makerworld.com/models/1400373"
  44. def test_without_profile_id_when_none(self):
  45. assert _canonical_url(1400373, None) == "https://makerworld.com/models/1400373"
  46. def test_with_profile_id(self):
  47. assert _canonical_url(1400373, 298919107) == ("https://makerworld.com/models/1400373#profileId-298919107")
  48. class TestStatus:
  49. @pytest.mark.asyncio
  50. async def test_status_reports_no_token_by_default(self, async_client, db_session):
  51. resp = await async_client.get("/api/v1/makerworld/status")
  52. assert resp.status_code == 200
  53. body = resp.json()
  54. # Fresh in-memory DB has no stored token, so can_download must be false
  55. assert body == {"has_cloud_token": False, "can_download": False}
  56. class TestResolve:
  57. @pytest.mark.asyncio
  58. async def test_rejects_non_makerworld_url(self, async_client):
  59. resp = await async_client.post(
  60. "/api/v1/makerworld/resolve",
  61. json={"url": "https://thingiverse.com/thing/1"},
  62. )
  63. assert resp.status_code == 400
  64. assert "makerworld" in resp.json()["detail"].lower()
  65. @pytest.mark.asyncio
  66. async def test_happy_path_returns_design_and_instances(self, async_client):
  67. design_payload = {"id": 1400373, "title": "Seed Starter"}
  68. instances_payload = {
  69. "total": 2,
  70. "hits": [
  71. {"id": 1452154, "profileId": 298919107, "title": "9 cells"},
  72. {"id": 1452158, "profileId": 298919564, "title": "12 cells"},
  73. ],
  74. }
  75. svc = _fake_service(get_design=design_payload, get_design_instances=instances_payload)
  76. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  77. resp = await async_client.post(
  78. "/api/v1/makerworld/resolve",
  79. json={"url": "https://makerworld.com/en/models/1400373-slug#profileId-1452154"},
  80. )
  81. assert resp.status_code == 200, resp.text
  82. body = resp.json()
  83. assert body["model_id"] == 1400373
  84. assert body["profile_id"] == 1452154
  85. assert body["design"] == design_payload
  86. assert len(body["instances"]) == 2
  87. assert body["already_imported_library_ids"] == []
  88. @pytest.mark.asyncio
  89. async def test_flags_already_imported_library_ids(self, async_client, db_session):
  90. # Seed a matching LibraryFile so resolve() reports it back
  91. existing = LibraryFile(
  92. filename="prev.3mf",
  93. file_path="library/files/prev.3mf",
  94. file_type="3mf",
  95. file_size=100,
  96. source_type="makerworld",
  97. source_url="https://makerworld.com/models/1400373",
  98. )
  99. db_session.add(existing)
  100. await db_session.commit()
  101. await db_session.refresh(existing)
  102. svc = _fake_service(
  103. get_design={"id": 1400373},
  104. get_design_instances={"total": 0, "hits": []},
  105. )
  106. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  107. resp = await async_client.post(
  108. "/api/v1/makerworld/resolve",
  109. json={"url": "https://makerworld.com/en/models/1400373"},
  110. )
  111. assert resp.status_code == 200, resp.text
  112. assert resp.json()["already_imported_library_ids"] == [existing.id]
  113. @pytest.mark.asyncio
  114. async def test_merges_compatibility_from_design_into_instances(self, async_client):
  115. """Per-instance printer compatibility info lives on
  116. ``design.instances[].extention.modelInfo`` but not on
  117. ``/instances/hits``. Resolve enriches each hit with both
  118. ``compatibility`` (primary printer the instance was sliced for) and
  119. ``otherCompatibility`` (extra printers the uploader marked it
  120. compatible with) so the frontend can show "sliced for A1 / also
  121. marked compatible with: H2D, P1S".
  122. """
  123. design_payload = {
  124. "id": 1400373,
  125. "title": "Seed Starter",
  126. "instances": [
  127. {
  128. "id": 1452154,
  129. "extention": {
  130. "modelInfo": {
  131. "compatibility": ["A1"],
  132. "otherCompatibility": ["H2D", "P1S"],
  133. }
  134. },
  135. },
  136. {
  137. "id": 1452158,
  138. "extention": {
  139. "modelInfo": {
  140. "compatibility": ["X1 Carbon"],
  141. "otherCompatibility": [],
  142. }
  143. },
  144. },
  145. ],
  146. }
  147. instances_payload = {
  148. "total": 2,
  149. "hits": [
  150. {"id": 1452154, "profileId": 298919107, "title": "9 cells"},
  151. {"id": 1452158, "profileId": 298919564, "title": "12 cells"},
  152. ],
  153. }
  154. svc = _fake_service(get_design=design_payload, get_design_instances=instances_payload)
  155. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  156. resp = await async_client.post(
  157. "/api/v1/makerworld/resolve",
  158. json={"url": "https://makerworld.com/en/models/1400373"},
  159. )
  160. assert resp.status_code == 200, resp.text
  161. instances = resp.json()["instances"]
  162. by_id = {i["id"]: i for i in instances}
  163. assert by_id[1452154]["compatibility"] == ["A1"]
  164. assert by_id[1452154]["otherCompatibility"] == ["H2D", "P1S"]
  165. assert by_id[1452158]["compatibility"] == ["X1 Carbon"]
  166. assert by_id[1452158]["otherCompatibility"] == []
  167. @pytest.mark.asyncio
  168. async def test_resolve_handles_missing_compatibility_gracefully(self, async_client):
  169. """Older designs (or hits without a matching design.instances entry)
  170. must not crash the resolve response — they just don't get the
  171. compat fields."""
  172. design_payload = {"id": 1400373, "instances": [{"id": 1452154}]} # no extention
  173. instances_payload = {
  174. "total": 2,
  175. "hits": [
  176. {"id": 1452154, "profileId": 298919107},
  177. {"id": 9999999, "profileId": 298919999}, # no design.instances match
  178. ],
  179. }
  180. svc = _fake_service(get_design=design_payload, get_design_instances=instances_payload)
  181. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  182. resp = await async_client.post(
  183. "/api/v1/makerworld/resolve",
  184. json={"url": "https://makerworld.com/en/models/1400373"},
  185. )
  186. assert resp.status_code == 200, resp.text
  187. instances = resp.json()["instances"]
  188. # First instance: design entry exists but no extention → fields absent or None.
  189. first = next(i for i in instances if i["id"] == 1452154)
  190. assert first.get("compatibility") is None
  191. assert first.get("otherCompatibility") is None
  192. # Second instance: no design entry at all → no enrichment, no crash.
  193. second = next(i for i in instances if i["id"] == 9999999)
  194. assert "compatibility" not in second or second["compatibility"] is None
  195. class TestImport:
  196. """End-to-end of POST /makerworld/import — mocks the service but exercises
  197. real DB writes, real ``save_3mf_bytes_to_library``, real folder auto-creation."""
  198. _FAKE_3MF_BYTES = b"PK\x03\x04not-a-real-3mf"
  199. @pytest.mark.asyncio
  200. async def test_returns_existing_on_source_url_match(self, async_client, db_session):
  201. """Re-importing a model we already have must NOT re-download.
  202. Dedupe key is ``{model_id}#profileId-{profile_id}`` — matches the
  203. canonical URL the route constructs, not the legacy model-only shape.
  204. """
  205. existing = LibraryFile(
  206. filename="already-here.3mf",
  207. file_path="library/files/already.3mf",
  208. file_type="3mf",
  209. file_size=500,
  210. source_type="makerworld",
  211. source_url="https://makerworld.com/models/1400373#profileId-298919107",
  212. )
  213. db_session.add(existing)
  214. await db_session.commit()
  215. await db_session.refresh(existing)
  216. svc = _fake_service(
  217. get_design=_default_design(),
  218. get_profile_download=_default_manifest(),
  219. )
  220. svc.download_3mf = AsyncMock() # must remain uncalled
  221. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  222. resp = await async_client.post(
  223. "/api/v1/makerworld/import",
  224. json={"model_id": 1400373, "profile_id": 298919107},
  225. )
  226. assert resp.status_code == 200, resp.text
  227. body = resp.json()
  228. assert body["library_file_id"] == existing.id
  229. assert body["was_existing"] is True
  230. assert body["profile_id"] == 298919107
  231. svc.download_3mf.assert_not_called()
  232. @pytest.mark.asyncio
  233. async def test_autocreates_makerworld_folder_when_folder_id_none(self, async_client, db_session):
  234. """Default destination — a top-level "MakerWorld" folder — is created
  235. on first import so users don't have to set it up."""
  236. svc = _fake_service(
  237. get_design=_default_design(),
  238. get_profile_download=_default_manifest(),
  239. download_3mf=(self._FAKE_3MF_BYTES, "benchy.3mf"),
  240. )
  241. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  242. resp = await async_client.post(
  243. "/api/v1/makerworld/import",
  244. json={"model_id": 1400373, "profile_id": 298919107, "folder_id": None},
  245. )
  246. assert resp.status_code == 200, resp.text
  247. # The new folder should exist, at the root.
  248. from sqlalchemy import select
  249. result = await db_session.execute(
  250. select(LibraryFolder).where(LibraryFolder.name == "MakerWorld", LibraryFolder.parent_id.is_(None))
  251. )
  252. folder = result.scalar_one()
  253. assert resp.json()["folder_id"] == folder.id
  254. @pytest.mark.asyncio
  255. async def test_uses_existing_folder_when_folder_id_provided(self, async_client, db_session):
  256. """Caller-supplied ``folder_id`` must be honoured even if the default
  257. ``MakerWorld`` folder also exists — no silent hijacking."""
  258. folder = LibraryFolder(name="MyCustomFolder", parent_id=None)
  259. db_session.add(folder)
  260. await db_session.commit()
  261. await db_session.refresh(folder)
  262. svc = _fake_service(
  263. get_design=_default_design(),
  264. get_profile_download=_default_manifest(),
  265. download_3mf=(self._FAKE_3MF_BYTES, "benchy.3mf"),
  266. )
  267. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  268. resp = await async_client.post(
  269. "/api/v1/makerworld/import",
  270. json={"model_id": 1400373, "profile_id": 298919107, "folder_id": folder.id},
  271. )
  272. assert resp.status_code == 200, resp.text
  273. assert resp.json()["folder_id"] == folder.id
  274. @pytest.mark.asyncio
  275. async def test_canonical_source_url_includes_profile_id(self, async_client, db_session):
  276. """The saved row's ``source_url`` must include ``#profileId-`` so two
  277. plates of the same model become two library rows (dedupe is per-plate)."""
  278. svc = _fake_service(
  279. get_design=_default_design(),
  280. get_profile_download=_default_manifest(),
  281. download_3mf=(self._FAKE_3MF_BYTES, "benchy.3mf"),
  282. )
  283. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  284. resp = await async_client.post(
  285. "/api/v1/makerworld/import",
  286. json={"model_id": 1400373, "profile_id": 298919107},
  287. )
  288. assert resp.status_code == 200, resp.text
  289. from sqlalchemy import select
  290. row = (
  291. await db_session.execute(select(LibraryFile).where(LibraryFile.id == resp.json()["library_file_id"]))
  292. ).scalar_one()
  293. assert row.source_url == "https://makerworld.com/models/1400373#profileId-298919107"
  294. @pytest.mark.asyncio
  295. async def test_filename_from_upstream_is_basenamed(self, async_client, db_session):
  296. """Defence-in-depth: a malicious ``name`` from the upstream manifest
  297. (e.g. ``"../../evil.3mf"``) must not persist path components into the
  298. library row. On-disk storage uses a UUID already, this is belt-and-
  299. braces protection for the human-readable field."""
  300. svc = _fake_service(
  301. get_design=_default_design(),
  302. get_profile_download={
  303. "name": "../../evil.3mf",
  304. "url": "https://makerworld.bblmw.com/makerworld/model/X/Y/f.3mf?exp=1&key=k",
  305. },
  306. download_3mf=(self._FAKE_3MF_BYTES, "fallback.3mf"),
  307. )
  308. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  309. resp = await async_client.post(
  310. "/api/v1/makerworld/import",
  311. json={"model_id": 1400373, "profile_id": 298919107},
  312. )
  313. assert resp.status_code == 200, resp.text
  314. assert resp.json()["filename"] == "evil.3mf"
  315. @pytest.mark.asyncio
  316. async def test_response_includes_profile_id(self, async_client, db_session):
  317. """UI matches imports back to the plate row via ``profile_id`` — the
  318. response field must always be populated, even when the caller provided
  319. it explicitly (rather than the backend falling back to design defaults)."""
  320. svc = _fake_service(
  321. get_design=_default_design(),
  322. get_profile_download=_default_manifest(),
  323. download_3mf=(self._FAKE_3MF_BYTES, "benchy.3mf"),
  324. )
  325. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  326. resp = await async_client.post(
  327. "/api/v1/makerworld/import",
  328. json={"model_id": 1400373, "profile_id": 298919107},
  329. )
  330. assert resp.status_code == 200, resp.text
  331. assert resp.json()["profile_id"] == 298919107
  332. class TestRecentImports:
  333. """GET /makerworld/recent-imports — sidebar feed on the MakerWorld page."""
  334. @pytest.mark.asyncio
  335. async def test_empty_when_no_makerworld_imports(self, async_client):
  336. resp = await async_client.get("/api/v1/makerworld/recent-imports")
  337. assert resp.status_code == 200
  338. assert resp.json() == []
  339. @pytest.mark.asyncio
  340. async def test_returns_items_newest_first(self, async_client, db_session):
  341. # Seed three rows with explicit, decreasing created_at timestamps so
  342. # ordering doesn't depend on auto-increment PK ordering.
  343. base = datetime(2025, 1, 1, 12, 0, 0)
  344. older = LibraryFile(
  345. filename="older.3mf",
  346. file_path="library/older.3mf",
  347. file_type="3mf",
  348. file_size=10,
  349. source_type="makerworld",
  350. source_url="https://makerworld.com/models/1",
  351. created_at=base,
  352. )
  353. middle = LibraryFile(
  354. filename="middle.3mf",
  355. file_path="library/middle.3mf",
  356. file_type="3mf",
  357. file_size=10,
  358. source_type="makerworld",
  359. source_url="https://makerworld.com/models/2",
  360. created_at=base + timedelta(hours=1),
  361. )
  362. newer = LibraryFile(
  363. filename="newer.3mf",
  364. file_path="library/newer.3mf",
  365. file_type="3mf",
  366. file_size=10,
  367. source_type="makerworld",
  368. source_url="https://makerworld.com/models/3",
  369. created_at=base + timedelta(hours=2),
  370. )
  371. # Unrelated non-MakerWorld file must NOT show up.
  372. other = LibraryFile(
  373. filename="manual.3mf",
  374. file_path="library/manual.3mf",
  375. file_type="3mf",
  376. file_size=10,
  377. source_type=None,
  378. source_url=None,
  379. created_at=base + timedelta(hours=3),
  380. )
  381. db_session.add_all([older, middle, newer, other])
  382. await db_session.commit()
  383. resp = await async_client.get("/api/v1/makerworld/recent-imports")
  384. assert resp.status_code == 200, resp.text
  385. body = resp.json()
  386. names = [row["filename"] for row in body]
  387. assert names == ["newer.3mf", "middle.3mf", "older.3mf"]
  388. @pytest.mark.asyncio
  389. async def test_response_matches_pydantic_shape(self, async_client, db_session):
  390. """Lock the exact key set so the frontend's typed ``MakerworldRecentImport``
  391. doesn't silently fall out of sync with the backend schema."""
  392. row = LibraryFile(
  393. filename="x.3mf",
  394. file_path="library/x.3mf",
  395. file_type="3mf",
  396. file_size=10,
  397. source_type="makerworld",
  398. source_url="https://makerworld.com/models/1#profileId-2",
  399. )
  400. db_session.add(row)
  401. await db_session.commit()
  402. resp = await async_client.get("/api/v1/makerworld/recent-imports")
  403. assert resp.status_code == 200, resp.text
  404. item = resp.json()[0]
  405. assert set(item.keys()) == {
  406. "library_file_id",
  407. "filename",
  408. "folder_id",
  409. "thumbnail_path",
  410. "source_url",
  411. "created_at",
  412. }
  413. assert item["source_url"] == "https://makerworld.com/models/1#profileId-2"
  414. @pytest.mark.asyncio
  415. async def test_limit_is_honoured(self, async_client, db_session):
  416. for i in range(5):
  417. db_session.add(
  418. LibraryFile(
  419. filename=f"f{i}.3mf",
  420. file_path=f"library/f{i}.3mf",
  421. file_type="3mf",
  422. file_size=10,
  423. source_type="makerworld",
  424. source_url=f"https://makerworld.com/models/{i}",
  425. )
  426. )
  427. await db_session.commit()
  428. resp = await async_client.get("/api/v1/makerworld/recent-imports?limit=2")
  429. assert resp.status_code == 200
  430. assert len(resp.json()) == 2
  431. @pytest.mark.asyncio
  432. async def test_limit_clamped_to_minimum(self, async_client, db_session):
  433. """``limit=0`` or negative must clamp to 1 — a zero limit would be
  434. silently swallowed by SQL and return nothing, which is surprising."""
  435. db_session.add(
  436. LibraryFile(
  437. filename="one.3mf",
  438. file_path="library/one.3mf",
  439. file_type="3mf",
  440. file_size=10,
  441. source_type="makerworld",
  442. source_url="https://makerworld.com/models/1",
  443. )
  444. )
  445. await db_session.commit()
  446. resp = await async_client.get("/api/v1/makerworld/recent-imports?limit=0")
  447. assert resp.status_code == 200
  448. assert len(resp.json()) == 1
  449. @pytest.mark.asyncio
  450. async def test_limit_clamped_to_maximum(self, async_client, db_session):
  451. """``limit`` is clamped to 50 so a pathological client can't request
  452. the whole table. We seed 60 rows and assert the response is capped."""
  453. for i in range(60):
  454. db_session.add(
  455. LibraryFile(
  456. filename=f"f{i}.3mf",
  457. file_path=f"library/f{i}.3mf",
  458. file_type="3mf",
  459. file_size=10,
  460. source_type="makerworld",
  461. source_url=f"https://makerworld.com/models/{i}",
  462. )
  463. )
  464. await db_session.commit()
  465. resp = await async_client.get("/api/v1/makerworld/recent-imports?limit=9999")
  466. assert resp.status_code == 200
  467. assert len(resp.json()) == 50