test_makerworld.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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.makerworld import (
  8. _MAX_3MF_BYTES,
  9. MAKERWORLD_API_BASE,
  10. MakerWorldAuthError,
  11. MakerWorldForbiddenError,
  12. MakerWorldNotFoundError,
  13. MakerWorldService,
  14. MakerWorldUnavailableError,
  15. MakerWorldUrlError,
  16. )
  17. class TestParseUrl:
  18. """MakerWorld URL extraction."""
  19. def test_strips_locale_prefix_and_slug(self):
  20. model, profile = MakerWorldService.parse_url(
  21. "https://makerworld.com/en/models/1400373-self-watering-seed-starter"
  22. )
  23. assert model == 1400373
  24. assert profile is None
  25. def test_extracts_profile_id_from_fragment(self):
  26. model, profile = MakerWorldService.parse_url("https://makerworld.com/en/models/1400373-slug#profileId-1452154")
  27. assert model == 1400373
  28. assert profile == 1452154
  29. def test_accepts_scheme_omitted(self):
  30. model, profile = MakerWorldService.parse_url("makerworld.com/models/999")
  31. assert model == 999
  32. assert profile is None
  33. def test_accepts_subdomain(self):
  34. # Defensive: if MakerWorld ever stands up a regional subdomain, still accept it
  35. model, _ = MakerWorldService.parse_url("https://www.makerworld.com/en/models/42")
  36. assert model == 42
  37. def test_rejects_non_makerworld_host(self):
  38. with pytest.raises(MakerWorldUrlError):
  39. MakerWorldService.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. MakerWorldService.parse_url("https://makerworld.com/en/creators/foo")
  44. def test_rejects_empty(self):
  45. with pytest.raises(MakerWorldUrlError):
  46. MakerWorldService.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_to_auth_error(self, service):
  128. resp = MagicMock()
  129. resp.status_code = 401
  130. resp.json.return_value = {"code": 1, "error": "Please log in"}
  131. service._client.get.return_value = resp
  132. with pytest.raises(MakerWorldAuthError) as exc_info:
  133. await service.get_design(1)
  134. # Upstream's own message is surfaced to the caller
  135. assert "Please log in" in str(exc_info.value)
  136. @pytest.mark.asyncio
  137. async def test_maps_403_to_forbidden_with_upstream_reason(self, service):
  138. """403 is distinct from 401: auth was valid, MakerWorld refuses the
  139. specific resource (content-gated, region-locked, etc.). The upstream
  140. reason must reach the user so they know what to do."""
  141. resp = MagicMock()
  142. resp.status_code = 403
  143. resp.json.return_value = {
  144. "code": 15001,
  145. "error": "This model is only available to members",
  146. }
  147. service._client.get.return_value = resp
  148. with pytest.raises(MakerWorldForbiddenError) as exc_info:
  149. await service.get_design(1)
  150. assert "members" in str(exc_info.value)
  151. @pytest.mark.asyncio
  152. async def test_maps_5xx_to_unavailable(self, service):
  153. resp = MagicMock()
  154. resp.status_code = 503
  155. service._client.get.return_value = resp
  156. with pytest.raises(MakerWorldUnavailableError):
  157. await service.get_design(1)
  158. @pytest.mark.asyncio
  159. async def test_maps_timeout_to_unavailable(self, service):
  160. service._client.get.side_effect = httpx.TimeoutException("tooo slow")
  161. with pytest.raises(MakerWorldUnavailableError):
  162. await service.get_design(1)
  163. @pytest.mark.asyncio
  164. async def test_rejects_non_dict_json(self, service):
  165. resp = MagicMock()
  166. resp.status_code = 200
  167. resp.json.return_value = [1, 2, 3] # list, not dict
  168. service._client.get.return_value = resp
  169. with pytest.raises(MakerWorldUnavailableError):
  170. await service.get_design(1)
  171. class TestGetProfileDownload:
  172. """The new auth-gated 3MF manifest endpoint on the Bambu iot-service.
  173. Replaces the removed ``get_instance_download`` / ``get_model_download``
  174. helpers — YASTL#51's endpoint mints the signed CDN URL from the same
  175. long-lived Bambu Cloud bearer users already have.
  176. """
  177. def _make_service(self, *, auth_token: str | None = "tok-abc") -> MakerWorldService:
  178. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient), auth_token=auth_token)
  179. svc._client.get = AsyncMock()
  180. return svc
  181. @pytest.mark.asyncio
  182. async def test_requires_auth_token(self):
  183. svc = self._make_service(auth_token=None)
  184. with pytest.raises(MakerWorldAuthError):
  185. await svc.get_profile_download(1452154, "US2bb73b106683e5")
  186. @pytest.mark.asyncio
  187. async def test_returns_signed_manifest(self):
  188. svc = self._make_service()
  189. resp = MagicMock()
  190. resp.status_code = 200
  191. resp.json.return_value = {
  192. "name": "benchy.3mf",
  193. "url": "https://makerworld.bblmw.com/makerworld/model/X/Y/f.3mf?exp=1&key=k",
  194. }
  195. svc._client.get.return_value = resp
  196. manifest = await svc.get_profile_download(1452154, "US2bb73b106683e5")
  197. assert manifest["url"].startswith("https://makerworld.bblmw.com/")
  198. assert manifest["name"] == "benchy.3mf"
  199. @pytest.mark.asyncio
  200. async def test_sends_bearer_and_model_id_query(self):
  201. """Auth goes in ``Authorization`` and the alphanumeric modelId as a
  202. ``model_id`` query param — this is what YASTL#51 reverse-engineered."""
  203. svc = self._make_service(auth_token="tok-abc")
  204. resp = MagicMock()
  205. resp.status_code = 200
  206. resp.json.return_value = {"url": "https://makerworld.bblmw.com/x.3mf"}
  207. svc._client.get.return_value = resp
  208. await svc.get_profile_download(1452154, "US2bb73b106683e5")
  209. call = svc._client.get.call_args
  210. url = call.args[0] if call.args else call.kwargs.get("url")
  211. assert url == "https://api.bambulab.com/v1/iot-service/api/user/profile/1452154"
  212. assert call.kwargs["headers"]["Authorization"] == "Bearer tok-abc"
  213. assert call.kwargs["params"] == {"model_id": "US2bb73b106683e5"}
  214. @pytest.mark.asyncio
  215. async def test_maps_401_to_auth_error(self):
  216. svc = self._make_service()
  217. resp = MagicMock()
  218. resp.status_code = 401
  219. resp.json.return_value = {"error": "token expired"}
  220. svc._client.get.return_value = resp
  221. with pytest.raises(MakerWorldAuthError):
  222. await svc.get_profile_download(1, "M1")
  223. @pytest.mark.asyncio
  224. async def test_maps_403_to_forbidden(self):
  225. svc = self._make_service()
  226. resp = MagicMock()
  227. resp.status_code = 403
  228. resp.json.return_value = {"error": "paid model"}
  229. svc._client.get.return_value = resp
  230. with pytest.raises(MakerWorldForbiddenError) as exc_info:
  231. await svc.get_profile_download(1, "M1")
  232. assert "paid model" in str(exc_info.value)
  233. @pytest.mark.asyncio
  234. async def test_maps_404_to_not_found(self):
  235. svc = self._make_service()
  236. resp = MagicMock()
  237. resp.status_code = 404
  238. svc._client.get.return_value = resp
  239. with pytest.raises(MakerWorldNotFoundError):
  240. await svc.get_profile_download(1, "M1")
  241. @pytest.mark.asyncio
  242. async def test_maps_timeout_to_unavailable(self):
  243. svc = self._make_service()
  244. svc._client.get.side_effect = httpx.TimeoutException("nope")
  245. with pytest.raises(MakerWorldUnavailableError):
  246. await svc.get_profile_download(1, "M1")
  247. @pytest.mark.asyncio
  248. async def test_rejects_non_dict_json(self):
  249. svc = self._make_service()
  250. resp = MagicMock()
  251. resp.status_code = 200
  252. resp.json.return_value = ["not", "a", "dict"]
  253. svc._client.get.return_value = resp
  254. with pytest.raises(MakerWorldUnavailableError):
  255. await svc.get_profile_download(1, "M1")
  256. class TestDownload3MF:
  257. """SSRF guard + size cap + streaming behaviour."""
  258. def _stream_ctx(self, resp):
  259. ctx = MagicMock()
  260. ctx.__aenter__ = AsyncMock(return_value=resp)
  261. ctx.__aexit__ = AsyncMock(return_value=None)
  262. return ctx
  263. @pytest.mark.asyncio
  264. @pytest.mark.parametrize(
  265. "url",
  266. [
  267. "https://example.com/steal.3mf",
  268. "https://169.254.169.254/meta", # EC2 metadata
  269. "http://internal.host/loot",
  270. "http://127.0.0.1/loot",
  271. ],
  272. )
  273. async def test_rejects_non_allowed_hosts(self, url):
  274. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  275. with pytest.raises(MakerWorldUrlError):
  276. await svc.download_3mf(url)
  277. @pytest.mark.asyncio
  278. async def test_s3_host_delegates_to_urllib_path(self):
  279. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  280. with patch(
  281. "backend.app.services.makerworld._download_s3_urllib",
  282. new=AsyncMock(return_value=(b"payload", "file.3mf")),
  283. ) as mocked:
  284. payload, filename = await svc.download_3mf(
  285. "https://s3.us-west-2.amazonaws.com/bucket/key/file.3mf?X-Amz-Signature=abc"
  286. )
  287. mocked.assert_awaited_once()
  288. # First arg is the verbatim URL — must NOT be round-tripped through
  289. # httpx/urlparse.urlencode since that breaks S3 SigV4.
  290. args = mocked.await_args.args
  291. assert args[0] == ("https://s3.us-west-2.amazonaws.com/bucket/key/file.3mf?X-Amz-Signature=abc")
  292. assert payload == b"payload"
  293. assert filename == "file.3mf"
  294. @pytest.mark.asyncio
  295. async def test_cdn_url_uses_httpx_with_minimal_headers(self):
  296. """Signed CDN URLs already carry the auth in the query string — don't
  297. leak the Bambu Cloud bearer to the CDN too. The client is reduced to a
  298. single ``User-Agent`` header; no ``Authorization``, no ``x-bbl-*``."""
  299. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient), auth_token="tok-abc")
  300. resp = MagicMock()
  301. resp.status_code = 200
  302. async def _chunks():
  303. yield b"PK\x03\x04"
  304. resp.aiter_bytes = lambda: _chunks()
  305. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  306. await svc.download_3mf("https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k")
  307. call = svc._client.stream.call_args
  308. headers = call.kwargs["headers"]
  309. # Minimal: UA only. No bearer to the CDN.
  310. assert "Authorization" not in headers
  311. assert all(not k.startswith("x-bbl") for k in headers)
  312. assert "User-Agent" in headers
  313. # Redirects off — host allowlist is only meaningful on the initial URL.
  314. assert call.kwargs["follow_redirects"] is False
  315. @pytest.mark.asyncio
  316. async def test_happy_path_streams_bytes(self):
  317. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  318. resp = MagicMock()
  319. resp.status_code = 200
  320. async def _chunks():
  321. yield b"PK\x03\x04" # 3MF = zip magic
  322. yield b"rest of file"
  323. resp.aiter_bytes = lambda: _chunks()
  324. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  325. payload, filename = await svc.download_3mf(
  326. "https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k"
  327. )
  328. assert payload.startswith(b"PK\x03\x04")
  329. assert filename == "foo.3mf"
  330. @pytest.mark.asyncio
  331. async def test_http_error_on_cdn_path_raises_unavailable(self):
  332. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  333. resp = MagicMock()
  334. resp.status_code = 500
  335. resp.aiter_bytes = lambda: (_ for _ in ())
  336. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  337. with pytest.raises(MakerWorldUnavailableError):
  338. await svc.download_3mf("https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k")
  339. @pytest.mark.asyncio
  340. async def test_exceeds_size_cap_raises(self):
  341. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  342. resp = MagicMock()
  343. resp.status_code = 200
  344. # Cap is 200 MB — emit one "chunk" that reports exceeding it.
  345. oversized = _MAX_3MF_BYTES + 1
  346. async def _chunks():
  347. # Emit a bytes object whose ``len()`` is oversized, without
  348. # actually allocating 200 MB in the test process.
  349. yield b"\x00" * oversized
  350. resp.aiter_bytes = lambda: _chunks()
  351. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  352. with pytest.raises(MakerWorldUnavailableError, match="cap"):
  353. await svc.download_3mf("https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k")
  354. class TestS3UrllibDownload:
  355. """Module-level ``_download_s3_urllib`` — the verbatim-URL path for S3."""
  356. @pytest.mark.asyncio
  357. async def test_returns_bytes_and_filename(self):
  358. from backend.app.services.makerworld import _download_s3_urllib
  359. fake_resp = MagicMock()
  360. fake_resp.status = 200
  361. # Simulate urllib's file-like ``read(n)`` interface.
  362. fake_resp.read = MagicMock(side_effect=[b"hello", b""])
  363. fake_resp.__enter__ = MagicMock(return_value=fake_resp)
  364. fake_resp.__exit__ = MagicMock(return_value=None)
  365. fake_opener = MagicMock()
  366. fake_opener.open = MagicMock(return_value=fake_resp)
  367. with patch("urllib.request.build_opener", return_value=fake_opener):
  368. data, filename = await _download_s3_urllib(
  369. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  370. "fallback.3mf",
  371. )
  372. assert data == b"hello"
  373. assert filename == "fallback.3mf"
  374. @pytest.mark.asyncio
  375. async def test_redirect_is_treated_as_error(self):
  376. """The ``_NoRedirect`` handler returns ``None`` from ``redirect_request``,
  377. which makes ``urllib`` raise ``HTTPError`` instead of following. The
  378. wrapper must surface that as ``MakerWorldUnavailableError``."""
  379. from backend.app.services.makerworld import _download_s3_urllib
  380. fake_opener = MagicMock()
  381. fake_opener.open = MagicMock(
  382. side_effect=HTTPError(
  383. "https://s3.example/redirect",
  384. 302,
  385. "Found",
  386. {}, # type: ignore[arg-type]
  387. None,
  388. )
  389. )
  390. with (
  391. patch("urllib.request.build_opener", return_value=fake_opener),
  392. pytest.raises(MakerWorldUnavailableError),
  393. ):
  394. await _download_s3_urllib(
  395. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  396. "fallback.3mf",
  397. )
  398. @pytest.mark.asyncio
  399. async def test_non_200_raises_unavailable(self):
  400. from backend.app.services.makerworld import _download_s3_urllib
  401. fake_resp = MagicMock()
  402. fake_resp.status = 403
  403. fake_resp.read = MagicMock(return_value=b"")
  404. fake_resp.__enter__ = MagicMock(return_value=fake_resp)
  405. fake_resp.__exit__ = MagicMock(return_value=None)
  406. fake_opener = MagicMock()
  407. fake_opener.open = MagicMock(return_value=fake_resp)
  408. with (
  409. patch("urllib.request.build_opener", return_value=fake_opener),
  410. pytest.raises(MakerWorldUnavailableError),
  411. ):
  412. await _download_s3_urllib(
  413. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  414. "fallback.3mf",
  415. )
  416. @pytest.mark.asyncio
  417. async def test_size_cap_enforced(self):
  418. from backend.app.services.makerworld import _download_s3_urllib
  419. fake_resp = MagicMock()
  420. fake_resp.status = 200
  421. # A single oversized chunk trips the cap on the first iteration.
  422. fake_resp.read = MagicMock(side_effect=[b"\x00" * (_MAX_3MF_BYTES + 1), b""])
  423. fake_resp.__enter__ = MagicMock(return_value=fake_resp)
  424. fake_resp.__exit__ = MagicMock(return_value=None)
  425. fake_opener = MagicMock()
  426. fake_opener.open = MagicMock(return_value=fake_resp)
  427. with (
  428. patch("urllib.request.build_opener", return_value=fake_opener),
  429. pytest.raises(MakerWorldUnavailableError, match="cap"),
  430. ):
  431. await _download_s3_urllib(
  432. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  433. "fallback.3mf",
  434. )
  435. @pytest.mark.asyncio
  436. async def test_network_error_mapped_to_unavailable(self):
  437. from backend.app.services.makerworld import _download_s3_urllib
  438. fake_opener = MagicMock()
  439. fake_opener.open = MagicMock(side_effect=URLError("dns fail"))
  440. with (
  441. patch("urllib.request.build_opener", return_value=fake_opener),
  442. pytest.raises(MakerWorldUnavailableError),
  443. ):
  444. await _download_s3_urllib(
  445. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  446. "fallback.3mf",
  447. )
  448. class TestFetchThumbnail:
  449. """Proxy the CDN thumbnails so img-src CSP doesn't need to allow external hosts."""
  450. @pytest.fixture
  451. def service(self):
  452. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  453. svc._client.get = AsyncMock()
  454. return svc
  455. @pytest.mark.asyncio
  456. async def test_rejects_non_cdn_host(self, service):
  457. with pytest.raises(MakerWorldUrlError):
  458. await service.fetch_thumbnail("https://evil.example.com/img.jpg")
  459. @pytest.mark.asyncio
  460. async def test_rejects_loopback(self, service):
  461. # SSRF: don't let anyone abuse this as an open proxy toward 127.0.0.1
  462. with pytest.raises(MakerWorldUrlError):
  463. await service.fetch_thumbnail("http://127.0.0.1/secret.jpg")
  464. @pytest.mark.asyncio
  465. async def test_does_not_follow_redirects(self, service):
  466. """Host allowlist is only enforced on the initial URL — a 302 from the
  467. CDN to any other host would otherwise bypass the allowlist. ``follow_
  468. redirects=False`` pins that behaviour in the wire contract."""
  469. resp = MagicMock()
  470. resp.status_code = 200
  471. resp.headers = {"content-type": "image/jpeg"}
  472. resp.content = b"\xff\xd8\xff\xe0JFIF"
  473. service._client.get.return_value = resp
  474. await service.fetch_thumbnail("https://makerworld.bblmw.com/makerworld/model/X/cover.jpg")
  475. assert service._client.get.call_args.kwargs["follow_redirects"] is False
  476. @pytest.mark.asyncio
  477. async def test_rejects_html_content_type_even_with_image_extension(self, service):
  478. # An upstream error page (HTML) at a .jpg URL must be refused —
  479. # otherwise we'd forward it to the browser under an image framing.
  480. resp = MagicMock()
  481. resp.status_code = 200
  482. resp.headers = {"content-type": "text/html"}
  483. resp.content = b"<html>error page</html>"
  484. service._client.get.return_value = resp
  485. with pytest.raises(MakerWorldUnavailableError):
  486. await service.fetch_thumbnail("https://makerworld.bblmw.com/makerworld/model/X/cover.jpg")
  487. @pytest.mark.asyncio
  488. async def test_happy_path_with_proper_image_content_type(self, service):
  489. resp = MagicMock()
  490. resp.status_code = 200
  491. resp.headers = {"content-type": "image/jpeg; charset=binary"}
  492. resp.content = b"\xff\xd8\xff\xe0JFIF" # JPEG magic bytes
  493. service._client.get.return_value = resp
  494. payload, content_type = await service.fetch_thumbnail(
  495. "https://makerworld.bblmw.com/makerworld/model/X/cover.jpg"
  496. )
  497. assert payload == b"\xff\xd8\xff\xe0JFIF"
  498. # Semi-colon params stripped
  499. assert content_type == "image/jpeg"
  500. @pytest.mark.asyncio
  501. async def test_infers_mime_from_extension_when_cdn_lies(self, service):
  502. """MakerWorld's CDN returns application/octet-stream for real PNG/JPG
  503. files. Relying on upstream content-type alone would fail every
  504. thumbnail request; fall back to the URL extension."""
  505. resp = MagicMock()
  506. resp.status_code = 200
  507. resp.headers = {"content-type": "application/octet-stream"}
  508. resp.content = b"\x89PNG\r\n\x1a\n" # PNG magic bytes
  509. service._client.get.return_value = resp
  510. payload, content_type = await service.fetch_thumbnail(
  511. "https://makerworld.bblmw.com/makerworld/model/X/design/abc.png"
  512. )
  513. assert payload.startswith(b"\x89PNG")
  514. assert content_type == "image/png"
  515. @pytest.mark.asyncio
  516. async def test_refuses_when_no_extension_and_non_image_type(self, service):
  517. """If the URL carries no image extension AND upstream doesn't declare
  518. image/*, we can't confidently serve it as an image — refuse."""
  519. resp = MagicMock()
  520. resp.status_code = 200
  521. resp.headers = {"content-type": "application/octet-stream"}
  522. resp.content = b"who knows what this is"
  523. service._client.get.return_value = resp
  524. with pytest.raises(MakerWorldUnavailableError):
  525. await service.fetch_thumbnail("https://makerworld.bblmw.com/makerworld/model/X/blob")