test_makerworld.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. """Tests for the MakerWorldService."""
  2. from __future__ import annotations
  3. from unittest.mock import AsyncMock, MagicMock, patch
  4. from urllib.error import HTTPError, URLError
  5. import httpx
  6. import pytest
  7. from backend.app.services.model_providers.base import ProviderResourceRef
  8. from backend.app.services.model_providers.makerworld.errors import (
  9. MakerWorldAuthError,
  10. MakerWorldForbiddenError,
  11. MakerWorldNotFoundError,
  12. MakerWorldUnavailableError,
  13. MakerWorldUrlError,
  14. )
  15. from backend.app.services.model_providers.makerworld.http import _MAX_3MF_BYTES, MAKERWORLD_API_BASE
  16. from backend.app.services.model_providers.makerworld.service import MakerWorldService, set_shared_http_client
  17. from backend.app.services.model_providers.makerworld.url import parse_url
  18. class TestParseUrl:
  19. """MakerWorld URL extraction — tests parse_url directly."""
  20. def test_strips_locale_prefix_and_slug(self):
  21. ref = parse_url("https://makerworld.com/en/models/1400373-self-watering-seed-starter")
  22. assert ref.external_id == "1400373"
  23. assert ref.sub_id is None
  24. def test_extracts_profile_id_from_fragment(self):
  25. ref = parse_url("https://makerworld.com/en/models/1400373-slug#profileId-1452154")
  26. assert ref.external_id == "1400373"
  27. assert ref.sub_id == "1452154"
  28. def test_accepts_scheme_omitted(self):
  29. ref = parse_url("makerworld.com/models/999")
  30. assert ref.external_id == "999"
  31. assert ref.sub_id is None
  32. def test_accepts_subdomain(self):
  33. # Defensive: if MakerWorld ever stands up a regional subdomain, still accept it
  34. ref = parse_url("https://www.makerworld.com/en/models/42")
  35. assert ref.external_id == "42"
  36. assert ref.sub_id is None
  37. def test_rejects_non_makerworld_host(self):
  38. with pytest.raises(MakerWorldUrlError):
  39. parse_url("https://thingiverse.com/things/123")
  40. def test_rejects_malformed_url(self):
  41. # No /models/ segment anywhere in path
  42. with pytest.raises(MakerWorldUrlError):
  43. parse_url("https://makerworld.com/en/creators/foo")
  44. def test_rejects_empty(self):
  45. with pytest.raises(MakerWorldUrlError):
  46. parse_url("")
  47. class TestApiBase:
  48. """Sanity check on the module-level constant — changing it is a deploy-risk."""
  49. def test_api_base_targets_bambulab_backend(self):
  50. # ``api.bambulab.com`` is not Cloudflare-fronted; ``makerworld.com`` is
  51. # and returns empty JSON to plain httpx. Regressing this constant
  52. # silently breaks the whole integration.
  53. assert MAKERWORLD_API_BASE == "https://api.bambulab.com/v1/design-service"
  54. class TestGetDesign:
  55. """Metadata endpoint happy-path + error mapping."""
  56. @pytest.fixture
  57. def service(self):
  58. # Use a MagicMock for the client so each call can be individually stubbed
  59. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  60. svc._client.get = AsyncMock()
  61. return svc
  62. @pytest.mark.asyncio
  63. async def test_returns_decoded_json(self, service):
  64. resp = MagicMock()
  65. resp.status_code = 200
  66. resp.json.return_value = {"id": 1400373, "title": "Benchy"}
  67. service._client.get.return_value = resp
  68. data = await service.get_design(1400373)
  69. assert data == {"id": 1400373, "title": "Benchy"}
  70. @pytest.mark.asyncio
  71. async def test_hits_bambulab_api_base(self, service):
  72. resp = MagicMock()
  73. resp.status_code = 200
  74. resp.json.return_value = {"id": 1}
  75. service._client.get.return_value = resp
  76. await service.get_design(1)
  77. call = service._client.get.call_args
  78. # First positional arg is the URL — must be on the api.bambulab.com
  79. # backend, not the Cloudflare-fronted makerworld.com host.
  80. url = call.args[0] if call.args else call.kwargs.get("url")
  81. assert url == "https://api.bambulab.com/v1/design-service/design/1"
  82. @pytest.mark.asyncio
  83. async def test_sends_honest_bambuddy_user_agent(self, service):
  84. """The client identifies honestly as Bambuddy, not as Firefox.
  85. Earlier iterations of this code stripped ``x-bbl-*`` Bambu-app
  86. identification headers but kept a Firefox User-Agent. Verified
  87. 2026-05-12 that MakerWorld treats ``Bambuddy/X.Y.Z`` identically to
  88. a Firefox UA at the Cloudflare edge — same response shape on
  89. ``/api/v1/design-service/*`` paths. Honest identification keeps us
  90. clearly outside Bambu Lab's "no falsified client identity" line
  91. from the 2026-05-12 cloud-access blog post.
  92. Referer is still sent because MakerWorld's CSRF / origin-check
  93. middleware uses it on some endpoints — that is functional, not
  94. client-impersonation.
  95. """
  96. resp = MagicMock()
  97. resp.status_code = 200
  98. resp.json.return_value = {"id": 1}
  99. service._client.get.return_value = resp
  100. await service.get_design(1)
  101. headers = service._client.get.call_args.kwargs["headers"]
  102. assert headers["User-Agent"].startswith("Bambuddy/")
  103. # Browser-impersonation strings must not creep back in
  104. assert "Mozilla" not in headers["User-Agent"]
  105. assert "Firefox" not in headers["User-Agent"]
  106. assert "Chrome" not in headers["User-Agent"]
  107. # Functional headers stay
  108. assert headers["Accept-Language"].startswith("en-US")
  109. assert headers["Referer"] == "https://makerworld.com/"
  110. assert "Accept" in headers
  111. # The deprecated Bambu-identification headers must no longer be sent.
  112. for dead_header in (
  113. "x-bbl-client-type",
  114. "x-bbl-client-version",
  115. "x-bbl-app-source",
  116. "x-bbl-client-name",
  117. ):
  118. assert dead_header not in headers
  119. @pytest.mark.asyncio
  120. async def test_maps_404_to_not_found(self, service):
  121. resp = MagicMock()
  122. resp.status_code = 404
  123. service._client.get.return_value = resp
  124. with pytest.raises(MakerWorldNotFoundError):
  125. await service.get_design(404)
  126. @pytest.mark.asyncio
  127. async def test_maps_401_without_token_to_auth_error(self, service):
  128. """No token was sent, so a 401 means "sign-in required" — not "your
  129. sign-in expired", and nothing gets marked dead (there is nothing to
  130. mark). The fixture's service carries no auth token."""
  131. resp = MagicMock()
  132. resp.status_code = 401
  133. resp.json.return_value = {"code": 1, "error": "Please log in"}
  134. service._client.get.return_value = resp
  135. with pytest.raises(MakerWorldAuthError) as exc_info:
  136. await service.get_design(1)
  137. assert "Bambu Cloud" in str(exc_info.value)
  138. @pytest.mark.asyncio
  139. async def test_401_with_token_reports_expired_and_hides_upstream_text(self):
  140. """Bambu answers a dead token with ``{"error": "Please login."}``. We used
  141. to forward that verbatim, which produced a "Please login." toast on a UI
  142. that simultaneously claimed the user was connected, and pointed at a
  143. Settings page that does not exist. Say what happened, name a real page,
  144. and record the credential as dead."""
  145. marked: list[bool] = []
  146. async def _on_auth_failure() -> None:
  147. marked.append(True)
  148. svc = MakerWorldService(
  149. client=MagicMock(spec=httpx.AsyncClient),
  150. auth_token="tok-abc",
  151. on_auth_failure=_on_auth_failure,
  152. )
  153. svc._client.get = AsyncMock()
  154. resp = MagicMock()
  155. resp.status_code = 401
  156. resp.json.return_value = {"code": 4, "error": "Please login.", "message": ""}
  157. svc._client.get.return_value = resp
  158. with pytest.raises(MakerWorldAuthError) as exc_info:
  159. await svc.get_design(1)
  160. message = str(exc_info.value)
  161. assert "Please login." not in message
  162. assert "expired" in message.lower()
  163. assert "Profiles" in message
  164. assert marked == [True], "a rejected token must be recorded as dead"
  165. @pytest.mark.asyncio
  166. async def test_transient_401_with_token_does_not_invalidate(self):
  167. """A 401 WITHOUT Bambu's expiry signature (endpoint/edge noise) must fail
  168. the request but NOT durably sign the user out — otherwise one stray 401
  169. from any single MakerWorld call kills the whole cloud integration."""
  170. marked: list[bool] = []
  171. async def _on_auth_failure() -> None:
  172. marked.append(True)
  173. svc = MakerWorldService(
  174. client=MagicMock(spec=httpx.AsyncClient),
  175. auth_token="tok-abc",
  176. on_auth_failure=_on_auth_failure,
  177. )
  178. svc._client.get = AsyncMock()
  179. resp = MagicMock()
  180. resp.status_code = 401
  181. resp.json.return_value = {"code": 1, "error": "forbidden"}
  182. svc._client.get.return_value = resp
  183. with pytest.raises(MakerWorldAuthError):
  184. await svc.get_design(1)
  185. assert marked == [], "a benign 401 must not record the credential as dead"
  186. @pytest.mark.asyncio
  187. async def test_maps_403_to_forbidden_with_upstream_reason(self, service):
  188. """403 is distinct from 401: auth was valid, MakerWorld refuses the
  189. specific resource (content-gated, region-locked, etc.). The upstream
  190. reason must reach the user so they know what to do."""
  191. resp = MagicMock()
  192. resp.status_code = 403
  193. resp.json.return_value = {
  194. "code": 15001,
  195. "error": "This model is only available to members",
  196. }
  197. service._client.get.return_value = resp
  198. with pytest.raises(MakerWorldForbiddenError) as exc_info:
  199. await service.get_design(1)
  200. assert "members" in str(exc_info.value)
  201. @pytest.mark.asyncio
  202. async def test_maps_5xx_to_unavailable(self, service):
  203. resp = MagicMock()
  204. resp.status_code = 503
  205. service._client.get.return_value = resp
  206. with pytest.raises(MakerWorldUnavailableError):
  207. await service.get_design(1)
  208. @pytest.mark.asyncio
  209. async def test_maps_timeout_to_unavailable(self, service):
  210. service._client.get.side_effect = httpx.TimeoutException("tooo slow")
  211. with pytest.raises(MakerWorldUnavailableError):
  212. await service.get_design(1)
  213. @pytest.mark.asyncio
  214. async def test_rejects_non_dict_json(self, service):
  215. resp = MagicMock()
  216. resp.status_code = 200
  217. resp.json.return_value = [1, 2, 3] # list, not dict
  218. service._client.get.return_value = resp
  219. with pytest.raises(MakerWorldUnavailableError):
  220. await service.get_design(1)
  221. class TestResolve:
  222. """``resolve`` — the interface-level "URL → metadata + plate list" flow the
  223. /makerworld/resolve route drives. The per-instance printer-compatibility
  224. merge lives here (not in the route) so every future provider gets it from
  225. its own ``resolve`` implementation."""
  226. @pytest.fixture
  227. def service(self):
  228. return MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  229. @pytest.mark.asyncio
  230. async def test_merges_compatibility_from_design_into_instances(self, service):
  231. """Per-instance printer compatibility info lives on
  232. ``design.instances[].extention.modelInfo`` but not on
  233. ``/instances/hits``. Resolve enriches each hit with both
  234. ``compatibility`` (primary printer the instance was sliced for) and
  235. ``otherCompatibility`` (extra printers the uploader marked it
  236. compatible with) so the frontend can show "sliced for A1 / also
  237. marked compatible with: H2D, P1S".
  238. """
  239. design_payload = {
  240. "id": 1400373,
  241. "title": "Seed Starter",
  242. "instances": [
  243. {
  244. "id": 1452154,
  245. "extention": {
  246. "modelInfo": {
  247. "compatibility": ["A1"],
  248. "otherCompatibility": ["H2D", "P1S"],
  249. }
  250. },
  251. },
  252. {
  253. "id": 1452158,
  254. "extention": {
  255. "modelInfo": {
  256. "compatibility": ["X1 Carbon"],
  257. "otherCompatibility": [],
  258. }
  259. },
  260. },
  261. ],
  262. }
  263. instances_payload = {
  264. "total": 2,
  265. "hits": [
  266. {"id": 1452154, "profileId": 298919107, "title": "9 cells"},
  267. {"id": 1452158, "profileId": 298919564, "title": "12 cells"},
  268. ],
  269. }
  270. service.get_design = AsyncMock(return_value=design_payload)
  271. service.get_design_instances = AsyncMock(return_value=instances_payload)
  272. resolved = await service.resolve(ProviderResourceRef(source_type="makerworld", external_id="1400373"))
  273. by_id = {i["id"]: i for i in resolved.instances}
  274. assert by_id[1452154]["compatibility"] == ["A1"]
  275. assert by_id[1452154]["otherCompatibility"] == ["H2D", "P1S"]
  276. assert by_id[1452158]["compatibility"] == ["X1 Carbon"]
  277. assert by_id[1452158]["otherCompatibility"] == []
  278. assert resolved.design == design_payload
  279. @pytest.mark.asyncio
  280. async def test_handles_missing_compatibility_gracefully(self, service):
  281. """Older designs (or hits without a matching design.instances entry)
  282. must not crash resolve — they just don't get the compat fields."""
  283. design_payload = {"id": 1400373, "instances": [{"id": 1452154}]} # no extention
  284. instances_payload = {
  285. "total": 2,
  286. "hits": [
  287. {"id": 1452154, "profileId": 298919107},
  288. {"id": 9999999, "profileId": 298919999}, # no design.instances match
  289. ],
  290. }
  291. service.get_design = AsyncMock(return_value=design_payload)
  292. service.get_design_instances = AsyncMock(return_value=instances_payload)
  293. resolved = await service.resolve(ProviderResourceRef(source_type="makerworld", external_id="1400373"))
  294. # First instance: design entry exists but no extention → fields absent or None.
  295. first = next(i for i in resolved.instances if i["id"] == 1452154)
  296. assert first.get("compatibility") is None
  297. assert first.get("otherCompatibility") is None
  298. # Second instance: no design entry at all → no enrichment, no crash.
  299. second = next(i for i in resolved.instances if i["id"] == 9999999)
  300. assert "compatibility" not in second or second["compatibility"] is None
  301. @pytest.mark.asyncio
  302. async def test_normalises_null_and_non_list_hits_to_empty(self, service):
  303. service.get_design = AsyncMock(return_value={"id": 1400373})
  304. service.get_design_instances = AsyncMock(return_value={"total": 0, "hits": None})
  305. resolved = await service.resolve(ProviderResourceRef(source_type="makerworld", external_id="1400373"))
  306. assert resolved.instances == []
  307. class TestGetDownload:
  308. """``get_download`` — the interface-level "resource → signed 3MF URL"
  309. flow the /makerworld/import route drives. The provider-specific dance
  310. lives here: the iot-service endpoint needs the *alphanumeric* modelId
  311. (not the integer design id), the profile falls back in two tiers, and
  312. three malformed-upstream shapes must map to UnavailableError (502)."""
  313. @pytest.fixture
  314. def service(self):
  315. return MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  316. def _design(self, **overrides):
  317. design = {
  318. "id": 1400373,
  319. "modelId": "US2bb73b106683e5",
  320. "instances": [{"profileId": 298919107, "title": "9 cells"}],
  321. }
  322. design.update(overrides)
  323. return design
  324. def _manifest(self, url="https://makerworld.bblmw.com/x.3mf?exp=1", name="benchy.3mf"):
  325. return {"url": url, "name": name}
  326. async def _run(self, service, ref):
  327. return await service.get_download(ref)
  328. @pytest.mark.asyncio
  329. async def test_resolves_alphanumeric_model_id_and_explicit_profile(self, service):
  330. """Explicit profile_id flows through; get_profile_download receives
  331. the alphanumeric modelId from the design, not the integer id."""
  332. service.get_design = AsyncMock(return_value=self._design())
  333. manifest = self._manifest()
  334. service.get_profile_download = AsyncMock(return_value=manifest)
  335. info = await self._run(
  336. service, ProviderResourceRef(source_type="makerworld", external_id="1400373", sub_id="298919107")
  337. )
  338. service.get_profile_download.assert_awaited_once_with(298919107, "US2bb73b106683e5")
  339. assert info.url == manifest["url"]
  340. assert info.suggested_filename == "benchy.3mf"
  341. # The enriched ref carries the resolved profile for the dedupe key.
  342. assert info.ref.sub_id == "298919107"
  343. @pytest.mark.asyncio
  344. async def test_falls_back_to_first_design_instance_profile(self, service):
  345. """No profile given → first ``design.instances[].profileId`` wins."""
  346. service.get_design = AsyncMock(return_value=self._design())
  347. service.get_profile_download = AsyncMock(return_value=self._manifest())
  348. info = await self._run(service, ProviderResourceRef(source_type="makerworld", external_id="1400373"))
  349. service.get_profile_download.assert_awaited_once_with(298919107, "US2bb73b106683e5")
  350. assert info.ref.sub_id == "298919107"
  351. @pytest.mark.asyncio
  352. async def test_second_tier_falls_back_to_instances_envelope(self, service):
  353. """Design carries no usable profileId → the ``/design/{id}/instances``
  354. envelope is consulted before giving up."""
  355. service.get_design = AsyncMock(return_value=self._design(instances=[{"title": "no profileId here"}]))
  356. service.get_design_instances = AsyncMock(return_value={"total": 1, "hits": [{"profileId": 298919564}]})
  357. service.get_profile_download = AsyncMock(return_value=self._manifest())
  358. info = await self._run(service, ProviderResourceRef(source_type="makerworld", external_id="1400373"))
  359. service.get_design_instances.assert_awaited_once_with(1400373)
  360. service.get_profile_download.assert_awaited_once_with(298919564, "US2bb73b106683e5")
  361. assert info.ref.sub_id == "298919564"
  362. @pytest.mark.asyncio
  363. async def test_missing_alphanumeric_model_id_is_unavailable(self, service):
  364. """A design without the ``modelId`` field can't reach iot-service."""
  365. service.get_design = AsyncMock(return_value={"id": 1400373})
  366. with pytest.raises(MakerWorldUnavailableError, match="modelId"):
  367. await self._run(service, ProviderResourceRef(source_type="makerworld", external_id="1400373"))
  368. @pytest.mark.asyncio
  369. async def test_no_profiles_anywhere_is_unavailable(self, service):
  370. service.get_design = AsyncMock(return_value=self._design(instances=[]))
  371. service.get_design_instances = AsyncMock(return_value={"total": 0, "hits": []})
  372. with pytest.raises(MakerWorldUnavailableError, match="no instances"):
  373. await self._run(service, ProviderResourceRef(source_type="makerworld", external_id="1400373"))
  374. @pytest.mark.asyncio
  375. async def test_manifest_without_url_is_unavailable(self, service):
  376. service.get_design = AsyncMock(return_value=self._design())
  377. service.get_profile_download = AsyncMock(return_value={"name": "benchy.3mf"})
  378. with pytest.raises(MakerWorldUnavailableError, match="download URL"):
  379. await self._run(service, ProviderResourceRef(source_type="makerworld", external_id="1400373"))
  380. class TestGetProfileDownload:
  381. """The new auth-gated 3MF manifest endpoint on the Bambu iot-service.
  382. Replaces the removed ``get_instance_download`` / ``get_model_download``
  383. helpers — YASTL#51's endpoint mints the signed CDN URL from the same
  384. long-lived Bambu Cloud bearer users already have.
  385. """
  386. def _make_service(self, *, auth_token: str | None = "tok-abc") -> MakerWorldService:
  387. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient), auth_token=auth_token)
  388. svc._client.get = AsyncMock()
  389. return svc
  390. @pytest.mark.asyncio
  391. async def test_requires_auth_token(self):
  392. svc = self._make_service(auth_token=None)
  393. with pytest.raises(MakerWorldAuthError):
  394. await svc.get_profile_download(1452154, "US2bb73b106683e5")
  395. @pytest.mark.asyncio
  396. async def test_returns_signed_manifest(self):
  397. svc = self._make_service()
  398. resp = MagicMock()
  399. resp.status_code = 200
  400. resp.json.return_value = {
  401. "name": "benchy.3mf",
  402. "url": "https://makerworld.bblmw.com/makerworld/model/X/Y/f.3mf?exp=1&key=k",
  403. }
  404. svc._client.get.return_value = resp
  405. manifest = await svc.get_profile_download(1452154, "US2bb73b106683e5")
  406. assert manifest["url"].startswith("https://makerworld.bblmw.com/")
  407. assert manifest["name"] == "benchy.3mf"
  408. @pytest.mark.asyncio
  409. async def test_sends_bearer_and_model_id_query(self):
  410. """Auth goes in ``Authorization`` and the alphanumeric modelId as a
  411. ``model_id`` query param — this is what YASTL#51 reverse-engineered."""
  412. svc = self._make_service(auth_token="tok-abc")
  413. resp = MagicMock()
  414. resp.status_code = 200
  415. resp.json.return_value = {"url": "https://makerworld.bblmw.com/x.3mf"}
  416. svc._client.get.return_value = resp
  417. await svc.get_profile_download(1452154, "US2bb73b106683e5")
  418. call = svc._client.get.call_args
  419. url = call.args[0] if call.args else call.kwargs.get("url")
  420. assert url == "https://api.bambulab.com/v1/iot-service/api/user/profile/1452154"
  421. assert call.kwargs["headers"]["Authorization"] == "Bearer tok-abc"
  422. assert call.kwargs["params"] == {"model_id": "US2bb73b106683e5"}
  423. @pytest.mark.asyncio
  424. async def test_maps_401_to_auth_error(self):
  425. svc = self._make_service()
  426. resp = MagicMock()
  427. resp.status_code = 401
  428. resp.json.return_value = {"error": "token expired"}
  429. svc._client.get.return_value = resp
  430. with pytest.raises(MakerWorldAuthError):
  431. await svc.get_profile_download(1, "M1")
  432. @pytest.mark.asyncio
  433. async def test_maps_403_to_forbidden(self):
  434. svc = self._make_service()
  435. resp = MagicMock()
  436. resp.status_code = 403
  437. resp.json.return_value = {"error": "paid model"}
  438. svc._client.get.return_value = resp
  439. with pytest.raises(MakerWorldForbiddenError) as exc_info:
  440. await svc.get_profile_download(1, "M1")
  441. assert "paid model" in str(exc_info.value)
  442. @pytest.mark.asyncio
  443. async def test_maps_404_to_not_found(self):
  444. svc = self._make_service()
  445. resp = MagicMock()
  446. resp.status_code = 404
  447. svc._client.get.return_value = resp
  448. with pytest.raises(MakerWorldNotFoundError):
  449. await svc.get_profile_download(1, "M1")
  450. @pytest.mark.asyncio
  451. async def test_maps_timeout_to_unavailable(self):
  452. svc = self._make_service()
  453. svc._client.get.side_effect = httpx.TimeoutException("nope")
  454. with pytest.raises(MakerWorldUnavailableError):
  455. await svc.get_profile_download(1, "M1")
  456. @pytest.mark.asyncio
  457. async def test_rejects_non_dict_json(self):
  458. svc = self._make_service()
  459. resp = MagicMock()
  460. resp.status_code = 200
  461. resp.json.return_value = ["not", "a", "dict"]
  462. svc._client.get.return_value = resp
  463. with pytest.raises(MakerWorldUnavailableError):
  464. await svc.get_profile_download(1, "M1")
  465. class TestDownload3MF:
  466. """SSRF guard + size cap + streaming behaviour."""
  467. def _stream_ctx(self, resp):
  468. ctx = MagicMock()
  469. ctx.__aenter__ = AsyncMock(return_value=resp)
  470. ctx.__aexit__ = AsyncMock(return_value=None)
  471. return ctx
  472. @pytest.mark.asyncio
  473. @pytest.mark.parametrize(
  474. "url",
  475. [
  476. "https://example.com/steal.3mf",
  477. "https://169.254.169.254/meta", # EC2 metadata
  478. "http://internal.host/loot",
  479. "http://127.0.0.1/loot",
  480. ],
  481. )
  482. async def test_rejects_non_allowed_hosts(self, url):
  483. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  484. with pytest.raises(MakerWorldUrlError):
  485. await svc.download_3mf(url)
  486. @pytest.mark.asyncio
  487. async def test_download_allowlist_is_driven_by_injected_hosts(self):
  488. """``download_hosts`` is the SSRF seam, not a hardcoded constant (review
  489. round 3 note 2). ``build_service`` passes ``ModelProvider.download_hosts()``
  490. into ``MakerWorldService`` — prove the injection is live by accepting a
  491. host inside a custom allowlist and refusing a MakerWorld CDN host that
  492. isn't in it."""
  493. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient), download_hosts=("cdn.example.com",))
  494. resp = MagicMock()
  495. resp.status_code = 200
  496. async def _chunks():
  497. yield b"PK\x03\x04"
  498. resp.aiter_bytes = lambda: _chunks()
  499. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  500. payload, _ = await svc.download_3mf("https://cdn.example.com/m/foo.3mf?exp=1&key=k")
  501. assert payload == b"PK\x03\x04"
  502. with pytest.raises(MakerWorldUrlError):
  503. await svc.download_3mf("https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k")
  504. @pytest.mark.asyncio
  505. async def test_s3_suffix_family_is_allowed_regardless_of_injected_hosts(self):
  506. """``_ALLOWED_DOWNLOAD_SUFFIXES`` is deliberately outside the
  507. ``download_hosts()`` seam: Bambu's presigned S3 endpoints are this
  508. provider's own signed-URL family, not an exact-host allowlist a
  509. provider declares. Pinned so narrowing the injected hosts can never
  510. silently take the S3 download path with it."""
  511. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient), download_hosts=("cdn.example.com",))
  512. with patch(
  513. "backend.app.services.model_providers.makerworld.service._download_s3_urllib",
  514. AsyncMock(return_value=(b"PK\x03\x04", "plate.3mf")),
  515. ) as s3:
  516. payload, name = await svc.download_3mf("https://s3.us-west-2.amazonaws.com/bucket/plate.3mf?sig=1")
  517. assert payload == b"PK\x03\x04"
  518. assert name == "plate.3mf"
  519. assert s3.await_count == 1
  520. @pytest.mark.asyncio
  521. async def test_s3_host_delegates_to_urllib_path(self):
  522. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  523. with patch(
  524. "backend.app.services.model_providers.makerworld.service._download_s3_urllib",
  525. new=AsyncMock(return_value=(b"payload", "file.3mf")),
  526. ) as mocked:
  527. payload, filename = await svc.download_3mf(
  528. "https://s3.us-west-2.amazonaws.com/bucket/key/file.3mf?X-Amz-Signature=abc"
  529. )
  530. mocked.assert_awaited_once()
  531. # First arg is the verbatim URL — must NOT be round-tripped through
  532. # httpx/urlparse.urlencode since that breaks S3 SigV4.
  533. args = mocked.await_args.args
  534. assert args[0] == ("https://s3.us-west-2.amazonaws.com/bucket/key/file.3mf?X-Amz-Signature=abc")
  535. assert payload == b"payload"
  536. assert filename == "file.3mf"
  537. @pytest.mark.asyncio
  538. async def test_cdn_url_uses_httpx_with_minimal_headers(self):
  539. """Signed CDN URLs already carry the auth in the query string — don't
  540. leak the Bambu Cloud bearer to the CDN too. The client is reduced to a
  541. single ``User-Agent`` header; no ``Authorization``, no ``x-bbl-*``."""
  542. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient), auth_token="tok-abc")
  543. resp = MagicMock()
  544. resp.status_code = 200
  545. async def _chunks():
  546. yield b"PK\x03\x04"
  547. resp.aiter_bytes = lambda: _chunks()
  548. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  549. await svc.download_3mf("https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k")
  550. call = svc._client.stream.call_args
  551. headers = call.kwargs["headers"]
  552. # Minimal: UA only. No bearer to the CDN.
  553. assert "Authorization" not in headers
  554. assert all(not k.startswith("x-bbl") for k in headers)
  555. assert "User-Agent" in headers
  556. # Redirects off — host allowlist is only meaningful on the initial URL.
  557. assert call.kwargs["follow_redirects"] is False
  558. @pytest.mark.asyncio
  559. async def test_happy_path_streams_bytes(self):
  560. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  561. resp = MagicMock()
  562. resp.status_code = 200
  563. async def _chunks():
  564. yield b"PK\x03\x04" # 3MF = zip magic
  565. yield b"rest of file"
  566. resp.aiter_bytes = lambda: _chunks()
  567. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  568. payload, filename = await svc.download_3mf(
  569. "https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k"
  570. )
  571. assert payload.startswith(b"PK\x03\x04")
  572. assert filename == "foo.3mf"
  573. @pytest.mark.asyncio
  574. async def test_http_error_on_cdn_path_raises_unavailable(self):
  575. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  576. resp = MagicMock()
  577. resp.status_code = 500
  578. resp.aiter_bytes = lambda: (_ for _ in ())
  579. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  580. with pytest.raises(MakerWorldUnavailableError):
  581. await svc.download_3mf("https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k")
  582. @pytest.mark.asyncio
  583. async def test_exceeds_size_cap_raises(self):
  584. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  585. resp = MagicMock()
  586. resp.status_code = 200
  587. # Cap is 200 MB — emit one "chunk" that reports exceeding it.
  588. oversized = _MAX_3MF_BYTES + 1
  589. async def _chunks():
  590. # Emit a bytes object whose ``len()`` is oversized, without
  591. # actually allocating 200 MB in the test process.
  592. yield b"\x00" * oversized
  593. resp.aiter_bytes = lambda: _chunks()
  594. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  595. with pytest.raises(MakerWorldUnavailableError, match="cap"):
  596. await svc.download_3mf("https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k")
  597. class TestS3UrllibDownload:
  598. """Module-level ``_download_s3_urllib`` — the verbatim-URL path for S3."""
  599. @pytest.mark.asyncio
  600. async def test_returns_bytes_and_filename(self):
  601. from backend.app.services.model_providers.makerworld.http import _download_s3_urllib
  602. fake_resp = MagicMock()
  603. fake_resp.status = 200
  604. # Simulate urllib's file-like ``read(n)`` interface.
  605. fake_resp.read = MagicMock(side_effect=[b"hello", b""])
  606. fake_resp.__enter__ = MagicMock(return_value=fake_resp)
  607. fake_resp.__exit__ = MagicMock(return_value=None)
  608. fake_opener = MagicMock()
  609. fake_opener.open = MagicMock(return_value=fake_resp)
  610. with patch("urllib.request.build_opener", return_value=fake_opener):
  611. data, filename = await _download_s3_urllib(
  612. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  613. "fallback.3mf",
  614. )
  615. assert data == b"hello"
  616. assert filename == "fallback.3mf"
  617. @pytest.mark.asyncio
  618. async def test_redirect_is_treated_as_error(self):
  619. """The ``_NoRedirect`` handler returns ``None`` from ``redirect_request``,
  620. which makes ``urllib`` raise ``HTTPError`` instead of following. The
  621. wrapper must surface that as ``MakerWorldUnavailableError``."""
  622. from backend.app.services.model_providers.makerworld.http import _download_s3_urllib
  623. fake_opener = MagicMock()
  624. fake_opener.open = MagicMock(
  625. side_effect=HTTPError(
  626. "https://s3.example/redirect",
  627. 302,
  628. "Found",
  629. {}, # type: ignore[arg-type]
  630. None,
  631. )
  632. )
  633. with (
  634. patch("urllib.request.build_opener", return_value=fake_opener),
  635. pytest.raises(MakerWorldUnavailableError),
  636. ):
  637. await _download_s3_urllib(
  638. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  639. "fallback.3mf",
  640. )
  641. @pytest.mark.asyncio
  642. async def test_non_200_raises_unavailable(self):
  643. from backend.app.services.model_providers.makerworld.http import _download_s3_urllib
  644. fake_resp = MagicMock()
  645. fake_resp.status = 403
  646. fake_resp.read = MagicMock(return_value=b"")
  647. fake_resp.__enter__ = MagicMock(return_value=fake_resp)
  648. fake_resp.__exit__ = MagicMock(return_value=None)
  649. fake_opener = MagicMock()
  650. fake_opener.open = MagicMock(return_value=fake_resp)
  651. with (
  652. patch("urllib.request.build_opener", return_value=fake_opener),
  653. pytest.raises(MakerWorldUnavailableError),
  654. ):
  655. await _download_s3_urllib(
  656. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  657. "fallback.3mf",
  658. )
  659. @pytest.mark.asyncio
  660. async def test_size_cap_enforced(self):
  661. from backend.app.services.model_providers.makerworld.http import _download_s3_urllib
  662. fake_resp = MagicMock()
  663. fake_resp.status = 200
  664. # A single oversized chunk trips the cap on the first iteration.
  665. fake_resp.read = MagicMock(side_effect=[b"\x00" * (_MAX_3MF_BYTES + 1), b""])
  666. fake_resp.__enter__ = MagicMock(return_value=fake_resp)
  667. fake_resp.__exit__ = MagicMock(return_value=None)
  668. fake_opener = MagicMock()
  669. fake_opener.open = MagicMock(return_value=fake_resp)
  670. with (
  671. patch("urllib.request.build_opener", return_value=fake_opener),
  672. pytest.raises(MakerWorldUnavailableError, match="cap"),
  673. ):
  674. await _download_s3_urllib(
  675. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  676. "fallback.3mf",
  677. )
  678. @pytest.mark.asyncio
  679. async def test_network_error_mapped_to_unavailable(self):
  680. from backend.app.services.model_providers.makerworld.http import _download_s3_urllib
  681. fake_opener = MagicMock()
  682. fake_opener.open = MagicMock(side_effect=URLError("dns fail"))
  683. with (
  684. patch("urllib.request.build_opener", return_value=fake_opener),
  685. pytest.raises(MakerWorldUnavailableError),
  686. ):
  687. await _download_s3_urllib(
  688. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  689. "fallback.3mf",
  690. )
  691. class TestFetchThumbnail:
  692. """Proxy the CDN thumbnails so img-src CSP doesn't need to allow external hosts."""
  693. @pytest.fixture
  694. def service(self):
  695. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  696. svc._client.get = AsyncMock()
  697. return svc
  698. @pytest.mark.asyncio
  699. async def test_rejects_non_cdn_host(self, service):
  700. with pytest.raises(MakerWorldUrlError):
  701. await service.fetch_thumbnail("https://evil.example.com/img.jpg")
  702. @pytest.mark.asyncio
  703. async def test_rejects_loopback(self, service):
  704. # SSRF: don't let anyone abuse this as an open proxy toward 127.0.0.1
  705. with pytest.raises(MakerWorldUrlError):
  706. await service.fetch_thumbnail("http://127.0.0.1/secret.jpg")
  707. @pytest.mark.asyncio
  708. async def test_does_not_follow_redirects(self, service):
  709. """Host allowlist is only enforced on the initial URL — a 302 from the
  710. CDN to any other host would otherwise bypass the allowlist. ``follow_
  711. redirects=False`` pins that behaviour in the wire contract."""
  712. resp = MagicMock()
  713. resp.status_code = 200
  714. resp.headers = {"content-type": "image/jpeg"}
  715. resp.content = b"\xff\xd8\xff\xe0JFIF"
  716. service._client.get.return_value = resp
  717. await service.fetch_thumbnail("https://makerworld.bblmw.com/makerworld/model/X/cover.jpg")
  718. assert service._client.get.call_args.kwargs["follow_redirects"] is False
  719. @pytest.mark.asyncio
  720. async def test_rejects_html_content_type_even_with_image_extension(self, service):
  721. # An upstream error page (HTML) at a .jpg URL must be refused —
  722. # otherwise we'd forward it to the browser under an image framing.
  723. resp = MagicMock()
  724. resp.status_code = 200
  725. resp.headers = {"content-type": "text/html"}
  726. resp.content = b"<html>error page</html>"
  727. service._client.get.return_value = resp
  728. with pytest.raises(MakerWorldUnavailableError):
  729. await service.fetch_thumbnail("https://makerworld.bblmw.com/makerworld/model/X/cover.jpg")
  730. @pytest.mark.asyncio
  731. async def test_happy_path_with_proper_image_content_type(self, service):
  732. resp = MagicMock()
  733. resp.status_code = 200
  734. resp.headers = {"content-type": "image/jpeg; charset=binary"}
  735. resp.content = b"\xff\xd8\xff\xe0JFIF" # JPEG magic bytes
  736. service._client.get.return_value = resp
  737. payload, content_type = await service.fetch_thumbnail(
  738. "https://makerworld.bblmw.com/makerworld/model/X/cover.jpg"
  739. )
  740. assert payload == b"\xff\xd8\xff\xe0JFIF"
  741. # Semi-colon params stripped
  742. assert content_type == "image/jpeg"
  743. @pytest.mark.asyncio
  744. async def test_infers_mime_from_extension_when_cdn_lies(self, service):
  745. """MakerWorld's CDN returns application/octet-stream for real PNG/JPG
  746. files. Relying on upstream content-type alone would fail every
  747. thumbnail request; fall back to the URL extension."""
  748. resp = MagicMock()
  749. resp.status_code = 200
  750. resp.headers = {"content-type": "application/octet-stream"}
  751. resp.content = b"\x89PNG\r\n\x1a\n" # PNG magic bytes
  752. service._client.get.return_value = resp
  753. payload, content_type = await service.fetch_thumbnail(
  754. "https://makerworld.bblmw.com/makerworld/model/X/design/abc.png"
  755. )
  756. assert payload.startswith(b"\x89PNG")
  757. assert content_type == "image/png"
  758. @pytest.mark.asyncio
  759. async def test_refuses_when_no_extension_and_non_image_type(self, service):
  760. """If the URL carries no image extension AND upstream doesn't declare
  761. image/*, we can't confidently serve it as an image — refuse."""
  762. resp = MagicMock()
  763. resp.status_code = 200
  764. resp.headers = {"content-type": "application/octet-stream"}
  765. resp.content = b"who knows what this is"
  766. service._client.get.return_value = resp
  767. with pytest.raises(MakerWorldUnavailableError):
  768. await service.fetch_thumbnail("https://makerworld.bblmw.com/makerworld/model/X/blob")
  769. class TestSharedHttpClient:
  770. """The app-scoped httpx client registered via ``set_shared_http_client``
  771. must be reused by per-request services (one shared connection pool, same
  772. pattern as ``bambu_cloud``). The setter has to live in the same module as
  773. the service class, or the import-time snapshot never sees the lifespan's
  774. late registration and every request spins up its own client."""
  775. @pytest.mark.asyncio
  776. async def test_reuses_registered_client(self):
  777. client = MagicMock(spec=httpx.AsyncClient)
  778. set_shared_http_client(client)
  779. try:
  780. svc = MakerWorldService()
  781. assert svc._client is client
  782. assert svc._owns_client is False
  783. # close() must NOT close a client it doesn't own
  784. await svc.close()
  785. client.aclose.assert_not_called()
  786. finally:
  787. set_shared_http_client(None)
  788. @pytest.mark.asyncio
  789. async def test_creates_and_owns_own_client_when_none_registered(self):
  790. set_shared_http_client(None)
  791. svc = MakerWorldService()
  792. assert svc._owns_client is True
  793. await svc.close()
  794. assert svc._client.is_closed