test_makerworld_routes.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. class TestImport:
  114. """End-to-end of POST /makerworld/import — mocks the service but exercises
  115. real DB writes, real ``save_3mf_bytes_to_library``, real folder auto-creation."""
  116. _FAKE_3MF_BYTES = b"PK\x03\x04not-a-real-3mf"
  117. @pytest.mark.asyncio
  118. async def test_returns_existing_on_source_url_match(self, async_client, db_session):
  119. """Re-importing a model we already have must NOT re-download.
  120. Dedupe key is ``{model_id}#profileId-{profile_id}`` — matches the
  121. canonical URL the route constructs, not the legacy model-only shape.
  122. """
  123. existing = LibraryFile(
  124. filename="already-here.3mf",
  125. file_path="library/files/already.3mf",
  126. file_type="3mf",
  127. file_size=500,
  128. source_type="makerworld",
  129. source_url="https://makerworld.com/models/1400373#profileId-298919107",
  130. )
  131. db_session.add(existing)
  132. await db_session.commit()
  133. await db_session.refresh(existing)
  134. svc = _fake_service(
  135. get_design=_default_design(),
  136. get_profile_download=_default_manifest(),
  137. )
  138. svc.download_3mf = AsyncMock() # must remain uncalled
  139. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  140. resp = await async_client.post(
  141. "/api/v1/makerworld/import",
  142. json={"model_id": 1400373, "profile_id": 298919107},
  143. )
  144. assert resp.status_code == 200, resp.text
  145. body = resp.json()
  146. assert body["library_file_id"] == existing.id
  147. assert body["was_existing"] is True
  148. assert body["profile_id"] == 298919107
  149. svc.download_3mf.assert_not_called()
  150. @pytest.mark.asyncio
  151. async def test_autocreates_makerworld_folder_when_folder_id_none(self, async_client, db_session):
  152. """Default destination — a top-level "MakerWorld" folder — is created
  153. on first import so users don't have to set it up."""
  154. svc = _fake_service(
  155. get_design=_default_design(),
  156. get_profile_download=_default_manifest(),
  157. download_3mf=(self._FAKE_3MF_BYTES, "benchy.3mf"),
  158. )
  159. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  160. resp = await async_client.post(
  161. "/api/v1/makerworld/import",
  162. json={"model_id": 1400373, "profile_id": 298919107, "folder_id": None},
  163. )
  164. assert resp.status_code == 200, resp.text
  165. # The new folder should exist, at the root.
  166. from sqlalchemy import select
  167. result = await db_session.execute(
  168. select(LibraryFolder).where(LibraryFolder.name == "MakerWorld", LibraryFolder.parent_id.is_(None))
  169. )
  170. folder = result.scalar_one()
  171. assert resp.json()["folder_id"] == folder.id
  172. @pytest.mark.asyncio
  173. async def test_uses_existing_folder_when_folder_id_provided(self, async_client, db_session):
  174. """Caller-supplied ``folder_id`` must be honoured even if the default
  175. ``MakerWorld`` folder also exists — no silent hijacking."""
  176. folder = LibraryFolder(name="MyCustomFolder", parent_id=None)
  177. db_session.add(folder)
  178. await db_session.commit()
  179. await db_session.refresh(folder)
  180. svc = _fake_service(
  181. get_design=_default_design(),
  182. get_profile_download=_default_manifest(),
  183. download_3mf=(self._FAKE_3MF_BYTES, "benchy.3mf"),
  184. )
  185. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  186. resp = await async_client.post(
  187. "/api/v1/makerworld/import",
  188. json={"model_id": 1400373, "profile_id": 298919107, "folder_id": folder.id},
  189. )
  190. assert resp.status_code == 200, resp.text
  191. assert resp.json()["folder_id"] == folder.id
  192. @pytest.mark.asyncio
  193. async def test_canonical_source_url_includes_profile_id(self, async_client, db_session):
  194. """The saved row's ``source_url`` must include ``#profileId-`` so two
  195. plates of the same model become two library rows (dedupe is per-plate)."""
  196. svc = _fake_service(
  197. get_design=_default_design(),
  198. get_profile_download=_default_manifest(),
  199. download_3mf=(self._FAKE_3MF_BYTES, "benchy.3mf"),
  200. )
  201. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  202. resp = await async_client.post(
  203. "/api/v1/makerworld/import",
  204. json={"model_id": 1400373, "profile_id": 298919107},
  205. )
  206. assert resp.status_code == 200, resp.text
  207. from sqlalchemy import select
  208. row = (
  209. await db_session.execute(select(LibraryFile).where(LibraryFile.id == resp.json()["library_file_id"]))
  210. ).scalar_one()
  211. assert row.source_url == "https://makerworld.com/models/1400373#profileId-298919107"
  212. @pytest.mark.asyncio
  213. async def test_filename_from_upstream_is_basenamed(self, async_client, db_session):
  214. """Defence-in-depth: a malicious ``name`` from the upstream manifest
  215. (e.g. ``"../../evil.3mf"``) must not persist path components into the
  216. library row. On-disk storage uses a UUID already, this is belt-and-
  217. braces protection for the human-readable field."""
  218. svc = _fake_service(
  219. get_design=_default_design(),
  220. get_profile_download={
  221. "name": "../../evil.3mf",
  222. "url": "https://makerworld.bblmw.com/makerworld/model/X/Y/f.3mf?exp=1&key=k",
  223. },
  224. download_3mf=(self._FAKE_3MF_BYTES, "fallback.3mf"),
  225. )
  226. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  227. resp = await async_client.post(
  228. "/api/v1/makerworld/import",
  229. json={"model_id": 1400373, "profile_id": 298919107},
  230. )
  231. assert resp.status_code == 200, resp.text
  232. assert resp.json()["filename"] == "evil.3mf"
  233. @pytest.mark.asyncio
  234. async def test_response_includes_profile_id(self, async_client, db_session):
  235. """UI matches imports back to the plate row via ``profile_id`` — the
  236. response field must always be populated, even when the caller provided
  237. it explicitly (rather than the backend falling back to design defaults)."""
  238. svc = _fake_service(
  239. get_design=_default_design(),
  240. get_profile_download=_default_manifest(),
  241. download_3mf=(self._FAKE_3MF_BYTES, "benchy.3mf"),
  242. )
  243. with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
  244. resp = await async_client.post(
  245. "/api/v1/makerworld/import",
  246. json={"model_id": 1400373, "profile_id": 298919107},
  247. )
  248. assert resp.status_code == 200, resp.text
  249. assert resp.json()["profile_id"] == 298919107
  250. class TestRecentImports:
  251. """GET /makerworld/recent-imports — sidebar feed on the MakerWorld page."""
  252. @pytest.mark.asyncio
  253. async def test_empty_when_no_makerworld_imports(self, async_client):
  254. resp = await async_client.get("/api/v1/makerworld/recent-imports")
  255. assert resp.status_code == 200
  256. assert resp.json() == []
  257. @pytest.mark.asyncio
  258. async def test_returns_items_newest_first(self, async_client, db_session):
  259. # Seed three rows with explicit, decreasing created_at timestamps so
  260. # ordering doesn't depend on auto-increment PK ordering.
  261. base = datetime(2025, 1, 1, 12, 0, 0)
  262. older = LibraryFile(
  263. filename="older.3mf",
  264. file_path="library/older.3mf",
  265. file_type="3mf",
  266. file_size=10,
  267. source_type="makerworld",
  268. source_url="https://makerworld.com/models/1",
  269. created_at=base,
  270. )
  271. middle = LibraryFile(
  272. filename="middle.3mf",
  273. file_path="library/middle.3mf",
  274. file_type="3mf",
  275. file_size=10,
  276. source_type="makerworld",
  277. source_url="https://makerworld.com/models/2",
  278. created_at=base + timedelta(hours=1),
  279. )
  280. newer = LibraryFile(
  281. filename="newer.3mf",
  282. file_path="library/newer.3mf",
  283. file_type="3mf",
  284. file_size=10,
  285. source_type="makerworld",
  286. source_url="https://makerworld.com/models/3",
  287. created_at=base + timedelta(hours=2),
  288. )
  289. # Unrelated non-MakerWorld file must NOT show up.
  290. other = LibraryFile(
  291. filename="manual.3mf",
  292. file_path="library/manual.3mf",
  293. file_type="3mf",
  294. file_size=10,
  295. source_type=None,
  296. source_url=None,
  297. created_at=base + timedelta(hours=3),
  298. )
  299. db_session.add_all([older, middle, newer, other])
  300. await db_session.commit()
  301. resp = await async_client.get("/api/v1/makerworld/recent-imports")
  302. assert resp.status_code == 200, resp.text
  303. body = resp.json()
  304. names = [row["filename"] for row in body]
  305. assert names == ["newer.3mf", "middle.3mf", "older.3mf"]
  306. @pytest.mark.asyncio
  307. async def test_response_matches_pydantic_shape(self, async_client, db_session):
  308. """Lock the exact key set so the frontend's typed ``MakerworldRecentImport``
  309. doesn't silently fall out of sync with the backend schema."""
  310. row = LibraryFile(
  311. filename="x.3mf",
  312. file_path="library/x.3mf",
  313. file_type="3mf",
  314. file_size=10,
  315. source_type="makerworld",
  316. source_url="https://makerworld.com/models/1#profileId-2",
  317. )
  318. db_session.add(row)
  319. await db_session.commit()
  320. resp = await async_client.get("/api/v1/makerworld/recent-imports")
  321. assert resp.status_code == 200, resp.text
  322. item = resp.json()[0]
  323. assert set(item.keys()) == {
  324. "library_file_id",
  325. "filename",
  326. "folder_id",
  327. "thumbnail_path",
  328. "source_url",
  329. "created_at",
  330. }
  331. assert item["source_url"] == "https://makerworld.com/models/1#profileId-2"
  332. @pytest.mark.asyncio
  333. async def test_limit_is_honoured(self, async_client, db_session):
  334. for i in range(5):
  335. db_session.add(
  336. LibraryFile(
  337. filename=f"f{i}.3mf",
  338. file_path=f"library/f{i}.3mf",
  339. file_type="3mf",
  340. file_size=10,
  341. source_type="makerworld",
  342. source_url=f"https://makerworld.com/models/{i}",
  343. )
  344. )
  345. await db_session.commit()
  346. resp = await async_client.get("/api/v1/makerworld/recent-imports?limit=2")
  347. assert resp.status_code == 200
  348. assert len(resp.json()) == 2
  349. @pytest.mark.asyncio
  350. async def test_limit_clamped_to_minimum(self, async_client, db_session):
  351. """``limit=0`` or negative must clamp to 1 — a zero limit would be
  352. silently swallowed by SQL and return nothing, which is surprising."""
  353. db_session.add(
  354. LibraryFile(
  355. filename="one.3mf",
  356. file_path="library/one.3mf",
  357. file_type="3mf",
  358. file_size=10,
  359. source_type="makerworld",
  360. source_url="https://makerworld.com/models/1",
  361. )
  362. )
  363. await db_session.commit()
  364. resp = await async_client.get("/api/v1/makerworld/recent-imports?limit=0")
  365. assert resp.status_code == 200
  366. assert len(resp.json()) == 1
  367. @pytest.mark.asyncio
  368. async def test_limit_clamped_to_maximum(self, async_client, db_session):
  369. """``limit`` is clamped to 50 so a pathological client can't request
  370. the whole table. We seed 60 rows and assert the response is capped."""
  371. for i in range(60):
  372. db_session.add(
  373. LibraryFile(
  374. filename=f"f{i}.3mf",
  375. file_path=f"library/f{i}.3mf",
  376. file_type="3mf",
  377. file_size=10,
  378. source_type="makerworld",
  379. source_url=f"https://makerworld.com/models/{i}",
  380. )
  381. )
  382. await db_session.commit()
  383. resp = await async_client.get("/api/v1/makerworld/recent-imports?limit=9999")
  384. assert resp.status_code == 200
  385. assert len(resp.json()) == 50